Categories
Linux

Adding a new disk in an existing Volume Group (LVM)

This is a guide about adding a new disk to an existing [LVM](http://en.wikipedia.org/wiki/Logical_Volume_Manager_%28Linux%29 "Logical Volume Manager") VG

Check current setup

  1. First let’s check and document the current setup.
  • Checking the /proc filesystem:
    # cat /proc/partitions 
     major minor  #blocks  name
        8        0    7880544 sda
        8        1     248832 sda1
        8        2          1 sda2
        8        5    7628800 sda5
      252        0    6574080 dm-0
      252        1    1036288 dm-1
    
  • Using lsblk:
    # lsblk
    NAME                   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda                      8:0    0  7,5G  0 disk 
    ├─sda1                   8:1    0  243M  0 part /boot
    ├─sda2                   8:2    0    1K  0 part 
    └─sda5                   8:5    0  7,3G  0 part 
      ├─ubuntu--vg-root   252:0    0  6,3G  0 lvm  /
      └─ubuntu--vg-swap_1 252:1    0 1012M  0 lvm  [SWAP]
    
  1. Check and document the LVM layout.
  • Volume Group info:
    # vgs
      VG         #PV #LV #SN Attr   VSize VFree 
      ubuntu-vg    1   2   0 wz--n- 7,27g 16,00m
    
  • Physical Volume info:
    # pvs
      PV         VG         Fmt  Attr PSize PFree 
      /dev/sda5  ubuntu-vg  lvm2 a--  7,27g 16,00m
    
  • Logical Volume info:
    # lvs
      LV     VG         Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
      root   ubuntu-vg  -wi-ao----    6,27g
      swap_1 ubuntu-vg  -wi-ao---- 1012,00m
    

Add a new physical or virtual disk

Now add the new disk on your server. On VMs it is possible to add a new disk without powering off. On physical servers this can be possible too if the server comes with hot-swap functionality. Check your server specs first!

  1. Check if the new disk is detected.
  • You can use this command if you want your system to detect the new disk without rebooting:
    # for SCSI_HOST in /sys/class/scsi_host/* ; do echo "- - -" > $SCSI_HOST/scan ; done
    

    The above command simply loops through the SCSI hosts under the /sys/class/scsi_host directory and sends the "– – –" string to them. This forces the SCSI hosts to detect the new disk that has been attached.

  • Using the /proc filesystem:

    # cat /proc/partitions
    major minor  #blocks  name<br />
       8        0    7880544 sda
       8        1     248832 sda1
       8        2          1 sda2
       8        5    7628800 sda5
       8       16   31522680 sdb
     252        0    6574080 dm-0
     252        1    1036288 dm-1
    

    The size of the disk in GB, is 30GB:

    # echo '31522680/1024/1024' | bc -l
    30.06237030029296875000
    

  • Using lsblk:
    # lsblk
    NAME                   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda                      8:0    0  7,5G  0 disk 
    ├─sda1                   8:1    0  243M  0 part /boot
    ├─sda2                   8:2    0    1K  0 part 
    └─sda5                   8:5    0  7,3G  0 part 
       ├─ubuntu--vg-root   252:0    0  6,3G  0 lvm  /
       └─ubuntu--vg-swap_1 252:1    0 1012M  0 lvm  [SWAP]
    sdb                      8:16   0 30,1G  0 disk
    

Add the new disk to LVM Volume Group

  1. Create a new partition on the new disk:
    # fdisk /dev/sdb
    
    Welcome to fdisk (util-linux 2.25.2).
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.
    
    Device does not contain a recognized partition table.
    Created a new DOS disklabel with disk identifier 0xaea3ab78.
    
    Command (m for help): n
    Partition type
      p   primary (0 primary, 0 extended, 4 free)
      e   extended (container for logical partitions)
    Select (default p):< Using default response p.
    Partition number (1-4, default 1): 
    First sector (2048-63045359, default 2048): 
    Last sector, +sectors or +size{K,M,G,T,P} (2048-63045359, default 63045359):
    
    Created a new partition 1 of type 'Linux' and of size 30,1 GiB.
    
    Command (m for help): t
    Selected partition 1
    Hex code (type L to list all codes): <strong>8e
    Changed type of partition 'Linux' to 'Linux LVM'.
    
    Command (m for help): <strong>w</strong>
    The partition table has been altered.
    Calling ioctl() to re-read partition table.
    Syncing disks.
    
  2. Verify the creation of a new partition:
    # fdisk -l /dev/sdb
    
    Disk /dev/sdb: 30,1 GiB, 32279224320 bytes, 63045360 sectors
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: dos
    Disk identifier: 0xaea3ab78
    
    Device     Boot Start      End  Sectors  Size Id Type
    /dev/sdb1        2048 63045359 63043312 30,1G 8e Linux LVM
    

Add the new partition to the Volume Group

  1. Extend the Volume Group by adding a new disk:
    # vgextend ubuntu-vg /dev/sdb1
     Physical volume "/dev/sdb1" successfully created
     Volume group "ubuntu-vg" successfully extended
    
  2. Check the current free space of the Volume Group:
    # vgs
     VG        #PV #LV #SN Attr   VSize  VFree 
     ubuntu-vg   2   2   0 wz--n- 37,33g 30,07g
    
  3. Verify the new Physical Volume:
    # pvs
     PV         VG        Fmt  Attr PSize  PFree 
     /dev/sda5  ubuntu-vg lvm2 a--   7,27g 16,00m
     /dev/sdb1  ubuntu-vg lvm2 a--  30,06g 30,06g
    
  4. Check the state of the Logical Volumes:
    # lvs
     LV     VG        Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
     root   ubuntu-vg -wi-ao---- 6,27g     
     swap_1 ubuntu-vg -wi-ao---- 1012,00m
    

    Nothing changed yet, of course.

Resize the logical volume

  1. Use the lvresize command to resize the root volume:

    # lvresize -L 30,07g /dev/ubuntu-vg/root
     Rounding size to boundary between physical extents: 30,07 GiB
     Size of logical volume ubuntu-vg/root changed from 6,27 GiB (1605 extents) to 30,07 GiB (7698 extents).
     Logical volume root successfully resized
    

  2. Verify the volume resize:
    # lvs
     LV     VG        Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
     root   ubuntu-vg -wi-ao----   30,07g
     swap_1 ubuntu-vg -wi-ao---- 1012,00m
    

    The root volume is now at 30,07GB. Good.

Resize the filesystem

  1. Check the current filesystem size:

    # df -hT
    Filesystem                   Type      Size  Used Avail Use% Mounted on
    udev                         devtmpfs  485M     0  485M   0% /dev
    tmpfs                        tmpfs     100M  5,1M   95M   6% /run
    /dev/mapper/ubuntu--vg-root  ext4      <strong>6,1G</strong>  2,4G  3,4G  41% /
    tmpfs                        tmpfs     496M     0  496M   0% /dev/shm
    tmpfs                        tmpfs     5,0M  4,0K  5,0M   1% /run/lock
    tmpfs                        tmpfs     496M     0  496M   0% /sys/fs/cgroup
    /dev/sda1                    ext2      236M   40M  184M  18% /boot
    tmpfs                        tmpfs     100M   16K  100M   1% /run/user/1000
    

    Still using the old size, as expected.

  2. Resize the root filesystem:

    # resize2fs /dev/mapper/ubuntu--vg-root
    resize2fs 1.42.12 (29-Aug-2014)
    Filesystem at /dev/mapper/ubuntu--vg-root is mounted on /; on-line resizing required
    old_desc_blocks = 1, new_desc_blocks = 2
    The filesystem on /dev/mapper/ubuntu--vg-root is now 7882752 (4k) blocks long.
    

  3. Verify the new size of the root filesystem:
    # df -hT
    Filesystem                   Type      Size  Used Avail Use% Mounted on
    udev                         devtmpfs  485M     0  485M   0% /dev
    tmpfs                        tmpfs     100M  5,1M   95M   6% /run
    /dev/mapper/ubuntu--vg-root  ext4       30G  2,4G   26G   9% /
    tmpfs                        tmpfs     496M     0  496M   0% /dev/shm
    tmpfs                        tmpfs     5,0M  4,0K  5,0M   1% /run/lock
    tmpfs                        tmpfs     496M     0  496M   0% /sys/fs/cgroup
    /dev/sda1                    ext2      236M   40M  184M  18% /boot
    tmpfs                        tmpfs     100M   16K  100M   1% /run/user/1000
    

So now we have a logical root volume that expands across multiple physical disks. Notice, however, that this is not a very solid setup, since the loss of one of the physical volumes can bring down the whole system along with your data. Thus make sure that you have a solid and tested backup procedure in place. The restore procedure should also be documented in every detail in your disaster recovery practices.

References

  • http://tldp.org/HOWTO/LVM-HOWTO/index.html
  • http://wingloon.com/2013/05/07/how-to-detect-a-new-hard-disk-without-rebooting-vmware-linux-guest/

By Theodotos Andreou

An old school Linux guy, a founding member of Ubuntucy and a founding member of Cyprus FOSS community. Currently working as sysadmin in the Cyprus University of Technology.

https://www.ubuntucy.org

https://ellak.org.cy

2 replies on “Adding a new disk in an existing Volume Group (LVM)”

Leave a Reply

Your email address will not be published. Required fields are marked *