Thursday, August 13, 2015

Migrating OS across hardware (including VM to bare metal and opposite).

In the OS which is to be migrated/replicated if you've not tampered with the udev configuration or have not done something which forces some modules to be loaded in the kernel which causes problems with the new hardware, you can migrate with ease.

First you need to take backup of the OS which you need to migrate. This is best done offline, however it is possible if the system is up and files/directories etc... belonging to the OS are NOT being upgraded. Reason being the files which are read to start the OS successfully will generally not be updated while the OS is operational; dynamically modified files are re-read anyway because changes are common there (like in /var).

When backing up a live system, you have to exclude  psudo (commonly /dev, /sys/, /proc and maybe run /run too (tmpfs)) filesystems. WARNING -- the /dev directory has device nodes which are in the rootFS and are used when devfs/udev is not mounted on early boot. You NEED these to boot the system especially if it does not have an initramfs (commonly Gentoo setups).  One tip is to mount --bind the RootFS; the mount is not recursive (not rbind) so you get the core rootFS exposed (which includes the contents of the /dev directory). Backup this mount --bind ed FS.

For an offline backup, I recommend systemrescuecd.

Backup can be done using squashfs or tar (use switches which preserve xattr/selinux (security.selinux namespace), permissions. With tar DO NOT use --attrs. It has bugs which I have to report (another pending bug report :( ).

Make partitions and FS to migrate the OS.

Mount the FS to the directory tree where you'll extract the backed up OS and extract it (note: tar swithces).

Fix fstab entries to reflect the changes in your new setup.

If the bootloader is not managed by another OS on the same system, then you've to update/install it too. First step is regenerating the grub.cfg (using grub2-mkconfig) or manually modify menu.lst. Then finally install the bootloader (grub2-install); you may have to repeat these steps a few time otherwise you may get a crippled boot menu.

Whenever I say 'rootfs' I mean all the FS across which OS files exist. Thus if you have a separate /usr, /boot etc... partitions

How is this supposed to work? Question is why will it not work. Udev do not store persistent information about the hardware -- it detects it every time you boot your Linux OS. So new hardware doesn't matter.

Of course if the target machine's hardware is not support or has driver issues, then the usual problems imply as with a fresh install.

This is the official installation technique of Gentoo and my preferred technique. I first install a template OS in a VM, customize it to my needs and then migrate it to the bare metal or other VMs.