blob: ad1e26fca84dd51231a73fdb844f9f6ceecd7058 (
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
|
#!/bin/sh
# depends on following packages:
# apt-get install virtinst libvirt-bin dosfstools mtools kvm kvm-qemu
# apt-get install -t wheezy util-linux
# defaults, configurable in config
VG='vg01'
ETC='/etc/cloudinit'
CONFIG="$ETC/cloudinit.cfg"
VARDIR='/var/lib/libvirt/cloudinit'
SCRIPTDIR='/usr/local/bin/leap_cloudadmin'
KVM_URI='qemu:///system'
SEED='cloudinit_seed.img'
# overwrite defaults
[ -e $CONFIG ] && . $CONFIG
# unconfigurable variables
# swapsize is swapsize of the base image
SWAPSIZE=4
MIN_DISKSIZE=10
size=$MIN_DISKSIZE
fail() { [ $# -eq 0 ] || echo "$@"; exit 1; }
bad_usage() { usage 1>&2; [ $# -eq 0 ] || fail "$@"; exit 1; }
usage() {
cat <<EOF
Usage: ${0##*/} -s .. guest_name
Creates a new guest VM using cloud-init to utilize nocloud.
Uses $ETC/<guest_name>.cfg as meta- und user-data file
options:
-h show usage
-s size disk size in GB, including $SWAPSIZE GB swap. Defaults to minimal value of $MIN_DISKSIZE GB.
EOF
}
short_opts="hs:"
getopt_out=$(getopt --name "${0##*/}" \
--options "${short_opts}" -- "$@") &&
eval set -- "${getopt_out}" ||
fail "Error !"
while [ $# -ne 0 ]; do
cur=${1}; next=${2};
case "$cur" in
-h) Usage ; exit 0;;
-s) size=$next; shift;;
--) shift; break;;
esac
shift;
done
[ $# -lt 1 ] && bad_usage "must provide guest name"
[ $# -gt 1 ] && bad_usage "too many arguments"
[ $size -lt $MIN_DISKSIZE ] && bad_usage "Minimal disk size is 10 GB !"
# requirements
echo "mtools_skip_check=1">~/.mtoolsrc
[ -e $VARDIR ] || ( mkdir $VARDIR ; chown libvirt-qemu $VARDIR )
vmname=$1
cfg="$ETC/$vmname.cfg"
[ -e /dev/$VG/$vmname ] && fail "/dev/$VG/$vmname exists - please delete it or choose another guest name"
[ -e $cfg ] || fail "please provide config file in $cfg"
echo
echo "Creating guest VM $vmname with size: $size GB, using $cfg for cloud-init"
echo "OK ? <Ctrl-C> to abort, <enter> to resume."
read enter
# is a VM with the same name defined ?
virsh dominfo $vmname > /dev/null 2>&1
if [ $? -eq 0 ]; then fail "Domain $vmname is defined in libvirt. Please undefine first."; fi
lvcreate -L ${size}g -n $vmname $VG
virt-clone --connect $KVM_URI -o leap-baseimage-wheezy -n $vmname -f /dev/$VG/$vmname --force
# resize second (root) partition
echo ",+," | sfdisk -N2 /dev/$VG/$vmname > /dev/null
# create cloudinit seed disk
csplit --prefix "$VARDIR/$vmname" $cfg '/#cloud-config/'
metadata="$VARDIR/${vmname}00"
userdata="$VARDIR/${vmname}01"
$SCRIPTDIR/libvirt-make-seed-disk $VARDIR/$vmname-$SEED $userdata $metadata
$SCRIPTDIR/libvirt-write-disk-attach-xml $VARDIR/$vmname-$SEED > $VARDIR/$vmname-disk-attach.xml
echo
echo "Attached cloudinit-seeddisk is at $VARDIR/$vmname-$SEED"
echo
echo "Finished creating guest $vmname. Enjoy."
echo
echo "Press <return> to start the new guest."
read bogus
echo
echo 'Starting new guest, attaching to console. Exit with Ctrl+]'
echo
virsh start $vmname
sleep 2
virsh attach-device $vmname $VARDIR/$vmname-disk-attach.xml
virsh console $vmname
#do we really need to detach the cloud-init disk ?
#virsh detach-device $vmname $VARDIR/$vmname-disk-attach.xml
|