disk cloning

Clone existing VM disk to boot up a new Virtual Machine.

disk cloning
Disk Cloning

In this article I am demonstrating clone of the existing VM hard disk. I am using CentOS 7 to perform the hard disk cloning.

Cloning is nothing but the creating the exact copy of the disk. Advantage of disk cloning is you can boot up a new VM in a fraction of seconds and this will save lots of repetitive efforts.

Let’s say we want to create another Virtual Machine with same operating system and same applications. We can bootup a new VM by cloning the existing VM disk rather creating the VM from first step.

Now we have existing VM with some pre-installed applications and we want to clone the VM disk to boot up the new VM with same applications in a fraction of seconds.

For this example, we are using below disks.

Disk 1: /dev/xvda                                                                ← existing VM disk.

Disk 2: /dev/xvdb                                                                ← clone of /dev/xvda disk.

Steps:

1. Check the partition structure and make sure second disk is added successfully to your VM.

   # lsblk

NAME      MAJ:MIN  RM  SIZE RO    TYPE MOUNTPOINT

xvda         202:0         0       100G        0 disk

├─xvda1  202:1        0       500M       0 part /boot

├─xvda2  202:2        0       2G            0 part [SWAP]

└─xvda3  202:3        0       97.5G      0 part /

xvdb         202:16       0       100G      0 disk

Note:

In our case, /dev/xvdb is second drive which we are using to clone the root disk (xvda).


2. Create a same partition structure on /dev/xvdb

   # fdisk /dev/xvdb

  • Type “n” to create new partition. (1st partition)

Respond as “p” to select partition as a primary partition

  • Create 2nd partition
  • Change the partition id by typing “t” and type “82” for swap partition type.
  • Create third partition using same steps (1 & 2).

Note: Here you can see error because fdisk prompt only accept the input as integer value, if you provide the value in float then it will give you the error as above.

  • Verify partitions by issuing “p” at fdisk command prompt.
  • Save the changes by typing “w” in fdisk command prompt and update the kernel partition table by issuing partprobe command.
  • Check the partition schema again using lsblk and you can see the difference there.
    # lsblk

NAME       MAJ:MIN   RM   SIZE RO   TYPE  MOUNTPOINT
xvda           202:0         0      100G          0       disk
├─xvda1   202:1         0      500M         0       part /boot
├─xvda2   202:2         0      2G              0       part [SWAP]
└─xvda3   202:3         0      97.5G        0       part /
xvdb           202:0         0      100G         0       disk
├─xvdb1   202:1         0      500M        0       part
├─xvdb2   202:2         0      2G             0       part
└─xvdb3   202:3         0      97.5G        0       part

  1. Format the partition with same fs type as you have used for /dev/xvda.
  • To check fs type of a /dev/xvda run below command.  
    # df –hT
  • Format the partition using ext4 as we have formatted the /dev/xvda using ext4

# mkfs -t ext4 /dev/xvdb1
# mkfs -t ext4 /dev/xvdb3

  • To make swap partition run below command.

# mkswap /dev/xvdb2

Note:  Swap partition does not have any file system type hence we haven’t formatted it in previous step.

  1. Partition Labelling

     Syntax:
     e2label <partition_name> <filesystem_label>

# e2label /dev/xvda1 /boot
# e2label /dev/xvda3 /
  1. Create temporary mount point to write data to created partitions (/dev/xvdb1, /dev/xvdb3).

# mkdir /clone_boot                                                                                (For boot partition data)
# mkdir /clone_root                                                                                 (For root filesystem data)

6. Mount /dev/xvdb1 and /dev/xvdb3 on created mount point.

# mount /dev/xvdb1 /clone_boot
# mount /dev/xvdb3 /clone_root

  1. Copy data

# cp -ax /boot/. /clone_boot                                           (Copy from /boot partition to /clone_boot)
# cp -ax /. /clone_root                                                    (Copy from / partition to /clone_root)

8. Synchronize the data to ensure that all data has been written to new disk.

# sync

9. Install grub (bootloader) on new disk.

# grub2-install /dev/xvdb

  1.  Make below changes in fstab and device.map file.
  • Identify the UUID of the created partitions using
  • Note down the UUIDs from the output.
  • Open fstab file located at /clone_root/etc/fstab and edit the UUIDs with the UUIDs generated in previous command output. As a result of that you can see the newly edit fstab file with /dev/xvdb drives UUID.
  • Edit device mappings in /clone_boot/grub2/device.map file.

Note:  Format of device.map file is, 1st field is the HDD number or location and 2nd field is device. (In our case it is /dev/xvdb)

  1. Update grub configuration with new grub file.

# grub2-mkconfig -o /clone_boot/grub2/grub.cfg

Above command will update mappings of new root disk in grub.cfg file which will then root the VM with cloned root file system. Also it will detect the drive where centos 7 installation is resides.

  1. Once everything is done reboot the system and you will see the new CentOS entries at bootloader screen like below.

Any inputs or corrections would be appreciated.

Scroll to Top