Tag: lvm

  • How to add physical volume and resize LVM

    How to add physical volume and resize LVM

    I have a LVM partition that uses 3 physical drives. I need to increase size of this LVM partition by adding a new disk.

    I have a drive with 452 GB free disk space.

    [root@sok ~]# parted /dev/nvme0n1 print
    Model: NVMe Device (nvme)
    Disk /dev/nvme0n1: 512GB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    Disk Flags: 
    
    Number  Start   End     Size    Type     File system     Flags
     1      1049kB  4296MB  4295MB  primary  linux-swap(v1)
     2      4296MB  6443MB  2147MB  primary  ext3
     3      6443MB  60.1GB  53.7GB  primary  ext4
     4      60.1GB  512GB   452GB   primary
    
    [root@sok ~]#
    

    Create Physical volume with pvcreate comand

    [root@sok ~]# pvcreate /dev/nvme0n1p4
      Physical volume "/dev/nvme0n1p4" successfully created.
    [root@sok ~]#
    

    Extend Volume Group

    Current volume group size is 4.19 TB

    [root@sok ~]# vgs
      VG  #PV #LV #SN Attr   VSize VFree
      vg1   3   1   0 wz--n- 4.19t    0 
    [root@sok ~]#
    

    Lets extend the volume group vg1 by adding the newly created physical volume

    [root@sok ~]# vgextend vg1 /dev/nvme0n1p4
      Volume group "vg1" successfully extended
    [root@sok ~]#
    

    Now the volume group size is 4.6 TB

    [root@sok ~]# vgs
      VG  #PV #LV #SN Attr   VSize VFree   
      vg1   4   1   0 wz--n- 4.60t <420.94g
    [root@sok ~]#
    

    Increase size of LVM

    Lets find details about the current logical volumes

    [root@sok ~]# lvs
      LV    VG  Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
      data1 vg1 -wi-a----- 4.19t                                                    
    [root@sok ~]#
    

    We have one logical volume with name data1, size is 4.9 TB.

    To extend the logical volume, rune the command

    [root@sok ~]# lvextend -l +100%FREE /dev/vg1/data1
      Size of logical volume vg1/data1 changed from 4.19 TiB (1098852 extents) to 4.60 TiB (1206612 extents).
      Logical volume vg1/data1 successfully resized.
    [root@sok ~]#
    

    This will extend logical volume "data1" to use all available free disk space on the volume group vg1.

    Lets check the new size of LVM with lvs command

    [root@sok ~]# lvs
      LV    VG  Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
      data1 vg1 -wi-a----- 4.60t                                                    
    [root@sok ~]# 
    

    Now LVM size is increased to 4.6 TB.

    Reszie filesystem

    Let us find more info on the logical volume using blkid command

    [root@sok ~]# blkid | grep data1
    /dev/mapper/vg1-data1: UUID="55a38b6b-a0a7-48a2-b314-36b1f0ce2f05" BLOCK_SIZE="512" TYPE="xfs"
    [root@sok ~]# 
    

    The volume is formatted as xfs file system.

    Let us find out the mount point.

    [root@sok ~]# df -h | grep data1
    /dev/mapper/vg1-data1  4.2T  1.1T  3.2T  25% /usr/share/nginx/html
    [root@sok ~]#
    

    The volume is mounted at /usr/share/nginx/html

    To resize the xfs filesystem, run the command

    [root@sok ~]# xfs_growfs /usr/share/nginx/html
    meta-data=/dev/mapper/vg1-data1  isize=512    agcount=5, agsize=268435455 blks
             =                       sectsz=512   attr=2, projid32bit=1
             =                       crc=1        finobt=1, sparse=1, rmapbt=0
             =                       reflink=1    bigtime=0 inobtcount=0
    data     =                       bsize=4096   blocks=1125224448, imaxpct=5
             =                       sunit=0      swidth=0 blks
    naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
    log      =internal log           bsize=4096   blocks=521728, version=2
             =                       sectsz=512   sunit=0 blks, lazy-count=1
    realtime =none                   extsz=4096   blocks=0, rtextents=0
    data blocks changed from 1125224448 to 1235570688
    [root@sok ~]#
    

    If the file system is not mounted, you need to mount it before resizing.

    Let us verify the size

    [root@sok ~]# df -h | grep data1
    /dev/mapper/vg1-data1  4.7T  1.1T  3.6T  23% /usr/share/nginx/html
    [root@sok ~]# 
    

    Size of the volume now changed from 4.2T to 4.7T.

    Back to lvm

  • Mounting partition stored inside Logical Volume

    Mounting partition stored inside Logical Volume

    On a KVM Virtual machine using LVM storage, the LVM disk is partitioned as follows.

    root@mail:~# parted /dev/vg1/iredmail print
    Model: Linux device-mapper (linear) (dm)
    Disk /dev/dm-1: 64.4GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags: 
    
    Number  Start   End     Size    File system  Name  Flags
     1      1049kB  2097kB  1049kB                     bios_grub
     2      2097kB  1076MB  1074MB  ext4
     3      1076MB  64.4GB  63.3GB
    
    root@mail:~# 
    

    I want to mount partition number 2 on the host machine.

     2      2097kB  1076MB  1074MB  ext4
    

    To make the partition available to mount, you need to use kpartx utility. Install kpartx with

    apt install kpartx
    

    To make the partitions inside the Logical volume available to mount, use the command

    kpartx -a LVM_DEIVE_NAME
    

    In this case, I used

    kpartx -a /dev/vg1/iredmail
    

    Now lvscan command will display the new device

    root@mail:~# lvscan
      ACTIVE            '/dev/vg1/win10' [100.00 GiB] inherit
      ACTIVE            '/dev/vg1/iredmail' [60.00 GiB] inherit
      ACTIVE            '/dev/ubuntu-vg/ubuntu-lv' [29.50 GiB] inherit
    root@mail:~# 
    

    To mount the device, you can use the command

    mount /dev/ubuntu-vg/ubuntu-lv /mnt
    

    IMPORTANT: do not mount the device on the host if it is used in the guest VM as it will result in data corruption. I mounted the disk on the host machine and created some files, but it did not show in the guest. After I reboot the guest VM, it did not boot back, I had to do fsck to get the VM to work again.

    Back to LVM

  • How to Remove a logical volume

    How to Remove a logical volume

    To remove a logical volume, you can use the command

    lvremove /dev/vg-name/lv-name
    

    Example

    lvremove /dev/vg1/win10
    

    This will remove logical volume with the name “win10” from volume group “vg1”.

    [root@Alma-85-amd64-base ~]# lvremove  vg1/data1
    Do you really want to remove active logical volume vg1/data1? [y/n]: y
      Logical volume "data1" successfully removed.
    [root@Alma-85-amd64-base ~]# 
    

    back to LVM

  • Resize EC2 file system with lvm

    Resize EC2 file system with lvm

    On an EC2 server with an LVM file system, I need to increase the size of the / partition. First, increase the size of the volume in the Amazon AWS console as per Resize Amazon EC2 Boot Disk, once volume size is increased, you need to resize your filesystem.

    [root@sok ~]# df -h
    Filesystem           Size  Used Avail Use% Mounted on
    devtmpfs             3.8G     0  3.8G   0% /dev
    tmpfs                3.8G     0  3.8G   0% /dev/shm
    tmpfs                3.8G   17M  3.8G   1% /run
    tmpfs                3.8G     0  3.8G   0% /sys/fs/cgroup
    /dev/mapper/cl-root   38G   33G  5.1G  87% /
    /dev/nvme0n1p1      1014M  412M  603M  41% /boot
    tmpfs                775M     0  775M   0% /run/user/0
    [root@sok ~]# 
    

    Here is result of parted -l

    [root@sok ~]# parted -l
    Model: Linux device-mapper (linear) (dm)
    Disk /dev/mapper/cl-swap: 1074MB
    Sector size (logical/physical): 512B/512B
    Partition Table: loop
    Disk Flags: 
    
    Number  Start  End     Size    File system     Flags
     1      0.00B  1074MB  1074MB  linux-swap(v1)
    
    
    Model: Linux device-mapper (linear) (dm)
    Disk /dev/mapper/cl-root: 40.7GB
    Sector size (logical/physical): 512B/512B
    Partition Table: loop
    Disk Flags: 
    
    Number  Start  End     Size    File system  Flags
     1      0.00B  40.7GB  40.7GB  xfs
    
    
    Model: NVMe Device (nvme)
    Disk /dev/nvme0n1: 172GB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    Disk Flags: 
    
    Number  Start   End     Size    Type     File system  Flags
     1      1049kB  1075MB  1074MB  primary  xfs          boot
     2      1075MB  10.7GB  9663MB  primary               lvm
     3      10.7GB  16.1GB  5369MB  primary               lvm
     4      16.1GB  42.9GB  26.8GB  primary               lvm
    
    
    [root@sok ~]# 
    

    /dev/nvme0n1 was resized to 172 GB, but the 4th partition was only using 26 GB.

    Resize the 4th partition with the command “growpart /dev/nvme0n1 4”

    [root@sok ~]# growpart /dev/nvme0n1 4
    CHANGED: partition=4 start=31457280 old: size=52428800 end=83886080 new: size=304087007 end=335544287
    [root@sok ~]# parted /dev/nvme0n1 print
    Model: NVMe Device (nvme)
    Disk /dev/nvme0n1: 172GB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    Disk Flags: 
    
    Number  Start   End     Size    Type     File system  Flags
     1      1049kB  1075MB  1074MB  primary  xfs          boot
     2      1075MB  10.7GB  9663MB  primary               lvm
     3      10.7GB  16.1GB  5369MB  primary               lvm
     4      16.1GB  172GB   156GB   primary               lvm
    
    [root@sok ~]#
    

    Resize physical volume with “pvresize /dev/nvme0n1p4” command.

    [root@sok ~]# pvresize /dev/nvme0n1p4
      Physical volume "/dev/nvme0n1p4" changed
      1 physical volume(s) resized or updated / 0 physical volume(s) not resized
    [root@sok ~]#
    

    Finally resize logical volume with the command “lvextend -r -l +100%FREE /dev/mapper/cl-root”. -r option resize the file system also.

    [root@sok ~]# lvextend -r -l +100%FREE /dev/mapper/cl-root
      Size of logical volume cl/root changed from 37.90 GiB (9703 extents) to <157.99 GiB (40445 extents).
      Logical volume cl/root successfully resized.
    meta-data=/dev/mapper/cl-root    isize=512    agcount=19, agsize=524032 blks
             =                       sectsz=512   attr=2, projid32bit=1
             =                       crc=1        finobt=0 spinodes=0
    data     =                       bsize=4096   blocks=9935872, imaxpct=25
             =                       sunit=0      swidth=0 blks
    naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
    log      =internal               bsize=4096   blocks=2560, version=2
             =                       sectsz=512   sunit=0 blks, lazy-count=1
    realtime =none                   extsz=4096   blocks=0, rtextents=0
    data blocks changed from 9935872 to 41415680
    [root@sok ~]# 
    

    Now the size of the / portion is increased to use full available disk space.

    [root@sok ~]# df -h
    Filesystem           Size  Used Avail Use% Mounted on
    devtmpfs             3.8G     0  3.8G   0% /dev
    tmpfs                3.8G     0  3.8G   0% /dev/shm
    tmpfs                3.8G   17M  3.8G   1% /run
    tmpfs                3.8G     0  3.8G   0% /sys/fs/cgroup
    /dev/mapper/cl-root  158G   33G  126G  21% /
    /dev/nvme0n1p1      1014M  412M  603M  41% /boot
    tmpfs                775M     0  775M   0% /run/user/0
    [root@sok ~]# 
    
  • pvdisplay

    pvdisplay is used to display all physical volumes on a LVM.

    root@server12:~# pvdisplay
      --- Physical volume ---
      PV Name               /dev/sda3
      VG Name               vg
      PV Size               <9.51 GiB / not usable 0   
      Allocatable           yes (but full)
      PE Size               4.00 MiB
      Total PE              2434
      Free PE               0
      Allocated PE          2434
      PV UUID               VA8sp6-2zKJ-GGtJ-aWe5-cPlm-uTHu-oKpiua
       
      --- Physical volume ---
      PV Name               /dev/sda4
      VG Name               vg
      PV Size               30.00 GiB / not usable 4.98 MiB
      Allocatable           yes (but full)
      PE Size               4.00 MiB
      Total PE              7679
      Free PE               0
      Allocated PE          7679
      PV UUID               LjsCxR-J0lC-5z2P-Fqew-MXoG-59X3-B7dmDP
       
      --- Physical volume ---
      PV Name               /dev/sda5
      VG Name               vg
      PV Size               <40.00 GiB / not usable 3.98 MiB
      Allocatable           yes (but full)
      PE Size               4.00 MiB
      Total PE              10239
      Free PE               0
      Allocated PE          10239
      PV UUID               km0iBF-XakC-s9GF-hFnf-K1b0-4Hec-FAEcwp
       
    root@server12:~# 
    

    See LVM

  • lvdisplay

    lvdisplay is used to display logical volumes. You can also use lvs command.

    root@server12:~# lvdisplay
      --- Logical volume ---
      LV Path                /dev/vg/lv_root
      LV Name                lv_root
      VG Name                vg
      LV UUID                aLjP9v-Cc6s-mxS6-kcKz-Fs4e-kW5c-Wuq009
      LV Write Access        read/write
      LV Creation host, time ubuntu-server, 2020-05-19 13:02:06 +0000
      LV Status              available
      # open                 1
      LV Size                78.99 GiB
      Current LE             20222
      Segments               3
      Allocation             inherit
      Read ahead sectors     auto
      - currently set to     256
      Block device           253:0
       
      --- Logical volume ---
      LV Path                /dev/vg/lv_swap
      LV Name                lv_swap
      VG Name                vg
      LV UUID                es9Yvn-Tfh1-KO6L-7cAw-N1jg-9NAL-9hd3To
      LV Write Access        read/write
      LV Creation host, time ubuntu-server, 2020-05-19 13:02:07 +0000
      LV Status              available
      # open                 2
      LV Size                520.00 MiB
      Current LE             130
      Segments               1
      Allocation             inherit
      Read ahead sectors     auto
      - currently set to     256
      Block device           253:1
       
    root@server12:~#
    

    See LVM

  • How to create Logical Volume

    To create a LVM, run

    lvcreate -L 3T -n backup vg1
    

    This command will create a logical volume with 3 TB size with name backup inside volume group vg1.

    To format it as xfs file system, run

    mkfs.xfs /dev/vg1/backup
    

    To mount it, add the following line in /etc/fstab file.

    /dev/vg1/backup /backup  xfs defaults 0 0
    

    Create LVM using all available disk space

    Create a LVM using all available disk space in Volume Group vg1

    root@ok:~# lvcreate --extents 100%FREE vg1 -n data1
      Logical volume "data1" created.
    root@ok:~# lvs
      LV    VG  Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
      data1 vg1 -wi-a----- 58.10t                                                    
    root@ok:~# 
    
    lvcreate --size 100G --type thin-pool --thinpool LV_NAME_HERE VG_NAME_HERE
    

    example

    [root@server ~]# lvcreate --size 100G --type thin-pool --thinpool thin_pool vg-storage
      Thin pool volume with chunk size 64.00 KiB can address at most 15.81 TiB of data.
      Logical volume "thin_pool" created.
    [root@server ~]#
    

    To remove the volume created, run

    [root@server ~]# lvremove vg-storage/thin_pool
    Do you really want to remove active logical volume vg-storage/thin_pool? [y/n]: y
      Logical volume "thin_pool" successfully removed
    [root@server ~]#
    

    See lvm

  • lvm

    vgcreate
    pvdisplay
    lvdisplay
    lvcreate
    Resize EC2 file system with LVM
    pvcreate Device /dev/sdb excluded by a filter
    How to Remove a logical volume
    Mounting partition stored inside Logical Volume
    How to add physical volume and resize LVM

    Some related commands

    cfdisk
    vgextend vg /dev/sda5
    lvextend -l+100%FREE /dev/vg/lv_root
    resize2fs /dev/mapper/vg-lv_root
    

    When I try to mount a partition I get error

    mount: unknown filesystem type ‘LVM2_member’

    This is because the partition is LVM. Got it fixed with

    root@sysresccd /root % parted -l
    Model: ATA Samsung SSD 850 (scsi)
    Disk /dev/sda: 500GB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    Disk Flags:
    
    Number  Start   End     Size    Type     File system  Flags
     1      1049kB  3146kB  2097kB  primary
     2      3146kB  527MB   524MB   primary  ext2         boot
     3      527MB   500GB   500GB   primary               lvm
    
    
    root@sysresccd /root % mount /dev/sda3 /mnt
    mount: unknown filesystem type 'LVM2_member'
    root@sysresccd /root % lvmdiskscan
      WARNING: lvmetad is running but disabled. Restart lvmetad before enabling it!
      /dev/loop0          [     275.14 MiB]
      /dev/mapper/vg-swap [      15.75 GiB]
      /dev/sda1           [       2.00 MiB]
      /dev/mapper/vg-tmp  [       1.00 GiB]
      /dev/sda2           [     500.00 MiB]
      /dev/mapper/vg-root [     448.46 GiB]
      /dev/sda3           [     465.27 GiB] LVM physical volume
      3 disks
      3 partitions
      0 LVM physical volume whole disks
      1 LVM physical volume
    root@sysresccd /root % lvscan
      WARNING: lvmetad is running but disabled. Restart lvmetad before enabling it!
      ACTIVE            '/dev/vg/swap' [15.75 GiB] inherit
      ACTIVE            '/dev/vg/tmp' [1.00 GiB] inherit
      ACTIVE            '/dev/vg/root' [448.46 GiB] inherit
    root@sysresccd /root % mount /dev/vg/root /mnt
    root@sysresccd /root %