User Tools

Site Tools


Sidebar

linux:lxc:lxc-to-lxd

LXC to LXD Migration


Warning: LXC & LXD work in a different way! Until today I was not able to successfully migrate a LXC-Container to LXD!



Changes

  • LXC-LVs contain just the rootfs, LXD contains also templates and metadata
  • LXD Containers run in unprivileged-mode (default) with uid/gid 100000:100000

Doing

Creating a new LXD Container

Mounting LXD-Container FS

Copy all Data

Changing file-owners

Here is a little script I wrote to change all uids & gids: You can simply run it on the mounted rootfs of the new lxd container.

#!/bin/bash
####################################################
# This Skript changed all uids & gids in the path of 
# '$1' to BASE_UID + uid & BASE_GID + gid
#
# License: GPLv3 or higher
####################################################

###
# You might need to change these:
BASE_UID=100000
BASE_GID=100000
###

# Params & Usage
DIR=$1
if [ -z "$DIR" ] ; then
    echo "Usage: $0 <Directory>"
    exit 1
fi

IFS=$'\n'
echo "Starting chown on $DIR... this might take a while..."
for line in $(find $DIR -exec stat -c '%n:%u:%g' {} \;) ; do
    # Split line into variables
    file=$(echo $line | cut -f1 -d:)
    old_owner=$(echo $line | cut -f2 -d:)
    old_group=$(echo $line | cut -f3 -d:)

    # Skip if something went wrong
    [ -z "$file" -o -z "$old_owner" -o -z "$old_group" ] && continue

    # Calculate new uids/gids
    new_owner=$old_owner
    new_group=$old_group
    if [ $old_owner -lt $BASE_UID ] ; then
        new_owner=$(($BASE_UID + $old_owner))
    fi

    if [ $old_group -lt $BASE_GID ] ; then
        new_group=$(($BASE_GID + $old_group))
    fi

    # Set new owner/group if it has to be changed
    if [ $new_owner -ne $old_owner -o $new_group -ne $old_group ] ; then
        echo "Changing: $file from $old_owner:$old_group to $new_owner:$new_group"
        chown $new_owner:$new_group "$file"
    fi
done

echo "DONE!"
exit 0
linux/lxc/lxc-to-lxd.txt · Last modified: 2016/04/07 11:33 by Nold