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
#!/bin/bash
# source: https://docs.technotim.live/posts/cloud-init-cloud-image/
# source: https://pve.proxmox.com/pve-docs/qm.1.html
set -e
#----------------------------------------------------------------------
IMAGE=jammy-server-cloudimg-amd64.img
URL=https://cloud-images.ubuntu.com/jammy/current/$IMAGE
# Settings that have to change each time a VM is created.
#===================
NAME=ubuntu-cloud1
IP=192.168.1.26/24
#IP=dhcp
ID=8002
#===================
MEMORY=4096
CORE=4
#DEST=Data
DEST=TrueNAS
DISK_SIZE=+45G
DISK_NAME=vm-$ID-disk-0
DISK_ID=$DEST:$DISK_NAME
DISK_DEVICE=scsi0
USER=dave
GW=192.168.1.1
NAMESERVER=192.168.1.1
#----------------------------------------------------------------------
if [ ! -f $IMAGE ]; then
wget $URL
fi
qm create $ID --memory $MEMORY --core $CORE --name $NAME --net0 virtio,bridge=vmbr0
qm importdisk $ID $IMAGE $DEST --format qcow2
# Successfully imported disk as 'unused0:TrueNAS:8003/vm-8003-disk-0.raw'
# TrueNAS:8002/vm-8002-disk-0.qcow2
qm set $ID --scsihw virtio-scsi-pci --$DISK_DEVICE $DISK_ID
qm resize $ID $DISK_DEVICE $DISK_SIZE
qm set $ID --ide2 $DEST:cloudinit
qm set $ID --boot c --bootdisk $DISK_DEVICE
qm set $ID --serial0 socket --vga serial0
qm set $ID --ipconfig0 ip=$IP,gw=$GW
#qm set $ID --ipconfig0 ip=dhcp
qm set $ID --agent enabled=1
# qm set $ID --autostart 1 #on crash
qm set $ID --nameserver $NAMESERVER
qm set $ID --onboot 0 # start VM on boot of host
qm set $ID --ciuser $USER
qm set $ID --sshkeys=/root/.ssh/id_rsa.pub
qm move-disk 300 scsi0 TrueNAS
Creating a VM in Proxmox using a BASH script
This post is licensed under
CC BY 4.0
by the author.