Howto LVM expand disk

6 minute read Published:

A new blog post by me.

Howto make a LVM disk bigger without downtime

In the current IT virtual infrastructure it is possible to add new disks to or resize disks of a system without downtime. In Debian Lenny it was not always possible to do the .a href=“http://www.tldp.org/HOWTO/LVM-HOWTO/index.html".LVM./a. extention without downtime. From .a href=“http://www.debian.org”.Debian./a. Squeeze and higher this is possible. The following steps can be used to add extra space to a disk in a LVM setup.

Requirements

To grow the filesystem online you need at least the following:

  • Debian 6.x (Squeeze) or higher
  • partprobe (part of the parted Debian package)

The steps

In this description we will use the first disk of the system (/dev/sda) that will be resized. And the volume group (VG) that is used is named [color="#ff0000”]vg_main[/color]. Ofcourse you have to substitute the right values for your own system.

What is the current size

First take a look at the current size of the disk, so that we can see if the disk has been resized later on. There are 2 ways to see what the current disk size is.

root@localhost:~# fdisk -l <span style="color:#ff0000;">/dev/sda</span> &#124; grep "Disk.*sda"
Disk /dev/sda: 19.3 GB, 19327352832 bytes
root@localhost:~# 

or

root@localhost:~# parted <span style="color:#ff0000;">/dev/sda</span> print &#124; grep Disk
Disk /dev/sda: 19.3GB
root@localhost:~#

Resize virtual disk

Now you have to increase the size of the disk on the virtual infrastructure. In VMware you can do this by using edit settings of a VM and increasing the size of the right harddisk. The right harddisk is the first harddisk in this example.

Notify the OS of the new size

The following three commands will all try to let the kernel know that he has to look again at the size of the disk. You should see a message in the kernel log (/var/log/messages or /var/log/kern.log). Something like this:

kernel: [387701.676394] sd 0:0:0:0: [sda] 52428800 512-byte logical blocks: (26.8 GB/25.0 GiB)
kernel: [387701.676439] sd 0:0:0:0: [sda] Cache data unavailable
kernel: [387701.676539] sda: detected capacity change from 19327352832 to 26843545600

You don’t need to run all the commands, but start with the first and continue until you see the message above. If you have run all the commands you can check the size of the disk again to see if it has changed.

This first command will rescan all devices. If you know the right numbers you can use it instead of the [0-9]:* The for loop is needed because you can’t redirect the data to multiple files at once.

root@localhost:~# for dev in /sys/bus/scsi/devices/[0-9]:*/rescan; do echo 1 > ${dev}; done
root@localhost:~#

The second try is a script. You can use the following command or use our .a href="/wp-content/uploads/2013/09/rescan-scsi-bus.sh".local version./a. of the script.

root@localhost:~# wget -q -O /var/tmp/rescan-scsi-bus.sh http://rescan-scsi-bus.sh/
root@localhost:~# bash /var/tmp/rescan-scsi-bus.sh

The third command is the last command you can use, also here you can use the right number instead of the [0-9].

root@localhost:~# for dev in /sys/class/scsi_host/host[0-9]/scan; do echo "- - -" > ${dev}; done

If the disk is not yet extended when looking at the total disksize, you will have to do a reboot.

Extend the extended partition

Depending on the disk layout, you might need to extent the extended partition.

root@localhost:~# fdisk -l <span style="color:#ff0000;">/dev/sda</span>

Disk /dev/sda: 26.8 GB, 26843545600 bytes
64 heads, 32 sectors/track, 25600 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000f3957

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           2         244      248832   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2             245       18432    18623489    5  Extended
Partition 2 does not end on cylinder boundary.
/dev/sda5             246        8191     8136704   8e  Linux LVM
/dev/sda6            8192       18432    10486768   8e  Linux LVM
root@localhost:~#

This is how you can extend the extended partition.

root@localhost:~# parted <span style="color:#ff0000;">/dev/sda</span> print &#124; grep extended
 <span style="color:#ff0000;">2      257</span>MB   19.3GB  19.1GB  extended
root@localhost:~# parted <span style="color:#ff0000;">/dev/sda</span> resize <span style="color:#ff0000;">2 257</span> 100%

Add a new LVM partition

Now that the extended partition has some space to create extra logical partitions, we create a new LVM partition (type 8e in fdisk).

root@localhost:~# fdisk <span style="color:#ff0000;">/dev/sda</span>

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (18433-25600, default 18433): 
Using default value 18433
Last cylinder, +cylinders or +size{K,M,G} (18433-25600, default 25600): 
Using default value 25600

Command (m for help): t
Partition number (1-7): 7
Hex code (type L to list codes): 8e
Changed system type of partition 7 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sda: 26.8 GB, 26843545600 bytes
64 heads, 32 sectors/track, 25600 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000f3957

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           2         244      248832   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2             245       25600    25963521    5  Extended
Partition 2 does not end on cylinder boundary.
/dev/sda5             246        8191     8136704   8e  Linux LVM
/dev/sda6            8192       18432    10486768   8e  Linux LVM
/dev/sda7           18433       25600     7340016   8e  Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
root@localhost:~#

Now let the kernel know a new partition is available:

root@localhost:~# partprobe -s
/dev/sda: msdos partitions 1 2 <5 6 7>
root@localhost:~#

Run LVM commands to extend the Logical Volume (LV)

Create a new physical volume (PV) on the new partition:

root@localhost:~# pvcreate <span style="color:#ff0000;">/dev/sda7</span>
  Physical volume "/dev/sda7" successfully created
root@localhost:~#

Add this PV to an existing volume group (VG) called vg_main:

root@localhost:~# vgextend <span style="color:#ff0000;">vg_main /dev/sda7</span>
  Volume group "<span style="color:#ff0000;">vg_main</span>" successfully extended
root@localhost:~#

Check if the VG contains the PV:

root@localhost:~# pvs
  PV         VG      Fmt  Attr PSize  PFree
  /dev/sda5  <span style="color:#ff0000;">vg_main</span> lvm2 a-    7.76g    0 
  /dev/sda6  <span style="color:#ff0000;">vg_main</span> lvm2 a-   10.00g    0 
  /dev/sda7  <span style="color:#ff0000;">vg_main</span> lvm2 a-    7.00g 7.00g
root@localhost:~# vgs
  VG      #PV #LV #SN Attr   VSize  VFree
  <span style="color:#ff0000;">vg_main</span>   3   5   0 wz--n- 24.75g 7.00g

Now extend the LV and the filesystem:

root@localhost:~# vgdisplay &#124; grep Free
  Free  PE / Size       1791 / 7.00 GiB
root@localhost:~# lvextend -l +1791 /dev/<span style="color:#ff0000;">vg_main</span>/root
  Extending logical volume root to 9.86 GiB
  Logical volume root successfully resized
root@localhost:~# resize2fs -p /dev/<span style="color:#ff0000;">vg_main</span>/root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/<span style="color:#ff0000;">vg_main</span>/root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/<span style="color:#ff0000;">vg_main</span>/root to 2583552 (4k) blocks.
The filesystem on /dev/<span style="color:#ff0000;">vg_main</span>/root is now 2583552 blocks long.

root@localhost:~# 
Recent posts
- full list -