Move/convert LXC(Linux Container) to Virtualbox/KVM/real machine

Some adjustments might be needed, but this will guide you trough the concepts of moving a linux container to a bare metal machine/hypervisor.

Backup contents of server with rsync.

lxc-stop -n name
rsync -av /opt/lxc/name/root-filesystem/* /media/usb-stick-with-linux-filesystem

Start virtualbox/real machine with ubuntu server iso, rescue mode. Open a terminal in installer environment, format and mount disk.

fdisk /dev/sdX
mkfs.ext4 /dev/sdX1
mkswap /dev/sdX2
mkdir /target
mount /dev/sdX1 /target

Copy contents of linux container to new root filesystem.

mount /dev/sdb1 /mnt
mount -o bind /target /mnt/mnt
mount -o bind /dev /mnt/dev
mount -o bind /run /mnt/run
mount -o bind /proc /mnt/proc
mount -o bind /sys /mnt/sys
chroot /mnt /bin/bash
rsync -av --exclude 'dev' --exclude 'run' --exclude 'proc' --exclude 'sys' --exclude 'mnt' / /mnt

Exit chroot of lxc container, and chroot into new partition.

cd /target
mkdir dev sys proc run mnt
mount -o bind /dev /target/dev
mount -o bind /run /target/run
mount -o bind /proc /target/proc
mount -o bind /sys /target/sys
chroot /target /bin/bash

Check fstab(normally filesystem is mounted by LXC-instance).

/dev/sda1 / ext4 defaults 0 0
/dev/sda2 none swap defaults 0 0

Install linux kernel and grub(make it bootable). Network is needed for this step, make sure resolv.conf is valid.

apt-get update
apt-get -o DPkg::Options::="--force-confmiss" install linux-image grub2

Since LXC does not use normal init/upstart, we need to make sure configfiles in /etc/init exists. This is done by reinstalling ubuntu-minimal dependencies.

sudo apt-get -o DPkg::Options::="--force-confmiss" --reinstall install adduser bzip2 console-setup dash dhcp3-client eject gnupg ifupdown initramfs-tools iproute iputils-ping kbd less locales lsb-release makedev mawk module-init-tools net-tools netbase netcat-openbsd ntpdate passwd procps python rsyslog sudo tasksel tzdata ubuntu-keyring udev upstart ureadahead vim-tiny whiptail

Make sure SSH is enabled.

update-rc.d ssh defaults

That should be it. Exit chroot, umount and reboot. Remove ubuntu-iso, cross fingers.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>