Skip to content

Scripts to automate template generation for Ubuntu Server and Desktop for Proxmox Virtual Environment (PVE)

License

Notifications You must be signed in to change notification settings

fmarcelinoPT/proxmox-cloud-init-templates

Repository files navigation

Proxmox Cloud-Init Image Builder

This project contains two shell scripts, build-cloudinit-image-desktop.sh and build-cloudinit-image-server.sh, which are used to create Proxmox Virtual Environment (PVE) templates for Ubuntu Desktop and Server, respectively.

These templates are configured with Cloud-Init to automatically set up the operating system during the first boot.

Table of Contents

Installation

  1. Clone the repository to your local machine.
  2. Make sure you have the necessary permissions to run the scripts and interact with Proxmox Virtual Environment.
  3. Install any dependencies required by the scripts.

Usage

Download the needed images:

wget https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img
wget https://cloud.centos.org/centos/9-stream/x86_64/images/CentOS-Stream-GenericCloud-x86_64-9-latest.x86_64.qcow2

To create a new template, run the appropriate script in a PVE node:

  • For Ubuntu Desktop:
./build-cloudinit-image-desktop.sh
  • For Ubuntu Server:
./build-cloudinit-image-server.sh

These scripts will create a new virtual machine, configure it with Cloud-Init, and then convert it into a template. The template can then be used to quickly create new virtual machines with the same configuration.

Configuration

The scripts use the following configuration files:

  • ubuntu-desktop.yaml
  • ubuntu-server.yaml
  • centos-server.yaml

Which are used by Cloud-Init to customize the packages installed on the operating system. These files should be registered on the PVE and stored in the local:snippets/ directory.

The ubuntu-desktop.yaml file is used in the build-cloudinit-image-desktop.sh script to install the KDE Plasma Desktop environment.

Preparation

SSH Keys

Create the ssh keys according to My Freakin Homelab > SSH-Keys.

Creating the vendor.yaml file for cloudinit

cat << EOF | tee /var/lib/vz/snippets/ubuntu-server.yaml
#cloud-config
apt:
  primary:
    - arches: [default]
      uri: http://pt.archive.ubuntu.com/ubuntu/

package_update: true

package_upgrade: true

locale: pt_PT

runcmd:
  - sudo useradd -m -s /bin/bash ansible
  - echo "ansible:password" | chpasswd
  - usermod -aG sudo ansible
  - mkdir -p /home/ansible/.ssh
  - echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDWDAlsCB5kRfoYW1jnd0/9kmZtalmN4cM8T5qJ8ualsMVTwo22ijKwieQG5yRpvgWAb87tBpoTtx+moKk7DIWezkyAAn5ptzHDeupyXith3otLtm8gy6uSvHJWrd3K24pnsjCuHAvlKmqtpC2IiPM0EoHsQLljiOPsSfJ4I8IBCrNMXOZI1tAzTIJB3GIsLk1D6E6/rwSWwqif87mQlqFV2dv3F0qpBN1ZPNldNCor2FD7RWmJsIGmGFadoIW/G/MXDlHnKqQQtPCytQNF2Q8JLf4eSytZRhcqrkOM3DmZ2hItM3JxbGB7NfUWS6gN9CzamE5Xumb2MxHQMqQrvJaKggsWTNecioDL1nm5FKfgbKMOoKpKkkw3ng0npay+rnpWcLceX3u9de49qEi68P6o/aF4MjdtXODFyT9lr6dmd51/7TYkmSlmIbcVsZi6rN9VvO7pOwy3tw4bPBj0IYX1OsDv1OTKPzKWPtTCY3Fa485vSRJP8y77OxlgzTSjhmskrWQNXd3rftjbuvWzg8uCVhh0edbo3TU5+z6dcMszkU1m7nPeyYp2pOcamKyXkOAgamfrqvOzPVun1OEwpc/5b7mSPufnGREj35RTWqYAFDpZ3BeUaHOKRwoUSsMNuoNPeaR723YZVhduaAUKZdnycHffduRWsp7S723AMWyaLw== support_pub_key" >> /home/ansible/.ssh/authorized_keys
  - chown -R ansible:ansible /home/ansible/.ssh
  - chmod 700 /home/ansible/.ssh
  - chmod 600 /home/ansible/.ssh/authorized_keys
  - apt install -y qemu-guest-agent
  - systemctl enable qemu-guest-agent
  - systemctl start qemu-guest-agent
  - loadkeys pt
EOF
cat << EOF | tee /var/lib/vz/snippets/ubuntu-desktop.yaml
#cloud-config
apt:
  primary:
    - arches: [default]
      uri: http://pt.archive.ubuntu.com/ubuntu/

package_update: true

package_upgrade: true

locale: pt_PT

write_files:
  - path: /etc/lightdm/lightdm.conf
    content: |
      [Seat:*]
      autologin-user-timeout=0
      user-session=plasma

runcmd:
  - sudo useradd -m -s /bin/bash ansible
  - echo "ansible:password" | chpasswd
  - usermod -aG sudo ansible
  - mkdir -p /home/ansible/.ssh
  - echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDWDAlsCB5kRfoYW1jnd0/9kmZtalmN4cM8T5qJ8ualsMVTwo22ijKwieQG5yRpvgWAb87tBpoTtx+moKk7DIWezkyAAn5ptzHDeupyXith3otLtm8gy6uSvHJWrd3K24pnsjCuHAvlKmqtpC2IiPM0EoHsQLljiOPsSfJ4I8IBCrNMXOZI1tAzTIJB3GIsLk1D6E6/rwSWwqif87mQlqFV2dv3F0qpBN1ZPNldNCor2FD7RWmJsIGmGFadoIW/G/MXDlHnKqQQtPCytQNF2Q8JLf4eSytZRhcqrkOM3DmZ2hItM3JxbGB7NfUWS6gN9CzamE5Xumb2MxHQMqQrvJaKggsWTNecioDL1nm5FKfgbKMOoKpKkkw3ng0npay+rnpWcLceX3u9de49qEi68P6o/aF4MjdtXODFyT9lr6dmd51/7TYkmSlmIbcVsZi6rN9VvO7pOwy3tw4bPBj0IYX1OsDv1OTKPzKWPtTCY3Fa485vSRJP8y77OxlgzTSjhmskrWQNXd3rftjbuvWzg8uCVhh0edbo3TU5+z6dcMszkU1m7nPeyYp2pOcamKyXkOAgamfrqvOzPVun1OEwpc/5b7mSPufnGREj35RTWqYAFDpZ3BeUaHOKRwoUSsMNuoNPeaR723YZVhduaAUKZdnycHffduRWsp7S723AMWyaLw== support_pub_key" >> /home/ansible/.ssh/authorized_keys
  - chown -R ansible:ansible /home/ansible/.ssh
  - chmod 700 /home/ansible/.ssh
  - chmod 600 /home/ansible/.ssh/authorized_keys
  - apt install -y qemu-guest-agent
  - systemctl enable qemu-guest-agent
  - systemctl start qemu-guest-agent
  - apt install -y lightdm
  - systemctl enable lightdm
  - systemctl start lightdm
  - apt install -y plasma-desktop
  - apt install -y konsole
  - apt remove -y vim
  - apt autoremove
  - loadkeys pt
EOF
cat << EOF | tee /var/lib/vz/snippets/centos-server.yaml
#cloud-config
package_update: true

package_upgrade: true

locale: pt_PT

runcmd:
  - yum install -y vim git net-tools
  - useradd ansiblebot
  - echo "ansiblebot ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
  - yum clean all
  - rm -rf /var/cache/yum
EOF

This files serves two purposes: the first one is quite evident (installing qemu-guest-agent), while the second purpose may not be as obvious. Due to the sequencing of CloudInit, it initiates after networking, resulting in the inability to SSH or even ping the VM using the assigned name. However, this package is executed only once, so following the reboot, you will have full accessibility to the VM.

Before start creating the image, open datacenter → storage → select local → add snippets to contents list:

Datacenter -> Storage -> Local

Added Snippets to Content

Build cloudinit image

sh build-cloudinit-image-server.sh
sh build-cloudinit-image-desktop.sh

Check cloud-init status:

cloud-init status --wait

Repeat cloud-init:

cloud-init clean --logs
cloud-init init --local
cloud-init init
cloud-init modules --mode=config
cloud-init modules --mode=final
cloud-init status --wait --long

Test VM creation

sh create-vm-cloned-server.sh
sh create-vm-cloned-desktop.sh

Desktop tweaks

Change theme on target Ubuntu Desktop: lookandfeeltool -a org.kde.breezedark.desktop

SSH fixes

Need to set right permissions to ssh key:

cp ./onemarc_rsa ~/.ssh/onemarc_rsa
cp ./onemarc_rsa.pub ~/.ssh/onemarc_rsa.pub
cp ./support_rsa ~/.ssh/support_rsa
cp ./support_rsa.pub ~/.ssh/support_rsa.pub
chmod 400 ./onemarc_rsa
chmod 400 ./onemarc_rsa.pub
chmod 400 ./support_rsa
chmod 400 ./support_rsa.pub

Cleanup

qm stop 1000 && qm unlock 1000 && qm destroy 1000 -destroy-unreferenced-disks 1 -purge 1
qm stop 1001 && qm unlock 1001 && qm destroy 1001 -destroy-unreferenced-disks 1 -purge 1

Contributing

Contributions to this project are welcome. If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.

License

License: MIT

Author Information

This role was created at 2024 by fmarcelinoPT. Feel free to customize or extend the role to fit your needs.

Resources

About

Scripts to automate template generation for Ubuntu Server and Desktop for Proxmox Virtual Environment (PVE)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages