Skip to content

Instantly share code, notes, and snippets.

@phonglh79
Forked from chriswayg/create-cloud-template.sh
Created December 17, 2021 08:52
Show Gist options
  • Select an option

  • Save phonglh79/94e026f8bbb4fbdf566ed87939c45120 to your computer and use it in GitHub Desktop.

Select an option

Save phonglh79/94e026f8bbb4fbdf566ed87939c45120 to your computer and use it in GitHub Desktop.

Revisions

  1. @chriswayg chriswayg revised this gist May 1, 2020. 1 changed file with 9 additions and 9 deletions.
    18 changes: 9 additions & 9 deletions create-cloud-template.sh
    Original file line number Diff line number Diff line change
    @@ -57,7 +57,7 @@ case $OSNR in

    2)
    OSNAME=debian9
    VMID_DEFAULT=52000
    VMID_DEFAULT=51100
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=debian-9-openstack-amd64.qcow2
    @@ -68,7 +68,7 @@ case $OSNR in

    3)
    OSNAME=debian10
    VMID_DEFAULT=53000
    VMID_DEFAULT=51200
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=debian-10-openstack-amd64.qcow2
    @@ -79,7 +79,7 @@ case $OSNR in

    4)
    OSNAME=ubuntu1804
    VMID_DEFAULT=54000
    VMID_DEFAULT=52000
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=bionic-server-cloudimg-amd64.img
    @@ -90,7 +90,7 @@ case $OSNR in

    5)
    OSNAME=centos7
    VMID_DEFAULT=55000
    VMID_DEFAULT=53100
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    RESIZE=+24G
    @@ -110,7 +110,7 @@ case $OSNR in
    # https://github.com/coreos/coreos-cloudinit/blob/master/Documentation/config-drive.md
    #
    # OSNAME=coreos
    # VMID_DEFAULT=56000
    # VMID_DEFAULT=54600
    # read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    # VMID=${VMID:-$VMID_DEFAULT}
    # RESIZE=+24G
    @@ -120,7 +120,7 @@ case $OSNR in
    # printf "$NOTE\n"
    # wget -P /tmp -N https://stable.release.core-os.net/amd64-usr/current/$VMIMAGE
    OSNAME=flatcar
    VMID_DEFAULT=56000
    VMID_DEFAULT=54600
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    RESIZE=+24G
    @@ -133,7 +133,7 @@ case $OSNR in

    7)
    OSNAME=arch
    VMID_DEFAULT=57000
    VMID_DEFAULT=54200
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    RESIZE=+29G
    @@ -145,7 +145,7 @@ case $OSNR in

    8)
    OSNAME=alpine311
    VMID_DEFAULT=58000
    VMID_DEFAULT=54000
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=alpine-311-cloudimg-amd64.qcow2
    @@ -157,7 +157,7 @@ case $OSNR in

    9)
    OSNAME=rancheros
    VMID_DEFAULT=59000
    VMID_DEFAULT=54400
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=rancheros-openstack.img
  2. @chriswayg chriswayg revised this gist Apr 16, 2020. 1 changed file with 16 additions and 16 deletions.
    32 changes: 16 additions & 16 deletions create-cloud-template.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/bin/bash
    set -e
    #set -x
    set -o errexit

    clear
    printf "\n*** This script will download a cloud image and create a Proxmox VM template from it. ***\n\n"

    @@ -13,9 +13,10 @@ printf "\n*** This script will download a cloud image and create a Proxmox VM te
    ### - download latest version of the script:
    ### curl wget https://gist.github.com/chriswayg/43fbea910e024cbe608d7dcb12cb8466/raw/create-cloud-template.sh > /usr/local/bin/create-cloud-template.sh && chmod -v +x /usr/local/bin/create-cloud-template.sh
    ### - (optionally) prepare a cloudinit user-config.yml in the working directory
    ### this could be copied and modified from the cloudinit user dump at the end of this script
    ### - run the script:
    ### - $ create-cloud-template.sh
    ### - clone the template from the Proxmox GUI and test
    ### $ create-cloud-template.sh
    ### - clone the finished template from the Proxmox GUI and test
    ###
    ### NOTES:
    ### - links to cloud images:
    @@ -27,11 +28,12 @@ printf "\n*** This script will download a cloud image and create a Proxmox VM te
    ### SUSE 15 SP1 JeOS: https://download.suse.com/Download?buildid=OE-3enq3uys~
    ### CirrOS http://download.cirros-cloud.net/
    ### CoreOS (EOL 05.2020): https://stable.release.core-os.net/amd64-usr/current/
    ### Flatcar: https://stable.release.flatcar-linux.net/amd64-usr/current/
    ### Flatcar (CoreOS fork): https://stable.release.flatcar-linux.net/amd64-usr/current/
    ### Gentoo: http://gentoo.osuosl.org/experimental/amd64/openstack
    ### Arch (also Gentoo): https://linuximages.de/openstack/arch/
    ### Alpine: https://github.com/chriswayg/packer-qemu-cloud/
    ### RancherOS: https://github.com/rancher/os/releases (also includes Proxmox version)
    ### RancherOS: https://github.com/rancher/os/releases (also includes Proxmox iso version)
    ###
    ### - most links will download the latest current (stable) version of the OS
    ### - older cloud-init versions do not support hashed passwords

    @@ -59,7 +61,7 @@ case $OSNR in
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=debian-9-openstack-amd64.qcow2
    NOTE="\n## Default user is 'debian'\n## NOTE: In Debian 9, setting a password via cloud-config does not seem to work!\n"
    NOTE="\n## Default user is 'debian'\n## NOTE: Setting a password via cloud-config does not work.\n"
    printf "$NOTE\n"
    wget -P /tmp -N https://cdimage.debian.org/cdimage/openstack/current-9/$VMIMAGE
    ;;
    @@ -69,22 +71,20 @@ case $OSNR in
    VMID_DEFAULT=53000
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE_DEFAULT=debian-10-openstack-amd64.qcow2
    VMIMAGE=debian-10-openstack-amd64.qcow2
    NOTE="\n## Default user is 'debian'\n"
    #printf "$NOTE## NOTE: Check, if the image is up to date on https://cdimage.debian.org/cdimage/openstack/\n\n"
    #read -p "Enter the new VM image filename [$VMIMAGE_DEFAULT]: " VMIMAGE
    VMIMAGE=${VMIMAGE:-$VMIMAGE_DEFAULT}
    printf "$NOTE\n"
    wget -P /tmp -N https://cdimage.debian.org/cdimage/openstack/current-10/$VMIMAGE
    ;;

    4)
    OSNAME=ubuntu1804
    VMID_DEFAULT=54000
    NOTE="\n## Default user is 'ubuntu'\n"
    printf "$NOTE\n"
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=bionic-server-cloudimg-amd64.img
    NOTE="\n## Default user is 'ubuntu'\n"
    printf "$NOTE\n"
    wget -P /tmp -N https://cloud-images.ubuntu.com/bionic/current/$VMIMAGE
    ;;

    @@ -95,7 +95,7 @@ case $OSNR in
    VMID=${VMID:-$VMID_DEFAULT}
    RESIZE=+24G
    VMIMAGE=CentOS-7-x86_64-GenericCloud.qcow2
    NOTE="\n## Default user is 'centos'\n## NOTE: CentOS seems to ignore hostname config.\n# use 'hostnamectl set-hostname centos7-cloud' inside VM\n"
    NOTE="\n## Default user is 'centos'\n## NOTE: CentOS ignores hostname config.\n# use 'hostnamectl set-hostname centos7-cloud' inside VM\n"
    printf "$NOTE\n"
    wget -P /tmp -N http://cloud.centos.org/centos/7/images/$VMIMAGE
    ;;
    @@ -149,7 +149,7 @@ case $OSNR in
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=alpine-311-cloudimg-amd64.qcow2
    NOTE="\n## Default user is 'alpine'\n## NOTE: Cloud-init on Alpine 3.11 is not able to apply network config.\n# Setting a password via cloud-config does not work.\n# CHANGE the default root passwword (usable via console only).\n"
    NOTE="\n## Default user is 'alpine'\n## NOTE: Cloud-init on Alpine 3.11 is not able to apply network config.\n# Setting a password via cloud-config does not work.\n# CHANGE the default root passwword (root can only login via console).\n"
    printf "$NOTE\n"
    wget -P /tmp -N https://github.com/chriswayg/packer-proxmox-templates/releases/download/v1.6/$VMIMAGE
    #cp -v /root/$VMIMAGE /tmp/ # for local testing
    @@ -162,7 +162,7 @@ case $OSNR in
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=rancheros-openstack.img
    CITYPE=configdrive2
    NOTE="\n## Default user is 'rancher'\n## NOTE: Setting a password via cloud-config does not work.\n# RancherOS does autologin on console.\n# SSH keys do not get applied from cloud-config.\n"
    NOTE="\n## Default user is 'rancher'\n## NOTE: Setting a password via cloud-config does not work.\n# RancherOS does autologin on console.\n"
    printf "$NOTE\n"
    wget -P /tmp -N https://github.com/rancher/os/releases/download/v1.5.5/$VMIMAGE
    ;;
  3. @chriswayg chriswayg revised this gist Apr 16, 2020. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions create-cloud-template.sh
    Original file line number Diff line number Diff line change
    @@ -126,7 +126,7 @@ case $OSNR in
    RESIZE=+24G
    VMIMAGE=flatcar_production_qemu_image.img.bz2
    CITYPE=configdrive2
    NOTE="\n## Default user is 'coreos'\n## NOTE: In Flatcar, setting a password via cloud-config does not seem to work!\n"
    NOTE="\n## Default user is 'coreos'\n## NOTE: Setting a password via cloud-config does not work.\n"
    printf "$NOTE\n"
    wget -P /tmp -N https://stable.release.flatcar-linux.net/amd64-usr/current/$VMIMAGE
    ;;
    @@ -138,7 +138,7 @@ case $OSNR in
    VMID=${VMID:-$VMID_DEFAULT}
    RESIZE=+29G
    VMIMAGE=arch-openstack-LATEST-image-bootstrap.qcow2
    NOTE="\n## Default user is 'arch'\n## NOTE: In Arch Linux, setting a password via cloud-config does not seem to work!\n# - Resizing does not happen automatically inside the VM\n"
    NOTE="\n## Default user is 'arch'\n## NOTE: Setting a password via cloud-config does not work.\n# Resizing does not happen automatically inside the VM\n"
    printf "$NOTE\n"
    wget -P /tmp -N https://linuximages.de/openstack/arch/$VMIMAGE
    ;;
    @@ -149,7 +149,7 @@ case $OSNR in
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=alpine-311-cloudimg-amd64.qcow2
    NOTE="\n## Default user is 'alpine'\n## NOTE: Cloud-init on Alpine 3.11 is not able to apply network config.\n# Setting a password via cloud-config does not seem to work.\n# CHANGE the default root passwword (usable via console only).\n"
    NOTE="\n## Default user is 'alpine'\n## NOTE: Cloud-init on Alpine 3.11 is not able to apply network config.\n# Setting a password via cloud-config does not work.\n# CHANGE the default root passwword (usable via console only).\n"
    printf "$NOTE\n"
    wget -P /tmp -N https://github.com/chriswayg/packer-proxmox-templates/releases/download/v1.6/$VMIMAGE
    #cp -v /root/$VMIMAGE /tmp/ # for local testing
    @@ -161,7 +161,8 @@ case $OSNR in
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=rancheros-openstack.img
    NOTE="\n## Default user is 'rancher'\n"
    CITYPE=configdrive2
    NOTE="\n## Default user is 'rancher'\n## NOTE: Setting a password via cloud-config does not work.\n# RancherOS does autologin on console.\n# SSH keys do not get applied from cloud-config.\n"
    printf "$NOTE\n"
    wget -P /tmp -N https://github.com/rancher/os/releases/download/v1.5.5/$VMIMAGE
    ;;
  4. @chriswayg chriswayg revised this gist Apr 16, 2020. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions create-cloud-template.sh
    Original file line number Diff line number Diff line change
    @@ -11,9 +11,7 @@ printf "\n*** This script will download a cloud image and create a Proxmox VM te
    ###
    ### - fork the gist and adapt the defaults (especially SSHKEY) as needed
    ### - download latest version of the script:
    ### - wget -N https://gist.github.com/chriswayg/43fbea910e024cbe608d7dcb12cb8466/raw/create-cloud-template.sh -P /usr/local/bin/
    ### - chmod +x /usr/local/bin/create-cloud-template.sh
    ###
    ### curl wget https://gist.github.com/chriswayg/43fbea910e024cbe608d7dcb12cb8466/raw/create-cloud-template.sh > /usr/local/bin/create-cloud-template.sh && chmod -v +x /usr/local/bin/create-cloud-template.sh
    ### - (optionally) prepare a cloudinit user-config.yml in the working directory
    ### - run the script:
    ### - $ create-cloud-template.sh
    @@ -40,7 +38,7 @@ printf "\n*** This script will download a cloud image and create a Proxmox VM te
    ## TODO
    ## - verify authenticity of downloaded images using hash or GPG

    printf "* Available templates to generate:\n 2) debian9\n 3) debian10\n 4) ubuntu1804\n 5) centos7\n 6) coreos/flatcar\n 7) arch\n 8) alpine311\n\n"
    printf "* Available templates to generate:\n 2) Debian 9\n 3) Debian 10\n 4) Ubuntu 18.04\n 5) Centos 7\n 6) CoreOS/Flatcar\n 7) Arch\n 8) Alpine 3.11\n 9) RancherOS 1.5.5\n\n"
    read -p "* Enter number of distro to use: " OSNR

    # defaults which are used for most templates
  5. @chriswayg chriswayg revised this gist Apr 16, 2020. 1 changed file with 12 additions and 1 deletion.
    13 changes: 12 additions & 1 deletion create-cloud-template.sh
    Original file line number Diff line number Diff line change
    @@ -151,12 +151,23 @@ case $OSNR in
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=alpine-311-cloudimg-amd64.qcow2
    NOTE="\n## Default user is 'alpine'\n## NOTE: Cloud-init on Alpine 3.11 is not yet able to apply network config.\n# Also setting a password via cloud-config does not seem to work!\n"
    NOTE="\n## Default user is 'alpine'\n## NOTE: Cloud-init on Alpine 3.11 is not able to apply network config.\n# Setting a password via cloud-config does not seem to work.\n# CHANGE the default root passwword (usable via console only).\n"
    printf "$NOTE\n"
    wget -P /tmp -N https://github.com/chriswayg/packer-proxmox-templates/releases/download/v1.6/$VMIMAGE
    #cp -v /root/$VMIMAGE /tmp/ # for local testing
    ;;

    9)
    OSNAME=rancheros
    VMID_DEFAULT=59000
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=rancheros-openstack.img
    NOTE="\n## Default user is 'rancher'\n"
    printf "$NOTE\n"
    wget -P /tmp -N https://github.com/rancher/os/releases/download/v1.5.5/$VMIMAGE
    ;;

    *)
    printf "\n** Unknown OS number. Please use one of the above!\n"
    exit 0
  6. @chriswayg chriswayg revised this gist Apr 15, 2020. 1 changed file with 12 additions and 10 deletions.
    22 changes: 12 additions & 10 deletions create-cloud-template.sh
    Original file line number Diff line number Diff line change
    @@ -4,17 +4,19 @@ set -e
    clear
    printf "\n*** This script will download a cloud image and create a Proxmox VM template from it. ***\n\n"

    ### How to use:
    ### HOW TO USE
    ### Pre-req:
    ### - run on a Proxmox 6 server
    ### - a dhcp server should be active on vmbr1
    ###
    ### - fork the gist and adapt the defaults (especially SSHKEY) as needed
    ### - download latest version of the script: cd /usr/local/bin/
    ### - wget -N https://gist.github.com/chriswayg/43fbea910e024cbe608d7dcb12cb8466/raw/create-cloud-template.sh
    ### - chmod +x /usr/local/bin/create-cloud-template.sh
    ### - prepare a cloudinit user-config.yml in the working directory (optional)
    ### - run the script
    ### - fork the gist and adapt the defaults (especially SSHKEY) as needed
    ### - download latest version of the script:
    ### - wget -N https://gist.github.com/chriswayg/43fbea910e024cbe608d7dcb12cb8466/raw/create-cloud-template.sh -P /usr/local/bin/
    ### - chmod +x /usr/local/bin/create-cloud-template.sh
    ###
    ### - (optionally) prepare a cloudinit user-config.yml in the working directory
    ### - run the script:
    ### - $ create-cloud-template.sh
    ### - clone the template from the Proxmox GUI and test
    ###
    ### NOTES:
    @@ -38,7 +40,7 @@ printf "\n*** This script will download a cloud image and create a Proxmox VM te
    ## TODO
    ## - verify authenticity of downloaded images using hash or GPG

    printf "* Available templates to generate:\n 2) debian9\n 3) debian10\n 4) ubuntu1804\n 5) centos7\n 6) coreos/flatcar\n 7) arch\n 8) alpine310\n\n"
    printf "* Available templates to generate:\n 2) debian9\n 3) debian10\n 4) ubuntu1804\n 5) centos7\n 6) coreos/flatcar\n 7) arch\n 8) alpine311\n\n"
    read -p "* Enter number of distro to use: " OSNR

    # defaults which are used for most templates
    @@ -144,12 +146,12 @@ case $OSNR in
    ;;

    8)
    OSNAME=alpine310
    OSNAME=alpine311
    VMID_DEFAULT=58000
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=alpine-311-cloudimg-amd64.qcow2
    NOTE="\n## Default user is 'alpine'\n## NOTE: Cloud-init on Alpine is not yet able to apply network config.\n# Also setting a password via cloud-config does not seem to work!\n"
    NOTE="\n## Default user is 'alpine'\n## NOTE: Cloud-init on Alpine 3.11 is not yet able to apply network config.\n# Also setting a password via cloud-config does not seem to work!\n"
    printf "$NOTE\n"
    wget -P /tmp -N https://github.com/chriswayg/packer-proxmox-templates/releases/download/v1.6/$VMIMAGE
    #cp -v /root/$VMIMAGE /tmp/ # for local testing
  7. @chriswayg chriswayg revised this gist Apr 15, 2020. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions create-cloud-template.sh
    Original file line number Diff line number Diff line change
    @@ -148,11 +148,11 @@ case $OSNR in
    VMID_DEFAULT=58000
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=alpine-310-cloudimg-amd64.qcow2
    VMIMAGE=alpine-311-cloudimg-amd64.qcow2
    NOTE="\n## Default user is 'alpine'\n## NOTE: Cloud-init on Alpine is not yet able to apply network config.\n# Also setting a password via cloud-config does not seem to work!\n"
    printf "$NOTE\n"
    #wget -P /tmp -N https://github.com/chriswayg/packer-qemu-cloud/releases/download/v0.4-beta/$VMIMAGE
    cp -v /root/$VMIMAGE /tmp/ # for local testing
    wget -P /tmp -N https://github.com/chriswayg/packer-proxmox-templates/releases/download/v1.6/$VMIMAGE
    #cp -v /root/$VMIMAGE /tmp/ # for local testing
    ;;

    *)
  8. @chriswayg chriswayg revised this gist Apr 14, 2020. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions create-cloud-template.sh
    Original file line number Diff line number Diff line change
    @@ -222,10 +222,10 @@ printf "#$NOTE\n" >> /etc/pve/nodes/proxmox/qemu-server/$VMID.conf
    printf "\n** Increasing the disk size\n"
    qm resize $VMID scsi0 $RESIZE

    printf "\n*** The following cloud-init configuration will be used ***\n\n"
    printf "------------- User ------------------\n"
    printf "\n*** The following cloud-init configuration will be used ***\n"
    printf "\n------------- User ------------------\n"
    qm cloudinit dump $VMID user
    printf "------------- Network ---------------\n"
    printf "\n------------- Network ---------------\n"
    qm cloudinit dump $VMID network

    # convert the vm into a template (TODO make this optional)
  9. @chriswayg chriswayg revised this gist Apr 14, 2020. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions create-cloud-template.sh
    Original file line number Diff line number Diff line change
    @@ -151,8 +151,8 @@ case $OSNR in
    VMIMAGE=alpine-310-cloudimg-amd64.qcow2
    NOTE="\n## Default user is 'alpine'\n## NOTE: Cloud-init on Alpine is not yet able to apply network config.\n# Also setting a password via cloud-config does not seem to work!\n"
    printf "$NOTE\n"
    wget -P /tmp -N https://github.com/chriswayg/packer-qemu-cloud/releases/download/v0.4-beta/$VMIMAGE
    #cp -v /root/$VMIMAGE /tmp/ # for local testing
    #wget -P /tmp -N https://github.com/chriswayg/packer-qemu-cloud/releases/download/v0.4-beta/$VMIMAGE
    cp -v /root/$VMIMAGE /tmp/ # for local testing
    ;;

    *)
    @@ -223,9 +223,9 @@ printf "\n** Increasing the disk size\n"
    qm resize $VMID scsi0 $RESIZE

    printf "\n*** The following cloud-init configuration will be used ***\n\n"
    printf "\n------------- User ------------------\n"
    printf "------------- User ------------------\n"
    qm cloudinit dump $VMID user
    printf "\n------------- Network ---------------\n"
    printf "------------- Network ---------------\n"
    qm cloudinit dump $VMID network

    # convert the vm into a template (TODO make this optional)
  10. @chriswayg chriswayg revised this gist Apr 14, 2020. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions create-cloud-template.sh
    Original file line number Diff line number Diff line change
    @@ -35,6 +35,9 @@ printf "\n*** This script will download a cloud image and create a Proxmox VM te
    ### - most links will download the latest current (stable) version of the OS
    ### - older cloud-init versions do not support hashed passwords

    ## TODO
    ## - verify authenticity of downloaded images using hash or GPG

    printf "* Available templates to generate:\n 2) debian9\n 3) debian10\n 4) ubuntu1804\n 5) centos7\n 6) coreos/flatcar\n 7) arch\n 8) alpine310\n\n"
    read -p "* Enter number of distro to use: " OSNR

    @@ -128,7 +131,6 @@ case $OSNR in
    wget -P /tmp -N https://stable.release.flatcar-linux.net/amd64-usr/current/$VMIMAGE
    ;;


    7)
    OSNAME=arch
    VMID_DEFAULT=57000
    @@ -220,9 +222,10 @@ printf "#$NOTE\n" >> /etc/pve/nodes/proxmox/qemu-server/$VMID.conf
    printf "\n** Increasing the disk size\n"
    qm resize $VMID scsi0 $RESIZE

    printf "\n*** The following cloud-init configuration for User and Network will be used ***\n\n"
    printf "\n*** The following cloud-init configuration will be used ***\n\n"
    printf "\n------------- User ------------------\n"
    qm cloudinit dump $VMID user
    printf "\n------------------------------\n"
    printf "\n------------- Network ---------------\n"
    qm cloudinit dump $VMID network

    # convert the vm into a template (TODO make this optional)
  11. @chriswayg chriswayg revised this gist Apr 14, 2020. 1 changed file with 6 additions and 5 deletions.
    11 changes: 6 additions & 5 deletions create-cloud-template.sh
    Original file line number Diff line number Diff line change
    @@ -99,10 +99,11 @@ case $OSNR in

    6)
    # - Proxmox creates a configdrive with the option: 'manage_etc_hosts: true'
    # which causes an error in 'user-configdrive.service':
    # 'Failed to apply cloud-config: Invalid option to manage_etc_hosts'
    # there is no problem, when supplying a compatible 'user-config.yml'
    # - coreos needs 'configdrive2'
    # which causes an error in 'user-configdrive.service':
    # 'Failed to apply cloud-config: Invalid option to manage_etc_hosts'
    # There is no problem, when supplying a compatible 'user-config.yml'.
    # - CoreOS needs 'configdrive2'
    # - CoreOS is End of Life in 05.2020, use Flatcar instead
    # https://github.com/coreos/coreos-cloudinit/blob/master/Documentation/config-drive.md
    #
    # OSNAME=coreos
    @@ -122,7 +123,7 @@ case $OSNR in
    RESIZE=+24G
    VMIMAGE=flatcar_production_qemu_image.img.bz2
    CITYPE=configdrive2
    NOTE="\n## Default user is 'flatcar'\n## NOTE: In Flatcar, setting a password via cloud-config does not seem to work!\n"
    NOTE="\n## Default user is 'coreos'\n## NOTE: In Flatcar, setting a password via cloud-config does not seem to work!\n"
    printf "$NOTE\n"
    wget -P /tmp -N https://stable.release.flatcar-linux.net/amd64-usr/current/$VMIMAGE
    ;;
  12. @chriswayg chriswayg revised this gist Apr 14, 2020. 1 changed file with 20 additions and 7 deletions.
    27 changes: 20 additions & 7 deletions create-cloud-template.sh
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ printf "\n*** This script will download a cloud image and create a Proxmox VM te
    ###
    ### - fork the gist and adapt the defaults (especially SSHKEY) as needed
    ### - download latest version of the script: cd /usr/local/bin/
    ### - wget https://gist.github.com/chriswayg/43fbea910e024cbe608d7dcb12cb8466/raw/create-cloud-template.sh
    ### - wget -N https://gist.github.com/chriswayg/43fbea910e024cbe608d7dcb12cb8466/raw/create-cloud-template.sh
    ### - chmod +x /usr/local/bin/create-cloud-template.sh
    ### - prepare a cloudinit user-config.yml in the working directory (optional)
    ### - run the script
    @@ -26,15 +26,16 @@ printf "\n*** This script will download a cloud image and create a Proxmox VM te
    ### Fedora: https://alt.fedoraproject.org/cloud/
    ### SUSE 15 SP1 JeOS: https://download.suse.com/Download?buildid=OE-3enq3uys~
    ### CirrOS http://download.cirros-cloud.net/
    ### CoreOS: https://stable.release.core-os.net/amd64-usr/current/
    ### CoreOS (EOL 05.2020): https://stable.release.core-os.net/amd64-usr/current/
    ### Flatcar: https://stable.release.flatcar-linux.net/amd64-usr/current/
    ### Gentoo: http://gentoo.osuosl.org/experimental/amd64/openstack
    ### Arch (also Gentoo): https://linuximages.de/openstack/arch/
    ### Alpine: https://github.com/chriswayg/packer-qemu-cloud/
    ### RancherOS: https://github.com/rancher/os/releases (also includes Proxmox version)
    ### - most links will download the latest current (stable) version of the OS
    ### - older cloud-init versions do not support hashed passwords

    printf "* Available templates to generate:\n 2) debian9\n 3) debian10\n 4) ubuntu1804\n 5) centos7\n 6) coreos\n 7) arch\n 8) alpine310\n\n"
    printf "* Available templates to generate:\n 2) debian9\n 3) debian10\n 4) ubuntu1804\n 5) centos7\n 6) coreos/flatcar\n 7) arch\n 8) alpine310\n\n"
    read -p "* Enter number of distro to use: " OSNR

    # defaults which are used for most templates
    @@ -103,18 +104,30 @@ case $OSNR in
    # there is no problem, when supplying a compatible 'user-config.yml'
    # - coreos needs 'configdrive2'
    # https://github.com/coreos/coreos-cloudinit/blob/master/Documentation/config-drive.md
    OSNAME=coreos
    #
    # OSNAME=coreos
    # VMID_DEFAULT=56000
    # read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    # VMID=${VMID:-$VMID_DEFAULT}
    # RESIZE=+24G
    # VMIMAGE=coreos_production_qemu_image.img.bz2
    # CITYPE=configdrive2
    # NOTE="\n## Default user is 'core'\n## NOTE: In CoreOS, setting a password via cloud-config does not seem to work!\n"
    # printf "$NOTE\n"
    # wget -P /tmp -N https://stable.release.core-os.net/amd64-usr/current/$VMIMAGE
    OSNAME=flatcar
    VMID_DEFAULT=56000
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    RESIZE=+24G
    VMIMAGE=coreos_production_qemu_image.img.bz2
    VMIMAGE=flatcar_production_qemu_image.img.bz2
    CITYPE=configdrive2
    NOTE="\n## Default user is 'core'\n## NOTE: In CoreOS, setting a password via cloud-config does not seem to work!\n"
    NOTE="\n## Default user is 'flatcar'\n## NOTE: In Flatcar, setting a password via cloud-config does not seem to work!\n"
    printf "$NOTE\n"
    wget -P /tmp -N https://stable.release.core-os.net/amd64-usr/current/$VMIMAGE
    wget -P /tmp -N https://stable.release.flatcar-linux.net/amd64-usr/current/$VMIMAGE
    ;;


    7)
    OSNAME=arch
    VMID_DEFAULT=57000
  13. @chriswayg chriswayg revised this gist Apr 14, 2020. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions create-cloud-template.sh
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ printf "\n*** This script will download a cloud image and create a Proxmox VM te
    ### - run on a Proxmox 6 server
    ### - a dhcp server should be active on vmbr1
    ###
    ### - fork the gist and adapt the defaults as needed
    ### - fork the gist and adapt the defaults (especially SSHKEY) as needed
    ### - download latest version of the script: cd /usr/local/bin/
    ### - wget https://gist.github.com/chriswayg/43fbea910e024cbe608d7dcb12cb8466/raw/create-cloud-template.sh
    ### - chmod +x /usr/local/bin/create-cloud-template.sh
    @@ -19,7 +19,7 @@ printf "\n*** This script will download a cloud image and create a Proxmox VM te
    ###
    ### NOTES:
    ### - links to cloud images:
    ### https://docs.openstack.org/image-guide/obtain-images.html
    ### Directory: https://docs.openstack.org/image-guide/obtain-images.html
    ### Debian http://cdimage.debian.org/cdimage/openstack/
    ### Ubuntu http://cloud-images.ubuntu.com/
    ### CentOS: http://cloud.centos.org/centos/7/images/
    @@ -65,12 +65,12 @@ case $OSNR in
    VMID_DEFAULT=53000
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE_DEFAULT=debian-10.0.3-20190815-openstack-amd64.qcow2
    VMIMAGE_DEFAULT=debian-10-openstack-amd64.qcow2
    NOTE="\n## Default user is 'debian'\n"
    printf "$NOTE## NOTE: Check, if the image is up to date on https://cdimage.debian.org/cdimage/openstack/\n\n"
    read -p "Enter the new VM image filename [$VMIMAGE_DEFAULT]: " VMIMAGE
    #printf "$NOTE## NOTE: Check, if the image is up to date on https://cdimage.debian.org/cdimage/openstack/\n\n"
    #read -p "Enter the new VM image filename [$VMIMAGE_DEFAULT]: " VMIMAGE
    VMIMAGE=${VMIMAGE:-$VMIMAGE_DEFAULT}
    wget -P /tmp -N https://cdimage.debian.org/cdimage/openstack/current/$VMIMAGE
    wget -P /tmp -N https://cdimage.debian.org/cdimage/openstack/current-10/$VMIMAGE
    ;;

    4)
  14. @chriswayg chriswayg revised this gist Apr 14, 2020. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions create-cloud-template.sh
    100644 → 100755
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,8 @@ printf "\n*** This script will download a cloud image and create a Proxmox VM te
    ### - a dhcp server should be active on vmbr1
    ###
    ### - fork the gist and adapt the defaults as needed
    ### - download the script into /usr/local/bin/
    ### - download latest version of the script: cd /usr/local/bin/
    ### - wget https://gist.github.com/chriswayg/43fbea910e024cbe608d7dcb12cb8466/raw/create-cloud-template.sh
    ### - chmod +x /usr/local/bin/create-cloud-template.sh
    ### - prepare a cloudinit user-config.yml in the working directory (optional)
    ### - run the script
    @@ -90,7 +91,7 @@ case $OSNR in
    VMID=${VMID:-$VMID_DEFAULT}
    RESIZE=+24G
    VMIMAGE=CentOS-7-x86_64-GenericCloud.qcow2
    NOTE="\n## Default user is 'centos'\n## NOTE: CentOS seems to ignore hostname config!\n# use 'hostnamectl set-hostname centos7-cloud'\n"
    NOTE="\n## Default user is 'centos'\n## NOTE: CentOS seems to ignore hostname config.\n# use 'hostnamectl set-hostname centos7-cloud' inside VM\n"
    printf "$NOTE\n"
    wget -P /tmp -N http://cloud.centos.org/centos/7/images/$VMIMAGE
    ;;
  15. @chriswayg chriswayg revised this gist Apr 14, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion create-cloud-template.sh
    Original file line number Diff line number Diff line change
    @@ -90,7 +90,7 @@ case $OSNR in
    VMID=${VMID:-$VMID_DEFAULT}
    RESIZE=+24G
    VMIMAGE=CentOS-7-x86_64-GenericCloud.qcow2
    NOTE="\n## Default user is 'centos'\n## NOTE: CentOS seems to ignore hostname config!\n# use `hostnamectl set-hostname centos7-cloud`\n"
    NOTE="\n## Default user is 'centos'\n## NOTE: CentOS seems to ignore hostname config!\n# use 'hostnamectl set-hostname centos7-cloud'\n"
    printf "$NOTE\n"
    wget -P /tmp -N http://cloud.centos.org/centos/7/images/$VMIMAGE
    ;;
  16. @chriswayg chriswayg revised this gist Aug 27, 2019. 1 changed file with 23 additions and 12 deletions.
    35 changes: 23 additions & 12 deletions create-cloud-template.sh
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,17 @@ printf "\n*** This script will download a cloud image and create a Proxmox VM te
    ### NOTES:
    ### - links to cloud images:
    ### https://docs.openstack.org/image-guide/obtain-images.html
    ### http://gentoo.osuosl.org/experimental/amd64/openstack
    ### Debian http://cdimage.debian.org/cdimage/openstack/
    ### Ubuntu http://cloud-images.ubuntu.com/
    ### CentOS: http://cloud.centos.org/centos/7/images/
    ### Fedora: https://alt.fedoraproject.org/cloud/
    ### SUSE 15 SP1 JeOS: https://download.suse.com/Download?buildid=OE-3enq3uys~
    ### CirrOS http://download.cirros-cloud.net/
    ### CoreOS: https://stable.release.core-os.net/amd64-usr/current/
    ### Gentoo: http://gentoo.osuosl.org/experimental/amd64/openstack
    ### Arch (also Gentoo): https://linuximages.de/openstack/arch/
    ### Alpine: https://github.com/chriswayg/packer-qemu-cloud/
    ### RancherOS: https://github.com/rancher/os/releases (also includes Proxmox version)
    ### - most links will download the latest current (stable) version of the OS
    ### - older cloud-init versions do not support hashed passwords

    @@ -40,7 +50,7 @@ case $OSNR in

    2)
    OSNAME=debian9
    VMID_DEFAULT=2000
    VMID_DEFAULT=52000
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=debian-9-openstack-amd64.qcow2
    @@ -51,7 +61,7 @@ case $OSNR in

    3)
    OSNAME=debian10
    VMID_DEFAULT=3000
    VMID_DEFAULT=53000
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE_DEFAULT=debian-10.0.3-20190815-openstack-amd64.qcow2
    @@ -64,7 +74,7 @@ case $OSNR in

    4)
    OSNAME=ubuntu1804
    VMID_DEFAULT=4000
    VMID_DEFAULT=54000
    NOTE="\n## Default user is 'ubuntu'\n"
    printf "$NOTE\n"
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    @@ -75,7 +85,7 @@ case $OSNR in

    5)
    OSNAME=centos7
    VMID_DEFAULT=5000
    VMID_DEFAULT=55000
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    RESIZE=+24G
    @@ -93,7 +103,7 @@ case $OSNR in
    # - coreos needs 'configdrive2'
    # https://github.com/coreos/coreos-cloudinit/blob/master/Documentation/config-drive.md
    OSNAME=coreos
    VMID_DEFAULT=6000
    VMID_DEFAULT=56000
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    RESIZE=+24G
    @@ -105,9 +115,8 @@ case $OSNR in
    ;;

    7)
    # https://linuximages.de/openstack/arch/
    OSNAME=arch
    VMID_DEFAULT=7000
    VMID_DEFAULT=57000
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    RESIZE=+29G
    @@ -119,7 +128,7 @@ case $OSNR in

    8)
    OSNAME=alpine310
    VMID_DEFAULT=8000
    VMID_DEFAULT=58000
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=alpine-310-cloudimg-amd64.qcow2
    @@ -155,6 +164,7 @@ qm set $VMID --boot c --bootdisk scsi0 --serial0 socket --vga serial0

    printf "\n** Using a dhcp server on $BRIDGE (or change to static IP)\n"
    qm set $VMID --ipconfig0 ip=dhcp
    #This would work in a bridged setup, but a routed setup requires a route to be added in the guest
    #qm set $VMID --ipconfig0 ip=10.10.10.222/24,gw=10.10.10.1

    printf "\n** Creating a cloudinit drive managed by Proxmox\n"
    @@ -165,12 +175,13 @@ qm set $VMID --citype $CITYPE

    printf "#** Made with create-cloud-template.sh - https://gist.github.com/chriswayg/43fbea910e024cbe608d7dcb12cb8466\n" >> /etc/pve/nodes/proxmox/qemu-server/$VMID.conf

    ## TODO: Also ask for a network configuration. Or create a config with routing for a static IP
    printf "\n*** The script can add a cloud-init configuration with users and SSH keys from a file in the current directory.\n"
    read -p "Supply the name of the cloud-init-config.yml (this will be skipped, if file not found) [$USERCONFIG_DEFAULT]: " USERCONFIG
    USERCONFIG=${USERCONFIG:-$USERCONFIG_DEFAULT}
    if [ -f $PWD/$USERCONFIG ]
    then
    # TODO - check: The cloud-init config file seems to override the settings done elsewhere
    # The cloud-init user config file overrides the user settings done elsewhere
    printf "\n** Adding user configuration\n"
    cp -v $PWD/$USERCONFIG $SNIPPETSPATH/$VMID-$OSNAME-$USERCONFIG
    qm set $VMID --cicustom "user=local:snippets/$VMID-$OSNAME-$USERCONFIG"
    @@ -188,7 +199,7 @@ else
    printf "#- cloud-config used: via Proxmox\n" >> /etc/pve/nodes/proxmox/qemu-server/$VMID.conf
    fi

    # The NOTE is added to the Summary section of the VM (there seems to be no 'qm' command for this)
    # The NOTE is added to the Summary section of the VM (TODO there seems to be no 'qm' command for this)
    printf "#$NOTE\n" >> /etc/pve/nodes/proxmox/qemu-server/$VMID.conf

    printf "\n** Increasing the disk size\n"
    @@ -199,7 +210,7 @@ qm cloudinit dump $VMID user
    printf "\n------------------------------\n"
    qm cloudinit dump $VMID network

    # convert the vm into a template
    # convert the vm into a template (TODO make this optional)
    qm template $VMID

    printf "\n** Removing previously downloaded image file\n\n"
  17. @chriswayg chriswayg revised this gist Aug 21, 2019. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion create-cloud-template.sh
    Original file line number Diff line number Diff line change
    @@ -91,6 +91,7 @@ case $OSNR in
    # 'Failed to apply cloud-config: Invalid option to manage_etc_hosts'
    # there is no problem, when supplying a compatible 'user-config.yml'
    # - coreos needs 'configdrive2'
    # https://github.com/coreos/coreos-cloudinit/blob/master/Documentation/config-drive.md
    OSNAME=coreos
    VMID_DEFAULT=6000
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    @@ -124,7 +125,7 @@ case $OSNR in
    VMIMAGE=alpine-310-cloudimg-amd64.qcow2
    NOTE="\n## Default user is 'alpine'\n## NOTE: Cloud-init on Alpine is not yet able to apply network config.\n# Also setting a password via cloud-config does not seem to work!\n"
    printf "$NOTE\n"
    https://github.com/chriswayg/packer-qemu-cloud/releases/download/v0.4-beta/$VMIMAGE
    wget -P /tmp -N https://github.com/chriswayg/packer-qemu-cloud/releases/download/v0.4-beta/$VMIMAGE
    #cp -v /root/$VMIMAGE /tmp/ # for local testing
    ;;

  18. @chriswayg chriswayg revised this gist Aug 20, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion create-cloud-template.sh
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ printf "\n*** This script will download a cloud image and create a Proxmox VM te
    ### - older cloud-init versions do not support hashed passwords

    printf "* Available templates to generate:\n 2) debian9\n 3) debian10\n 4) ubuntu1804\n 5) centos7\n 6) coreos\n 7) arch\n 8) alpine310\n\n"
    read -p "* Enter number of OS to use: " OSNR
    read -p "* Enter number of distro to use: " OSNR

    # defaults which are used for most templates
    RESIZE=+30G
  19. @chriswayg chriswayg revised this gist Aug 20, 2019. 1 changed file with 41 additions and 26 deletions.
    67 changes: 41 additions & 26 deletions create-cloud-template.sh
    Original file line number Diff line number Diff line change
    @@ -17,38 +17,42 @@ printf "\n*** This script will download a cloud image and create a Proxmox VM te
    ### - clone the template from the Proxmox GUI and test
    ###
    ### NOTES:
    ### - most cloud images are linked from here:
    ### - links to cloud images:
    ### https://docs.openstack.org/image-guide/obtain-images.html
    ### http://gentoo.osuosl.org/experimental/amd64/openstack
    ### - most links will download the latest current (stable) version of the OS
    ### - older cloud-init versions do not support hashed passwords

    read -p "Enter OS name of template to generate: debian9, debian10, ubuntu1804, centos7, coreos, arch, alpine310 : " OSNAME
    printf "* Available templates to generate:\n 2) debian9\n 3) debian10\n 4) ubuntu1804\n 5) centos7\n 6) coreos\n 7) arch\n 8) alpine310\n\n"
    read -p "* Enter number of OS to use: " OSNR

    # defaults which are used for most templates
    RESIZE=+30G
    MEMORY=2048
    BRIDGE=vmbr1
    USERCONFIG_DEFAULT=none
    USERCONFIG_DEFAULT=none # cloud-init-config.yml
    CITYPE=nocloud
    SNIPPETSPATH=/var/lib/vz/snippets
    SSHKEY=~/.ssh/2019_id_rsa.pub
    SSHKEY=~/.ssh/2019_id_rsa.pub # ~/.ssh/id_rsa.pub
    NOTE=""

    case $OSNAME in
    case $OSNR in

    debian9)
    2)
    OSNAME=debian9
    VMID_DEFAULT=2000
    read -p "Enter a VM ID [$VMID_DEFAULT]: " VMID
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=debian-9-openstack-amd64.qcow2
    NOTE="\n## Default user is 'debian'\n## NOTE: In Debian 9, setting a password via cloud-config does not seem to work!\n"
    printf "$NOTE\n"
    wget -P /tmp -N https://cdimage.debian.org/cdimage/openstack/current-9/$VMIMAGE
    ;;

    debian10)
    3)
    OSNAME=debian10
    VMID_DEFAULT=3000
    read -p "Enter a VM ID [$VMID_DEFAULT]: " VMID
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE_DEFAULT=debian-10.0.3-20190815-openstack-amd64.qcow2
    NOTE="\n## Default user is 'debian'\n"
    @@ -58,19 +62,21 @@ case $OSNAME in
    wget -P /tmp -N https://cdimage.debian.org/cdimage/openstack/current/$VMIMAGE
    ;;

    ubuntu1804)
    4)
    OSNAME=ubuntu1804
    VMID_DEFAULT=4000
    NOTE="\n## Default user is 'ubuntu'\n"
    printf "$NOTE\n"
    read -p "Enter a VM ID [$VMID_DEFAULT]: " VMID
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=bionic-server-cloudimg-amd64.img
    wget -P /tmp -N https://cloud-images.ubuntu.com/bionic/current/$VMIMAGE
    ;;

    centos7)
    5)
    OSNAME=centos7
    VMID_DEFAULT=5000
    read -p "Enter a VM ID [$VMID_DEFAULT]: " VMID
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    RESIZE=+24G
    VMIMAGE=CentOS-7-x86_64-GenericCloud.qcow2
    @@ -79,13 +85,15 @@ case $OSNAME in
    wget -P /tmp -N http://cloud.centos.org/centos/7/images/$VMIMAGE
    ;;

    coreos)
    # Proxmox creates a configdrive with the option: 'manage_etc_hosts: true'
    6)
    # - Proxmox creates a configdrive with the option: 'manage_etc_hosts: true'
    # which causes an error in 'user-configdrive.service':
    # 'Failed to apply cloud-config: Invalid option to manage_etc_hosts'
    # there is no problem, when supplying a compatible 'user-config.yml'
    # - coreos needs 'configdrive2'
    OSNAME=coreos
    VMID_DEFAULT=6000
    read -p "Enter a VM ID [$VMID_DEFAULT]: " VMID
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    RESIZE=+24G
    VMIMAGE=coreos_production_qemu_image.img.bz2
    @@ -95,10 +103,11 @@ case $OSNAME in
    wget -P /tmp -N https://stable.release.core-os.net/amd64-usr/current/$VMIMAGE
    ;;

    arch)
    7)
    # https://linuximages.de/openstack/arch/
    OSNAME=arch
    VMID_DEFAULT=7000
    read -p "Enter a VM ID [$VMID_DEFAULT]: " VMID
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    RESIZE=+29G
    VMIMAGE=arch-openstack-LATEST-image-bootstrap.qcow2
    @@ -107,18 +116,20 @@ case $OSNAME in
    wget -P /tmp -N https://linuximages.de/openstack/arch/$VMIMAGE
    ;;

    alpine310)
    8)
    OSNAME=alpine310
    VMID_DEFAULT=8000
    read -p "Enter a VM ID [$VMID_DEFAULT]: " VMID
    read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=alpine-310-cloudimg-amd64.qcow2.bz2
    VMIMAGE=alpine-310-cloudimg-amd64.qcow2
    NOTE="\n## Default user is 'alpine'\n## NOTE: Cloud-init on Alpine is not yet able to apply network config.\n# Also setting a password via cloud-config does not seem to work!\n"
    printf "$NOTE\n"
    wget -P /tmp -N https://github.com/chriswayg/packer-qemu-cloud/releases/download/v0.3-beta/$VMIMAGE
    https://github.com/chriswayg/packer-qemu-cloud/releases/download/v0.4-beta/$VMIMAGE
    #cp -v /root/$VMIMAGE /tmp/ # for local testing
    ;;

    *)
    printf "\n** Unknown OS name. Please use one of the above!\n"
    printf "\n** Unknown OS number. Please use one of the above!\n"
    exit 0
    ;;
    esac
    @@ -153,8 +164,8 @@ qm set $VMID --citype $CITYPE

    printf "#** Made with create-cloud-template.sh - https://gist.github.com/chriswayg/43fbea910e024cbe608d7dcb12cb8466\n" >> /etc/pve/nodes/proxmox/qemu-server/$VMID.conf

    printf "\n*** The script can add a cloud-init configuration with users and SSH keys from a file in the current directory.\n\n"
    read -p "Supply the name of the file (this will be skipped, if file not found) [$USERCONFIG_DEFAULT]: " USERCONFIG
    printf "\n*** The script can add a cloud-init configuration with users and SSH keys from a file in the current directory.\n"
    read -p "Supply the name of the cloud-init-config.yml (this will be skipped, if file not found) [$USERCONFIG_DEFAULT]: " USERCONFIG
    USERCONFIG=${USERCONFIG:-$USERCONFIG_DEFAULT}
    if [ -f $PWD/$USERCONFIG ]
    then
    @@ -176,17 +187,21 @@ else
    printf "#- cloud-config used: via Proxmox\n" >> /etc/pve/nodes/proxmox/qemu-server/$VMID.conf
    fi

    # The NOTE is added to the Summary section of the VM (there seems to be no 'qm' command for this)
    printf "#$NOTE\n" >> /etc/pve/nodes/proxmox/qemu-server/$VMID.conf

    printf "\n** Increasing the disk size\n"
    qm resize $VMID scsi0 $RESIZE

    printf "\n*** The following cloud-init configuration for User and Network will be used ***\n\n"
    qm cloudinit dump $VMID user
    printf "\n------------------------------\n"
    qm cloudinit dump $VMID network

    # convert the vm into a template
    qm template $VMID

    # printf "\n** Removing previously downloaded image file\n"
    printf "\n** Removing previously downloaded image file\n\n"
    rm -v /tmp/$VMIMAGE

    printf "$NOTE\n\n"
  20. @chriswayg chriswayg revised this gist Aug 19, 2019. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions create-cloud-template.sh
    Original file line number Diff line number Diff line change
    @@ -111,11 +111,10 @@ case $OSNAME in
    VMID_DEFAULT=8000
    read -p "Enter a VM ID [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=alpine-310-cloudimg-amd64.qcow2
    VMIMAGE=alpine-310-cloudimg-amd64.qcow2.bz2
    NOTE="\n## Default user is 'alpine'\n## NOTE: Cloud-init on Alpine is not yet able to apply network config.\n# Also setting a password via cloud-config does not seem to work!\n"
    printf "$NOTE\n"
    cp -v /root/$VMIMAGE /tmp/
    #wget https://$VMIMAGE
    wget -P /tmp -N https://github.com/chriswayg/packer-qemu-cloud/releases/download/v0.3-beta/$VMIMAGE
    ;;

    *)
    @@ -190,4 +189,4 @@ qm cloudinit dump $VMID network
    qm template $VMID

    # printf "\n** Removing previously downloaded image file\n"
    rm -v /tmp/$VMIMAGE
    rm -v /tmp/$VMIMAGE
  21. @chriswayg chriswayg revised this gist Aug 19, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions create-cloud-template.sh
    Original file line number Diff line number Diff line change
    @@ -9,8 +9,8 @@ printf "\n*** This script will download a cloud image and create a Proxmox VM te
    ### - run on a Proxmox 6 server
    ### - a dhcp server should be active on vmbr1
    ###
    ### - fork the gist and adapt the defaults
    ### - clone the script into /usr/local/bin/
    ### - fork the gist and adapt the defaults as needed
    ### - download the script into /usr/local/bin/
    ### - chmod +x /usr/local/bin/create-cloud-template.sh
    ### - prepare a cloudinit user-config.yml in the working directory (optional)
    ### - run the script
  22. @chriswayg chriswayg revised this gist Aug 19, 2019. 1 changed file with 36 additions and 29 deletions.
    65 changes: 36 additions & 29 deletions create-cloud-template.sh
    Original file line number Diff line number Diff line change
    @@ -6,19 +6,20 @@ printf "\n*** This script will download a cloud image and create a Proxmox VM te

    ### How to use:
    ### Pre-req:
    ### - use on a Proxmox 6 server
    ### - a dhcp server on vmbr1
    ### - run on a Proxmox 6 server
    ### - a dhcp server should be active on vmbr1
    ###
    ### save the script as /usr/local/bin/create_cloud_template
    ### chmod +x /usr/local/bin/create_cloud_template
    ### - prepare a cloud-init config in the Proxmox snippets directory (optional)
    ### - fork the gist and adapt the defaults
    ### - clone the script into /usr/local/bin/
    ### - chmod +x /usr/local/bin/create-cloud-template.sh
    ### - prepare a cloudinit user-config.yml in the working directory (optional)
    ### - run the script
    ### - clone the template from the Proxmox GUI and test
    ###
    ### NOTES:
    ### - most cloud images are linked from here:
    ### https://docs.openstack.org/image-guide/obtain-images.html
    ###
    ### - most links will download the latest current (stable) version of the OS
    ### - older cloud-init versions do not support hashed passwords

    read -p "Enter OS name of template to generate: debian9, debian10, ubuntu1804, centos7, coreos, arch, alpine310 : " OSNAME
    @@ -30,7 +31,7 @@ BRIDGE=vmbr1
    USERCONFIG_DEFAULT=none
    CITYPE=nocloud
    SNIPPETSPATH=/var/lib/vz/snippets
    SSHKEY=~/.ssh/id_rsa.pub
    SSHKEY=~/.ssh/2019_id_rsa.pub
    NOTE=""

    case $OSNAME in
    @@ -40,8 +41,8 @@ case $OSNAME in
    read -p "Enter a VM ID [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=debian-9-openstack-amd64.qcow2
    NOTE="In Debian 9, setting a password via cloud-config does not seem to work!\n"
    printf "\n** Default user is 'debian'\n** NOTE: $NOTE\n"
    NOTE="\n## Default user is 'debian'\n## NOTE: In Debian 9, setting a password via cloud-config does not seem to work!\n"
    printf "$NOTE\n"
    wget -P /tmp -N https://cdimage.debian.org/cdimage/openstack/current-9/$VMIMAGE
    ;;

    @@ -50,15 +51,17 @@ case $OSNAME in
    read -p "Enter a VM ID [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE_DEFAULT=debian-10.0.3-20190815-openstack-amd64.qcow2
    printf "\n** Default user is 'debian'\n** NOTE: Check, if the image is up to date on https://cdimage.debian.org/cdimage/openstack/\n\n"
    NOTE="\n## Default user is 'debian'\n"
    printf "$NOTE## NOTE: Check, if the image is up to date on https://cdimage.debian.org/cdimage/openstack/\n\n"
    read -p "Enter the new VM image filename [$VMIMAGE_DEFAULT]: " VMIMAGE
    VMIMAGE=${VMIMAGE:-$VMIMAGE_DEFAULT}
    wget -P /tmp -N https://cdimage.debian.org/cdimage/openstack/current/$VMIMAGE
    ;;

    ubuntu1804)
    VMID_DEFAULT=4000
    printf "\n** Default user is 'ubuntu'\n\n"
    NOTE="\n## Default user is 'ubuntu'\n"
    printf "$NOTE\n"
    read -p "Enter a VM ID [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=bionic-server-cloudimg-amd64.img
    @@ -71,8 +74,8 @@ case $OSNAME in
    VMID=${VMID:-$VMID_DEFAULT}
    RESIZE=+24G
    VMIMAGE=CentOS-7-x86_64-GenericCloud.qcow2
    NOTE="CentOS seems to ignore hostname config!\n"
    printf "\n** Default user is 'centos'\n** NOTE: $NOTE\n"
    NOTE="\n## Default user is 'centos'\n## NOTE: CentOS seems to ignore hostname config!\n# use `hostnamectl set-hostname centos7-cloud`\n"
    printf "$NOTE\n"
    wget -P /tmp -N http://cloud.centos.org/centos/7/images/$VMIMAGE
    ;;

    @@ -87,8 +90,8 @@ case $OSNAME in
    RESIZE=+24G
    VMIMAGE=coreos_production_qemu_image.img.bz2
    CITYPE=configdrive2
    NOTE="In CoreOS, setting a password via cloud-config does not seem to work!\n"
    printf "\n** Default user is 'core'\n** NOTE: $NOTE\n"
    NOTE="\n## Default user is 'core'\n## NOTE: In CoreOS, setting a password via cloud-config does not seem to work!\n"
    printf "$NOTE\n"
    wget -P /tmp -N https://stable.release.core-os.net/amd64-usr/current/$VMIMAGE
    ;;

    @@ -97,9 +100,10 @@ case $OSNAME in
    VMID_DEFAULT=7000
    read -p "Enter a VM ID [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    RESIZE=+29G
    VMIMAGE=arch-openstack-LATEST-image-bootstrap.qcow2
    NOTE="In Arch Linux, setting a password via cloud-config does not seem to work!\n"
    printf "\n** Default user is 'arch'\n** NOTE: $NOTE\n"
    NOTE="\n## Default user is 'arch'\n## NOTE: In Arch Linux, setting a password via cloud-config does not seem to work!\n# - Resizing does not happen automatically inside the VM\n"
    printf "$NOTE\n"
    wget -P /tmp -N https://linuximages.de/openstack/arch/$VMIMAGE
    ;;

    @@ -108,8 +112,8 @@ case $OSNAME in
    read -p "Enter a VM ID [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=alpine-310-cloudimg-amd64.qcow2
    NOTE="Cloud-init on Alpine is not yet able to apply network config.\n Also setting a password via cloud-config does not seem to work!\n"
    printf "\n** Default user is 'alpine'\n** NOTE: $NOTE\n"
    NOTE="\n## Default user is 'alpine'\n## NOTE: Cloud-init on Alpine is not yet able to apply network config.\n# Also setting a password via cloud-config does not seem to work!\n"
    printf "$NOTE\n"
    cp -v /root/$VMIMAGE /tmp/
    #wget https://$VMIMAGE
    ;;
    @@ -127,7 +131,7 @@ esac

    # TODO: could prompt for the VM name
    printf "\n** Creating a VM with $MEMORY MB using network bridge $BRIDGE\n"
    qm create $VMID --name $OSNAME-templ --memory $MEMORY --net0 virtio,bridge=$BRIDGE
    qm create $VMID --name $OSNAME-cloud --memory $MEMORY --net0 virtio,bridge=$BRIDGE

    printf "\n** Importing the disk in qcow2 format (as 'Unused Disk 0')\n"
    qm importdisk $VMID /tmp/$VMIMAGE local -format qcow2
    @@ -148,28 +152,31 @@ qm set $VMID --ide2 local:cloudinit
    printf "\n** Specifying the cloud-init configuration format\n"
    qm set $VMID --citype $CITYPE

    printf "#Made with create-cloud-template.sh https://gist.github.com/chriswayg/43fbea910e024cbe608d7dcb12cb8466\n" >> /etc/pve/nodes/proxmox/qemu-server/$VMID.conf
    printf "#** Made with create-cloud-template.sh - https://gist.github.com/chriswayg/43fbea910e024cbe608d7dcb12cb8466\n" >> /etc/pve/nodes/proxmox/qemu-server/$VMID.conf

    printf "\n*** The script can add a cloud-init configuration with users and SSH keys from a file in the current directory.\n\n"
    read -p "Supply the name of the file (this will be skipped, if file not found) [$USERCONFIG_DEFAULT]: " USERCONFIG
    USERCONFIG=${USERCONFIG:-$USERCONFIG_DEFAULT}
    if [ -f $PWD/$USERCONFIG ]
    then
    # TODO - check: The cloud-init configuration file seems to override the settings done elsewhere
    printf "** Adding user configuration as: $SNIPPETSPATH/$VMID-$OSNAME-$USERCONFIG\n" \
    cp -v $PWD/$USERCONFIG $SNIPPETSPATH/$VMID-$OSNAME-$USERCONFIG \
    # TODO - check: The cloud-init config file seems to override the settings done elsewhere
    printf "\n** Adding user configuration\n"
    cp -v $PWD/$USERCONFIG $SNIPPETSPATH/$VMID-$OSNAME-$USERCONFIG
    qm set $VMID --cicustom "user=local:snippets/$VMID-$OSNAME-$USERCONFIG"
    printf "#Config used: $VMID-$OSNAME-$USERCONFIG\n" >> /etc/pve/nodes/proxmox/qemu-server/$VMID.conf
    printf "#* cloud-config: $VMID-$OSNAME-$USERCONFIG\n" >> /etc/pve/nodes/proxmox/qemu-server/$VMID.conf
    else
    # The SSH key should be supplied either in the cloud-init config file or here
    printf "\n** Skipping config file, as none was found\n\n** Adding SSH key\n"
    qm set $VMID --sshkey $SSHKEY
    printf "\n"
    read -p "Supply an optional password for the default user (press Enter for none): " PASSWORD
    [ ! -z "$PASSWORD" ] \
    && printf "\n** Adding the password to the config\n" \
    && qm set $VMID --cipassword $PASSWORD
    printf "#Config used: via Proxmaox\n" >> /etc/pve/nodes/proxmox/qemu-server/$VMID.conf
    && qm set $VMID --cipassword $PASSWORD \
    && printf "#* a password has been set for the default user\n" >> /etc/pve/nodes/proxmox/qemu-server/$VMID.conf
    printf "#- cloud-config used: via Proxmox\n" >> /etc/pve/nodes/proxmox/qemu-server/$VMID.conf
    fi

    printf "#$NOTE\n" >> /etc/pve/nodes/proxmox/qemu-server/$VMID.conf

    printf "\n** Increasing the disk size\n"
    @@ -180,7 +187,7 @@ qm cloudinit dump $VMID user
    qm cloudinit dump $VMID network

    # convert the vm into a template
    #qm template $VMID
    qm template $VMID

    # printf "\n** Removing previously downloaded image file\n"
    # rm -v /tmp/$VMIMAGE
    rm -v /tmp/$VMIMAGE
  23. @chriswayg chriswayg revised this gist Aug 19, 2019. 1 changed file with 16 additions and 10 deletions.
    26 changes: 16 additions & 10 deletions create-cloud-template.sh
    Original file line number Diff line number Diff line change
    @@ -31,6 +31,7 @@ USERCONFIG_DEFAULT=none
    CITYPE=nocloud
    SNIPPETSPATH=/var/lib/vz/snippets
    SSHKEY=~/.ssh/id_rsa.pub
    NOTE=""

    case $OSNAME in

    @@ -39,7 +40,8 @@ case $OSNAME in
    read -p "Enter a VM ID [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=debian-9-openstack-amd64.qcow2
    printf "\n** Default user is 'debian'\n** NOTE: In Debian 9, setting a password via cloud-config does not seem to work!\n\n"
    NOTE="In Debian 9, setting a password via cloud-config does not seem to work!\n"
    printf "\n** Default user is 'debian'\n** NOTE: $NOTE\n"
    wget -P /tmp -N https://cdimage.debian.org/cdimage/openstack/current-9/$VMIMAGE
    ;;

    @@ -48,7 +50,7 @@ case $OSNAME in
    read -p "Enter a VM ID [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE_DEFAULT=debian-10.0.3-20190815-openstack-amd64.qcow2
    printf "\n** Default user is 'debian'\n** NOTE: Check, if the default is up to date on https://cdimage.debian.org/cdimage/openstack/\n\n"
    printf "\n** Default user is 'debian'\n** NOTE: Check, if the image is up to date on https://cdimage.debian.org/cdimage/openstack/\n\n"
    read -p "Enter the new VM image filename [$VMIMAGE_DEFAULT]: " VMIMAGE
    VMIMAGE=${VMIMAGE:-$VMIMAGE_DEFAULT}
    wget -P /tmp -N https://cdimage.debian.org/cdimage/openstack/current/$VMIMAGE
    @@ -69,7 +71,8 @@ case $OSNAME in
    VMID=${VMID:-$VMID_DEFAULT}
    RESIZE=+24G
    VMIMAGE=CentOS-7-x86_64-GenericCloud.qcow2
    printf "\n** Default user is 'centos'\n** NOTE: CentOS seems to ignore hostname config!\n\n"
    NOTE="CentOS seems to ignore hostname config!\n"
    printf "\n** Default user is 'centos'\n** NOTE: $NOTE\n"
    wget -P /tmp -N http://cloud.centos.org/centos/7/images/$VMIMAGE
    ;;

    @@ -84,7 +87,8 @@ case $OSNAME in
    RESIZE=+24G
    VMIMAGE=coreos_production_qemu_image.img.bz2
    CITYPE=configdrive2
    printf "\n** Default user is 'core'\n** NOTE: In CoreOS9, setting a password via cloud-config does not seem to work!\n\n"
    NOTE="In CoreOS, setting a password via cloud-config does not seem to work!\n"
    printf "\n** Default user is 'core'\n** NOTE: $NOTE\n"
    wget -P /tmp -N https://stable.release.core-os.net/amd64-usr/current/$VMIMAGE
    ;;

    @@ -94,7 +98,8 @@ case $OSNAME in
    read -p "Enter a VM ID [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=arch-openstack-LATEST-image-bootstrap.qcow2
    printf "\n** Default user is 'arch'\n** NOTE: In Arch Linux, setting a password via cloud-config does not seem to work!\n\n"
    NOTE="In Arch Linux, setting a password via cloud-config does not seem to work!\n"
    printf "\n** Default user is 'arch'\n** NOTE: $NOTE\n"
    wget -P /tmp -N https://linuximages.de/openstack/arch/$VMIMAGE
    ;;

    @@ -103,7 +108,8 @@ case $OSNAME in
    read -p "Enter a VM ID [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=alpine-310-cloudimg-amd64.qcow2
    printf "\n** Default user is 'alpine'\n** NOTE: Cloud-init on Alpine is not yet able to apply network config.\n Also setting a password via cloud-config does not seem to work!\n\n"
    NOTE="Cloud-init on Alpine is not yet able to apply network config.\n Also setting a password via cloud-config does not seem to work!\n"
    printf "\n** Default user is 'alpine'\n** NOTE: $NOTE\n"
    cp -v /root/$VMIMAGE /tmp/
    #wget https://$VMIMAGE
    ;;
    @@ -142,7 +148,7 @@ qm set $VMID --ide2 local:cloudinit
    printf "\n** Specifying the cloud-init configuration format\n"
    qm set $VMID --citype $CITYPE

    echo "#Made with create_cloud_template" >> /etc/pve/nodes/proxmox/qemu-server/$VMID.conf
    printf "#Made with create-cloud-template.sh https://gist.github.com/chriswayg/43fbea910e024cbe608d7dcb12cb8466\n" >> /etc/pve/nodes/proxmox/qemu-server/$VMID.conf

    printf "\n*** The script can add a cloud-init configuration with users and SSH keys from a file in the current directory.\n\n"
    read -p "Supply the name of the file (this will be skipped, if file not found) [$USERCONFIG_DEFAULT]: " USERCONFIG
    @@ -153,8 +159,7 @@ then
    printf "** Adding user configuration as: $SNIPPETSPATH/$VMID-$OSNAME-$USERCONFIG\n" \
    cp -v $PWD/$USERCONFIG $SNIPPETSPATH/$VMID-$OSNAME-$USERCONFIG \
    qm set $VMID --cicustom "user=local:snippets/$VMID-$OSNAME-$USERCONFIG"
    $USERCONFIG=$VMID-$OSNAME-$USERCONFIG
    echo "#Config used: $USERCONFIG" >> /etc/pve/nodes/proxmox/qemu-server/$VMID.conf
    printf "#Config used: $VMID-$OSNAME-$USERCONFIG\n" >> /etc/pve/nodes/proxmox/qemu-server/$VMID.conf
    else
    printf "\n** Skipping config file, as none was found\n\n** Adding SSH key\n"
    qm set $VMID --sshkey $SSHKEY
    @@ -163,8 +168,9 @@ else
    [ ! -z "$PASSWORD" ] \
    && printf "\n** Adding the password to the config\n" \
    && qm set $VMID --cipassword $PASSWORD
    echo "#Config used: via Proxmaox" >> /etc/pve/nodes/proxmox/qemu-server/$VMID.conf
    printf "#Config used: via Proxmaox\n" >> /etc/pve/nodes/proxmox/qemu-server/$VMID.conf
    fi
    printf "#$NOTE\n" >> /etc/pve/nodes/proxmox/qemu-server/$VMID.conf

    printf "\n** Increasing the disk size\n"
    qm resize $VMID scsi0 $RESIZE
  24. @chriswayg chriswayg created this gist Aug 19, 2019.
    180 changes: 180 additions & 0 deletions create-cloud-template.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,180 @@
    #!/bin/bash
    set -e
    #set -x
    clear
    printf "\n*** This script will download a cloud image and create a Proxmox VM template from it. ***\n\n"

    ### How to use:
    ### Pre-req:
    ### - use on a Proxmox 6 server
    ### - a dhcp server on vmbr1
    ###
    ### save the script as /usr/local/bin/create_cloud_template
    ### chmod +x /usr/local/bin/create_cloud_template
    ### - prepare a cloud-init config in the Proxmox snippets directory (optional)
    ### - run the script
    ### - clone the template from the Proxmox GUI and test
    ###
    ### NOTES:
    ### - most cloud images are linked from here:
    ### https://docs.openstack.org/image-guide/obtain-images.html
    ###
    ### - older cloud-init versions do not support hashed passwords

    read -p "Enter OS name of template to generate: debian9, debian10, ubuntu1804, centos7, coreos, arch, alpine310 : " OSNAME

    # defaults which are used for most templates
    RESIZE=+30G
    MEMORY=2048
    BRIDGE=vmbr1
    USERCONFIG_DEFAULT=none
    CITYPE=nocloud
    SNIPPETSPATH=/var/lib/vz/snippets
    SSHKEY=~/.ssh/id_rsa.pub

    case $OSNAME in

    debian9)
    VMID_DEFAULT=2000
    read -p "Enter a VM ID [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=debian-9-openstack-amd64.qcow2
    printf "\n** Default user is 'debian'\n** NOTE: In Debian 9, setting a password via cloud-config does not seem to work!\n\n"
    wget -P /tmp -N https://cdimage.debian.org/cdimage/openstack/current-9/$VMIMAGE
    ;;

    debian10)
    VMID_DEFAULT=3000
    read -p "Enter a VM ID [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE_DEFAULT=debian-10.0.3-20190815-openstack-amd64.qcow2
    printf "\n** Default user is 'debian'\n** NOTE: Check, if the default is up to date on https://cdimage.debian.org/cdimage/openstack/\n\n"
    read -p "Enter the new VM image filename [$VMIMAGE_DEFAULT]: " VMIMAGE
    VMIMAGE=${VMIMAGE:-$VMIMAGE_DEFAULT}
    wget -P /tmp -N https://cdimage.debian.org/cdimage/openstack/current/$VMIMAGE
    ;;

    ubuntu1804)
    VMID_DEFAULT=4000
    printf "\n** Default user is 'ubuntu'\n\n"
    read -p "Enter a VM ID [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=bionic-server-cloudimg-amd64.img
    wget -P /tmp -N https://cloud-images.ubuntu.com/bionic/current/$VMIMAGE
    ;;

    centos7)
    VMID_DEFAULT=5000
    read -p "Enter a VM ID [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    RESIZE=+24G
    VMIMAGE=CentOS-7-x86_64-GenericCloud.qcow2
    printf "\n** Default user is 'centos'\n** NOTE: CentOS seems to ignore hostname config!\n\n"
    wget -P /tmp -N http://cloud.centos.org/centos/7/images/$VMIMAGE
    ;;

    coreos)
    # Proxmox creates a configdrive with the option: 'manage_etc_hosts: true'
    # which causes an error in 'user-configdrive.service':
    # 'Failed to apply cloud-config: Invalid option to manage_etc_hosts'
    # there is no problem, when supplying a compatible 'user-config.yml'
    VMID_DEFAULT=6000
    read -p "Enter a VM ID [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    RESIZE=+24G
    VMIMAGE=coreos_production_qemu_image.img.bz2
    CITYPE=configdrive2
    printf "\n** Default user is 'core'\n** NOTE: In CoreOS9, setting a password via cloud-config does not seem to work!\n\n"
    wget -P /tmp -N https://stable.release.core-os.net/amd64-usr/current/$VMIMAGE
    ;;

    arch)
    # https://linuximages.de/openstack/arch/
    VMID_DEFAULT=7000
    read -p "Enter a VM ID [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=arch-openstack-LATEST-image-bootstrap.qcow2
    printf "\n** Default user is 'arch'\n** NOTE: In Arch Linux, setting a password via cloud-config does not seem to work!\n\n"
    wget -P /tmp -N https://linuximages.de/openstack/arch/$VMIMAGE
    ;;

    alpine310)
    VMID_DEFAULT=8000
    read -p "Enter a VM ID [$VMID_DEFAULT]: " VMID
    VMID=${VMID:-$VMID_DEFAULT}
    VMIMAGE=alpine-310-cloudimg-amd64.qcow2
    printf "\n** Default user is 'alpine'\n** NOTE: Cloud-init on Alpine is not yet able to apply network config.\n Also setting a password via cloud-config does not seem to work!\n\n"
    cp -v /root/$VMIMAGE /tmp/
    #wget https://$VMIMAGE
    ;;

    *)
    printf "\n** Unknown OS name. Please use one of the above!\n"
    exit 0
    ;;
    esac

    [[ $VMIMAGE == *".bz2" ]] \
    && printf "\n** Uncompressing image (waiting to complete...)\n" \
    && bzip2 -d --force /tmp/$VMIMAGE \
    && VMIMAGE=$(echo "${VMIMAGE%.*}") # remove .bz2 file extension from file name

    # TODO: could prompt for the VM name
    printf "\n** Creating a VM with $MEMORY MB using network bridge $BRIDGE\n"
    qm create $VMID --name $OSNAME-templ --memory $MEMORY --net0 virtio,bridge=$BRIDGE

    printf "\n** Importing the disk in qcow2 format (as 'Unused Disk 0')\n"
    qm importdisk $VMID /tmp/$VMIMAGE local -format qcow2

    printf "\n** Attaching the disk to the vm using VirtIO SCSI\n"
    qm set $VMID --scsihw virtio-scsi-pci --scsi0 /var/lib/vz/images/$VMID/vm-$VMID-disk-0.qcow2

    printf "\n** Setting boot and display settings with serial console\n"
    qm set $VMID --boot c --bootdisk scsi0 --serial0 socket --vga serial0

    printf "\n** Using a dhcp server on $BRIDGE (or change to static IP)\n"
    qm set $VMID --ipconfig0 ip=dhcp
    #qm set $VMID --ipconfig0 ip=10.10.10.222/24,gw=10.10.10.1

    printf "\n** Creating a cloudinit drive managed by Proxmox\n"
    qm set $VMID --ide2 local:cloudinit

    printf "\n** Specifying the cloud-init configuration format\n"
    qm set $VMID --citype $CITYPE

    echo "#Made with create_cloud_template" >> /etc/pve/nodes/proxmox/qemu-server/$VMID.conf

    printf "\n*** The script can add a cloud-init configuration with users and SSH keys from a file in the current directory.\n\n"
    read -p "Supply the name of the file (this will be skipped, if file not found) [$USERCONFIG_DEFAULT]: " USERCONFIG
    USERCONFIG=${USERCONFIG:-$USERCONFIG_DEFAULT}
    if [ -f $PWD/$USERCONFIG ]
    then
    # TODO - check: The cloud-init configuration file seems to override the settings done elsewhere
    printf "** Adding user configuration as: $SNIPPETSPATH/$VMID-$OSNAME-$USERCONFIG\n" \
    cp -v $PWD/$USERCONFIG $SNIPPETSPATH/$VMID-$OSNAME-$USERCONFIG \
    qm set $VMID --cicustom "user=local:snippets/$VMID-$OSNAME-$USERCONFIG"
    $USERCONFIG=$VMID-$OSNAME-$USERCONFIG
    echo "#Config used: $USERCONFIG" >> /etc/pve/nodes/proxmox/qemu-server/$VMID.conf
    else
    printf "\n** Skipping config file, as none was found\n\n** Adding SSH key\n"
    qm set $VMID --sshkey $SSHKEY
    printf "\n"
    read -p "Supply an optional password for the default user (press Enter for none): " PASSWORD
    [ ! -z "$PASSWORD" ] \
    && printf "\n** Adding the password to the config\n" \
    && qm set $VMID --cipassword $PASSWORD
    echo "#Config used: via Proxmaox" >> /etc/pve/nodes/proxmox/qemu-server/$VMID.conf
    fi

    printf "\n** Increasing the disk size\n"
    qm resize $VMID scsi0 $RESIZE

    printf "\n*** The following cloud-init configuration for User and Network will be used ***\n\n"
    qm cloudinit dump $VMID user
    qm cloudinit dump $VMID network

    # convert the vm into a template
    #qm template $VMID

    # printf "\n** Removing previously downloaded image file\n"
    # rm -v /tmp/$VMIMAGE