Scripts and utilities for provisioning
  • Shell 84.3%
  • Python 15.7%
Find a file
2026-04-17 14:10:32 -07:00
kvm update etc-profile.d-libvirt-mgmt.sh 2026-03-19 17:07:29 -07:00
mmdebstrap update mmds-d13-bridge-dhcp; update mmds-d13-bridge-static; update mmds-d13-bridge-static-extra; update README 2026-04-17 14:08:37 -07:00
README.md update README 2026-04-17 14:10:32 -07:00

Provisioning scripts

Scripts used for provisioning

mmdebstrap

Scripts to create systemd-nspawn containers using mmdebstrap

Recommended to use the current mmdebstrap systemd-container handles name resolution for contianers via libnss-mymachines

Please consult the nss-mymachines(8) man page for correct configuration info.

mmds-d13-bridge-dhcp

  • Create a Debian systemd-nspawn container with its network interface attached to a bridge interface on the host.
  • IPv4 address is configured via DHCP.

Usage

sudo ./mmds-d13-bridge-dhcp lab2-host0

mmds-d13-bridge-static

  • Create a Debian systemd-nspawn container with its network interface attached to a specified bridge interface on the host.
  • IPv4 address is manually configured.

Usage

sudo ./mmds-d13-bridge-static lab2-host1 br1 192.168.2.45/24

mmds-d13-bridge-static-extra

  • Create a Debian systemd-nspawn container with the container's primary host0 interface attached to a bridge interface on the host.
  • Create an additional virtual ethernet interface connected to a second bridge interface on the host.

It takes the following arguments:

  1. Container name
  2. Host's bridge to connect the primary host0 interface to, this would usually be on the outward-facing network
  3. Host's brige to connect the additional virtual ethernet interface to. this could be a private network.
  4. Sequence number to use when generating the additional virtual ethernet interface. Each container must use a unique sequence number.
  5. IP address and subnet in form: 192.168.0.1/24

Note: This script assumes there exists two bridges on the host. A primary bridge that's connected to an external interface, and a second bridge that can be on a private subnet. There must be an IPv4 DHCP server running on the second bridge's network.

Containers should all be addressable via their container (host) names from both the host and from other containers.

I encountered an issue with container interfaces not registering correctly with libnss-mymachines. A workaround has been implemented in this script which first creates the container without the additional veth interface. It then starts and stops the container to register it with libnss-mymachines, then adds the veth configuration and restarts the container. It runs a ping test at the end which should be successful.

Usage

sudo ./mmds-d13-bridge-static-extra lab2-proxy1 br0 br1 1 192.168.12.41/24

KVM

etc-profile.d-virsh.sh

Useful Bash aliases (actually they are functions) for managing KVM containers.

For these aliases to be system-wide for Bash users, put them in: /etc/profile.d/virsh.sh

For a user only, they can be copied into ~/.bashrc

vlist               List machines and their status
vstart <machine>    Start <machine>
vsave <machine>     Save (suspend) <machine>
vtop <machine>      Stop (gracefully shutdown) <machine>
vkill <machine>     Kill (force shutdown) <machine>
vconsole <machine>  Open console on <machine>
vrm <machine>       Completely remove (destroy) <machine>

kvm-setup

Bash script to assist with setting up a Linux virtual machine under KVM/QEMU from an ISO image file.

Usage

Edit the variables machineName, isoImage, machineMemory, diskSize, and cpuNumber. machineMemory is the number of Gigabytes. Recommended values are:

machineMemory=2
diskSize=40
cpuNumber=2

vclone.py

Clone a KVM virtual machine (a.k.a. 'domain').

Example usage

vclone.py -template debian13-template -hostname myhost -ip 192.168.1.10 -subnet 24

kvm-setup-from-img

Bash script to set up a Linux virtual machine under KVM/QEMU from an existing qcow2 image file.

vclone

Script to clone a virtual machine. The idea is to create a base (or template) image that you want to use and then you can create as many copies of it that you want.

vclone will remove any temporary files, re-initialize the machine as new, and generate a new MAC address and SSH host keys. It will not remove any ~/.ssh/authorized_keys files that exist in user directories so you can have your SSH keys seeded into the machines.

vclone relies on the name of the base (template) machine being used in /etc/hosts, /etc/hostname, and /etc/motd. It replaces the base machine name with the new machine name.

Finally, vclone will print the IPv6 link-local address of the newly-cloned machine. This address can be used in inventory files and ssh/config stanzas like so:

Host master-node
  Hostname fe80::3172:00ff:fe86:4103
  BindInterface br1

where BindInterface is the interface on the host that is on the same segment as the virtual machine. Because we're using a link-local address the interface name must be provided to SSH.

This is a useful script to extract the IPv6 link-local address from a KVM machine using the machine's MAC address.

Put it in /usr/local/bin or $HOME/bin

Installing KVM/QEMU

For info on setting up KVM/QEMU and the necessary tools on the host machine refer to the Debian Wiki KVM page.

In addition to what is recommended there, it is recommended to also install ovmf which allows UEFI booting

Here is what I usually use:

apt install --no-install-recommends qemu-system libvirt-clients libvirt-daemon-system ovmf
apt install --no-install-recommends virtinst libguestfs-tools

Don't forget:

usermod -aG libvirt <user>

and re-login or run newgrp libvirt as the user

osinfo-db

This will make sure that the osinfo database contains the lastest release information for distributions, such as debian12 (or any later versions), which can then be set for the --os-variant virt-install switch.

This is done with:

git clone https://gitlab.com/libosinfo/osinfo-db.git
apt install gettext osinfo-db-tools libosinfo-bin
cd osinfo-db
make
# this runs osinfo-db-import to import the latest data:
sudo make install

It may also be necessary to run this to fully install the updated db system-wide, replace with the actual version that was built:

sudo osinfo-db-import --root / --system osinfo-db-<version>.tar.xz

# test it:
osinfo-query os