-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick-vm
More file actions
executable file
·70 lines (62 loc) · 2 KB
/
quick-vm
File metadata and controls
executable file
·70 lines (62 loc) · 2 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
#!/usr/bin/bash
#
# Use libguestfs and libvirt to create a qemu-based Debian 12 VM. The first
# argument to this script will be used as the domain name for libvirt. The
# resulting VM can be managed through any libvirt compatible tool like "virsh".
#
# After first boot, the VM will have an SSH server up and running. A user can
# connect to it as the "root" user by using the SSH key stored in the file
# <name>-root-key. The VM also supports mDNS so if the host and network are
# configured properly, a user can login with:
#
# ssh -i <generated-root-key> root@<name>.local
#
# Some error checking is included.
#
if [[ -z $1 ]]
then
echo "Usage: $0 <VM-name>"
exit 1
fi
NAME=$1
IMAGE=$NAME.img
KEY=$NAME-root-key
if virsh domid $NAME > /dev/null
then
echo "A VM named \"$NAME\" exists. Abort."
exit 2
fi
if [[ -f $KEY && -f $KEY.pub ]]
then
echo "SSH key pair for $NAME exists. Re-use existing key."
else
# Remove in case one of the 2 files was present.
rm -f $KEY $KEY.pub
ssh-keygen -f $KEY -C root -P ""
fi
if [[ -f $IMAGE && $IMAGE -nt $KEY ]]
then
echo "Image file $IMAGE exists and is newer that $KEY. Skip image creation."
else
virt-builder debian-12 \
--size 6G \
--memsize 1024 \
--format raw \
--hostname $NAME \
--output $IMAGE \
--root-password password:toor \
--install network-manager,avahi-daemon \
--upload $KEY.pub:/root/key \
--firstboot firstboot.sh
fi
virt-install \
--name $NAME \
--ram 1024 \
--vcpus 2 \
--disk path=$IMAGE \
--osinfo debian12 \
--console pty,target_type=serial \
--network type=bridge,source=virbr0 \
--graphics none \
--import \
--noautoconsole