Skip to content

Instantly share code, notes, and snippets.

@florian-obradovic
Last active December 17, 2023 23:38
Show Gist options
  • Save florian-obradovic/07ddaa37a614a028661f683279c25c1e to your computer and use it in GitHub Desktop.
Save florian-obradovic/07ddaa37a614a028661f683279c25c1e to your computer and use it in GitHub Desktop.
#!/bin/bash
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# I customized the script written by @ninjabes to my needs
# https://forum.proxmox.com/threads/problems-migrating-lxc-from-ubuntu2004-to-proxmox.100815/post-435303
# This is currently the only workaround I know for the UID/GID mapping issues after importing rootfs exports from LXD / LXC
# Ubuntu 20.04 into proxmox 7.2-11
# The resulting containers have various permission issues and UIDs & GIDs aren't mapped correctly.
# Example: ls -alh
# total 44K
# drwxr-xr-x 5 1001001 1001001 11 Dec 1 14:10 .
# drwxr-xr-x 4 1000000 1000000 4 Dec 1 10:13 ..
# -rw------- 1 1001001 1001001 645 Dec 1 14:22 .bash_history
### Attention: Due to taking a backup from the running container,
### this will likely result in data inconsistency so make sure to stop any I/O serives like mysql, etc.
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
set -x
set -e
image_storage_dir="/tank/encrptd/Backups/lxc2proxmox-migration-20221017"
target_server="[email protected]"
target_path="/var/lib/vz/template/cache"
start_id=111
# create tar.gz archive
# For all LXC containers use:
container_list=`lxc list | awk -F' ' '{ print $2}' | sed '/^[[:space:]]*$/d' | awk -F'/' '{ print $1" "$2 }' | sort | uniq | egrep -v 'more|ALIAS|NAME'`
container_list=`echo $container_list | tr '\n' ' '`
# For a manual list of containers use:
#container_list='bookstack gitea invoice-ninja invoiceninja librenms nextcloud powerDNS rvproxy01'
# Make sure to set "--rootfs local-zfs:120" to fit your current container sizes
for container_name in $container_list; do
if [[ $container_name == * ]]; then
# pack tar.gz archive
set +e
lxc exec "$container_name" -- bash -c "cd / && tar --exclude=${container_name}.tar.gz --exclude='proc/*' --exclude='tmp/*' --exclude='mnt/*' --exclude='dev/*' --exclude='sys/*' --exclude='run/*' --exclude='media/*' --exclude='var/cache/apt/archives/*' -cpf - ./* | pv | pigz > ${container_name}.tar.gz"
set -e
lxc file pull "${container_name}/${container_name}.tar.gz" "${image_storage_dir}/${container_name}.tar.gz"
lxc exec "$container_name" -- bash -c "sudo rm /${container_name}.tar.gz"
# copy to remote instace
scp "${image_storage_dir}/${container_name}.tar.gz" "$target_server:$target_path"
# get mac of container
# we assume that the ip link is the interface to copy
interface_name=`lxc exec "${container_name}" -- ip route show default | awk '/default/ {print $5}'`
mac_addr=`lxc exec "${container_name}" -- cat /sys/class/net/"${interface_name}"/address`
lxc stop "$container_name"
# create container in proxmox
ssh "$target_server" "pct create ${start_id} ${target_path}/${container_name}.tar.gz -description ${container_name} -hostname ${container_name} -memory 1024 -net0 name=eth0,hwaddr=${mac_addr},ip=dhcp,ip6=dhcp,bridge=vmbr0 -storage local-zfs --rootfs local-zfs:120 -unprivileged 1 --ostype ubuntu -password "ContainerPassword""
# delete image on remote server after install
#ssh "$target_server" "rm ${target_path}/${container_name}.tar.gz"
# start container on proxmox
ssh "$target_server" "pct start ${start_id}"
# increase ID
start_id=$(( start_id+1 ))
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment