Key Visual It blog 4

    Migrating Linux from a Virtual Machine to a Physical Machine

    Imagine, you borrowed a phone from someone for a month. You set up all your contacts and everything you need in the phone. Now you buy a new phone and need to return this phone, which you already have spent time to personalize. What this guide does is, it helps migrating all the data and contacts to the new phone without having to set up everything from scratch. You could just move the entire system of the previous phone to the new phone and that would save you a lot of time. The context of this blog is based on developer tools and programs rather than contacts and data, which are much simpler things.

    In this article, you will read about:

    1. Persisting program and personal data when migrating to another machine.
    2. Backing up an entire disk or partition to clone it on any other machine.
    3. Repairing boot possibly broken after migration.

    Background

    When I joined Mercateo as a remote developer, I didn’t get the office laptop in time and for certain reasons the delivery to my home in Islamabad was delayed to a month. In the meantime, I set up a virtual machine with Ubuntu on my personal device so I could onboard and start the work. When the device arrived after the prolonged wait, I had a lot of things set up on the virtual machine which I would have needed to set up again on the new physical device and that would have taken multiple days. That’s where the idea of migrating the virtual machine to the physical machine came. This blog explains the process I used to migrate my fully set up virtual machine to my new office laptop.

    Requirements

    1. Rufus (if you are on Windows) or unetbootin (if you are on Linux) for creating bootable USB drive.
    2. Clonezilla
      • Handles the partition sizes automatically — super cool.
    3. boot-repair-disk
      • This is needed if you want to clone only a single partition and not the entire disk.
    4. A primary USB storage device for booting Clonezilla and boot-repair-disk on the target machine.
    5. A secondary USB storage device (a stick or an external hard drive) to store backup of the virtual machine. The storage size depends on the size of your virtual machine.

    Note: We will be using VMWare Workstation for backup of the virtual machine.

    Migration

    Create Backup

    The backup image will be created from the virtual machine. Nothing has to be done on the target machine for this.

    Boot To Clonezilla On The Virtual Machine

    1. Setup Clonezilla for boot. Go to the settings of your virtual machine. Under “Hardware > CD/DVD (SATA)“, enable “Connect at power on“, choose the “Connection“ as “Use ISO image file“ and specify the path to the Clonezilla iso file.

    Fawad Virtual Machine Settings

    1. Start the virtual machine and on the boot screen press “Escape“ to open the boot menu and boot to CD/DVD.
    2. After booting to Clonezilla, select your preferred language and then choose “Start Clonezilla“.

    Fawad_Bild 2

    Create Backup Image

    1. Once in Clonezilla, choose “device-image“ to work with disks or partitions using images.

    Fawad_Bild 3

    1. On the next screen, choose “local_dev“.
    2. Connect the secondary USB storage device on which you want to save the backup. Note that the size of this device has to be large so the entire virtual machine setup can be cloned and saved. After you have connected the device and can see it on the screen, press Ctrl-C to move to the next step.

    Fawad_Bild 4

    1. On the next screen, select the device you want to save the backup in.

    Fawad_Bild 5

    1. Now choose “no-fsck“ to skip any file system check before creating backup. You can also choose “fsck“ if you want to check and repair the file system.

    Fawad_Bild 6

    1. Now choose the directory you want to save the backup in and select “Done“. After that press Enter to continue to the next step.
    2. On the next screen, choose “Beginner“ to create the backup with a simple beginner level interface.

    Fawad_Bild 7

    1. On the next screen you can either choose “savedisk“ or “saveparts“.
    2. savedisk: The entire disk including all partitions on the virtual machine will be cloned. The downside to this is that any data on the target machine (where you will restore the backup) will be lost because the disk will be formatted including all partitions. However, the process will be smoother.

    3. saveparts: A single partition on the virtual machine will be cloned. Since we are migrating the operating system, this will be the partition containing the OS. The downside to this is that it will require repairing the boot of target machine after restore using boot-repair-disk. I will choose “saveparts” since I only want to restore the partition and keep other data on the target disk.

    Fawad_Bild 8

    1. After choosing the save option, specify the name for the backup image and choose “Ok“.
    2. If you chose the “saveparts“ option, you will need to specify the partition in the next step. Choose the partition you want to clone (this should be the partition containing the OS).

    Fawad_Bild 9

    1. Choose either “zip“ or “z9p“ for the compression option dependending on your preference.
    2. Choose “sfsck“ to skip checking source file system.
    3. On the next step, choose “Yes, check the saved image“ to make sure that the saved image is correct and can be restored.

    Fawad_Bild 10

    1. Now choose either “senc“ (not encrypt image) or “enc“ (encrypt image) based on your preference.
    2. Choose the action you want to perform when the backup is finished.
    3. Now follow the commandline instructions to create the backup. The image creation process will now start. Note that the process will take some time depending on the size of the disk/partition being cloned.

    Restore Backup

    Boot To Clonezilla On The Target Machine

    1. Using Rufus or unetbootin, make the primary USB drive bootable for Clonezilla with the downloaded Clonezilla iso.

    2. Connect the primary USB device with bootable Clonezilla and boot to Clonezilla on the target machine.

    Restore Backup Image

    1. Follow the same steps as backup upto step 8.

      1. Once in Clonezilla, choose “device-image“.
      2. On the next step, choose “local_dev“.
      3. Connect the secondary USB storage device from which you want to restore the backup.
      4. On the next screen, select the device you want to restore the backup from.
      5. Now choose “no-fsck“ to skip any file system check before restoring backup. You can also choose “fsck“ if you want to check and repair the file system.
      6. Now choose the directory you have the backup in. This should be the directory containing folder with the name you specified for the backup. Clonezilla will recognize that the directory is a Clonezilla image. Choose “Done“ to move to the next step.
      7. On the next screen, choose “Beginner“.
    2. On this step, you will see the same backup and restore options as in the backup process.

    3. Choose “restoredisk“ if you chose “savedisk“ in the backup process.
    4. Choose “restoreparts“ if you chose “saveparts“ in the backup process. Note that we will need to repair the partition after restore using boot-repair-disk.
    5. Now you will be prompted to select the image you want to restore. Select the image we created in the backup process.
    6. On the next step, choose the partition of the virtual machine you want to restore. This is the same OS partition we made the backup of. If you chose the “restoredisk“ option, you might see different options. Just follow the on screen instructions.

    Fawad_Bild 11

    1. After that, you will need to choose the target partition that needs to be overwritten with the backup of the partition you created. Note that it has to have the size equal to or greater than the backup partition. If not, use some partition tool to increase the target partition size.

    Fawad_Bild 12

    1. On the next step, if you want to check the image restorability before restoring, choose “Yes, check image before restoring“ but this should be unnecessary because we chose to check the backup image when making the backup.

    Fawad_Bild 13

    1. Now choose the action to perform when the restore has finished. Start the restore by following the commandline instructions.
    2. Note that the restore could fail because the secondary USB device containing the backup might get disconnected. If that happens, just let Clonezilla revert the changes and try again until the restore succeeds. I had to try 5-6 times because my exernal hard drive kept getting disconnected. If an error occurs, make sure to check the error messages to identify if there is some other problem. The error messages are verbose enough.
    3. If you chose the “restoredisk“ option, it should be safe to restart the system and the system should be able to boot to the OS.

    Fixing Boot After Restoring Partition Using boot-repair-disk

    If you chose “saveparts“ for backup and “restoreparts“ for restore and restarted the system, you will get boot errors and won’t be able to boot to the system. Worry not, this can be fixed. Don’t try anything else and just use boot-repair-disk.

    1. Using Rufus or unetbootin, make the primary USB drive bootable for boot-repair-disk with the downloaded boot-repair-disk iso.
    2. Connect the primary USB device with bootable boot-repair-disk and boot to boot-repair-disk on the target machine. boot-repair-disk has a user interface so it’s easy to follow.
    3. After booting to boot-repair-disk, connect the device to internet before doing anything else.
    4. After that, simply choose “Recommended repair“, follow the on-screen steps and let boot-repair-disk do its magic.

    Fawad_Bild 14

    1. After the repair is complete, you should be able to boot into the restored partition.

    Hola! The migration is complete. Hope this guide helped you.

    Wer schreibt hier?

    Fawad Ali

    I work as a remote frontend developer at Mercateo. I am involved in all development aspects of highly scalable frontend projects with state-of-the-art infrastructure. My passion lies in building modern user interfaces for end-user applications keeping everything extensible and reusable. The coolest thing about working at Mercateo is the ability of teams and thus developers to be autonomous in what they do. We have the autonomy to choose which technology fits best for the implementation of business functions, so we work with the most interesting and advanced technologies.

    Fawad Ali