Converting Proxmox VMs to Containers Easily!
If you've got a Proxmox Server, you'll know how awesome it is to run everything as (LXC) Containers, they're light & resource-efficient, easier to manage and can be templated. But often, you start off with a VM, set it up the way you want and have no way of easily converting. Now you can, quite easily!
Introducing proxmox-vm-to-ct
With Proxmox VM to CT you can easily convert your existing VMs to a Container by passing in some basic options. Plus it has some extra lean DietPi optimisations for an even more resource efficient DietPi container!
As it’s a self-contained bash script, you can grab it easily from the github repo:
wget https://raw.githubusercontent.com/thushan/proxmox-vm-to-ct/main/proxmox-vm-to-ct.sh
chmod +x ./proxmox-vm-to-ct.sh
We just finished migrating just over 200 VMs to containers and templating a few for future musings.
For instance, suppose you have a VM called the-matrix
that you want to convert to a container named matrix-reloaded
and your Proxmox storage is called local-zfs
, you can simply call the script like so:
./proxmox-vm-to-ct.sh --source the-matrix \
--target matrix-reloaded \
--storage local-zfs \
--default-config
You can even convert VM’s that you’ve installed Docker, Podman or containerd installations, you can use the --default-config-containerd
switch:
./proxmox-vm-to-ct.sh --source the-matrix \
--target matrix-reloaded \
--storage local-zfs \
--default-config-containerd
If you want to keep the source output from the script for future containers, use the --source-output
switch:
./proxmox-vm-to-ct.sh --source the-matrix \
--target matrix-reloaded \
--storage local-zfs \
--default-config-containerd \
--source-output ~/my-first-vm.tar.gz
Then you can reuse that same template to create more containers:
./proxmox-vm-to-ct.sh --source ~/my-first-vm.tar.gz \
--target matrix-revolutions \
--storage local-zfs \
--default-config-containerd
The --source
switch supports two different types of inputs:
- SSH: IP or Hostname (Eg.
192.168.0.101
orthematrix.fritz.box
) - Archive: GZip root file system (Eg.
*.tar.gz
)
Which renders out something similar to this - on our test rig named gandalf
:
Securely set passwords
By default, the script will auto-generate a password for you and set it on the container, but you can opt to have it prompt you for a password with the --prompt-password
switch.
It will whip open a whiptail
window asking you for a password!
Default Configurations
The default configuration for the script is quite basic, but you can opt to create your own machine/cluster specific configurations inside a (sort of an) env file.
Following the example default.config, create a file with only the settings you want to change from the default:
# loggins-cpu.config
CT_CPU=8
CT_RAM=10240
CT_HDD=500
Then load the configuration overiding the default options:
./proxmox-vm-to-ct.sh --source ~/my-first-vm.tar.gz \
--target kenny-loggins \
--storage local-zfs \
--default-config \
--target-config loggins-cpu.config
DietPi optimisations
If your VM is based on DietPi (6.x to 9.x), you can have the script optimise it for Containers by default which is documented here.
- Sets the
.dietpi_hw_model_identifier
from 21 (x86_64) to 75 (container) as per documentation - Sets up first-login install sequence (even if you’ve done it already) so each container gets updates and updating of passwords instead of any randomly generated ones from the script by modifying
/boot/dietpi/.installstage
. - Stops DietPi-CloudShell which is CloudHell when you reboot as a container in Proxmox otherwise.
- Adds the purging of
grub-pc tiny-initramfs linux-image-amd64
packages which aren’t required as a container - see Michalng’s comment.
They can be disabled with --ignore-dietpi
and won’t be done on non-dietpi VMs (like Debian etc).
Acknowledgements
This script uses the handiwork by @my5t3ry/machine-to-proxmox-lxc-ct-converter and in particular the change by blauskaerm that tar’s up the filesystem.