Skip to content

Instantly share code, notes, and snippets.

@marshyon
Last active November 2, 2023 23:46
Show Gist options
  • Save marshyon/c0e65d4980fb8a7e6843a9a7cef89939 to your computer and use it in GitHub Desktop.
Save marshyon/c0e65d4980fb8a7e6843a9a7cef89939 to your computer and use it in GitHub Desktop.
proxmox bash script to tear down 2 vms and recreate from a clone

Description

A bash script that will tear down and recreate 2 virtual machines from a clone of another VM on a proxmox server.

this script will DESTROY proxmox hosts

be certain to edit the variables master and worker to match the IDs of VMs in proxmox that you wish to replace for example:

master=100
worker=101

Prerequisites

A host to clone from - this ideally has cloud-init installed but if using Ubuntu server edition this should already come preinstalled.

Also ensure that on the vm that is being cloned from in the case of Ubuntu that the /etc/machine-id file is emtpy before cloning to stop duplcation of IP addresses.

As this script is simple it does not consider that the target host ids may not already exist. If this is the case simply create them for the first time on a root shell session on your proxmox server with something like :

export clone="your-vm-to-create"
qm clone $clone 100 --description "k8s master" --full --name "k8s-master"
qm clone $clone 101 --description "k8s worker" --full --name "k8s-worker" 

preparing a vm for cloning

This section is if you dont already have a vm to clone from. This is based upon Ubuntu server, here 2204 LTS so mileage may vary with other distros.

download and store a recent and up to date iso image to your proxmox server. I favour ubunutu, an LTS version and server, not desktop.

create a vm from this image and boot it up. When creating the vm ensure to have enough disk, cpu and memory for typical starting workloads for example 4 cpu, 4Gig memory, 30+ Gb disk. When installing initial image make sure to have sshd installed.

get the ip address from the booted VM from its console, typically ip a can do this.

from your trusted workstation, add an ssh pub key to the user you intend to access the new vms with

ssh-copy-id <username@<ip or hostname of your running vm>

use this user and key access to shell in and within the booted VM sudo -i to get a root session and do the following things :

make a new sudo entry in /etc/sudoers.d with something like this :

echo 'bob ALL=(ALL) NOPASSWD:ALL' > /etc/sudoers.d/builduser

where bob is the user that you want to be able to run sudo commands without a password

install some nice to have tools :

sudo apt install -y vim net-tools qemu-guest-agent
qemu-ga -D >> /etc/qemu/qemu-ga.conf

add anything to default profiles for your use case

echo "EDITOR=vi" >>  /etc/bash.bashrc 

last important step

trunctate the machine-id file in /etc :

> /etc/machine-id

( this last step needs repeated whenever you reboot this vm host for cloning as it will create a new machine id on next boot )

any vm then cloned from a machine with a machine-id already in /etc/machine-id will use that machine id, not one it has created itslef - so you end up with duplicate IP addresses on your LAN. This is not what we want to happen.

shut down the vm

telinit 0

this host will remain shut down for cloning but can be rebooted to do updates / changes but the above /etc/machine-id file will need to be truncated before again shutting it down for future clones

cloning for the first time

cloning can be done from the web console but for convenience here are the commands to do this from a shell session as the root user of the proxmox server itself where the vm to be clonded has an id of 503 and the target vm id is 201 :

create the cloned vm with

qm clone 503 201 --description "k8s master" --full --name "k8s-master"

enable the qemu guest in proxmox with

qm set 201 --agent enabled=1

start the newly cloned vm

qm start 201

proxmox will have a name in its console that reflects your qm command, here k8s-master but the host itself will still be the same as the vm that it was cloned from

this is not a bug but a feature - so they say but this is down to the many and varied ways in which different Linux distrobutions choose to define their hostname. In the case of Ubuntu ( debian based ) distros and others I could care to mention this is simply a matter of editting the file /etc/hostname which can be done from our trusted workstation with

ssh -t <ip address or hostname here> "echo k8s-master | sudo tee /etc/hostname"

Next steps - kubernetes

to deploy a test kubernetes to this pair of servers then use something like this repo of ansible to do that or similar


also k3s on github has a simple to use ansible repository at 

> https://github.com/k3s-io/k3s-ansible.git

which is easy to use - follow its readme to create a list of your own hosts an inventory file and install a k3s cluster in 1 command

very quick and easy
#!/usr/bin/env bash
master=100
worker=101
clone=502
echo ""
echo "warning : this script is about to DESTROY proxmox hosts $master and $worker"
echo ""
tear_down ()
{
echo "running tear down..."
qm stop $master && \
qm stop $worker && \
qm destroy $master && \
qm destroy $worker && \
qm clone $clone $master --description "k8s master" --full --name "k8s-master" && \
qm clone $clone $worker --description "k8s worker" --full --name "k8s-worker" && \
qm start $master && \
qm start $worker
}
quit ()
{
echo "exiting ..."
exit
}
while true; do
read -p "Do you wish to continue? [y|n] > " yn
case $yn in
[Yy]* ) tear_down; break;;
[Nn]* ) quit;;
* ) echo "Please answer yes or no.";;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment