forked from koverstreet/ktest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
root_image
executable file
·300 lines (239 loc) · 6.98 KB
/
root_image
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env bash
#
# Create a VM image suitable for running automated tests
# Output: vm_image
set -o nounset
set -o errexit
set -o errtrace
ktest_dir=$(dirname "$(readlink -f "$0")")
debootstrap=$ktest_dir/debootstrap/debootstrap
. "$ktest_dir/lib/util.sh"
if [[ $(id -u) != 0 ]] ; then
echo this script must be run as root
exit 1
fi
checkdep fallocate util-linux
checkdep mkfs.ext4 e2fsprogs
IMAGE_SIZE="10G"
MIRROR=https://deb.debian.org/debian/
usage()
{
echo "root_image: create/update virtual machine root images for ktest"
echo "Usage: root_image cmd [options]"
echo " create Create a new image"
echo " update Update an existing image"
echo
echo "options:"
echo " -h Display this help and exit"
echo " -a <arch> Architecture for vm image"
echo " -m <mirror> Debian mirror"
echo ' -i <image> Image to create/update, defaults to /var/lib/ktest/root.$arch'
}
if [[ $# = 0 ]]; then
usage
exit 1
fi
ktest_image=""
CMD="cmd_$1"
shift
while getopts "ha:m:i:" arg; do
case $arg in
h)
usage
exit 0
;;
a)
ktest_arch=$OPTARG
;;
m)
MIRROR=$OPTARG
;;
i)
ktest_image=$OPTARG
;;
esac
done
shift $(( OPTIND - 1 ))
parse_arch "$ktest_arch"
[[ -z $ktest_image ]] && ktest_image=/var/lib/ktest/root.$DEBIAN_ARCH
mkdir -p "$(dirname "$ktest_image")"
PACKAGES=(kexec-tools less psmisc openssh-server \
build-essential make gcc g++ \
autoconf automake autopoint bison \
pkg-config libtool-bin \
gdb strace linux-perf trace-cmd blktrace sysstat iotop htop \
hdparm mdadm lvm2 \
btrfs-progs jfsutils nilfs-tools f2fs-tools \
bc attr gawk acl rsync git python3-docutils \
stress-ng)
# stress testing:
PACKAGES+=(fio dbench bonnie++ fsmark)
# bcachefs-tools build dependencies:
PACKAGES+=(libblkid-dev uuid-dev libscrypt-dev libsodium-dev)
PACKAGES+=(libkeyutils-dev liburcu-dev libudev-dev zlib1g-dev libattr1-dev)
PACKAGES+=(libaio-dev libzstd-dev liblz4-dev libfuse3-dev valgrind)
PACKAGES+=(llvm libclang-dev)
# quota tools:
PACKAGES+=(libudev-dev libldap2-dev)
# xfstests:
PACKAGES+=(acct bsdextrautils xfsprogs xfslibs-dev quota libcap2-bin)
PACKAGES+=(libattr1-dev libaio-dev libgdbm-dev libacl1-dev gettext)
PACKAGES+=(libssl-dev libgdbm-dev libgdbm-compat-dev liburing-dev)
PACKAGES+=(duperemove thin-provisioning-tools)
# xfsprogs:
PACKAGES+=(libinih-dev)
# nfs testing:
PACKAGES+=(nfs-kernel-server)
# nbd testing
PACKAGES+=(nbd-client nbd-server)
# dm testing:
PACKAGES+=(cryptsetup)
# weird block layer crap
PACKAGES+=(multipath-tools sg3-utils srptools)
# suspend testing:
# [[ $KERNEL_ARCH = x86 ]] && PACKAGES+=(uswsusp)
EXCLUDE=(dmidecode nano rsyslog logrotate cron \
iptables nfacct \
debconf-i18n info gnupg libpam-systemd)
SYSTEMD_MASK=(dev-hvc0.device \
getty.target \
getty-static.service \
avahi-daemon.service \
crond.service \
exim4.service \
kdump.service \
hdparm.service \
cdrom.mount \
mdadm-raid.service \
lvm2-activation-early.service \
aoetools.service \
sysstat.service \
kexec-load.service \
kexec.service \
systemd-ask-password-console.path \
systemd-ask-password-wall.path \
systemd-update-utmp-runlevel.service \
systemd-update-utmp.service \
time-sync.target \
multipathd.service)
export DEBIAN_FRONTEND=noninteractive
export DEBCONF_NONINTERACTIVE_SEEN=true
export LC_ALL=C
export LANGUAGE=C
export LANG=C
_chroot()
{
PATH=/usr/sbin:/usr/bin:/sbin:/bin chroot "$@"
}
update_files()
{
install -m0644 "$ktest_dir/lib/fstab" "$MNT/etc/fstab"
install -m0755 "$ktest_dir/lib/testrunner.wrapper" "$MNT/sbin/testrunner.wrapper"
install -m0644 "$ktest_dir/lib/testrunner.service" "$MNT/lib/systemd/system/testrunner.service"
ln -sf /lib/systemd/system/testrunner.service "$MNT/etc/systemd/system/multi-user.target.wants/testrunner.service"
touch "$MNT/etc/resolv.conf"
chmod 644 "$MNT/etc/resolv.conf"
mkdir -p "$MNT/root/"
install -m0644 "$MNT/etc/skel/.bashrc" "$MNT/root/"
install -m0644 "$MNT/etc/skel/.profile" "$MNT/root/"
mkdir -p "$MNT/var/log/core"
chmod 777 "$MNT/var/log/core"
# Disable systemd/udev stuff we don't need:
# systemctl mask doesn't work for foreign archs
#_chroot "$MNT" systemctl mask "${SYSTEMD_MASK[@]}"
for i in "${SYSTEMD_MASK[@]}"; do
(cd "$MNT/etc/systemd/system"; ln -sf /dev/null "$i")
done
cat > "$MNT/etc/systemd/journald.conf" <<-ZZ
[Journal]
Storage=none
ZZ
mkdir -p "$MNT/etc/network"
cat > "$MNT/etc/network/interfaces" <<-ZZ
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
ZZ
# disable network interface renaming - it's unreliable
mkdir -p "$MNT/etc/udev/rules.d/"
ln -sf /dev/null "$MNT/etc/udev/rules.d/80-net-setup-link.rules"
rm -f "$MNT/lib/udev/rules.d/*persistent*"
rm -f "$MNT/lib/udev/rules.d/*lvm*"
rm -f "$MNT/lib/udev/rules.d/*dm*"
rm -f "$MNT/lib/udev/rules.d/*md-raid*"
rm -f "$MNT/lib/udev/rules.d/*btrfs*"
rm -f "$MNT/lib/udev/rules.d/*hdparm*"
echo $(hostname)-kvm >"$MNT/etc/hostname"
}
update_packages()
{
cp /etc/resolv.conf "$MNT/etc/resolv.conf"
_chroot "$MNT" mount -t proc none /proc
_chroot "$MNT" apt-get -qq update
_chroot "$MNT" apt-get -qq upgrade
_chroot "$MNT" apt-get -qq install --no-install-recommends "${PACKAGES[@]}"
rm -f "$MNT/var/cache/apt/archives/*.deb"
}
trim_image()
{
e2fsck -f "$1"
resize2fs -M "$1" # shrinks the file
resize2fs "$1" "$IMAGE_SIZE" # re-grows as sparse
}
umount_image()
{
# Unmount everything under $MNT
awk '{print $2}' /proc/mounts|
grep "^$MNT"|
sort -r|
xargs umount
rmdir "$MNT"
trap '' EXIT
}
cmd_update()
{
if [[ ! -e $ktest_image ]]; then
echo "$ktest_image does not exist"
exit 1
fi
MNT=$(mktemp --tmpdir -d $(basename "$0")-XXXXXXXXXX)
trap 'umount_image' EXIT
cp "$ktest_image" "$ktest_image".new
mount "$ktest_image".new "$MNT"
update_packages
update_files
umount_image
trim_image "$ktest_image".new
mv "$ktest_image".new "$ktest_image"
}
cmd_create()
{
if [[ -e $ktest_image ]]; then
echo "$ktest_image already exists"
exit 1
fi
(cd "$ktest_dir"; git submodule update --init debootstrap)
MNT=$(mktemp --tmpdir -d $(basename "$0")-XXXXXXXXXX)
trap 'umount_image; rm "$ktest_image"' EXIT
fallocate -l "$IMAGE_SIZE" "$ktest_image"
mkfs.ext4 -F "$ktest_image"
mount "$ktest_image" "$MNT"
DEBOOTSTRAP_DIR=$ktest_dir/debootstrap $debootstrap \
--no-check-gpg \
--arch="$DEBIAN_ARCH" \
--exclude=$(join_by , "${EXCLUDE[@]}") \
--foreign \
bullseye "$MNT" "$MIRROR"
_chroot "$MNT" /debootstrap/debootstrap --second-stage
_chroot "$MNT" dpkg --configure -a
update_packages
update_files
umount_image
trim_image "$ktest_image"
}
if [[ $(type -t "$CMD") != function ]]; then
usage
exit 1
fi
$CMD "$@"