GNU/Linux Distro Transplantation
LINUX
GNU/Linux Distro Transplantation
2017-05-03
By
David "DeMO" Martínez Oliveira

Not so long ago I had to configure a machine following some strict rules. It was pretty painful and, when I was done, I was asked to provide an image of the configured machine to some remote team to do some checks on it. The machine was installed in a 1 TB disk, so making and image and send it around will require quite some time (either to transfer the file or to wait for the post to deliver some physical storage device).
So I had to transplant that installation into a virtual machine, at the same time that I had to shrink it to try to fit the whole thing in a file as small as possible. Maybe just compressing the whole disk image will produce a similar result but, at that time, I didn't have enough disk space to extract the image, an I also had to deploy it on a HD which was different to the original... So I had to play GNU/Linux Distro Surgery.

Getting the data

So, the first step was to fetch all the data from the original machine. That was pretty easy. I just booted the machine with a Live distro (the original installation CD did the trick) and just create a tar file by partition.

This way I ensured there will not be any temporal file in the tar and also all the temporal filesystems will just be empty (i.e. /proc). I just dumped the data in a external USB device... It took a few Gb and it was definitely faster than DDing and compressing the whole disk.

I ended up with a file for the root file system, another for the home, and another for var... yes each one was in a separate partition.

First Transplantation

So, the first transplantation was into a virtual machine. I fired up VirtualBox and configured a pretty minimal machine with a hard-drive a bit bigger than the amount of data I extracted from the original installation.

Here, I went the easy way... just installed the base system using the system installation (yes it was an Ubuntu installation). I think this is not really needed but I didn't try to skip this step so I won't make assumptions here... maybe I update this post whenever I have a chance to test this.

Any way, I create the same number of partitions (but obviously smaller) and did a default installation. When it was done, I rebooted the machine using, again, the installation disk but this time I went into the live CD instead of running the installation program.

Once there I just had to uncompress the tar files I got from the original computer into the virtual machine hard drive. Each one on its partition.

Fixing the Booting

Now that all the files are in the right place we just need to fix the boot. This requires two simple steps:
  • Update the /etc/fstab file.
    As we have just uncompressed the original files, the fstab file contains the block device ids of the original disk. We have to edit the file and substitute each partition id with its new value. To figure out the new ids use the blkid (run it with sudo).

  • Re install grub.

Let's see in detail how to reinstall grub.

Reinstalling grub

In order to re-install grub we have to... let's say.. cold boot our new filesystem... yes that think called chroot. So we just follow the standard process... mount the temporal file system required to run the system and just chroot into the disk.

# mount -o bind /proc /path_to_new_root/proc
# mount -o bind /dev /path_to_new_root/dev
# mount -o bind /sys /path_to_new_root/sys
# chroot /path_to_new_root/proc
  

That's it. You may want to also bind /dev/pty but I didn't really need it. Once you are in the chroot environment you can just install grub normally.

# grub-install /dev/sda
# update-grub

For the virtual machine /dev/sda was the right disk. If you are using a different configuration you may need to use a different device.

With this last step I could just reboot the virtual machine and a replica of my original installation popped-up.

The second Transplantation

Eventually the original machine was delivery and, at some point I had to re-configure a new one... so it was surgery time again. The process I followed was exactly the same, but on a real machine instead of on a virtual machine. So, I do not really know what I have even added a section for saying this.

Oh yes!. I remember. For some reason my partition table got corrupted. I have no idea why. My guess is that I made some stupid mistake that I haven't even noticed. Anyway, the point is that, my first attempt to boot the machine failed with a scary The Partition Table is Corrupt.

There are many pages with instructions on the Internet explaining how to fix the problem and restore the partition table. In my case the situation was a bit strange. I could list the partitions in the disk using fdisk. Everything looked fine, but the computer couldn't boot directly from the hard drive. So, in case it may be useful for you, this is what I did:

  • I booted again into a Live-CD session
  • Launched cfdisk against the disk
    # cfdisk /dev/sda
      

  • The partitions were all there so I just chose the Write option in the menu and write the partition table back to the disk
  • Reboot and everything was fine

Skipping The Installation

As a final note, I was running a complete installation of the system just to override all the files a few minutes later. Installing a base distro nowadays does not take too long but it doesn't seem to be the most efficient way to proceed.

In theory, and I have to say that I haven't tested this but there is no reason why it shouldn't work, we can just create the partitions using fdisk or cfdisk from the Live session and immediately proceed with the copying of files and re-installation of grub.

So, this is it. Nothing really special but it may be useful for some other people and also for me to refresh things in the future!

Header Image Credits: Jon Butterworth