-
Notifications
You must be signed in to change notification settings - Fork 3
/
kvmstrap
executable file
·122 lines (102 loc) · 3.02 KB
/
kvmstrap
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
#!/bin/bash
# Version info
# TODO: Manually increase this number after any substantial changes
VERSION=1
set -e
BASE="$(dirname "$0")"
SRC="$BASE/rootfs"
DST=/tmp/target
INPUT="${1:-input.qcow2}"
APT_SOURCE="${APT_SOURCE:-https://mirrors.ustc.edu.cn}"
cd "$BASE"
. ./variables
run() {
chroot "$DST" "$@"
}
add_file() {
while [ "$#" -ne 0 ]; do
rsync -rlp "$SRC/$1" "$DST/$1"
shift
done
}
add_package() {
DEBIAN_FRONTEND=noninteractive run apt-get install -y --no-install-recommends "$@"
}
remove_package() {
DEBIAN_FRONTEND=noninteractive run apt-get purge -y --autoremove "$@"
}
mount_all() {
mount -t tmpfs none "$DST/dev"
chmod 755 "$DST/dev"
mknod -m0666 "$DST/dev/null" c 1 3
mknod -m0666 "$DST/dev/random" c 1 8
mknod -m0666 "$DST/dev/urandom" c 1 9
mknod -m0666 "$DST/dev/zero" c 1 5
ln -sf /proc/self/fd/0 "$DST/dev/stdin"
ln -sf /proc/self/fd/1 "$DST/dev/stdout"
ln -sf /proc/self/fd/2 "$DST/dev/stderr"
mkdir -p "$DST/dev/pts"
mount -t devpts none "$DST/dev/pts"
mount -t proc proc "$DST/proc"
mount -t tmpfs none "$DST/run"
mount -t sysfs sys "$DST/sys"
mount -t tmpfs none "$DST/tmp"
}
umount_all() {
umount "$DST/dev/pts"
umount "$DST/dev"
umount "$DST/proc"
umount "$DST/run"
umount "$DST/sys"
umount "$DST/tmp"
umount "$DST"
}
mkdir -p "$DST"
if [ -b "$INPUT" ]; then
mount "$INPUT" "$DST"
else
qemu-img resize "$INPUT" 4096M
guestmount -a "$INPUT" -m /dev/sda1 "$DST"
fi
mount_all
trap umount_all EXIT
# Preliminary cleanup
rm -f "$DST/etc/resolv.conf"
cp -f /etc/resolv.conf "$DST/etc/resolv.conf"
add_file /etc/apt/
run sed -i "s,%APT_SOURCE%,$APT_SOURCE,g" /etc/apt/sources.list.d/ubuntu.sources
run apt-mark auto '~i!~M'
run apt-get update
run apt-get -y dist-upgrade
add_package linux-virtual ubuntu-minimal ubuntu-server ubuntu-standard
# Add files
add_file /etc/{apt,cloud,default,profile.d,skel,ssh,sysctl.d,systemd}/ /etc/locale.gen
# Remove "pass" from fstab
sed -Ei 's/\b[0-9]+$/0/g' "$DST"/etc/fstab
# Remove root password
#sed -Ei 's/^(root:)[^:]*(:.*$)/\1\2/' "$DST"/etc/shadow
run passwd -d root
# Simulate systemctl enable
#ln -sfn /etc/systemd/system/vlab-startup.service "$DST"/etc/systemd/system/multi-user.target.wants/vlab-startup.service
run systemctl enable vlab-startup.service
# Write version info
VLAB_VERSION="$VERSION:$(date +%y%m%d)"
if [ -n "$GITHUB_RUN_NUMBER" ]; then
VLAB_VERSION="$VLAB_VERSION-gh-$GITHUB_RUN_NUMBER"
fi
FILE="$DST"/etc/vlab_version
echo "VLAB_DISTRO=ubuntu" > "$FILE"
echo "VLAB_VERSION=$VLAB_VERSION" >> "$FILE"
chmod 444 "$FILE"
# Set the apt source of the resulting image to USTC mirrors
add_file /etc/apt/sources.list.d/ubuntu.sources
run sed -i "s,%APT_SOURCE%,https://mirrors.ustc.edu.cn,g" /etc/apt/sources.list.d/ubuntu.sources
run apt clean
# Final cleanup
set +e
rm -rf "$DST/var/lib/apt/lists"/* "$DST/var/lib/dpkg"/*-old
rm -rf "$DST/var/cache"/*
rm -f "$DST/etc/resolv.conf"
ln -sfn ../run/systemd/resolve/stub-resolv.conf "$DST/etc/resolv.conf"
rm -rf "$DST/etc/ssh"/ssh_host_*_key{,.pub}
rmdir "$DST"/*.usr-is-merged || true