Let's assume you have a small computer running Linux and you want to convert it to a VM, or vice versa. You'll need to transfer the contents of the disk to the target machine, then configure the target machine to boot off the tranferred file system. It is assumed the filesystem contains all the drivers and modules required for the target system.

Creating the Clone Image

Use Clonezilla to make a bootable image. The output is a .img file. The .img file is transferred to a USB stick or mounted as a CD-ROM, then booted as a Live image. This can be done using the instructions from the Clonezilla website: https://clonezilla.org.

Transfering the Image to a Bootable USB

Let's assume you are using a Mac. See "How Do I Create a Bootable USB Disk using Mac OS" for a description of how to do this.

Create a VM

Create an Ubuntu VM using the clonezilla-img.iso. A scratch disk is used for conversion. It must be big enough to hold a copy of the ISO, ~5GB. The target disk must be 15GB to hold a 12.5GB filesystem and swap. Create an image with these two disks and ISO mounted.

  • Ubuntu 64-bit- 2048 MB RAM
  • Disk 1 - 32GB /dev/sda
  • Disk 2 - 5 GB /dev/sdb

Boot the VM.

Select clonezilla "Other Mode" to copy everything to RAM. It will fail, press enter a few times, then enter the shell, become root.

$ sudo -i
# mkdir /mnt/cdrom
# mount -r /dev/cdrom /mnt/cdrom

Create /dev/sdb1 with a filesystem:

# fdisk /dev/sdb
n
p
1
Enter
Enter
w
# mkfs.ext4 /dev/sdb1

Mount the filesystem and copy the image to it:

# mkdir /mnt/sdb1
# mount /dev/sdb1 /mnt/sdb1
# rsync -aHAX /mnt/cdrom/home/partimag/ /mnt/sdb1

Make everything writeable:

# cd /mnt/sdb1/
# chmod -R +w VGW*

Prepare the target disk:

# fdisk /dev/sda
n
p
1
Enter
Enter
w

Convert the clone data:

# mkdir ../VGW
# ocs-cvt-dev -d VGW-210-2020-05-08-18-img . mmcblk0 sda
Are you sure? y

Unmount the clone data disk:

# cd
# umount /mnt/sdb1

Restore the image:

# ocs-live
local_dev
Enter
^C
sdb
VGW...
done
Beginner
OK
restoredisk
OK
VGW...
OK
sda
OK
-scr
-p choose
OK
Enter
y
y

The filesystem is now contained on /dev/sda2.

Mount the filesystem and change /etc/fstab:

# mkdir /mnt/sda2
# mount /dev/sda2 /mnt/sda2
# vi /mnt/sda2/etc/fstab

Change it to:

/dev/sda1   /       ext4    errors=remount-ro   0   1
/dev/sda2   none    swap    sw                  0   0

Reorganize the Disks

The current set of disks are too big. In the end, we want the clone data disk to have a copy of /mnt/sda2. To do this another disk is needed to fit 8.7GB of data. Create a 10GB disk and attach it.

Create a 10GB RootFS.vdi file. Power off. Attach the disk. Boot into clonezilla in RAM.

Make a partition on /dev/sdc and filesystem on /dev/sdc1.

# fdisk /dev/sdc
n
p


w

# mkfs.ext4 /dev/sdc1

Mount the source and destination disks, then copy the data to RootFS.

# mkdir /mnt/sdc1 /mnt/sda2
# mount /dev/sda2 /mnt/sda2
# rsync -aHAX /mnt/sda2/ /mnt/sdc1

Note: the trailing slash on sda2 is important.

Verify /mnt/sdc1/etc/fstab is correct, then shutdown the VM.

# poweroff

Setting a boot-able system

At this point RootFS.vdi contains the rootfs needed. This is the only disk required, the others can be destroyed. A new 16GB disk is created to hold the rootfs and swap for the final system. To make it boot-able, grub2 must be used from a Live boot of Ubuntu.

Using Cmd-D, release the Velocity_1, Velocity_2 disks and remove/trash them.

Create a new Velocity_1.vdi, 16GB and attach it to SATA 0:0. Attach the ubuntu iso to the CD drive, using the Live option. Boot. Click on Try Ubuntu. Open a terminal.

Partition /dev/sda and populate it:

$ sudo -i
# fdisk /dev/sda
n
p
1

+14GB

n
p
2


t
82
w
# mkfs.ext4 /dev/sda1
# mkdir /mnt/sda1 /mnt/sdb1
# mount /dev/sda1 /mnt/sda1
# mount /dev/sdb1 /mnt/sdb1
# rsync -aHAX /mnt/sdb1/ /mnt/sda1

Time to make the disk boot-able.

Need to grub using chroot. chrootsda1.sh created from from Wikipedia does it.

From the VM, setup ssh:

$ ssh-keygen
$ ssh-copy-id joeb@10.0.2.2
$ scp joeb@10.0.2.2:Desktop/VL-1152/chrootsda1.sh .

Install the boot on /dev/sda and poweroff:

$ sudo ./chrootsda1.sh
$ sudo mkswap /dev/sda2
$ sudo poweroff

Eject the CDROM.

All done.

RootFS.vdi can be detached. It is useful to re-image in the future if necessary.

Re-Imaging

Boot Ubuntu Live.

Wipe out /dev/sda and re-populate the filesystem:

$ sudo -i
# mkfs.ext4 /dev/sda1
# mkdir /mnt/sda1 /mnt/sdb1
# mount /dev/sda1 /mnt/sda1
# mount /dev/sdb1 /mnt/sdb1
# echo remember the /
# rsync -aHAX /mnt/sdb1/ /mnt/sda1
# umount /mnt/sda1
# exit

Install boot and make swap:

$ sudo ./chrootsda1.sh
$ sudo mkswap /dev/sda2
$ sudo poweroff

Reboot:

$ sudo powerdown

Eject CD, power up.