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:
Persisting program and personal data when migrating to another machine.
Backing up an entire disk or partition to clone it on any other machine.
Repairing boot possibly broken after migration.
Rufus (if you are on Windows) or unetbootin (if you are on Linux) for creating bootable USB drive.
This is needed if you want to clone only a single partition and not the
A primary USB storage device for booting Clonezilla and boot-repair-disk on the target machine.
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.
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
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.
Start the virtual machine and on the boot screen press “Escape“ to open the boot menu and boot to CD/DVD.
After booting to Clonezilla, select your preferred language and then choose “Start Clonezilla“.
Create Backup Image
Once in Clonezilla, choose “device-image“ to work with disks or partitions using images.
On the next screen, choose “local_dev“.
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.
On the next screen, select the device you want to save the backup in.
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.
Now choose the directory you want to save the backup in and select “Done“. After that press Enter to continue to the next step.
On the next screen, choose “Beginner“ to create the backup with a simple beginner level interface.
On the next screen you can either choose “savedisk“ or “saveparts“.
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.
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.
After choosing the save option, specify the name for the backup image and choose “Ok“.
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).
Choose either “zip“ or “z9p“ for the compression option dependending on your preference.
Choose “sfsck“ to skip checking source file system.
On the next step, choose “Yes, check the saved image“ to make sure that the saved image is correct and can be restored.
Now choose either “senc“ (not encrypt image) or “enc“ (encrypt image) based on your preference.
Choose the action you want to perform when the backup is finished.
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.
Boot To Clonezilla On The Target Machine
Using Rufus or unetbootin, make the primary USB drive bootable for Clonezilla with the downloaded Clonezilla iso.
Connect the primary USB device with bootable Clonezilla and boot to Clonezilla on the target machine.
Restore Backup Image
Follow the same steps as backup upto step 8.
Once in Clonezilla, choose “device-image“.
On the next step, choose “local_dev“.
Connect the secondary USB storage device from which you want to restore the backup.
On the next screen, select the device you want to restore the backup from.
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.
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.
On the next screen, choose “Beginner“.
On this step, you will see the same backup and restore options as in the backup process.
Choose “restoredisk“ if you chose “savedisk“ in the backup process.
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.
Now you will be prompted to select the image you want to restore. Select the image we created in the backup process.
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.
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.
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.
Now choose the action to perform when the restore has finished. Start the restore by following the commandline instructions.
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.
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.
Using Rufus or unetbootin, make the primary USB drive bootable for boot-repair-disk with the downloaded boot-repair-disk iso.
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.
After booting to boot-repair-disk, connect the device to internet before doing anything else.
After that, simply choose “Recommended repair“, follow the on-screen steps and let boot-repair-disk do its magic.
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.
Who writes here?
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.