DietPi LXC containers in Proxmox

This blog post describes how to generate your own DietPi LXC container template in the Proxmox VE. Based on the template you can then instantiate LXC containers.

Proxmox GUI with container list
DietPi LXC containers (151, 153, 155, 156) and the DietPi LXC template (990)

1. Overview

The generation is done in the following steps:

  1. Generation of a DietPi VM
  2. Conversion of the VM to a LXC container using the script described below
  3. Conversion of the LXC container to an LXC container template
  4. Have fun

The basis for your LXC container is a Proxmox DietPi VM which you want to convert to a Proxmox LXC container template. Find it with install instructions at our download page: https://dietpi.com/#download

2. Conversion script

The conversion script is based on Sascha Basts GitHub page. I did some minor changes to this script.

Important: The script runs in the Proxmox VE console, so you need to login as root onto your Proxmox machine, edit the script and run it on your machine (with root rights).

Generate a script convert-vm-to-lxc.sh with an editor and set it to executable via
chmod + x convert-vm-to-lxc.sh.

#!/bin/bash
usage()
{
    cat <<EOF
$1 
 -h|--help
 -n|--name [target lxc container name]
 -t|--target [source virtual machine ssh uri (IP or hostname)]
 -i|--id [target lxc container proxmox container id]
 -s|--root-size [target lxc container rootfs size in GB]
 -m|--memory [target lxc container memory in MB]
 -c|--cores [target lxc container cpu cores]
 -p|--password [root password for target lxc container (min. 5 chars)]
EOF
    return 0
}

options=$(getopt -o n:t:i:s:m:c:p:f -l help,name:,target:,id:,root-size:,memory:,cores:,password:,foo: -- "$@")
if [ $? -ne 0 ]; then
        usage $(basename $0)
    exit 1
fi
eval set -- "$options"

while true
do
    case "$1" in
        -h|--help)      usage $0 && exit 0;;
        -n|--name)      name=$2; shift 2;;
        -t|--target)    target=$2; shift 2;;
        -i|--id)        id=$2; shift 2;;
        -s|--root-size) rootsize=$2; shift 2;;
        -m|--memory)    memory=$2; shift 2;;
        -c|--cores)     cores=$2; shift 2;;
        -p|--password)  password=$2; shift 2;;
        --)             shift 2; break ;;
        *)              break ;;
    esac
done

collectFS() {
    tar -czvvf - -C / \
        --exclude="sys" \
        --exclude="dev" \
        --exclude="run" \
        --exclude="proc" \
        --exclude="*.log" \
        --exclude="*.log*" \
        --exclude="*.gz" \
        --exclude="*.sql" \
        --exclude="swap.img" \
        .
}

ssh "root@$target" "$(typeset -f collectFS); collectFS" \
    > "/tmp/$name.tar.gz"

pct create $id "/tmp/$name.tar.gz" \
  --description LXC \
  --hostname $name \
  --features nesting=1 \
  --memory $memory \
  --cores $cores \
  --net0 name=eth0,ip=dhcp,ip6=auto,bridge=vmbr0,firewall=1 \
  --rootfs $rootsize --storage local-lvm --password $password

rm -rf "/tmp/$name.tar.gz"

Remarks

  • The entry ip6=auto is comparable to this Proxmox setting:
    grafik-1
  • If you run the script, care for enough disk space on your Proxmox VE, because the script generates a temporary file which can be large:
    • The script first generates a /tmp/<target lxc container name>.tar.gz
    • It then uses the pct create command to generate an LXC container
    • At lease the file /tmp/<target lxc container name>.tar.gz is deleted

3. Conversion

3.1 Step 1: Convert VM to LXC container

Execute the script like this:

./convert-vm-to-lxc.sh -n lxc-test -t lxc-conv-test.fritz.box -i 153 -s 4 -c 1 -m 2048 -p dietpi

The script example generates an LXC container called lxc-test from the base DietPi VM lxc-conv-test with the container ID 153 (see screenshot at the top) with a 4 GiB root disk size, one CPU core, 2 GiB memory and dietpi as the root password of the new container.

In case of script problems, first find out the script options via ./convert-vm-to-lxc.sh --help and check whether your command line options are correct:

./convert-vm-to-lxc.sh
 -h|--help
 -n|--name [target lxc container name]
 -t|--target [source virtual machine ssh uri (IP or hostname)]
 -i|--id [target lxc container proxmox container id]
 -s|--root-size [target lxc container rootfs size in GB]
 -m|--memory [target lxc container memory in MB]
 -c|--cores [target lxc container cpu cores]
 -p|--password [root password for target lxc container (min. 5 chars)]

To let DietPi know that it is a container and no VM anymore, the following change should be done:

  1. Change the value in /etc/.dietpi_hw_model_identifier from 20 to 75.
  2. If you are doing this within the running container, either reboot, or run the following two commands:
/boot/dietpi/func/dietpi-obtain_hw_model
source /boot/dietpi/.hw_model

3.2 Step 2: Convert LXC container to LXC container template

Finally, you might convert your new LXC container to a template for later usage:

Remark

If you are faced with the problem that an LXC container generated shows dietpi-cloudshell messages on your console, you can get rid of it by executing the following command before you convert it to an LXC container template:

systemctl disable --now dietpi-cloudshell systemd-networkd systemd-resolved

Proxmox seems to enable some services as part of the necessary VLAN network setup, but also the completely unrelated dietpi-cloudshell service.

References

DietPi LXC containers in Proxmox

11 thoughts on “DietPi LXC containers in Proxmox

  1. I get tis:
    root@GateKeeper:~/DietPi/proxmox-dietpi-installer# ./convertStS.sh -n lxc-test -t lxc-conv-test.fritz.box -i 153 -s 4 -m 2048 -p dietpi
    ssh: Could not resolve hostname lxc-conv-test.fritz.box: Name or service not known
    400 too many arguments
    pct create [OPTIONS]

    1. Which VM did you want to convert?
      See above in the script about the command line option -t.

    2. Could you also add a -c 1 or -c 2 (or whatever cores you want to use) to your script call?

  2. Hi, after executing script i’ve got the following error
    400 too many arguments
    pct create [OPTIONS]

    i use same argument in your example

    Thanks

    1. Which Proxmox VE release do you use?
      Can you post the command line you used (possibly some misentered arguments)?

      1. ./jmdietpi.sh -n lxc-dietpilxc -t 192.168.8.84 -i 131 -s 4 -m 2048 -p dietpi

        proxmox version 7.3.6

        1. So you do not have any present VM or LXC container with ID 131, you have a DietPi VM with IP 192.168.8.84 where you can login via ssh (ssh root@192.168.8.84) and you do not have any present VM or LXC container with the name lxc-dietpilxc?

        2. i add -c 2 and it works, ive just used your example and -c isnt in it.
          regards

  3. In the Container i’ve got the Message “A reboot is required to finalise a recent kernel upgrade” but Rebooting the LXC doesnt change anything? What can i do?

    1. Ah, the guide misses the steps to purge kernel, initramfs and bootloader, which are all not needed within a container, or course. But the check should be actually skipped in containers. Did you apply the last step to change the hardware ID? And could you show the output of:

      grep -zaq '^container=' /proc/1/environ

      To purge unneeded packages:

      apt autopurge linux-image-amd64 tiny-initramfs grub-pc

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top