-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-vm-disk.sh
executable file
·77 lines (59 loc) · 1.48 KB
/
make-vm-disk.sh
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
#!/bin/bash
# Take one argument from the commandline: VM name
if ! [ $# -eq 4 ]; then
echo "Usage: $0 <input_img> <outputname> <size> <ip>"
echo "ie: $0 d12-kube.raw sandbox 30G 172.29.123.10/20"
exit 1
fi
INPUT_IMG=$1
OUTPUT_NAME=$2
SIZE=$3
IP=$4
DEVICE=/dev/loop0
ROOTFS="/tmp/installing-rootfs"
cp $INPUT_IMG $OUTPUT_NAME.raw
losetup -D
losetup -fP $OUTPUT_NAME.raw
echo "Mount OS partition"
ROOTFS="/tmp/installing-rootfs"
mkdir -p ${ROOTFS}
mount ${DEVICE}p1 ${ROOTFS}
echo $OUTPUT_NAME > ${ROOTFS}/etc/hostname
# ifupdown
if [ ! $IP == "dhcp" ]; then
cat << EOF > ${ROOTFS}/etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
allow-hotplug eth0
iface eth0 inet static
address $IP
gateway $GATEWAY
dns-nameservers $DNS
EOF
# netplan static ip
rm ${ROOTFS}/etc/netplan/*
cat << EOF > ${ROOTFS}/etc/netplan/01-netcfg.yaml
network:
version: 2
ethernets:
eth0:
dhcp4: true
dhcp6: false
eth1:
dhcp4: false
dhcp6: false
addresses:
- $IP
EOF
fi
echo "Unmounting filesystems"
umount ${ROOTFS}
losetup -D
qemu-img convert -f raw -O qcow2 $OUTPUT_NAME.raw $OUTPUT_NAME.qcow2
qemu-img resize -f qcow2 $OUTPUT_NAME.qcow2 $SIZE
qemu-img convert -f qcow2 -O vpc $OUTPUT_NAME.qcow2 $OUTPUT_NAME.vhd