PVE Adopt a Native LXD/LXC

From WikiMLT
Revision as of 22:02, 23 September 2022 by Spas (talk | contribs)

Con­vert­ing an LXD/LXC to a ProxmoxVE/​​​LXC is pret­ty easy, be­cause the are (al­most) the same thing.

Ex­port the Con­tain­er

Go in­to the in­stance where the LXD is in­stalled and cre­ate a back­up of the LXC you want to clone. If the con­tain­er is in­stalled with­in LVM you need to mount the root filesys­tem some­where and cre­ate the archive from in­side the mount point. I will go­ing to play the oth­er sce­nario where the LXC is in­stalled with­in a di­rec­to­ry on Ubun­tu 22.04.

lxc config show --expanded webserver | grep 'privileged'
security.privileged: "false"
cd /var/snap/lxd/common/lxd/storage-pools/default/containers/webserver/rootfs
tar --exclude=dev --exclude=sys --exclude=proc --exclude=tmp/* \
-czvf /home/backups/lxc-webserver.tar.gz ./

Note in the com­mands above:

  • web­serv­er is the name of the con­tain­er.
  • lxc-webserver.tar.gz is the name of the archive file of that con­tain­er.

In this case the back­up is cre­at­ed while the con­tain­er is run­ning, oth­er­wise the –ex­clude op­tions may not be need­ed. In ad­di­tion I would say us­ing this ap­proach is much eas­i­er than us­ing a na­tive LXD/LXC back­up, where we need to ex­tract on­ly the rootfs/​​​ and archive it again.

Trans­fer the Con­tain­er to the PVE In­stance

Copy the back­up of an LXD/LXC to a ProxmoxVE's tem­plate di­rec­to­ry with­in its stor­age. In my case I'm us­ing a com­mand as the shown be­low.

rsync --progress \
remote.host:/home/backups/lxc-webserver.tar.gz \
/mnt/pve/ssd-1TB/template/cache/

Note in the com­mands above:

  • ssd-1TB is one of my PVE's stor­age de­vices A where Con­tain­er tem­plates B are al­lowed as Con­tent type.

Cre­ate CT with­in Prox­moxVE

At this point we can use the back­up file lxc-webserver.tar.gz as con­tain­er tem­plate C. And we have two po­tions: 1) to use WEB GUI or 2) to use the CLI of PVE.

Cre­ate CT via WEB GUI of PVE

It is pret­ty easy to use the GUI but it can't be used for script­ing. The spe­cif­ic steps are shown at Screen 1.

Screen 1. Create Linux container (CT) within ProxmoxVE 7.2.
Screen 1. Cre­ate Lin­ux con­tain­er (CT) with­in Prox­moxVE 7.2. Screen 1. Create Linux container (CT) within ProxmoxVE 7.2.

Cre­ate CT via CLI of PVE

Gere is how to cre­ate unpriv­i­leged con­tain­er by us­ing the com­mand pct.

pveam list ssd-1TB
NAME                                                         SIZE  
ssd-1TB:vztmpl/lxc-webserver.tar.gz                          2541.03MB
ssd-1TB:vztmpl/ubuntu-20.04-standard_20.04-1_amd64.tar.gz    204.28MB
pct create 177 ssd-1TB:vztmpl/lxc-webserver.tar.gz --password "SuperStrong:)" \
--description "LXC migrated" --hostname "webserver" --ostype "ubuntu" \
--cores 2 --memory 2048 --swap 512 --rootfs ssd-1TB:32 \
--features fuse=1,nesting=1 --unprivileged 1 \
--nameserver 172.16.17.151,8.8.8.8 \
--net0 name=eth0,hwaddr=64:6B:5C:36:48:E3,ip=172.16.17.177/24,gw=172.16.17.1,bridge=vmbr0
Formatting '/mnt/pve/ssd-1TB/images/177/vm-177-disk-0.raw', fmt=raw size=34359738368 preallocation=off
Creating filesystem with 8388608 4k blocks and 2097152 inodes
Filesystem UUID: f7f1fa30-0906-482c-9b5d-afc9be42533f
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
	4096000, 7962624
extracting archive '/mnt/pve/ssd-1TB/template/cache/lxc-webserver.tar.gz'
Total bytes read: 6269102080 (5.9GiB, 124MiB/s)
Detected container architecture: amd64...

Notes:

  • In­stead of -storage ssd-1TB we need to use -rootfs ssd-1TB:32 in or­der to cre­ate large enough vm-disk (32G in this case), oth­er­wise with the de­fault size in most cas­es you will end up with in­suf­fi­cient disk space space.
  • In­stead of ssd-1TB­:vztmpl­/­lxc­-­webserver­.­tar­.­gz, we can use /mnt­/­pve­/­ssd­-­1TB­/­tem­pla­te­/­cache­/­lxc­-webserver.tar.gz or even just lxc­-­webserver­.­tar­.­gz.
  • Al­so the op­tions could be pro­vid­ed by dou­ble -- or by sin­gle - dash.

Post In­stal­la­tion Set­up

1. De­pend­ing on the way how the orig­i­nal con­tain­er is ini­tial­ly cre­at­ed, you may need to re­move some files from /etc/update-motd.d/. Here is the con­tent of that di­rec­to­ry of a naive­ly cre­at­ed PVE CT (Lin­ux con­tain­er).

ls -1 /etc/update-motd.d/
00-header
10-help-text
50-motd-news
88-esm-announce
91-contract-ua-esm-status
91-release-upgrade
92-unattended-upgrades

2. De­pend­ing on your LAN DHCP set­up you may want man­u­al­ly set a sta­t­ic IP from the in­stance.

sudo nano /etc/netplan/50-cloud-init.yaml   # The name of your conf file culd be different
network:
    version: 2
    ethernets:
        eth0:
            addresses:
                - 172.16.17.177/24
            nameservers:
                addresses: [172.16.17.151, 1.1.1.1, 8.8.8.8, 8.8.4.4]
            routes:
                - to: default
                  via: 172.16.17.1
sudo nano /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
network: {config: disabled}
sudo netplan apply
sudo ip addr show eth0 | grep -w 'inet'
inet 172.16.17.177/24 metric 100 brd 172.16.17.255 scope global dynamic eth0

3. Fi­nal­ly you may want to re­move some pack­ages and apt repos­i­to­ries.

sudo rm /etc/apt/sources.list.d/droplet-agent.list
sudo apt remove droplet-agent droplet-agent-keyring

Ref­er­ences