User Tools

Site Tools


tech:server:linux:lvm:lvm

LVM

LVM stands for Logical Volume Manager for Linux.

Extend Volume and ext4 Filesystem

In this case, assume the target volume contains ext4 root filesystem.

So, We extend Logical Volume First. Next extend ext4 filesystem.

root@hotaru:~# df -h
Filesystem                      Size  Used Avail Use% Mounted on
udev                            7.8G     0  7.8G   0% /dev
tmpfs                           1.6G  920K  1.6G   1% /run
/dev/mapper/hotaru_system-root   73G   37G   33G  53% /
tmpfs                           7.8G     0  7.8G   0% /dev/shm
tmpfs                           5.0M     0  5.0M   0% /run/lock
tmpfs                           7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/sda2                       923M  214M  646M  25% /boot
/dev/sda1                       952M  6.1M  946M   1% /boot/efi
tmpfs                           1.6G     0  1.6G   0% /run/user/1000
root@hotaru:~# vgdisplay hotaru_system
  --- Volume group ---
  VG Name               hotaru_system
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <236.61 GiB
  PE Size               4.00 MiB
  Total PE              60572
  Alloc PE / Size       28609 / 111.75 GiB
  Free  PE / Size       31963 / <124.86 GiB
  VG UUID               0ZsSS3-SUao-RVhd-KyKV-pSMl-NPRL-OfCPUy
root@hotaru:~# lvdisplay hotaru_system
  --- Logical volume ---
  LV Path                /dev/hotaru_system/root
  LV Name                root
  VG Name                hotaru_system
  LV UUID                IGzl8K-uJLC-yaaF-IUAz-4PHP-rO4r-KbQ5rH
  LV Write Access        read/write
  LV Creation host, time hotaru, 2019-08-13 16:07:52 +0900
  LV Status              available
  # open                 1
  LV Size                74.50 GiB
  Current LE             19073
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

  --- Logical volume ---
  LV Path                /dev/hotaru_system/swap
  LV Name                swap
  VG Name                hotaru_system
  LV UUID                6r82gv-4bBd-YIdW-LMdZ-Cn1E-9ans-HyJbKQ
  LV Write Access        read/write
  LV Creation host, time hotaru, 2019-08-13 16:08:11 +0900
  LV Status              available
  # open                 2
  LV Size                37.25 GiB
  Current LE             9536
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
sudo lvextend -l +100%FREE /dev/hotaru_system/root
root@hotaru:~# sudo lvextend -l +100%FREE /dev/hotaru_system/root
  Size of logical volume hotaru_system/root changed from 74.50 GiB (19073 extents) to <199.36 GiB (51036 extents).
  Logical volume hotaru_system/root successfully resized.
root@hotaru:~# vgdisplay hotaru_system
  --- Volume group ---
  VG Name               hotaru_system
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <236.61 GiB
  PE Size               4.00 MiB
  Total PE              60572
  Alloc PE / Size       60572 / <236.61 GiB
  Free  PE / Size       0 / 0
  VG UUID               0ZsSS3-SUao-RVhd-KyKV-pSMl-NPRL-OfCPUy
root@hotaru:~# lvdisplay hotaru_system/root
  --- Logical volume ---
  LV Path                /dev/hotaru_system/root
  LV Name                root
  VG Name                hotaru_system
  LV UUID                IGzl8K-uJLC-yaaF-IUAz-4PHP-rO4r-KbQ5rH
  LV Write Access        read/write
  LV Creation host, time hotaru, 2019-08-13 16:07:52 +0900
  LV Status              available
  # open                 1
  LV Size                <199.36 GiB
  Current LE             51036
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
sudo resize2fs /dev/hotaru_system/root
root@hotaru:~# sudo resize2fs /dev/hotaru_system/root
resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/hotaru_system/root is mounted on /; on-line resizing required
old_desc_blocks = 10, new_desc_blocks = 25
The filesystem on /dev/hotaru_system/root is now 52260864 (4k) blocks long.

root@hotaru:~# df -h
Filesystem                      Size  Used Avail Use% Mounted on
udev                            7.8G     0  7.8G   0% /dev
tmpfs                           1.6G  920K  1.6G   1% /run
/dev/mapper/hotaru_system-root  196G   37G  151G  20% /
tmpfs                           7.8G     0  7.8G   0% /dev/shm
tmpfs                           5.0M     0  5.0M   0% /run/lock
tmpfs                           7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/sda2                       923M  214M  646M  25% /boot
/dev/sda1                       952M  6.1M  946M   1% /boot/efi
tmpfs                           1.6G     0  1.6G   0% /run/user/1000

LVM Thin Provisioning

root@hotaru:~# vgdisplay hotaru_data
  --- Volume group ---
  VG Name               hotaru_data
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  10
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                4
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <232.88 GiB
  PE Size               32.00 MiB
  Total PE              7452
  Alloc PE / Size       7432 / 232.25 GiB
  Free  PE / Size       20 / 640.00 MiB
  VG UUID               z2DNUN-WOZi-2bsc-a4Bz-9lX9-0PYC-Tx07fu

root@hotaru:~# lvdisplay hotaru_data/thinpool
  --- Logical volume ---
  LV Name                thinpool
  VG Name                hotaru_data
  LV UUID                mRjVYM-9clP-ITxr-sQ7q-yac6-04Ut-TNMsXd
  LV Write Access        read/write
  LV Creation host, time hotaru, 2020-01-05 15:26:05 +0900
  LV Pool metadata       thinpool_tmeta
  LV Pool data           thinpool_tdata
  LV Status              available
  # open                 4
  LV Size                232.00 GiB
  Allocated pool data    0.00%
  Allocated metadata     10.39%
  Current LE             7424
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:4

root@hotaru:~# lvdisplay hotaru_data
  --- Logical volume ---
  LV Name                thinpool
  VG Name                hotaru_data
  LV UUID                mRjVYM-9clP-ITxr-sQ7q-yac6-04Ut-TNMsXd
  LV Write Access        read/write
  LV Creation host, time hotaru, 2020-01-05 15:26:05 +0900
  LV Pool metadata       thinpool_tmeta
  LV Pool data           thinpool_tdata
  LV Status              available
  # open                 4
  LV Size                232.00 GiB
  Allocated pool data    0.00%
  Allocated metadata     10.39%
  Current LE             7424
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:4

  --- Logical volume ---
  LV Path                /dev/hotaru_data/vol-gitlab1.example.com-data
  LV Name                vol-gitlab1.example.com-data
  VG Name                hotaru_data
  LV UUID                JhLxkJ-N1dw-qbxE-APxn-wOAu-amoj-bc2pzL
  LV Write Access        read/write
  LV Creation host, time hotaru, 2020-01-05 15:29:49 +0900
  LV Pool name           thinpool
  LV Status              available
  # open                 0
  LV Size                80.00 GiB
  Mapped size            0.00%
  Current LE             2560
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:6

  --- Logical volume ---
  LV Path                /dev/hotaru_data/vol-mx1.example.com-data
  LV Name                vol-mx1.example.com-data
  VG Name                hotaru_data
  LV UUID                JpEuJ5-b4dq-Do5I-apg6-LhVA-kPBd-fBmYLI
  LV Write Access        read/write
  LV Creation host, time hotaru, 2020-01-05 15:30:12 +0900
  LV Pool name           thinpool
  LV Status              available
  # open                 0
  LV Size                40.00 GiB
  Mapped size            0.00%
  Current LE             1280
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:7

  --- Logical volume ---
  LV Path                /dev/hotaru_data/vol-nextcloud1.example.com-data
  LV Name                vol-nextcloud1.example.com-data
  VG Name                hotaru_data
  LV UUID                lWf4gM-83sB-aN2r-OmPX-T0If-ng3k-6so4t8
  LV Write Access        read/write
  LV Creation host, time hotaru, 2020-01-05 15:31:59 +0900
  LV Pool name           thinpool
  LV Status              available
  # open                 0
  LV Size                80.00 GiB
  Mapped size            0.00%
  Current LE             2560
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:8

root@hotaru:~# lvs
  LV                           VG            Attr       LSize    Pool     Origin Data%  Meta%  Move Log Cpy%Sync Convert
  thinpool                     hotaru_data   twi-aotz--  232.00g                 0.00   10.39
  vol-gitlab1.example.com-data    hotaru_data   Vwi-a-tz--   80.00g thinpool        0.00
  vol-mx1.example.com-data        hotaru_data   Vwi-a-tz--   40.00g thinpool        0.00
  vol-nextcloud1.example.com-data hotaru_data   Vwi-a-tz--   80.00g thinpool        0.00
  root                         hotaru_system -wi-ao---- <199.36g
  swap                         hotaru_system -wi-ao----   37.25g

create snapshot

sudo lvcreate -s -n snap-nextcloud1.example.com-data-202001062100-initial-snapshot hotaru_data/vol-nextcloud1.example.com-data
sudo lvcreate -s -n snap-mx1.example.com-data-202001062100-initial-snapshot hotaru_data/vol-mx1.example.com-data
root@hotaru:~# sudo lvcreate -s -n snap-nextcloud1.example.com-data-202001062100-initial-snapshot hotaru_data/vol-nextcloud1.example.com-data
  Using default stripesize 64.00 KiB.
  WARNING: Sum of all thin volume sizes (280.00 GiB) exceeds the size of thin pool hotaru_data/thinpool and the size of whole volume group (<232.88 GiB).
  WARNING: You have not turned on protection against thin pools running out of space.
  WARNING: Set activation/thin_pool_autoextend_threshold below 100 to trigger automatic extension of thin pools before they get full.
  Logical volume "snap-nextcloud1.example.com-data-202001062100-initial-snapshot" created.
root@hotaru:~# sudo lvcreate -s -n snap-mx1.example.com-data-202001062100-initial-snapshot hotaru_data/vol-mx1.example.com-data
  Using default stripesize 64.00 KiB.
  WARNING: Sum of all thin volume sizes (320.00 GiB) exceeds the size of thin pool hotaru_data/thinpool and the size of whole volume group (<232.88 GiB).
  WARNING: You have not turned on protection against thin pools running out of space.
  WARNING: Set activation/thin_pool_autoextend_threshold below 100 to trigger automatic extension of thin pools before they get full.
  Logical volume "snap-mx1.example.com-data-202001062100-initial-snapshot" created.
root@hotaru:~# lvs
  LV                                                             VG            Attr       LSize    Pool     Origin                          Data%  Meta%  Move Log Cpy%Sync Convert
  snap-mx1.example.com-data-202001062100-initial-snapshot        hotaru_data   Vwi---tz-k   40.00g thinpool vol-mx1.example.com-data
  snap-nextcloud1.example.com-data-202001062100-initial-snapshot hotaru_data   Vwi---tz-k   80.00g thinpool vol-nextcloud1.example.com-data
  thinpool                                                       hotaru_data   twi-aotz--  232.00g                                          0.00   10.39
  vol-gitlab1.example.com-data                                   hotaru_data   Vwi-a-tz--   80.00g thinpool                                 0.00
  vol-mx1.example.com-data                                       hotaru_data   Vwi-a-tz--   40.00g thinpool                                 0.00
  vol-nextcloud1.example.com-data                                hotaru_data   Vwi-a-tz--   80.00g thinpool                                 0.00
  root                                                           hotaru_system -wi-ao---- <199.36g
  swap                                                           hotaru_system -wi-ao----   37.25g
  • Revert to snapshot
The snapshot will be removed when reverted to original volume!! Create snapshot again if you keep the reverted snapshot volume data.

quote of man page

--merge
       An alias for --mergethin, --mergemirrors, or --mergesnapshot, depending on the type of LV.

--mergesnapshot
       Merge COW snapshot LV into its origin.  When merging a snapshot, if both the origin and snapshot LVs are not open, the merge will start immediately. Other‐
       wise, the merge will start the first time either the origin or snapshot LV are activated and both are closed. Merging a snapshot into an origin that cannot
       be closed, for example a root filesystem, is deferred until the next time the origin volume is activated. When merging starts, the resulting LV will have
       the origin's name, minor number and UUID. While the merge is in progress, reads or writes to the origin appear as being directed to the snapshot being
       merged. When the merge finishes, the merged snapshot is removed.  Multiple snapshots may be specified on the command line or a @tag may be used to specify
       multiple snapshots be merged to their respective origin.

--mergethin
       Merge thin LV into its origin LV.  The origin thin LV takes the content of the thin snapshot, and the thin snapshot LV is removed.  See lvmthin(7) for more
       information.
root@hotaru:/var/lib/libvirt/images# lvs
  LV                                                          VG            Attr       LSize    Pool     Origin                             Data%  Meta%  Move Log Cpy%Sync Convert
  snap-mx1.example.com-data-202001062100-initial-snapshot        hotaru_data   Vwi---tz-k   40.00g thinpool vol-mx1.example.com-data
  snap-nextcloud1.example.com-data-202001062100-initial-snapshot hotaru_data   Vwi---tz-k   80.00g thinpool vol-nextcloud1.example.com-data
  thinpool                                                    hotaru_data   twi-aotz--  232.00g                                             0.03   10.41
  vol-gitlab1.example.com-data                                   hotaru_data   Vwi-a-tz--   80.00g thinpool                                 0.00
  vol-mx1.example.com-data                                       hotaru_data   Vwi-a-tz--   40.00g thinpool                                 0.00
  vol-nextcloud1.example.com-data                                hotaru_data   Vwi-a-tz--   80.00g thinpool                                 0.10
  root                                                        hotaru_system -wi-ao---- <199.36g
  swap                                                        hotaru_system -wi-ao----   37.25g
sudo lvconvert --merge /dev/hotaru_data/snap-nextcloud1.example.com-data-202001062100-initial-snapshot
root@hotaru:/var/lib/libvirt/images# sudo lvconvert --merge /dev/hotaru_data/snap-nextcloud1.example.com-data-202001062100-initial-snapshot
  Volume hotaru_data/snap-nextcloud1.example.com-data-202001062100-initial-snapshot replaced origin hotaru_data/vol-nextcloud1.example.com-data.

cehck LV status.

root@hotaru:/var/lib/libvirt/images# lvs
  LV                                                   VG            Attr       LSize    Pool     Origin                      Data%  Meta%  Move Log Cpy%Sync Convert
  snap-mx1.example.com-data-202001062100-initial-snapshot hotaru_data   Vwi---tz-k   40.00g thinpool vol-mx1.example.com-data
  thinpool                                             hotaru_data   twi-aotz--  232.00g                                      0.00   10.39
  vol-gitlab1.example.com-data                            hotaru_data   Vwi-a-tz--   80.00g thinpool                          0.00
  vol-mx1.example.com-data                                hotaru_data   Vwi-a-tz--   40.00g thinpool                          0.00
  vol-nextcloud1.example.com-data                         hotaru_data   Vwi-a-tz--   80.00g thinpool                          0.00
  root                                                 hotaru_system -wi-ao---- <199.36g
  swap                                                 hotaru_system -wi-ao----   37.25g

original LV reverted and the reverted source snpapshot removed.

create snapshot again now.

sudo lvcreate -s -n snap-nextcloud1.example.com-data-202001062100-initial-snapshot hotaru_data/vol-nextcloud1.example.com-data
root@hotaru:/var/lib/libvirt/images# sudo lvcreate -s -n snap-nextcloud1.example.com-data-202001062100-initial-snapshot hotaru_data/vol-nextcloud1.example.com-data
  Using default stripesize 64.00 KiB.
  WARNING: Sum of all thin volume sizes (320.00 GiB) exceeds the size of thin pool hotaru_data/thinpool and the size of whole volume group (<232.88 GiB).
  WARNING: You have not turned on protection against thin pools running out of space.
  WARNING: Set activation/thin_pool_autoextend_threshold below 100 to trigger automatic extension of thin pools before they get full.
  Logical volume "snap-nextcloud1.example.com-data-202001062100-initial-snapshot" created.
root@hotaru:/var/lib/libvirt/images# lvs
  LV                                                          VG            Attr       LSize    Pool     Origin                             Data%  Meta%  Move Log Cpy%Sync Convert
  snap-mx1.example.com-data-202001062100-initial-snapshot        hotaru_data   Vwi---tz-k   40.00g thinpool vol-mx1.example.com-data
  snap-nextcloud1.example.com-data-202001062100-initial-snapshot hotaru_data   Vwi---tz-k   80.00g thinpool vol-nextcloud1.example.com-data
  thinpool                                                    hotaru_data   twi-aotz--  232.00g                                             0.00   10.39
  vol-gitlab1.example.com-data                                   hotaru_data   Vwi-a-tz--   80.00g thinpool                                 0.00
  vol-mx1.example.com-data                                       hotaru_data   Vwi-a-tz--   40.00g thinpool                                 0.00
  vol-nextcloud1.example.com-data                                hotaru_data   Vwi-a-tz--   80.00g thinpool                                 0.00
  root                                                        hotaru_system -wi-ao---- <199.36g
  swap                                                        hotaru_system -wi-ao----   37.25g

Snapshot removal

simply use lvremove.

root@hotaru:/var/lib/libvirt/images# lvs
  LV                                                                 VG            Attr       LSize    Pool     Origin                              Data%  Meta%  Move Log Cpy%Sync Convert
  snap-mx1.example.com-data-202001062100-initial-snapshot               hotaru_data   Vwi---tz-k   40.00g thinpool vol-mx1.example.com-data
  snap-nextcloud1.example.com-data-202001062100-initial-snapshot        hotaru_data   Vwi---tz-k   80.00g thinpool vol-nextcloud1.example.com-data
  snap-nextcloud1.example.com-data-202001121134-nextcloud-installed     hotaru_data   Vwi---tz-k   80.00g thinpool vol-nextcloud1.example.com-data
  snap-nextcloud1.example.com-data-202002011558-nextcloud-before-update hotaru_data   Vwi---tz-k   80.00g thinpool vol-nextcloud1.example.com-data
  thinpool                                                              hotaru_data   twi-aotz--  232.00g                                          13.67  16.68
  vol-gitlab1.example.com-data                                          hotaru_data   Vwi-a-tz--   80.00g thinpool                                 0.00
  vol-mx1.example.com-data                                              hotaru_data   Vwi-aotz--   40.00g thinpool                                 0.00
  vol-nextcloud1.example.com-data                                       hotaru_data   Vwi-a-tz--   80.00g thinpool                                 39.63
  root                                                                  hotaru_system -wi-ao---- <199.36g
  swap                                                                  hotaru_system -wi-ao----   37.25g
root@hotaru:/var/lib/libvirt/images# lvremove hotaru_data/snap-nextcloud1.example.com-data-202001062100-initial-snapshot
Do you really want to remove and DISCARD logical volume hotaru_data/snap-nextcloud1.example.com-data-202001062100-initial-snapshot? [y/n]: y
  Logical volume "snap-nextcloud1.example.com-data-202001062100-initial-snapshot" successfully removed
root@hotaru:/var/lib/libvirt/images# lvremove hotaru_data/snap-nextcloud1.example.com-data-202001121134-nextcloud-installed
Do you really want to remove and DISCARD logical volume hotaru_data/snap-nextcloud1.example.com-data-202001121134-nextcloud-installed? [y/n]: y
  Logical volume "snap-nextcloud1.example.com-data-202001121134-nextcloud-installed" successfully removed
root@hotaru:/var/lib/libvirt/images# lvs
  LV                                                                 VG            Attr       LSize    Pool     Origin                             Data%  Meta%  Move Log Cpy%Sync Convert
  snap-mx1.example.com-data-202001062100-initial-snapshot               hotaru_data   Vwi---tz-k   40.00g thinpool vol-mx1.example.com-data
  snap-nextcloud1.example.com-data-202002011558-nextcloud-before-update hotaru_data   Vwi---tz-k   80.00g thinpool vol-nextcloud1.example.com-data
  thinpool                                                              hotaru_data   twi-aotz--  232.00g                                          13.67  16.67
  vol-gitlab1.example.com-data                                          hotaru_data   Vwi-a-tz--   80.00g thinpool                                 0.00
  vol-mx1.example.com-data                                              hotaru_data   Vwi-aotz--   40.00g thinpool                                 0.00
  vol-nextcloud1.example.com-data                                       hotaru_data   Vwi-a-tz--   80.00g thinpool                                 39.63
  root                                                                  hotaru_system -wi-ao---- <199.36g
  swap                                                                  hotaru_system -wi-ao----   37.25g

References

tech/server/linux/lvm/lvm.txt · Last modified: 2020/02/01 16:05 by wnoguchi