dd increase hardisk size partition

Posted under » Ubuntu » Linux on 4 January 2024

In my previous articles, Format USB drive to ext4 and mount and Raspberry pi 4 boot from USB we have use several disk tools.

Now I am trying to enlarge a boot disk because the current disk running the system has ran out of space.

First you get a bigger hardisk to replace the current one.

You will then transfer the contents to the bigger disk using dd. dd is an utility that will duplicate or clone the disk. dd can create a disk image, which is great for backup work.

$ dd if=/dev/sdb of=/tmp/sda.img // w/o options
$ dd if=/dev/sdb of=/tmp/sda.img bs=1k conv=noerror 

When you clone a disk, you will also clone the partition. A boot disk normally has 2 or 3 partitions. The first being the boot partition. Example of fdisk and parted. After the boot comes the system partition which are the one that will run out.

With dd you can select the whole disk or just the partition you want to clone.

You have to unmount the source disk. You can't make an image while using the disk ie dd on the active OS running dd.

As usual, identify the source disk by the 'fdisk -l' command. Assuming it is sdb

$ dd if=/tmp/sdb.img of=/dev/sdc/ bs=4096
$ dd if=/dev/sdb of=/dev/sdc //if not image

/dev/sdc is the bigger hardisk but dd just copy byte for byte to the new disk. The partition remains the same despite have a bigger disk.

The best tool to expand the partition is using Gparted. I like it because of the GUI. Running gparted on live CD will ensure you will only focus on partitioning on the particular disk. Simply drag and extend the system partition that is running out of space and execute. When you run df you will see

Filesystem                        1K-blocks     Used Available Use% Mounted on
tmpfs                                867176     1648    865528   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv  13893712 13793712         5  99% /
tmpfs                               4335868       12   4335856   1% /dev/shm
tmpfs                                  5120        0      5120   0% /run/lock
/dev/sda2                           1992552   256864   1614448  14% /boot
tmpfs                                867172        4    867168   1% /run/user/1000

So your system usage is critically low even after gparted which extend the partition. You will also notice that your system is running on '/dev/mapper/ubuntu--vg-ubuntu--lv'. What is that?

That is the LVM (Logical Volume Manager). It is also /dev/sda3 or the system partition I mentioned earlier. We need to expand or extend this as well. gparted only enlarge and then extend the partition but the logical volume is still small.

To check the space

$ vgdisplay // check volume space
$ lvdisplay // check the logical space

vgdisplay shows the available volume space which include the unused space. lvdisplay shows only the portion that is being used or logical volume. So you need to expand the logical volume.

$ lvresize -L +40G /dev/mapper/ubuntu--vg-ubuntu--lv 
// or 
$ lvresize --size 120G /dev/mapper/ubuntu--vg-ubuntu--lv 

When you 'df' or 'lvdisplay', you will see that you still are running out of space. The final step is to `resize2fs'.

The resize2fs command is used to enlarge or shrink an ext2/3/4 file system on a device. You can enlarge a mounted file system You can specify the desired size of the file system in order to either enlarge or shrink it. If you don’t specify a size, the file system will be resized to the same size as the logical volume.

$ resize2fs /dev/sda3 
// or 
$ resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv

A final check

$ vgdisplay
$ lvdisplay
$ df

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data