summaryrefslogtreecommitdiff
path: root/root_image
blob: f5ec92635eb714813251e2c5c2148d940514efc5 (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
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
#!/bin/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")")

. "$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
checkdep debootstrap

IMAGE_SIZE="2G"

PACKAGES="kexec-tools,less,psmisc,openssh-server"
PACKAGES+=",make,gcc,g++,gdb,strace,ltrace"
PACKAGES+=",hdparm,btrfs-tools,mdadm,lvm2,aoetools,vblade"
PACKAGES+=",linux-tools,blktrace,sysstat"
PACKAGES+=",bc,attr,gawk,acl"
PACKAGES+=",build-essential"
PACKAGES+=",rsync"

# stress testing:
PACKAGES+=",fio,dbench,bonnie++,fsmark"

#PACKAGES+=",valgrind" #broken

# suspend testing:
PACKAGES+=",uswsusp"

# bcache-tools build dependencies:
PACKAGES+=",pkg-config,libblkid-dev,uuid-dev,libscrypt-dev"
PACKAGES+=",libsodium-dev,libkeyutils-dev,liburcu-dev"

# xfstests:
#PACKAGES+=",xfsprogs,xfslibs-dev"
PACKAGES+=",autoconf,automake"
PACKAGES+=",libtool-bin,libattr1-dev,libaio-dev,libgdbm-dev,libacl1-dev,gettext"
PACKAGES+=",libssl-dev"
PACKAGES+=",quota"

# nfs testing:
PACKAGES+=",nfs-kernel-server"

EXCLUDE="dmidecode,nano,rsyslog,logrotate,cron,iptables,nfacct"
EXCLUDE+=",debconf-i18n,info,gnupg"
EXCLUDE+="libpam-systemd"

MIRROR=http://ftp.us.debian.org/debian/

SYSTEMD_MASK=(dev-hvc0.device				\
    getty.target					\
    getty-static.service				\
    avahi-daemon.service				\
    crond.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				\
    systemd-journald.service				\
    systemd-journal-flush.service			\
    systemd-journald-dev-log.socket			\
    systemd-journald.socket				\
    time-sync.target)

usage()
{
    echo "root_image_create: Create a virtual machine image for ktest"
    echo "Usage: create_vm_image [ -m debian mirror ] [ -a architecture ] new_image"
}

if [[ $# = 0 ]]; then
    usage
    exit 1
fi

IMG=""
CMD="cmd_$1"
shift

while getopts "ha:m:i:" arg; do
    case $arg in
	h)
	    usage
	    exit 0
	    ;;
	a)
	    ARCH=$OPTARG
	    ;;
	m)
	    MIRROR=$OPTARG
	    ;;
	i)
	    IMG=$OPTARG
	    ;;
    esac
done
shift $(( OPTIND - 1 ))

if [[ -z $IMG ]]; then
    echo "Please specify an image"
    exit 1
fi

parse_arch "$ARCH"

update_files()
{
    install -m0644 "$ktest_dir/lib/fstab" "$MNT/etc/fstab"
    install -m0755 "$ktest_dir/lib/rc.local" "$MNT/etc/rc.local"
}

cmd_update()
{
    if [[ ! -e $IMG ]]; then
	echo "$IMG does not exist"
	exit 1
    fi

    MNT=$(mktemp -d)
    trap 'umount "$MNT"; rmdir "$MNT"' EXIT

    mount "$IMG" "$MNT"
    update_files

    chroot "$MNT" apt update
    chroot "$MNT" apt upgrade -y
    chroot "$MNT" apt clean

    umount	"$MNT"
    rmdir	"$MNT"
    trap '' EXIT

    # Trim deleted data from the image (around 75MB)
    e2fsck -f "$IMG"
    resize2fs -M "$IMG"		# shrinks the file
    resize2fs "$IMG" "$IMAGE_SIZE"	# re-grows as sparse
}

cmd_create()
{
    if [[ -e $IMG ]]; then
	echo "$IMG already exists"
	exit 1
    fi

    MNT=$(mktemp -d)
    trap 'umount "$MNT"; rmdir "$MNT"; rm "$IMG"' EXIT

    fallocate -l "$IMAGE_SIZE" "$IMG"
    mkfs.ext4 -F "$IMG"
    mkdir -p "$MNT"
    mount "$IMG" "$MNT"

    debootstrap				\
	--no-check-gpg			\
	--arch="$DEBIAN_ARCH"		\
	--include="$PACKAGES"		\
	--exclude="$EXCLUDE"		\
	--foreign				\
	sid "$MNT" "$MIRROR"

    mkdir -p "$MNT/etc/network"
    cat > "$MNT/etc/network/interfaces" <<-ZZ
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp
ZZ

    update_files

    cp -P "/etc/timezone" "/etc/localtime" "$MNT/etc/"

    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/root/.ssh"
    install -m0600 "$ktest_dir/ssh_id.pub" "$MNT/root/.ssh/authorized_keys"

    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 -s /dev/null "$i")
    done

    # disable network interface renaming - it's unreliable
    mkdir -p "$MNT/etc/udev/rules.d/"
    ln -s /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*"

    # xfstests:
    chroot "$MNT" useradd fsgqa

    DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \
     LC_ALL=C LANGUAGE=C LANG=C chroot "$MNT" /debootstrap/debootstrap --second-stage

    DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \
     LC_ALL=C LANGUAGE=C LANG=C chroot "$MNT" dpkg --configure -a

    DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \
     LC_ALL=C LANGUAGE=C LANG=C chroot "$MNT" apt update

    rm -f "$MNT/var/cache/apt/archives/*.deb"

    # Unmount everything in the root
    awk '{print $2}' /proc/mounts|
	grep "^$MNT"|
	sort -r|
	xargs umount

    rmdir "$MNT"
    trap '' EXIT

    # Trim deleted data from the image (around 75MB)
    e2fsck -f "$IMG"
    resize2fs -M "$IMG"		# shrinks the file
    resize2fs "$IMG" "$IMAGE_SIZE"	# re-grows as sparse
}

if [[ $(type -t "$CMD") != function ]]; then
    usage
    exit 1
fi

$CMD "$@"