Skip to content

Cloning a VirtualBox Ubuntu Server onto a KVM Slice with a smaller SSD than the VDI size

  • by

Start off by shutting down your virtual server and cloning it (complete copy) of the original container. Download GParted LiveCD and boot it inside your cloned virtualbox container. Shrink and left align the partitions in gparted. Reboot and boot into your system. Boot back into gparted. Setup networking with the icon on the desktop.

You will have 3 partitions. One is the grub partition, one is the boot partition, and one is your root directory. In the case you are using LVM, your LVM will appear in one partition. In this particular case, in VirtualBox, our partitions appear as /dev/sda#. On our KVM slice, our partitions are /dev/vda#, so tweak your commands to suit.

This command will copy partition images from /dev/sda# to a location on our SSHD. Do these commands from your VirtualBox container.

dd if=/dev/sda1 | ssh <user>@<host> dd of=<location>/sda1.img
dd if=/dev/sda2 | ssh <user>@<host> dd of=<location>/sda2.img
dd if=/dev/sda3 | ssh <user>@<host> dd of=<location>/sda3.img

I also recommend backing up the first 446 bytes of the MBR. This step may not be necessary because we’ll manually install grub later, however this step makes the later step optional. Tweak to your liking, because your KVM Slice may not want to boot off the main drive.

dd if=/dev/sda of=/tmp/mbr.bak bs=512 count=1
dd if=/tmp/mbr.bak | ssh <user>@<host> dd of=<location>/mbr.bak bs=446 count=1

Next, boot into your KVM Slice with gparted. You can go ahead and setup networking now if you like. You’ll need to create the 3 partitions in GParted before you can copy your partition image to them. I recommend not formatting them, just leave them as “cleared” or “unformatted”. Whatever you choose will be overwritten anyway.

It is okay to create the partitions larger than you need. Your boot partition should be the same size as on the original server. I recommend setting the grub partition to 16MiB, which is larger than you’ll need, but it will work fine. Your root partition can fill the entire drive. Run the commands:

ssh <user>@<host> cat <location>/sda1.img | dd of=/dev/vda1
ssh <user>@<host> cat <location>/sda2.img | dd of=/dev/vda2
ssh <user>@<host> cat <location>/sda3.img | dd of=/dev/vda3

Restart gparted or refresh it. Right click /dev/vda3 and deactivate it if it isn’t already. Follow gparted’s warnings about checking the partitions. This will both check the partition and expand it to use all available size.

Mount your root directory into /mnt:

mount /dev/<VG Name>/<LVM Name> /mnt

Mount your /boot directory:

mount /dev/vda2 /mnt/boot

Bind your directories:

mount --bind /dev /mnt/dev
mount --bind /proc /mnt/proc
mount --bind /sys /mnt/sys

Chroot into /mnt and install grub.

chroot /mnt
grub-install --root-directory=/ /dev/vda

Reboot your KVM Slice. You’ll wind up in the recovery console. Use the blkid command to get the UUID of the boot partition and modify /etc/fstab. This process can be repeated for your root partition if you are not using LVM. If you are, go to the next step.

ls /dev/disk/by-id

Copy the line starting with dm-uuid-LVM-* into /etc/fstab for the root partition. Make SURE there are no typos.

Reboot. You should be greeted with your login prompt. At this point, you’ll need to configure your networking and any address binding since it is likely your configuration has changed. Good luck!

Leave a Reply

Your email address will not be published. Required fields are marked *