Live Resizing Linux Disks

These are my personal notes so I don’t forget next time… Runnin ESXI 5, VM is Debian 7 using lvm. Server runs Electrum and since the blockchain has grown so much, I’m out of space again.

Resize disk in ESXI

Disk was 110GB prior.

Fdisk output & disk rescan

root@electrum-server:~# fdisk -l /dev/sda

Disk /dev/sda: 118.1 GB, 118111600640 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      499711      248832   83  Linux
/dev/sda2          499712   230686719   115093504    5  Extended
/dev/sda5          501760   230686719   115092480   8e  Linux LVM
root@electrum-server:~# echo '1' > /sys/class/scsi_disk/0\:0\:0\:0/device/rescan
root@electrum-server:~# fdisk -l /dev/sda

Disk /dev/sda: 139.6 GB, 139586437120 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      499711      248832   83  Linux
/dev/sda2          499712   230686719   115093504    5  Extended
/dev/sda5          501760   230686719   115092480   8e  Linux LVM

Current LVM setup

root@electrum-server:~# pvs
  PV         VG              Fmt  Attr PSize   PFree
  /dev/sda5  electrum-server lvm2 a--  109.76g    0 

Delete & re-create partitions

Need to fdisk /dev/sda, delete sda5 and then sda2. Create new extended partition using all free space, then create logical partition sda5. Set ‘type’ to 8e. Write new partition table and quit fdisk.

root@electrum-server:/# pvresize /dev/sda5
  Physical volume "/dev/sda5" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized
root@electrum-server:/# pvs
  PV         VG              Fmt  Attr PSize   PFree 
  /dev/sda5  electrum-server lvm2 a--  129.76g 20.00g

root@electrum-server:/# lvresize -L +19G electrum-server/home
  Extending logical volume home to 108.89 GiB
  Logical volume home successfully resized

root@electrum-server:~# pvs
  PV         VG              Fmt  Attr PSize   PFree
  /dev/sda5  electrum-server lvm2 a--  129.76g 1.00g

I left 1gb for emergency space just in case. I tried to use partprobe -s; however it didn’t seem to work to get the new partition size recognized. I had to do a quick reboot, which sorta defeats the whole ‘live resizing’ thing but I’m pretty sure it was because /home was 100% filled.

Resizing volumes

root@electrum-server:/# resize2fs /dev/mapper/electrum--server-home 
resize2fs 1.42.5 (29-Jul-2012)
Filesystem at /dev/mapper/electrum--server-home is mounted on /home; on-line resizing required
old_desc_blocks = 6, new_desc_blocks = 7
Performing an on-line resize of /dev/mapper/electrum--server-home to 28546048 (4k) blocks.
The filesystem on /dev/mapper/electrum--server-home is now 28546048 blocks long.

root@electrum-server:/# df -h
Filesystem                         Size  Used Avail Use% Mounted on
/dev/mapper/electrum--server-home  108G   83G   20G  82% /home

Yay, now we’re happy with some extra room. I’ve adopted a script to warn me with /home gets low:

admin@electrum-server:~$ cat mailwarn.sh 
#!/bin/bash
CURRENT=$(df /home | grep / | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=70

if [ "$CURRENT" -gt "$THRESHOLD" ] ; then
    mail -s 'Disk Space Alert' jason@doelger.org << EOF
Your root partition remaining free space is critically low. Used: $CURRENT%
EOF
fi

It will run every day at 9am.