3 # COPYRIGHT NOTICE STARTS HERE
5 # Copyright 2018 © Samsung Electronics Co., Ltd.
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
19 # COPYRIGHT NOTICE ENDS HERE
30 ${CMD} - create a chroot directory from docker image
33 It will export docker image into a directory capable of chrooting.
34 It needs and will run these commands (requires docker service):
39 ${CMD} [-h|--help|help]
42 ${CMD} convert <docker-name> <name-of-directory>
44 It will convert docker image into directory - no chroot yet.
45 The name of the docker image must be imported already (not a file):
48 The directory will be created and so this command will fail if some
49 directory or a file of this name (filepath) already exists!
50 There is another script run_chroot.sh with which you can do chroot
51 on this newly created directory - so it is expected that this
52 directory is kept clean and as it is.
53 If you don't care about this feature (run_chroot.sh) and you know
54 what are you doing, then do necessary mounts and execute:
55 chroot <name-of-directory>/chroot /bin/sh -l
60 # PLEASE DON'T TOUCH ME
63 # readme file for run_chroot.sh
68 cat > "$CHROOT_METADIR"/README.md <<EOF
77 **Don't modify insides of this directory (where this README.md lies).**
79 The structure is needed as it is.
81 If you wish to just run chroot by yourself, you can do:
83 chroot ./chroot /bin/sh -l
91 # directory structure:
106 match=$(docker image ls --no-trunc -q "$image" | wc -l)
110 echo ERROR: "Docker image does not exist: ${DOCKER_IMAGE}" >&2
117 echo ERROR: "Multiple results for this docker name: ${DOCKER_IMAGE}" >&2
127 if [ -n "$DOCKER_CONTAINER" ] ; then
128 echo INFO: "Delete the export container: ${DOCKER_CONTAINER}" >&2
129 if ! docker rm "$DOCKER_CONTAINER" > /dev/null ; then
130 echo ERROR: "Failed to delete: ${DOCKER_CONTAINER}" >&2
153 echo ERROR: "Bad usage" >&2
162 echo ERROR: "Nothing to do - missing command" >&2
167 if [ -z "$DOCKER_IMAGE" ] || [ -z "$CHROOT_METADIR" ] ; then
168 echo ERROR: "Missing argument" >&2
173 if [ -e "$CHROOT_METADIR" ] ; then
174 echo ERROR: "Filepath already exists: ${CHROOT_METADIR}" >&2
175 echo ERROR: "Please rename it, remove it or use different name" >&2
176 echo ERROR: "I need my working directory empty, thanks" >&2
180 # check if docker image is there
181 check_docker_image "$DOCKER_IMAGE"
184 if [ "$(id -u)" -ne 0 ] ; then
185 echo ERROR: "I need root privileges and you are not root: $(id -nu)" >&2
189 # making sure that CHROOT_METADIR is absolute path
190 CHROOT_METADIR=$(readlink -f "$CHROOT_METADIR")
193 trap on_exit INT QUIT TERM EXIT
196 mkdir -p "$CHROOT_METADIR"/
200 DOCKER_CONTAINER=$(docker create "$DOCKER_IMAGE")
201 if [ -z "$DOCKER_CONTAINER" ] ; then
202 echo ERROR: "I could not create a container from: ${DOCKER_IMAGE}" >&2
207 mkdir -p "$CHROOT_METADIR"/chroot
208 echo INFO: "Export started - it can take a while to finish..." >&2
209 if ! docker export "$DOCKER_CONTAINER" | tar -C "$CHROOT_METADIR"/chroot -xf - ; then
210 echo ERROR: "Unpacking failed - permissions?" >&2
213 echo INFO: "Export success: $CHROOT_METADIR/chroot" >&2
214 echo INFO: "Checkout the README file: $CHROOT_METADIR/README.md" >&2