Tuesday, October 12, 2021

How to clone Ubuntu OS partition?

I'm using Macbook Pro 13-inch Late 2011 running Ubuntu 20.04. I used the built-in application called Disks to backup the Ubuntu system partition into a disk image (*.img file) and then restore the disk image back to the partition.

IMPORTANT 

  • When backing up a system partition, the EFI system partition should also be backed up if it exists (using either Disks application or cp command)
  • The backed up or source partition must be smaller than or equal to the destination partition
  • The size of the disk image (*.img file) equals the size of the (backed up) source partition even if the partition has free space. The source partition should be resized as small as possible so that the resulting disk image is acceptably small.


To Backup

Backing up Ubuntu partition:

  • Boot into Ubuntu Live (on USB) and chose Try Ubuntu
  • Open Disks application from the menu (press Windows key and type Disks)
  • Select the partition to backup 

  • (Optional) Click on the settings icon for more options and select Resize
  • Click on the settings icon for more options then select Create Partition Image...

  • Make sure the file name and location are correct. The partition for storing the disk image must be mounted or it'll fail with a message like there is not enough space or something else confusing.

Backing up EFI system partition:

The EFI system partition is where the Ubuntu bootloader locates in because I manually chose the partition for the bootloader while installing Ubuntu (It's a must to install Ubuntu using EFI mode).

I used the method above to clone (backup) this partition, and also manually copied its content using cp command. Of course, using cp command is simpler and works just fine. The partition has only one directory named EFI.

> sudo mkdir /mnt/sda1
> sudo mount -o loop /dev/sda1 /mnt/sda1
> sudo cp -rf /mnt/sda1/EFI /media/myusb/ubuntu-efi/


To Restore

Restoring Ubuntu partition:

  • Boot into Ubuntu Live (on USB) and click Try Ubuntu
  • Open Disks application from the menu
  • Select the (destination) partition for running Ubuntu 

  • Click on the settings icon for more options and select Restore Partition Image...

  • Browse to the (source) partition that the disk image (*.img file) is stored on

  • Select the disk image file and click Start Restoring

Restoring EFI system partition:

I opened Terminal and used the cp command to copy it back from the USB drive to the EFI system partition (in my case, /dev/sda1). 

> sudo mkdir /mnt/sda1     
> sudo mount -o loop /dev/sda1 /mnt/sda1    
> sudo cp -rf /media/myusb/ubuntu-efi/EFI/ubuntu /mnt/sda1/EFI/
> sudo cp -rf /media/myusb/ubuntu-efi/EFI/BOOT /mnt/sda1/EFI/


Successfully Tested Scenarios

Scenario 1 

  1. Create a virtual machine and install Ubuntu with VirtualBox, enabling EFI mode
  2. - Partition 1: 500 MB (EFI system)
    - Partition 2: 50 GB (FAT32 - empty)
    - Partition 3: 200 GB (NTFS - empty)
    - Partition 4: 40 GB (EXT4 - Ubuntu)
  3. Reboot into Ubuntu Live through a virtual CD-ROM attached with the Ubuntu installation ISO file and choose Try Ubuntu
  4. Open Disks application from the menu and use it to backup Partition 4 (Ubuntu system partition) to a disk image (*.img file) stored on Partition 3.
  5. Restore the disk image file to Partition 2
  6. Format and erase Partition 4
  7. Optional: backup Partition 1 to a disk image stored on Partition 3
  8. Close the virtual machine and start it again

Scenario 2

  1. Boot into Recovery mode on MacBook Pro Late 2011
  2. Create the following partitions:
  3. - Partition 1: 70 GB (Mac OS Extended)
    - Partition 2: 100 GB (exFAT)
    - Partition 3: 90 GB (FAT 32)
    - Partition 4: 200 GB (exFAT)
  4. Install macOS High Sierra on Partition 1 
  5. Install Ubuntu 20.04 on Partition 3 (formatted as ext4)
  6. Boot into Ubuntu Live USB and choose Try Ubuntu
  7. Open Disks application from the menu
  8. Format Partition 4 as NTFS
  9. Optional: resize Partition 3 to 20 GB (leaving 70 GB of free space)
  10. Backup Partition 3 (Ubuntu) as a disk image (*.img file) and store it on Partition 4 
  11. Format and erase Partition 3 (this can also be done in macOS)
  12. Restore the disk image from Partition 4 to Partition 3
  13. Reboot the machine


No comments:

Post a Comment