Building a fault-tolerant firewall system with virtual machines: Creating disk image


Each virtual machine needs a disk image containing the operating system. We have three virtual machines so we need three disk images. However, just create one image and then make copies of the other two.

Creating partitions and filesystem on disk
An operating system generally needs one root partition and one swap partition. We practice on a USB stick or a USB hard disk. Assuming the device name is /dev/sdb. The commands below create a 10G root partition (exactly, minus the first 1M on disk) and a 2G swap partition, and create the root filesystem on the root partition

sudo wipefs -a /dev/sdb
sudo parted /dev/sdb mklabel msdos
sudo parted /dev/sdb mkpart primary ext4 1MiB 10240MiB
sudo parted /dev/sdb mkpart primary linux-swap 10240MiB 12288MiB
sudo mkfs.ext4 -L firewall /dev/sdb1

To view info of the partitions and the root filesystem, run the following commands:

sudo parted /dev/sdb unit MiB print
sudo fsck /dev/sdb1

The size of the root partition is 10239M. The filesystem has 2621184 blocks. Each block 4K. So the size of the filesystem is 2621184 * 4 / 1024 = 10239 (M), equal to the partition size. However, these two dimensions are not always equal.
We need to calculate the filesystem size exactly because the disk image needs to be compact. Next is to install the operating system onto the disk as usual.

Increasing the filesystem size
Maybe you have a need to increase the filesystem size to install more software or perform tasks that require more space. Remember that increasing the partition size does not increase the filesystem size. With the partitions and filesystem organized on the disk as above, assuming that the system is used and needs to preserve data, you need to delete partition 2 first, and then increase the size of partition 1, for example from 10G to 12G. Run the commands below:

sudo parted /dev/sdb rm 2
sudo parted /dev/sdb resizepart 1 12288MiB

Then recreate partition 2, i.e. swap partition:

sudo parted /dev/sdb mkpart primary linux-swap 12288MiB 14336MiB

Now the root partition size has increased but the filesystem size has not changed

To increase the filesystem size to the same as the partition size, run this command:

sudo resize2fs /dev/sdb1

The last thing is to run sudo mkswap /dev/sdb2 to set up the swap area on the new swap partition. Then update /etc/fstab if necessary. If the system is running, you need to run sudo swapoff /dev/sdb2 before deleting the old swap partition. To enable the swapping, run sudo swapon -a

Creating disk image
For the above example we use 14336M in disk, you run the command below to create a raw disk image file named disk-omarine-fw.raw

sudo dd if=/dev/sdb of=disk-omarine-fw.raw bs=4M count=3584 conv=spars

14336 / 1024 = 14. Thus, the disk image size is exactly 14G.

Increasing the disk image size
Once the disk image has been used, we can still resize it. However, this only makes sense when adding disk operations inside the virtual machine. To view info of the disk image disk-omarine-fw.raw, run this command:

qemu-img info disk-omarine-fw.raw

The command below increases the size of the disk image disk-omarine-fw.raw from 14G to 16G

qemu-img resize -f raw disk-omarine-fw.raw 16G

The above command adds a 2G free space inside the virtual disk

You still have to reorganize the disk to take advantage of the free space, doing inside the virtual machine. Disk manipulation in a virtual machine is no different from that of a real machine.

Currently unrated


There are currently no comments

New Comment


required (not published)



What is 2 - 1?