-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomize-cloud-image
executable file
·55 lines (50 loc) · 1.46 KB
/
customize-cloud-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
#!/bin/sh -ex
IMAGE=$1
shift
modprobe nbd
for nbd_dev in /sys/devices/virtual/block/nbd*;
do
if [ -e ${nbd_dev}/pid ]
then
continue
else
NBD="/dev/$(basename $nbd_dev)"
NBDp1="/dev/mapper/$(basename $nbd_dev)p1"
fi
if qemu-nbd -c $NBD $IMAGE; then break; fi
echo "$NBD is not available. Try to use the next released device."
NBD=""
NBDp1=""
sleep 1s
done
if [ -z "$NBD" ]; then
exit 1
fi
trap "kpartx -d $NBD 2>/dev/null; qemu-nbd -d $NBD" INT QUIT TERM 0
# resize part1
growpart $NBD 1
# copy files
kpartx -a $NBD
sleep 2
mount $NBDp1 /mnt
mkdir /mnt/extras
cp -a "$@" /mnt/extras
chroot /mnt systemctl disable systemd-resolved.service
rm -f /mnt/etc/resolv.conf
chroot /mnt sed -i '/127.0.0.1 localhost/a 127.0.1.1 boot' /etc/hosts
chroot /mnt dpkg -r --force-depends systemd-timesyncd
# We should prevent foreign routing policy rules and routes from being dropped.
# ref. https://github.com/cybozu-go/neco/issues/1918
chroot /mnt mkdir -p /etc/systemd/networkd.conf.d
tee /mnt/etc/systemd/networkd.conf.d/01-cybozu.conf << EOF >/dev/null
[Network]
ManageForeignRoutingPolicyRules=no
ManageForeignRoutes=no
EOF
# There is a bug that fails to load kernel modules by 9pnet at starts up.
# So we should force to load kernel modules.
# ref. https://bugzilla.redhat.com/show_bug.cgi?id=1499896
chroot /mnt sh -c "echo 9pnet_virtio > /etc/modules-load.d/9pnet_virtio.conf"
chroot /mnt sh -c "dpkg -i /extras/*.deb"
sync
umount /mnt