-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcreate-vm
More file actions
executable file
·848 lines (726 loc) · 17.5 KB
/
create-vm
File metadata and controls
executable file
·848 lines (726 loc) · 17.5 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
#!/bin/bash -eu
#
# Create a base libvirt guest VM
#
function pr_error()
{
echo -e "\033[31m${*}\033[0m" 1>&2
}
function pr_warn()
{
echo -e "\033[96m${*}\033[0m"
}
function pr_info()
{
echo -e "\033[33m${*}\033[0m"
}
function out()
{
local rc=${?}
trap - EXIT INT TERM HUP
if [ -n "${IMG_MOUNTP}" ] ; then
unmount_image "${IMG_MOUNTP}"
fi
rm -rf "${TMP_DIR}"
if [ "${rc}" -ne 0 ] ; then
pr_error "Script failed"
fi
exit "${rc}"
}
function print_var()
{
local var val
for var in "${@}" ; do
val=${!var}
if [ -n "${val}" ] ; then
pr_info "$(printf "%-13s: %s\n" "${var}" "${val}")"
fi
done
}
function mount_image()
{
local image=${1} mountp=${2}
local kernel
# HACK: guestmount needs to be able to read the latest kernel
kernel=$(find /boot/ -maxdepth 1 -name 'vmlinuz-*' | sort -V | tail -1)
test -r "${kernel}" || sudo chmod 644 "${kernel}"
guestmount --pid-file "${mountp}.pid" -a "${image}" -m /dev/sda1 --ro \
-o dev -o allow_root "${mountp}"
IMG_MOUNTP=${mountp}
}
function unmount_image()
{
local mountp=${1}
local pid count
pid=$(cat "${mountp}.pid")
guestunmount "${mountp}"
count=10
while kill -0 "${pid}" 2>/dev/null && [ ${count} -gt 0 ] ; do
sleep 1
((count--))
done
if [ ${count} -eq 0 ] ; then
pr_error "Failed to unmount image (timeout)"
exit 1
fi
rm -f "${mountp}.pid"
IMG_MOUNTP=
}
function url_valid()
{
local url=${1}
curl --head --silent --fail "${url}" 2>/dev/null
}
function download_image()
{
local url=${1}
local suffix tmp_img
pr_info "Download image ${url}"
tmp_img=${TMP_DIR}/base-img
# Is it a compressed image?
case "${url}" in
*.xz)
suffix=.xz
;;
*)
suffix=
;;
esac
# Download the image, decompress it and copy it to the cache
wget -O "${tmp_img}${suffix}" "${url}"
# Decompress the image
if [ -n "${suffix}" ] ; then
unxz -f "${tmp_img}${suffix}"
fi
# Move the image to the cache
sudo mv "${tmp_img}" "${BASE_IMAGE}"
sudo chmod 644 "${BASE_IMAGE}"
}
function download_fedora_image()
{
local url pattern name
if [ "${RELEASE}" -gt 27 ] ; then
url=https://download.fedoraproject.org/pub/fedora/linux/releases/${RELEASE}/Cloud/${ARCH}/images
else
url=https://download.fedoraproject.org/pub/fedora/linux/releases/${RELEASE}/CloudImages/${ARCH}/images
fi
pattern=">Fedora-Cloud-Base-${RELEASE}-.*\.${ARCH}\.qcow2<"
name=$(wget -q -O - "${url}" | grep -oP "${pattern}" | tr -d '><')
url=${url}/${name}
download_image "${url}"
}
function download_debian_image()
{
local url name
url=https://cdimage.debian.org/cdimage/cloud/${RELEASE}/daily/latest
name=$(wget -q -O - "${url}"/SHA512SUMS | grep -- "-generic-${ARCH}-.*\.qcow" | sed 's/.* //')
url=${url}/${name}
download_image "${url}"
}
function download_ubuntu_image()
{
local suffix relnum name url
suffix=
if [ "${RELEASE}" = "precise" ] || [ "${RELEASE}" = "trusty" ] || \
[ "${RELEASE}" = "xenial" ] ; then
if [ "${BOOT_MODE}" = "uefi" ] ; then
suffix="-uefi1"
else
suffix="-disk1"
fi
fi
relnum=$(ubuntu-distro-info --series "${RELEASE}" --release)
relnum=${relnum%% *}
# Release image
name=ubuntu-${relnum}-server-cloudimg-${ARCH}${suffix}.img
url=https://cloud-images.ubuntu.com/releases/${RELEASE}/release/${name}
if url_valid "${url}" ; then
download_image "${url}"
return
fi
# Daily image
name=${RELEASE}-server-cloudimg-${ARCH}${suffix}.img
url=https://cloud-images.ubuntu.com/daily/server/${RELEASE}/current/${name}
if url_valid "${url}" ; then
download_image "${url}"
return
fi
pr_error "Faild to download image"
exit 1
}
function download_core_image()
{
local channel num name build url
if [ -n "${GRADE}" ] ; then
channel="${GRADE#-}"
else
channel="stable"
fi
num=${RELEASE#core}
name=ubuntu-core-${num}-${ARCH}.img.xz
for build in current pending ; do
url=http://cdimage.ubuntu.com/ubuntu-core/${num}/${channel}/${build}/${name}
if url_valid "${url}" ; then
download_image "${url}"
return
fi
done
pr_error "Faild to download image"
exit 1
}
function download_opensuse_image()
{
local release name url
if [ "${RELEASE}" = "tumbleweed" ] ; then
url=https://download.opensuse.org/tumbleweed/appliances/openSUSE-Tumbleweed-JeOS.x86_64-OpenStack-Cloud.qcow2
else
release=${RELEASE^}
name=openSUSE-${release}-OpenStack.x86_64.qcow2
url=https://download.opensuse.org/repositories/Cloud:/Images:/${release/-/_}/images/${name}
fi
download_image "${url}"
}
function download_base_image()
{
# Check if the image is already cached
if [ -e "${BASE_IMAGE}" ] && [ "${DOWNLOAD}" -eq 0 ] ; then
pr_info "Use cached base image"
return
fi
# Download the image
case "${RELEASE}" in
core*)
download_core_image
;;
*)
download_"${VENDOR}"_image
esac
}
function create_os_image()
{
local tmp_xml tmp_mnt
if virsh vol-info --pool default "${OS_IMAGE}" >/dev/null 2>&1 ; then
pr_info "OS image exists already"
return
fi
tmp_xml=${TMP_DIR}/os-image-xml
tmp_mnt=${TMP_DIR}/os-image-mnt
# Extract the kernel and initrd from the image for direct boot
if [ "${BOOT_MODE}" = "direct" ] ; then
pr_info "Extract kernel and initrd from base image"
mkdir -p "${tmp_mnt}"
mount_image "${BASE_IMAGE}" "${tmp_mnt}"
# Create the libvirt volume for the kernel
cat <<EOF >"${tmp_xml}"
<volume type='file'>
<name>${KERNEL}</name>
<capacity unit='bytes'>0</capacity>
<target>
<format type='raw'/>
</target>
</volume>
EOF
virsh vol-create --pool default "${tmp_xml}"
virsh vol-upload --pool default "${KERNEL}" "${tmp_mnt}/boot/vmlinuz"
# Create the libvirt volume for the initrd
cat <<EOF >"${tmp_xml}"
<volume type='file'>
<name>${INITRD}</name>
<capacity unit='bytes'>0</capacity>
<target>
<format type='raw'/>
</target>
</volume>
EOF
virsh vol-create --pool default "${tmp_xml}"
virsh vol-upload --pool default "${INITRD}" "${tmp_mnt}/boot/initrd.img"
unmount_image "${tmp_mnt}"
fi
pr_info "Creating OS image"
# Create the libvirt volume for the OS image
cat <<EOF >"${tmp_xml}"
<volume type='file'>
<name>${OS_IMAGE}</name>
<capacity unit='bytes'>0</capacity>
<target>
<format type='qcow2'/>
</target>
</volume>
EOF
virsh vol-create --pool default "${tmp_xml}"
virsh vol-upload --pool default "${OS_IMAGE}" "${BASE_IMAGE}"
virsh vol-resize --pool default "${OS_IMAGE}" "${SIZE}"G
}
function create_config_drive()
{
local public_key tmp_img tmp_xml tmp_dir
if [ -z "${CONFIG_DRIVE}" ] ; then
pr_info "No config drive required"
return
fi
if virsh vol-info --pool default "${CONFIG_DRIVE}" >/dev/null 2>&1 ; then
pr_info "Config drive exists already"
return
fi
pr_info "Create config drive"
public_key=$(cat ~/.ssh/id_rsa.pub)
tmp_img=${TMP_DIR}/config-drive-img
tmp_xml=${TMP_DIR}/config-drive-xml
tmp_dir=${TMP_DIR}/config-drive-dir
mkdir -p "${tmp_dir}"
# meta data
cat <<EOF >"${tmp_dir}/meta-data"
{ instance-id: $(uuidgen) }
EOF
# user data
cat <<EOF >"${tmp_dir}/user-data"
#cloud-config
hostname: ${NAME}.localdomain
manage_etc_hosts: localhost
chpasswd: { expire: false }
password: ${VENDOR}
ssh_authorized_keys: [ ${public_key} ]
ssh_pwauth: true
# Datasource timeout
max_wait: 2
# Copy the content of the config drive and run the setup-vm init script
runcmd:
- |
if ! [ -d /etc/cloud/cidata ] ; then
test -d /mnt/cidata || mkdir /mnt/cidata
dev=\$(findfs LABEL=cidata)
mount \${dev} /mnt/cidata
cp -aR /mnt/cidata/ /etc/cloud/
umount /mnt/cidata
/etc/cloud/cidata/setup-vm &
fi
EOF
# Copy the setup-vm init script and create its config file
cp "${CONFIG_DIR}"/create-vm/setup-vm "${tmp_dir}"
cat <<EOF >"${tmp_dir}"/setup-vm.config
REBOOT=${REBOOT}
SHUTDOWN=${SHUTDOWN}
TYPE=${TYPE}
EOF
# Copy the APT ESM sources list
if [ -e "${CONFIG_DIR}/${RELEASE}-esm-ppa.list" ] ; then
cp "${CONFIG_DIR}/${RELEASE}-esm-ppa.list" "${tmp_dir}"/esm-ppa.list
fi
# Create the config drive ISO image
genisoimage -ldots -allow-lowercase -allow-multidot -l -J -r \
-o "${tmp_img}" -V cidata "${tmp_dir}"
# Create the libvirt volume for the config drive
cat <<EOF >"${tmp_xml}"
<volume type='file'>
<name>${CONFIG_DRIVE}</name>
<capacity unit='bytes'>0</capacity>
<target>
<format type='raw'/>
</target>
</volume>
EOF
virsh vol-create --pool default "${tmp_xml}"
virsh vol-upload --pool default "${CONFIG_DRIVE}" "${tmp_img}"
}
function define_vm()
{
local tmp_xml
pr_info "Define VM"
tmp_xml=${TMP_DIR}/vm-xml
# Defaults
XML_UEFI_BOOT_BEGIN="<!--"
XML_UEFI_BOOT_END="-->"
XML_DIRECT_BOOT_BEGIN="<!--"
XML_DIRECT_BOOT_END="-->"
XML_OS_IMAGE_DISK_SATA_BEGIN="<!--"
XML_OS_IMAGE_DISK_SATA_END="-->"
XML_OS_IMAGE_DISK_VIRTIO_BEGIN="<!--"
XML_OS_IMAGE_DISK_VIRTIO_END="-->"
XML_CONFIG_DRIVE_DISK_BEGIN="<!--"
XML_CONFIG_DRIVE_DISK_END="-->"
# Arch-specific XML settings
case "${ARCH}" in
i386|i686)
XML_ARCH=i686
;;
amd64|x86_64)
XML_ARCH=x86_64
;;
s390x)
XML_ARCH=s390x
;;
arm64)
XML_ARCH=aarch64
;;
armhf)
XML_ARCH=armv7l
;;
ppc64el)
XML_ARCH=ppc64le
;;
*)
pr_error "Unsupported architecture: ${ARCH}"
exit 1
;;
esac
if [ "${BOOT_MODE}" = "uefi" ] ; then
if [ -e /usr/share/OVMF/OVMF_CODE_4M.fd ] ; then
XML_OVMF_CODE=/usr/share/OVMF/OVMF_CODE_4M.fd
else
XML_OVMF_CODE=/usr/share/OVMF/OVMF_CODE.fd
fi
XML_UEFI_BOOT_BEGIN=
XML_UEFI_BOOT_END=
elif [ "${BOOT_MODE}" = "direct" ] ; then
XML_KERNEL_PATH=$(virsh vol-path --pool default "${KERNEL}")
XML_INITRD_PATH=$(virsh vol-path --pool default "${INITRD}")
XML_DIRECT_BOOT_BEGIN=
XML_DIRECT_BOOT_END=
fi
XML_OS_IMAGE_PATH=$(virsh vol-path --pool default "${OS_IMAGE}")
if [ "${RELEASE#core}" != "${RELEASE}" ] ; then
# Ubuntu core is special :-(
XML_OS_IMAGE_DISK_SATA_BEGIN=
XML_OS_IMAGE_DISK_SATA_END=
else
XML_OS_IMAGE_DISK_VIRTIO_BEGIN=
XML_OS_IMAGE_DISK_VIRTIO_END=
fi
if [ -n "${CONFIG_DRIVE}" ] ; then
XML_CONFIG_DRIVE_PATH=$(virsh vol-path --pool default "${CONFIG_DRIVE}")
XML_CONFIG_DRIVE_DISK_BEGIN=
XML_CONFIG_DRIVE_DISK_END=
fi
XML_NAME=${NAME}
XML_UUID=$(uuidgen)
XML_MEMORY=${MEMORY}
XML_VCPU=${VCPU}
XML_VCPU_THREADS=${VCPU_THREADS}
XML_MAC_ADDRESS="52:54:00:${XML_UUID:0:2}:${XML_UUID:2:2}:${XML_UUID:4:2}"
# Export the variables for the domain XML
# shellcheck disable=SC2090
export XML_ARCH
export XML_OVMF_CODE
export XML_UEFI_BOOT_BEGIN XML_UEFI_BOOT_END
export XML_KERNEL_PATH XML_INITRD_PATH \
XML_DIRECT_BOOT_BEGIN XML_DIRECT_BOOT_END
export XML_OS_IMAGE_PATH \
XML_OS_IMAGE_DISK_SATA_BEGIN XML_OS_IMAGE_DISK_SATA_END \
XML_OS_IMAGE_DISK_VIRTIO_BEGIN XML_OS_IMAGE_DISK_VIRTIO_END
export XML_CONFIG_DRIVE_PATH \
XML_CONFIG_DRIVE_DISK_BEGIN XML_CONFIG_DRIVE_DISK_END
export XML_NAME XML_UUID XML_MEMORY XML_VCPU XML_VCPU_THREADS \
XML_MAC_ADDRESS
# Create the domain XML and define the VM
envsubst < "${CONFIG_DIR}/create-vm/domain-${XML_ARCH}.xml" > \
"${tmp_xml}"
virsh define "${tmp_xml}"
}
function usage()
{
cat <<EOF
Usage: create-vm [-a ARCH] [-b MODE] [-d] [-f] [-g GRADE] [-h] [-m SIZE] [-o]
[-s SIZE] [-t TYPE] [-y] DIST [NAME]
Create a libvirt guest VM.
Positional arguments:
DIST A recognized distribution, like bionic, stretch,
fedora-26, ...
NAME The libvirt domain name. If not specified, defaults to
<DIST>[-<ARCH>][-uefi].
Optional arguments:
-a, --arch ARCH Architecture of the VM. If not specified, defaults to
the host architecture.
-b, --boot-mode MODE Set the boot mode (bios, uefi, direct). If not
provided, defaults to 'bios'.
-d, --debug Enable debugging (set -x).
-f, --force Force the recreation of an existing VM.
-g, --grade GRADE Image grade (edge, beta, candidate, stable, dangerous-edge, ...).
-h, --help Show this help text and exit.
-i, --image IMAGE Use the image IMAGE as the base image.
-m, --memory SIZE Memory size in KB. If not provided, defaults to
1024(KB).
-o, --download Download and use a new cloud image rather than using an
already cached image.
-s, --size SIZE Image size in GB. If not provided, defaults to 8(GB).
-t, --type TYPE Create a VM of type <TYPE>. Valid types are:
dep8: For dep8 kernel testing.
adt: For ADT testing.
-y, --dry-run
EOF
}
# -----------------------------------------------------------------------------
# Main entry point
ARCH=
FORCE=0
GRADE=
DOWNLOAD=0
SIZE=8
TYPE=
BOOT_MODE="bios"
DIST=
NAME=
DRY_RUN=0
MEMORY=1024
BASE_IMAGE=
VCPU=1
VCPU_THREADS=1
IMG_MOUNTP=
START=0
SHUTDOWN=0
REBOOT=1
while [ ${#} -gt 0 ] ; do
case ${1} in
-a|--arch)
shift
ARCH=${1}
;;
-b|--boot-mode)
shift
BOOT_MODE=${1}
;;
-d|--debug)
export LIBGUESTFS_DEBUG=1
set -x
;;
-f|--force)
FORCE=1
;;
-g|--grade)
shift
GRADE=-${1}
;;
-h|--help)
usage
exit
;;
-i|--image)
shift
BASE_IMAGE=${1}
;;
-m|--memory)
shift
MEMORY=${1}
;;
-o|--download)
DOWNLOAD=1
;;
-s|--size)
shift
SIZE=${1}
;;
-t|--type)
shift
TYPE=${1}
;;
-y|--dry-run)
DRY_RUN=1
;;
-*)
pr_error "Unexpected option: ${1}"
exit 2
;;
*)
if [ -z "${DIST}" ] ; then
DIST=${1}
elif [ -z "${NAME}" ] ; then
NAME=${1}
else
pr_error "Unexpected argument: ${1}"
exit 2
fi
;;
esac
shift
done
if [ -z "${DIST}" ] ; then
usage
exit 2
fi
case "${BOOT_MODE}" in
bios|uefi|direct) ;;
*)
pr_error "Invalid boot mode: ${BOOT_MODE}"
exit 1
;;
esac
case "${TYPE}" in
""|dep8|adt) ;;
*)
pr_error "Invalid type: ${TYPE}"
exit 1
;;
esac
if [ -n "${BASE_IMAGE}" ] && ! [ -e "${BASE_IMAGE}" ] ; then
pr_error "No such image file: ${BASE_IMAGE}"
exit 1
fi
# -----------------------------------------------------------------------------
# Set global variables
# The image vendor and release
case "${DIST}" in
leap-*|tumbleweed)
VENDOR=opensuse
RELEASE=${DIST}
;;
*-*)
VENDOR=${DIST%-*}
RELEASE=${DIST#*-}
;;
sid|trixie|bookworm|bullseye)
VENDOR=debian
RELEASE=${DIST}
;;
*)
VENDOR=ubuntu
RELEASE=${DIST}
;;
esac
# The vendor specific host architecture name
case "${VENDOR}" in
fedora|centos|opensuse)
host_arch=$(dpkg-architecture -qDEB_HOST_GNU_CPU)
;;
*)
host_arch=$(dpkg-architecture -qDEB_HOST_ARCH)
;;
esac
# Set the image architecture if not specified
if [ -z "${ARCH}" ] ; then
ARCH=${host_arch}
fi
# Check/force the boot mode for certain releases
case "${RELEASE}/${ARCH}/${BOOT_MODE}" in
precise/*/uefi)
pr_error "Unsupported boot mode for Precise: uefi"
exit 1
;;
*/armhf/bios)
pr_error "Unsupported boot mode for armhf: bios"
exit 1
;;
*/arm64/*)
if [ "${BOOT_MODE}" != "uefi" ] ; then
pr_warn "Force boot mode to: uefi"
BOOT_MODE="uefi"
fi
;;
core*/*)
if [ "${BOOT_MODE}" != "uefi" ] ; then
pr_warn "Force boot mode to: uefi"
BOOT_MODE="uefi"
fi
;;
esac
# The name of the VM
if [ -z "${NAME}" ] ; then
NAME=${VENDOR}-${RELEASE}${GRADE}
if [ "${ARCH}" != "${host_arch}" ] ; then
NAME=${NAME}-${ARCH}
fi
if [ "${BOOT_MODE}" != "bios" ] ; then
NAME=${NAME}-${BOOT_MODE}
fi
if [ -n "${TYPE}" ] ; then
NAME=${NAME}-${TYPE}
fi
fi
# Type-specific special handling
case "${TYPE}" in
dep8)
VCPU=2
VCPU_THREADS=2
MEMORY=2048
;;
adt)
VCPU=2
VCPU_THREADS=2
MEMORY=2048
NAME=adt-${RELEASE}-${ARCH}
START=1
SHUTDOWN=1
;;
esac
# Ubuntu core special handling
config_drive=1
if [ "${RELEASE#core}" != "${RELEASE}" ] ; then
# Ubuntu core doesn't need a config drive
config_drive=0
if [ "${SIZE}" -lt 12 ] ; then
# Ubuntu core2* is big :-(
SIZE=12
fi
if [ "${MEMORY}" -lt 2048 ] ; then
# 1GB is not enough for grub to loopmount the kernel snap
MEMORY=2048
fi
fi
# OpenSUSE special handling
if [ "${VENDOR}" = "opensuse" ] ; then
if [ "${SIZE}" -lt 12 ] ; then
SIZE=12
fi
fi
# Image and config directories
IMAGES_DIR=/data/images/cloud
if ! [ -d "${IMAGES_DIR}" ] ; then
IMAGES_DIR=~/.cache/images/cloud
if ! [ -d "${IMAGES_DIR}" ] ; then
mkdir -p "${IMAGES_DIR}"
fi
fi
CONFIG_DIR=$(dirname "$(readlink -e "${0}")")/config
# The full path of the (cached) base image
if [ -z "${BASE_IMAGE}" ] ; then
BASE_IMAGE=${IMAGES_DIR}/${VENDOR}-${RELEASE}${GRADE}-${ARCH}.img
fi
# The OS and config drive image names
OS_IMAGE=${NAME}.img
if [ "${config_drive}" -eq 1 ] ; then
CONFIG_DRIVE=${NAME}-config.iso
else
CONFIG_DRIVE=
fi
# The kernel and initrd images for direct boot
if [ "${BOOT_MODE}" = "direct" ] ; then
KERNEL=${NAME}-kernel
INITRD=${NAME}-initrd
else
KERNEL=
INITRD=
fi
print_var NAME VCPU VCPU_THREADS MEMORY ARCH VENDOR DIST RELEASE BOOT_MODE \
GRADE OS_IMAGE CONFIG_DRIVE KERNEL INITRD BASE_IMAGE
echo
# Check if this is a dry run
if [ "${DRY_RUN}" -eq 1 ] ; then
echo "(dry-run) Exiting..."
exit
fi
# Purge existing VM
if virsh dumpxml "${NAME}" >/dev/null 2>&1 ; then
if [ "${FORCE}" -eq 0 ] ; then
pr_error "VM '${NAME}' exists already. Use -f, --force to recreate it."
exit 1
fi
virsh destroy "${NAME}" || true
for vol in "${OS_IMAGE}" "${CONFIG_DRIVE}" "${KERNEL}" "${INITRD}" ; do
virsh vol-delete --pool default "${vol}" 2>/dev/null || true
done
virsh undefine --nvram "${NAME}" || true
fi
# Create a temporary working directory and install a cleanup handler
TMP_DIR=$(mktemp -d)
trap "out" EXIT INT TERM HUP
# Download the cloud image
download_base_image
# Create the OS image
create_os_image
# Create the config drive
create_config_drive
# Define the VM
define_vm
# Start the VM
if [ "${START}" -eq 1 ] ; then
pr_info "Start VM"
virsh start "${NAME}"
fi