Skip to content

Instantly share code, notes, and snippets.

@pshirshov
Last active June 2, 2018 23:17
Show Gist options
  • Save pshirshov/114c77ef89df3fc6c728f2535c4e7dc0 to your computer and use it in GitHub Desktop.
Save pshirshov/114c77ef89df3fc6c728f2535c4e7dc0 to your computer and use it in GitHub Desktop.
LVM: shrink volume

Copy partition table:

sfdisk -d /dev/sda > part_table
sfdisk /dev/sda < part_table

Copy boot partition

dd if=/dev/sda1 of=/dev/sdb1 bs=50M

Reduce fs, reduce volume, extend fs:

resize2fs /dev/VG/root 180G
lvreduce -L 200G /dev/VG/root
resize2fs /dev/polar/root

Move to new HD:

pvcreate /dev/sdbX
vgextend vgX /dev/sdbX
#pvmove /dev/sdaX
pvmove /dev/sdaX /dev/sdbX
#vgsplit oldvg newvg /dev/sda1
#vgreduce vgX /dev/sdaX

Also useful:

lvextend -l +100%FREE /dev/volgroup/logvol

Mount, chroot,

mkdir /mnt/tmp
mount /dev/VG/root /mnt/tmp
mount --types proc /proc /mnt/tmp/proc
mount --rbind /sys /mnt/tmp/sys
mount --make-rslave /mnt/tmp/sys
mount --rbind /dev /mnt/tmp/dev
mount --make-rslave /mnt/tmp/dev 
cd /mnt/tmp
chroot .

Useful: https://falstaff.agner.ch/2013/12/22/online-resize-root-filesystem-on-a-gpt-partition/

This short tutorial shows how to resize a ext4 root filesystem online. Most people use gparted from a recovery system for this task, and I also recommend that for if you are not familiar with the tools used in this guide. Also, make sure you have a backup of the data (I did this myself on a system I just installed, so I would not mind if anything goes wrong). Generally this is not different from doing a manual MBR/fdisk resize, except that we need to treat the unique partition GUID specially since we should maintain it (for boot loaders/boot managers).

So, here is how I resized my ext4 root partition (sda2) using gdisk and resize2fs:

gdisk /dev/sda
Print information (i<enter>2)

Partition GUID code: 0FC63DAF-8483-4772-8E79-3D69D8477DE4 (Linux filesystem)
Partition unique GUID: 7FAC4BC8-6186-4CDF-B6CA-EB73D7C942D1
First sector: 206848 (at 101.0 MiB)
Last sector: 109051903 (at 52.0 GiB)
Partition size: 108845056 sectors (51.9 GiB)
Attribute flags: 0000000000000000
Partition name: ''

Delete the partition (d2)

Command (? for help): d
Partition number (1-4): 2

Recreate a new, large partition (n2….) on my setup, gdisk guessed the right first sector. The end sector was also guessed right, since I wanted to fill it up all remaining space

Command (? for help): n
Partition number (2-128, default 2): 
First sector (34-500118158, default = 206848) or {+-}size{KMGTP}: 
Last sector (206848-336072703, default = 336072703) or {+-}size{KMGTP}: 
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'

Now copy the old Partition unique GUID to the new partition (xc2)

Expert command (? for help): c
Partition number (1-4): 2
Enter the partition's new unique GUID ('R' to randomize): 7FAC4BC8-6186-4CDF-B6CA-EB73D7C942D1
New GUID is 7FAC4BC8-6186-4CDF-B6CA-EB73D7C942D1

At this point one can verify everything again, after pressing w (to safe the partition), your data might be lost if you did not do this carefully… Just be warned 🙂 Reread your block device partition (sudo blockdev –rereadpt /dev/sda) Note: This did not work for me, so I had to reboot the system once. However, I still could resize online afterwards, so no big down-time and no external recovery media required 🙂

sudo resize2fs /dev/sda2

resize2fs 1.42.8 (20-Jun-2013)
Filesystem at /dev/sda2 is mounted on /; on-line resizing required
old_desc_blocks = 4, new_desc_blocks = 11
The filesystem on /dev/sda2 is now 41983232 blocks long.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment