bersama catatan peribadi & teknikalnya.

How to Resize A LVM Partition Without Losing Data With Gdisk

An Alternative to Parted/Gparted

#lvm | #gdisk | #gpt
This post is awfully outdated.

  • Please back up your data prior to doing this. I will not be held liable should you incur any data loss by following this tutorial.
  • This action was done in a VM (QEMU/KVM) and was only tested on a partition formatted as EXT4. I cannot guarantee that it works with other type of formats.
  • Quoted from archwiki:
    "Warning: While enlarging, a file system can often be done on-line (i.e. while it is mounted), even for the root partition, shrinking will nearly always require to first unmount the file system so as to prevent data loss. Make sure your FS supports what you are trying to do."

As mentioned in my previous (Archived) entry, I am writing this post to explain how to resize a partition without losing any data with gdisk. By this, I mean resizing the physical partition itself, not the physical volume.
  1. Boot the archiso on the VM with QEMU. If you have installed grub on ESP partition, chances are you will be dropped to the installed grub bootloader as shown in [this image] instead of the one that comes with the archiso. In this case, press 'c' for a command-line and run commands as indicated in the image below in order to be able to boot into it:-
  2. Heads-Up: Do take note that I am shrinking my physical partition. Should there be a need for you to enlarge your physical partition, the order of the steps will be in the reverse/opposite sequence. In that case, you have to resize the involved partition with gdisk first, followed by pvresize and lastly lvresize.
    Please ensure that the file system is unmounted (in this case it would be the Logical Volume containing the / partition). One way to know is by running 'lsblk' command. It will list information about all available block devices including the MOUNTPOINT of those devices. If MOUNTPOINT returns no output for the device, that means it is not mounted.
    First, resize the Logical Volume and its file system all at once (with '-r' option) by running this command:
    lvresize -L new-size -r vgname>/lvname
  3. Next, run pvresize with the below command:-
    pvresize --setphysicalvolumesize new-size /dev/vdaX
    The new size of the PV is advisable to be slightly bigger than the shrunken LV's otherwise it may prompt you an error.
  4. And then, run 'gdisk /dev/vda', enter 'p' to print the partition table, and enter 'i' to obtain the information of the partition that we want to resize where we will take note of its Partition unique GUID.
  5. After that, enter 'd' and the number of the partition we want to delete. Create a new partition by entering 'n', enter a new size which I recommend being slightly bigger than the resized Physical Volume's and change the type of partition to Linux LVM (8e00) which is of the same type to the previous partition prior to deletion. Enter 'x' for expert command followed by letter 'c' to key in the partition's unique GUID which we took note of previously. Be careful with this action and ensure that the unique GUID is identical before writing the changes or otherwise you may encounter data loss.
  6. Reboot and see if the installed Arch system is booting successfully after the shrinking process.

Well, actually! There is an easier way to have this exact purpose done without all these hassles (with the exception of lvresize and pvresize. They are still required to be done before you execute the task with any partitioning tool of your choice, if you are shrinking it).

Use parted or Gparted!

I however, prefer gdisk over these two partitioning tools due to:
  1. too much typing when using parted and the way the sizing is managed is just complicated. Or maybe it's just me who wants to think so.
  2. a lengthy time of progress it takes when resizing a large partition by using Gparted.

BONUS. The radio buttons below feature the complete steps I did to add a new partition with the remaining free space gained as a result of shrinking the physical partition, in the form of screenshots. This new partition parks the archiso that I can boot into by adding a menuentry in the grub config file.
Boot into the archiso again to create a new partition for the remaining partition obtained by the prior resizing.

Confirming the free space available with any disk partitioning software.

Create a new partition which will be used to store an archiso image. This is useful to boot into it from GRUB should something bad happen to the root partition.

Once done, reboot into the archiso again, and format the new partition.

Copy / place the archiso image into a mounted directory in the new partition just created.

Add a grub menuentry for it.

Reboot. There shall be a menuentry seen for the archiso image which was added into the grub config file before.

The loop device (archiso image) that is booted into would be seen as mounted now.

Kali terakhir dikemaskini: