blob: f8e256dab4db3e7d97fc964496bc5ef8448514f1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
#!/bin/sh
# COPYRIGHT NOTICE STARTS HERE
# Copyright 2018 © Samsung Electronics Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# COPYRIGHT NOTICE ENDS HERE
set -e
CMD=$(basename "$0")
help()
{
echo "
NAME:
${CMD} - create a chroot directory from docker image
DESCRIPTION:
It will export docker image into a directory capable of chrooting.
It needs and will run these commands (requires docker service):
docker create
docker export
USAGE:
${CMD} [-h|--help|help]
This help
${CMD} convert <docker-name> <name-of-directory>
It will convert docker image into directory - no chroot yet.
The name of the docker image must be imported already (not a file):
docker image ls
The directory will be created and so this command will fail if some
directory or a file of this name (filepath) already exists!
There is another script run_chroot.sh with which you can do chroot
on this newly created directory - so it is expected that this
directory is kept clean and as it is.
If you don't care about this feature (run_chroot.sh) and you know
what are you doing, then do necessary mounts and execute:
chroot <name-of-directory>/chroot /bin/sh -l
"
}
#
# PLEASE DON'T TOUCH ME
#
# readme file for run_chroot.sh
readme()
{
md_codequote='```'
cat > "$CHROOT_METADIR"/README.md <<EOF
# RUN CHROOT COMMAND
# usage:
${md_codequote}
run_chroot.sh help
${md_codequote}
**Don't modify insides of this directory (where this README.md lies).**
The structure is needed as it is.
If you wish to just run chroot by yourself, you can do:
${md_codequote}
chroot ./chroot /bin/sh -l
${md_codequote}
# requirements:
* root privileges
* docker service
# directory structure:
${md_codequote}
README.md
chroot/
.overlay
.workdir
.merged
${md_codequote}
EOF
}
# arg: <docker-name>
check_docker_image()
{
image="$1"
match=$(docker image ls --no-trunc -q "$image" | wc -l)
case $match in
0)
echo ERROR: "Docker image does not exist: ${DOCKER_IMAGE}" >&2
exit 1
;;
1)
:
;;
*)
echo ERROR: "Multiple results for this docker name: ${DOCKER_IMAGE}" >&2
exit 1
;;
esac
return 0
}
cleanup()
{
if [ -n "$DOCKER_CONTAINER" ] ; then
echo INFO: "Delete the export container: ${DOCKER_CONTAINER}" >&2
if ! docker rm "$DOCKER_CONTAINER" > /dev/null ; then
echo ERROR: "Failed to delete: ${DOCKER_CONTAINER}" >&2
fi
fi
}
on_exit()
{
set +e
cleanup
}
action=nil
case "$1" in
''|-h|--help|help)
help
exit 0
;;
convert)
action=convert
DOCKER_IMAGE="$2"
CHROOT_METADIR="$3"
;;
*)
echo ERROR: "Bad usage" >&2
help >&2
exit 1
;;
esac
case "$action" in
''|nil)
echo ERROR: "Nothing to do - missing command" >&2
help >&2
exit 1
;;
convert)
if [ -z "$DOCKER_IMAGE" ] || [ -z "$CHROOT_METADIR" ] ; then
echo ERROR: "Missing argument" >&2
help >&2
exit 1
fi
if [ -e "$CHROOT_METADIR" ] ; then
echo ERROR: "Filepath already exists: ${CHROOT_METADIR}" >&2
echo ERROR: "Please rename it, remove it or use different name" >&2
echo ERROR: "I need my working directory empty, thanks" >&2
exit 1
fi
# check if docker image is there
check_docker_image "$DOCKER_IMAGE"
# we must be root
if [ "$(id -u)" -ne 0 ] ; then
echo ERROR: "I need root privileges and you are not root: $(id -nu)" >&2
exit 1
fi
# making sure that CHROOT_METADIR is absolute path
CHROOT_METADIR=$(readlink -f "$CHROOT_METADIR")
# set trap
trap on_exit INT QUIT TERM EXIT
# making readme
mkdir -p "$CHROOT_METADIR"/
readme
# create container
DOCKER_CONTAINER=$(docker create "$DOCKER_IMAGE")
if [ -z "$DOCKER_CONTAINER" ] ; then
echo ERROR: "I could not create a container from: ${DOCKER_IMAGE}" >&2
exit 1
fi
# unpacking of image
mkdir -p "$CHROOT_METADIR"/chroot
echo INFO: "Export started - it can take a while to finish..." >&2
if ! docker export "$DOCKER_CONTAINER" | tar -C "$CHROOT_METADIR"/chroot -xf - ; then
echo ERROR: "Unpacking failed - permissions?" >&2
exit 1
else
echo INFO: "Export success: $CHROOT_METADIR/chroot" >&2
echo INFO: "Checkout the README file: $CHROOT_METADIR/README.md" >&2
fi
;;
esac
exit 0
|