-
Notifications
You must be signed in to change notification settings - Fork 0
/
img-chroot
executable file
·207 lines (170 loc) · 7.07 KB
/
img-chroot
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
#! /usr/bin/env bash
#
# image-chroot.sh – script for execute commands in file-image, copy files
# Copyright 2018 Artem B. Smirnov
#
# 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.
set -e # Exit immidiately on non-zero result
echo_stamp() {
# TEMPLATE: echo_stamp <TEXT> <TYPE>
# TYPE: SUCCESS, ERROR, INFO
# More info there https://www.shellhacks.com/ru/bash-colors/
TEXT="$(date '+[%Y-%m-%d %H:%M:%S]') $1"
TEXT="\e[1m$TEXT\e[0m" # BOLD
case "$2" in
SUCCESS)
TEXT="\e[32m${TEXT}\e[0m";; # GREEN
ERROR)
TEXT="\e[31m${TEXT}\e[0m";; # RED
*)
TEXT="\e[34m${TEXT}\e[0m";; # BLUE
esac
echo -e ${TEXT}
}
# This script doesn't work on Ubuntu because OS`s losetup does not consist --partscan (-P).
# Idea: use `mount -o loop,offset`
# https://stefanoprenna.com/blog/2014/09/22/tutorial-how-to-mount-raw-images-img-images-on-linux/
# REPO_DIR=$(mktemp -d --suffix=.builder_repo)
# mount -t ext4 -o loop,offset=$((94208 * 512)) image/clever_qemu_test_2_20180822_163141.img "$REPO_DIR"
# mount -t vfat -o loop,offset=$((8192 * 512)) image/clever_qemu_test_2_20180822_163141.img "$REPO_DIR/boot"
execute() {
# execute <IMAGE_PATH> [<EXECUTE_FILE> [...]]
echo_stamp "Mount loop-image: $1"
local DEV_IMAGE=$(losetup -Pf $1 --show)
sleep 0.5
local MOUNT_POINT=$(mktemp -d --suffix=.builder_image)
echo_stamp "Mount dirs ${MOUNT_POINT} & ${MOUNT_POINT}/boot"
mount "${DEV_IMAGE}p2" ${MOUNT_POINT}
mount "${DEV_IMAGE}p1" ${MOUNT_POINT}/boot
REGISTER=':arm:M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/usr/bin/qemu-wrapper:'
if [[ $(arch) != 'armv7l' ]]; then
echo_stamp "Enable qemu-arm-static"
mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc 2> /dev/null || true
echo ${REGISTER} > /proc/sys/fs/binfmt_misc/register 2> /dev/null || true
cp '/usr/share/qemu-wrapper' "${MOUNT_POINT}/usr/bin/qemu-wrapper"
cp '/usr/share/qemu-arm-static' "${MOUNT_POINT}/usr/bin/qemu-arm-static"
else echo_stamp "qemu-arm-static is not need"
fi
echo_stamp "Bind system dirs"
echo_stamp "Mounting /proc in chroot... "
if [ ! -d ${MOUNT_POINT}/proc ]; then
mkdir -p ${MOUNT_POINT}/proc; fi
mount -t proc -o nosuid,noexec,nodev proc ${MOUNT_POINT}/proc \
&& echo_stamp "OK" "SUCCESS" \
|| (echo_stamp "Failed" "ERROR"; exit 1)
echo_stamp "Mounting /sys in chroot... "
if [ ! -d ${MOUNT_POINT}/sys ]; then
mkdir -p ${MOUNT_POINT}/sys; fi
mount -t sysfs -o nosuid,noexec,nodev sysfs ${MOUNT_POINT}/sys \
&& echo_stamp "OK" "SUCCESS" \
|| (echo_stamp "Failed" "ERROR"; exit 1)
echo_stamp "Mounting /dev/ and /dev/pts in chroot... " \
&& mkdir -p -m 755 ${MOUNT_POINT}/dev/pts \
&& mount -t devtmpfs -o mode=0755,nosuid devtmpfs ${MOUNT_POINT}/dev \
&& mount -t devpts -o gid=5,mode=620 devpts ${MOUNT_POINT}/dev/pts \
&& echo_stamp "OK" "SUCCESS" \
|| (echo_stamp "Failed" "ERROR"; exit 1)
echo_stamp "Copy DNS records" \
&& cp -L /etc/resolv.conf ${MOUNT_POINT}/etc/resolv.conf \
&& echo_stamp "OK" "SUCCESS" \
|| (echo_stamp "Failed" "ERROR"; exit 1)
set +e
if [[ $# > 1 ]]; then
echo_stamp "Copy script to chroot fs"
local SCRIPT_NAME="$(basename $2).$(tr -dc 'A-F0-9' < /dev/urandom | dd bs=1 count=7 2>/dev/null)"
local SCRIPT_DIR="${MOUNT_POINT}/root"
cp "$2" "${SCRIPT_DIR}/${SCRIPT_NAME}"
# Run script in chroot with additional arguments
chroot ${MOUNT_POINT} /bin/sh -c "/root/${SCRIPT_NAME} $3 $4 $5 $6 $7"
# Removing script from chroot fs
rm "${SCRIPT_DIR}/${SCRIPT_NAME}"
else
# https://wiki.archlinux.org/index.php/Change_root_(%D0%A0%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9)
# http://www.unix-lab.org/posts/chroot/
# https://habrahabr.ru/post/141012/
# https://losst.ru/vosstanovlenie-grub2
# http://unixteam.ru/content/virtualizaciya-ili-zapuskaem-prilozhenie-v-chroot-okruzhenii-razmyshleniya
# http://help.ubuntu.ru/wiki/%D0%B2%D0%BE%D1%81%D1%81%D1%82%D0%B0%D0%BD%D0%BE%D0%B2%D0%BB%D0%B5%D0%BD%D0%B8%D0%B5_grub
echo_stamp "Entering chroot" \
&& chroot ${MOUNT_POINT} /bin/bash
fi
umount_system ${MOUNT_POINT} ${DEV_IMAGE}
set -e
}
# https://gist.github.com/letmaik/caa0f6cc4375cbfcc1ff26bd4530c2a3
# https://github.com/travis-ci/travis-build/blob/master/lib/travis/build/templates/header.sh
my_travis_retry() {
local result=0
local count=1
while [ $count -le 3 ]; do
[ $result -ne 0 ] && {
echo -e "\n${ANSI_RED}The command \"$@\" failed. Retrying, $count of 3.${ANSI_RESET}\n" >&2
}
# ! { } ignores set -e, see https://stackoverflow.com/a/4073372
! { "$@"; result=$?; }
[ $result -eq 0 ] && break
count=$(($count + 1))
sleep 1
done
[ $count -gt 3 ] && {
echo -e "\n${ANSI_RED}The command \"$@\" failed 3 times.${ANSI_RESET}\n" >&2
}
return $result
}
umount_system() {
# TEMPLATE: umount_system <MOUNT_POINT> <DEV_IMAGE>
echo_stamp "Unmount chroot rootfs and boot partition: $1"
my_travis_retry umount -fR $1
losetup -d $2
}
copy_to_chroot() {
# copy_to_chroot <IMAGE_PATH> <MOVE_FILE> <MOVE_TO>
echo_stamp "Mount loop-image: $1"
local DEV_IMAGE=$(losetup -Pf $1 --show)
sleep 0.5
local MOUNT_POINT=$(mktemp -d --suffix=.builder_image)
echo_stamp "Mount dirs ${MOUNT_POINT} & ${MOUNT_POINT}/boot"
mount "${DEV_IMAGE}p2" ${MOUNT_POINT}
mount "${DEV_IMAGE}p1" ${MOUNT_POINT}/boot
local dir_name=$(dirname "${MOUNT_POINT}$3 /")
if [[ -f $2 ]]; then
[[ ! -d ${dir_name} ]] && mkdir -p ${dir_name} \
&& echo_stamp "Created ${dir_name}" "SUCCESS"
fi
cp -r "$2" "${MOUNT_POINT}$3"
umount_system ${MOUNT_POINT} ${DEV_IMAGE}
}
if [ $(whoami) != "root" ]; then
echo ""
echo "********************************************************************"
echo "******************** This should be run as root ********************"
echo "********************************************************************"
echo ""
exit 1
fi
echo "================================================================================"
for ((i=0; i<=$#; i++)); do echo "\$$i: ${!i}"; done
echo "================================================================================"
if [[ $# > 0 ]]; then
[[ -f $1 ]] || (echo_stamp "$1 does not exist" "ERROR"; echo "Template: image-chroot.sh <IMAGE> [ exec <SCRIPT> [...] | copy <MOVE_FILE> <MOVE_TO> ]"; exit 1)
if [[ ! -z $2 ]]; then
case "$2" in
exec)
execute $1 $3 ${@:4};;
copy)
copy_to_chroot $1 $3 $4;;
*)
echo "Template: image-chroot.sh <IMAGE> [ exec <SCRIPT> [...] | copy <MOVE_FILE> <MOVE_TO> ]";;
esac
else execute $1; fi
fi