aboutsummaryrefslogtreecommitdiff
path: root/files/pve-create-template.sh
blob: 58fc26a93c44d12321ef0b8523bd2d76639ccf60 (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
#!/bin/bash

usage() {
    printf "%s\n" "usage: $(basename $0) -n <template name> -q <qcow image> -i <vm id>"
    exit 1
}

while getopts "n:q:i:h" opts; do
    case $opts in
        n) name="${OPTARG}";;
        q) qcow="${OPTARG}";;
        i) vmid="${OPTARG}";;
        h) usage
    esac
done

if test "${name}" && test "${qcow}" && test "${vmid}"; then
    if test -f "${qcow}"; then
        qm create "${vmid}"            \
            --name "${name}"           \
            --memory 4096              \
            --net0 virtio,bridge=vmbr0 \
            --bootdisk scsi0           \
            --boot order=scsi0

        qm importdisk "${vmid}" "${qcow}" local-lvm

        qm set "${vmid}"               \
            -scsihw virtio-scsi-pci    \
            -scsi0 "local-lvm:vm-"${vmid}"-disk-0"

        qm template "${vmid}"
        printf "%s\n" "[inf] created ${name} (${vmid}) from ${qcow}"
    else
        printf "%s\n" "[err] ${qcow} does not exist"
        exit 1
    fi
else
    usage
fi