summaryrefslogtreecommitdiff
path: root/create-guest-with-cloudinit
blob: 8b64fd8b01e03fd7aacbb7d4b1bf33137199fb5c (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
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
#!/bin/bash 

# depends on following packages:
# apt-get install virtinst libvirt-bin dosfstools mtools kvm

# For resizing the root partition we tried two ways: 

# sfdisk 
# ------
# For sfdisk we need util-linux >= 2.17.2-3 (debian wheezy, ubuntu oneiric), 
# because of the sfdisk-feature which lets us script the growing of the root partition.
# see https://bugs.launchpad.net/ubuntu/+source/util-linux/+bug/686124
# apt-get install -t wheezy util-linux

# parted
# ------
# seems to be more robust than sfdisk, so we use that approach
# unfortunatly, there's no way to resize a partition with parted >= 2.4
# deleting and recreating is the only way, which leaves us with an 
# dirty workaround, giving the beginning of the root partition in sectors
# with the variable $ROOT_PARTITION_BEGINNING


# defaults, configurable in config
VG='vg01'
ETC='/etc/libvirt/local/cloudinit'
CONFIG="$ETC/cloudinit.cfg"
VARDIR='/var/lib/libvirt/cloudinit'
SCRIPTDIR='/usr/local/bin/leap_cloudadmin'
KVM_URI='qemu:///system'
SEED='cloudinit_seed.img'

BASEIMAGE='leap-baseimage'
SWAPUUID='3a79d708-2a74-4344-a953-15e955dbdae7'

# Defaults for leap-baseimage, change if you 
# have a different disk layout 
ROOT_PARTITION_NR='2'
ROOT_PARTITION_BEGINNING='4096s'

# unconfigurable variables
MIN_ROOTSIZE=2
DEFAULT_SWAPSIZE=512
rootsize=$MIN_ROOTSIZE
swapsize=$DEFAULT_SWAPSIZE


# overwrite defaults
[ -e $CONFIG ] && . $CONFIG

[ -z "$LC_ALL" ] && LC_ALL="en_US.UTF-8"


fail() { [ $# -eq 0 ] || echo "$@"; exit 1; }
bad_usage() { usage 1>&2; [ $# -eq 0 ] || fail "$@"; exit 1; }

usage() {
       cat <<EOF

Usage: ${0##*/} -r <gb> -s <mb> -n 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
    -r size        root size in GB. Defaults to minimal value of $MIN_ROOTSIZE GB.
    -s size        swap size in MB. Defaults to $DEFAULT_SWAPSIZE MB.
    -n name	   guestname
EOF
}


# Parse cmdline options

while getopts "hr:s:n:" OPTION
do
  #echo $OPTION $OPTARG
  case $OPTION in
     h) usage
        exit 0;;
     r) rootsize=$OPTARG;; 
     s) swapsize=$OPTARG;;
     n) vmname=$OPTARG;; 
   esac
done

[ -z $vmname  ] && bad_usage "must provide guest name"
[ $# -gt 6 ] && bad_usage "too many arguments"
[ $rootsize -lt $MIN_ROOTSIZE ] && bad_usage "Minimal root size is $MIN_ROOTSIZE GB !"


cfg="$ETC/$vmname.cfg"
rootdev="/dev/$VG/${vmname}-root"
swapdev="/dev/$VG/${vmname}-swap"

# requirements
echo "mtools_skip_check=1">~/.mtoolsrc
[ -e $VARDIR ] || ( mkdir $VARDIR ; chown libvirt-qemu $VARDIR )

[ -e $rootdev ] && fail "$rootdev exists - please delete it or choose another guest name"
[ -e $swapdev ] && fail "$swapdev exists - please delete it or choose another guest name"
[ -e $cfg ] ||  fail "please provide config file in $cfg"

echo
echo "Creating guest VM $vmname:"
echo "Root size: $rootsize GB, using $rootdev"
echo "Swap size: $swapsize MB, using $swapdev"
echo "using $cfg for cloud-init"
echo
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

# create root and swap disk
lvcreate -L ${rootsize}g -n ${vmname}-root $VG
lvcreate -L ${swapsize}m -n ${vmname}-swap $VG
mkswap  -U $SWAPUUID $swapdev

sleep 4 
virt-clone --connect $KVM_URI -o $BASEIMAGE -n $vmname -f $rootdev  --force

# resize root partition

# echo ",+," | sfdisk -q -N2  /dev/$VG/$vmname > /dev/null 2>&1
# sfdisk is unreliable for us and doesn't work on a new kvm host
parted $rootdev --script -- rm $ROOT_PARTITION_NR
parted $rootdev --script -- mkpart primary $ROOT_PARTITION_BEGINNING -1s

# create cloudinit seed disk 
csplit -q --prefix "$VARDIR/$vmname" $cfg '/#cloud-config/'
metadata="$VARDIR/${vmname}00"
userdata="$VARDIR/${vmname}01"
$SCRIPTDIR/libvirt-make-seed-disk $VARDIR/$vmname-$SEED $userdata $metadata


echo
echo "Cloudinit-seeddisk is at $VARDIR/$vmname-$SEED, attaching after booting"
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 

# create seed- and swapdisk definition and attach them
virsh attach-disk $vmname --source $swapdev --target vdb  --config
virsh attach-disk $vmname --source $VARDIR/$vmname-$SEED --target vdc --sourcetype file --type disk --driver qemu --subdriver raw --config

virsh console $vmname