-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkvmgr.sh
executable file
·92 lines (77 loc) · 2.48 KB
/
kvmgr.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash
set -eo pipefail
# PREP:
# arch: pacman -S libvirt qemu ebtables dmidecode libguestfs virt-install cpio cloud-utils edk2-ovmf
# --> Setup environment
source ./defaults.conf
source ./user.conf
# --> Parse arguments:
if [ -z $1 ]; then
echo "Command help: "
exit 1
fi
while [[ "$#" -gt 0 ]]; do
case $1 in
-c|--cpus) CPUS=$2 ;;
-m|--mem ) MEM=$2 ;;
-d|--disk) DISK=$2 ;;
-i|--net ) NET=$2 ;;
-o|--os ) OS=$2 ;;
-n|--name) NAME=$2;;
*);;
esac
shift
done;
echo -e "System Parameters:\n -> cpus=$CPUS\n -> mem=$MEM \n -> disk=$DISK \n -> net=$NET \n -> os=$OS \n -> name=$NAME"
# Expand the path
BASEPATH=$(readlink --canonicalize ~/virt)
# Check if image exists, and download if needed.
IMGPATH="$BASEPATH/images/$OS-server-cloudimg-amd64.img"
if [ -f $BASEPATH/images/$OS-server-cloudimg-amd64.img ]; then
echo "[+] Image present: $OS-server-cloudimg-amd64.img"
else
echo "[!] Image does not exist. Downloading new cloudimg to: $BASEPATH/images/$OS-server-cloudimg-amd64.img"
wget https://cloud-images.ubuntu.com/$OS/current/$OS-server-cloudimg-amd64.img -O $BASEPATH/images/$OS-server-cloudimg-amd64.img
fi
# Create the VM disk and initialize the metadata image
if [ -f $BASEPATH/vms/$NAME/$NAME.qcow2 ]; then
echo "[!] VM Image already exists. Stopping here to prevent data loss!"
exit 1
fi
mkdir -p $BASEPATH/vms/$NAME/
qemu-img convert -f qcow2 $BASEPATH/images/$OS-server-cloudimg-amd64.img $BASEPATH/vms/$NAME/$NAME.qcow2
qemu-img resize $BASEPATH/vms/$NAME/$NAME.qcow2 $DISK
# Create cloud-config file
echo "[+] Creating cloud-config file"
cat << EOF > $BASEPATH/vms/$NAME/cloud-config.yml
#cloud-config
hostname: $NAME
ssh_pwauth: False
password: $AUTH_PASSWD
chpasswd: { expire: False }
manage_etc_hosts: true
users:
- default
- name: $AUTH_USERNAME
sudo: ALL=(ALL) NOPASSWD:ALL
groups: sudo
shell: /bin/bash
lock_passwd: true
ssh_authorized_keys:
- $AUTH_PUBKEY
EOF
# Create cloud-config device
cloud-localds $BASEPATH/vms/$NAME/metadata.img $BASEPATH/vms/$NAME/cloud-config.yml
# Spin up the VM
echo "[+] Starting VM import..."
virt-install --connect qemu:///system \
--name $NAME \
--memory $MEM \
--vcpus $CPUS \
--disk $BASEPATH/vms/$NAME/$NAME.qcow2,device=disk,bus=virtio \
--disk $BASEPATH/vms/$NAME/metadata.img,device=cdrom \
--os-type linux \
--os-variant ubuntu20.04 \
--virt-type kvm \
--graphics none \
--network network=$NET,model=virtio --import