-
Notifications
You must be signed in to change notification settings - Fork 2
/
qemu-debian-create-image
executable file
·184 lines (148 loc) · 5.22 KB
/
qemu-debian-create-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
#!/bin/bash
set -e
# Example usage:
# - use local APT cache, and set-up serial console
# MIRROR="http://localhost:9999/debian" GRUB_CMDLINE="console=tty0 console=ttyS0" ./qemu-debian-create-image foo.qcow2 myhostname stretch
# - use local APT cache, and set-up serial console, no network interface configuration (default is DHCP)
# MIRROR="http://localhost:9999/debian" GRUB_CMDLINE="console=tty0 console=ttyS0" ETH_DEVICE="no" ./qemu-debian-create-image foo.qcow2 myhostname stretch
# Then start with:
# kvm -hda foo.img -net nic -net user -m 256 -serial stdio
# Configs overwritable via environment variables
VSYSTEM=${VSYSTEM:=qemu} # Either 'qemu' or 'kvm'
FLAVOUR=${FLAVOUR:=debian} # Either 'debian' or 'ubuntu'
INCLUDES=${INCLUDES:="less,vim,sudo,openssh-server,dbus,net-tools,traceroute,mtr-tiny,telnet,wget,curl,ethtool"}
MIRROR=${MIRROR:="http://deb.debian.org/debian"}
#MIRROR=${MIRROR:="http://localhost:3142/debian"}
#MIRROR=${MIRROR:="http://archive.ubuntu.com/ubuntu/"}
#MIRROR=${MIRROR:="http://localhost:3142/ubuntu"}
ARCH=${ARCH:=amd64}
APT_CACHER=${APT_CACHER:=no}
IMGSIZE=${IMGSIZE:=8G}
ROOTPASSWD=${ROOTPASSWD:=root}
DISKNAME=${DISKNAME:=sda}
GRUB_CMDLINE=${GRUB_CMDLINE:=console=ttyS0}
ETH_DEVICE=${ETH_DEVICE:=ens3}
clean_debian() {
[ "$MNT_DIR" != "" ] && chroot $MNT_DIR umount /proc/ /sys/ /dev/ /boot/
sleep 1s
[ "$MNT_DIR" != "" ] && umount $MNT_DIR
sleep 1s
[ "$DISK" != "" ] && $VSYSTEM-nbd -d $DISK
sleep 1s
[ "$MNT_DIR" != "" ] && rm -r $MNT_DIR
}
fail() {
clean_debian
echo ""
echo "FAILED: $1"
exit 1
}
cancel() {
fail "CTRL-C detected"
}
if [ $# -lt 3 ]
then
echo "author: Kamil Trzcinski (http://ayufan.eu)"
echo "license: GPL"
echo "usage: $0 <image-file> <hostname> <release> [optional debootstrap args]" 1>&2
exit 1
fi
FILE=$1
HOSTNAME=$2
RELEASE=$3
shift 3
trap cancel INT
echo "Installing $RELEASE into $FILE..."
MNT_DIR=`tempfile`
rm $MNT_DIR
mkdir $MNT_DIR
DISK=
# add apt cacher for faster rebuilds, runs on 3142
if [ "$APT_CACHER" == "yes" ]; then
echo "Installing apt-cacher-ng for fast rebuilds"
apt-get install apt-cacher-ng
fi
if [ ! -f $FILE ]; then
echo "Creating $FILE"
$VSYSTEM-img create -f qcow2 $FILE $IMGSIZE
fi
if [ $FLAVOUR == "debian" ]; then
BOOT_PKG="linux-image-$ARCH grub-pc"
elif [ $FLAVOUR == "ubuntu" ]; then
BOOT_PKG="linux-image-generic grub-pc"
fi
echo "Looking for nbd device..."
modprobe nbd max_part=16 || fail "failed to load nbd module into kernel"
for i in /dev/nbd*
do
if $VSYSTEM-nbd -c $i $FILE
then
DISK=$i
break
fi
done
[ "$DISK" == "" ] && fail "no nbd device available"
echo "Connected $FILE to $DISK"
# New sfdisk on stretch doesn't do -D and -uM anymore
echo "Partitioning $DISK..."
sfdisk $DISK -q << EOF || fail "cannot partition $FILE"
label: dos
,200M,83,*
;
EOF
echo "Creating boot partition..."
mkfs.ext4 -q ${DISK}p1 || fail "cannot create /boot ext4"
echo "Creating root partition..."
mkfs.ext4 -q ${DISK}p2 || fail "cannot create / ext4"
echo "Mounting root partition..."
mount ${DISK}p2 $MNT_DIR || fail "cannot mount /"
echo "Installing Debian $RELEASE..."
debootstrap --include=$INCLUDES $* $RELEASE $MNT_DIR $MIRROR || fail "cannot install $RELEASE into $DISK"
echo "Configuring system..."
cat <<EOF > $MNT_DIR/etc/fstab
/dev/${DISKNAME}1 /boot ext4 sync 0 2
/dev/${DISKNAME}2 / ext4 errors=remount-ro 0 1
EOF
echo $HOSTNAME > $MNT_DIR/etc/hostname
cat <<EOF > $MNT_DIR/etc/hosts
127.0.0.1 localhost
127.0.1.1 $HOSTNAME
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
EOF
cat <<EOF > $MNT_DIR/etc/network/interfaces
auto lo
iface lo inet loopback
EOF
if [ "$ETH_DEVICE" != "no" ]; then
cat <<EOF > $MNT_DIR/etc/network/interfaces
auto ${ETH_DEVICE}
iface ${ETH_DEVICE} inet dhcp
EOF
fi
mount --bind /dev/ $MNT_DIR/dev || fail "cannot bind /dev"
chroot $MNT_DIR mount -t ext4 ${DISK}p1 /boot || fail "cannot mount /boot"
chroot $MNT_DIR mount -t proc none /proc || fail "cannot mount /proc"
chroot $MNT_DIR mount -t sysfs none /sys || fail "cannot mount /sys"
LANG=C DEBIAN_FRONTEND=noninteractive chroot $MNT_DIR apt-get install -y --force-yes -q $BOOT_PKG || fail "cannot install $BOOT_PKG"
chroot $MNT_DIR grub-install --skip-fs-probe $DISK || fail "cannot install grub"
sed -i "s|GRUB_CMDLINE_LINUX_DEFAULT=.*|GRUB_CMDLINE_LINUX_DEFAULT=\"${GRUB_CMDLINE}\"|" $MNT_DIR/etc/default/grub
sed -i "s|GRUB_TIMEOUT=5|GRUB_TIMEOUT=1|" $MNT_DIR/etc/default/grub
echo 'GRUB_DISABLE_OS_PROBER=true' >> $MNT_DIR/etc/default/grub
chroot $MNT_DIR update-grub || fail "cannot update grub"
chroot $MNT_DIR apt-get clean || fail "unable to clean apt cache"
chroot $MNT_DIR rm -rf /var/lib/apt/lists/*
sed -i "s|${DISK}p1|/dev/${DISKNAME}1|g" $MNT_DIR/boot/grub/grub.cfg
sed -i "s|${DISK}p2|/dev/${DISKNAME}2|g" $MNT_DIR/boot/grub/grub.cfg
# Set root password
echo "root:${ROOTPASSWD}" | chroot $MNT_DIR chpasswd
echo "Finishing grub installation..."
grub-install $DISK --root-directory=$MNT_DIR --modules="biosdisk part_msdos" --skip-fs-probe || fail "cannot reinstall grub"
echo "SUCCESS!"
clean_debian
echo "Shrinking qcow2 file..."
qemu-img convert -O qcow2 $FILE $FILE.new
mv $FILE.new $FILE
exit 0