Albert Lacambra BasilAlbert Lacambra Basil

Structure of volumes and filesystems

volumes and filesystems

Devices like SDA, SDB, …​ are partitioned creating physical partition.

physical partition can be added to a volume group

Note

The Volume Group is the highest level abstraction used within the LVM. It gathers together a collection of Logical Volumes and Physical Volumes into one administrative unit.

A Volume Group will have assigned server logical volumes and each logical volume will contain a file system

Commands

fdisk

fdisk - manipulate disk partition table fdisk (in the first form of invocation) is a menu-driven program for creation and manipulation of partition tables. It understands GPT (experimental for now), MBR, Sun, SGI and BSD partition tables.

fdsik /dev/sda
# (option n -> create)

-l options list all partitions

/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200  1953523711   975712256   8e  Linux LVM

partprobe

partprobe is a program that informs the operating system kernel of partition table changes, by requesting that the operating system re-read the partition table.

partprobe /dev/sda

pvcreate

pvcreate initializes a physical volume so that it is recognized as belonging to LVM, and allows the physical volume to be used in a VG. A physical volume can be a disk partition, whole disk, meta device, or loopback file.

pvcreate /dev/sdaX

pvs

pvs - Display information about physical volumes

[xxx ~]# pvs
PV         VG     Fmt  Attr PSize    PFree
/dev/sda2  centos lvm2 a--  <930.51g <689.20g

vgs

vgs - Display information about volume groups

[XXX ~]# vgs
VG     #PV #LV #SN Attr   VSize    VFree
centos   1   4   0 wz--n- <930.51g <689.20g

vgcreate

vgcreate creates a new volume group on block devices. If the devices were not previously intialized as physical volumes with pvcreate(8), vgcreate will inititialize them, making them physical volumes. The pvcreate options for initializing devices are also available with vgcreate.

vgcreate vgdata /dev/sdaX

lvcreate

lvcreate creates a new logical volume in a volume group. For standard logical volumes, this requires allocating logical extents from the volume group's free physical extents. If there is not enough free space, the volume group can be extended with other physical volumes (vgextend(8)), or existing logical volumes can be reduced or removed (lvremove(8), lvreduce(8).)

lvcreate -n lvdata -L 1G vgdata

lvs

lvs - Display information about logical volumes

[xxx ~]# lvs
  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home centos -wi-ao----  20.00g
  root centos -wi-ao----  90.00g
  swap centos -wi-a-----  31.31g
  vm1  centos -wi-ao---- 100.00g

pvdisplay

pvdisplay shows the attributes of physical volume, like size, physical extent size, space used for the volume group descriptor area, etc.

[XXX ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sdaX
  VG Name               centos
  PV Size               930.51 GiB / not usable 4.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              238210
  Free PE               176434
  Allocated PE          61776
  PV UUID               xxxxxxx-xxxx-xxxx-xxxx-xxxx-xxxxx-xxxxxx

lvdisplay

lvdisplay shows the attributes of logical volumes, like size, read/write status, snapshot information, etc.

[XXX ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                xxxxxxx-xxxx-xxxx-xxxx-xxxx-xxxxx-xxxxxx
  LV Write Access        read/write
  LV Creation host, time host_name, 2019-09-22 16:46:16 +0200
  LV Status              available
  # open                 0
  LV Size                31.31 GiB
  Current LE             8016
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                xxxxxxx-xxxx-xxxx-xxxx-xxxx-xxxxx-xxxxxx
  LV Write Access        read/write
  LV Creation host, time host_name, 2019-09-22 16:46:19 +0200
  LV Status              available
  # open                 1
  LV Size                90.00 GiB
  Current LE             23040
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

....
....
....

lvresize

lvresize resizes an logical volume in the same way as lvextend and lvreduce. See lvextend(8) and lvreduce(8) for more information.

lvremove

lvremove removes one or more LVs. For standard LVs, this returns the logical extents that were used by the LV to the VG for use by other LVs.

xfs_growfs

xfs_growfs expands an existing XFS filesystem (see xfs(5)). The mount-point argument is the pathname of the directory where the filesystem is mounted. The filesystem must be mounted to be grown (see mount(8)). The existing contents of the filesystem are undisturbed, and the added space becomes available for additional file storage.

xfsdump

xfsdump backs up files and their attributes in a filesystem. The files are dumped to storage media, a regular file, or standard output. Options allow the operator to have all files dumped, just files that have changed since a previous dump, or just files contained in a list of pathnames.

xfsrestore

xfsrestore restores filesystems from dumps produced by xfsdump(8).

mkfs.XXX

mkfs is used to build a Linux filesystem on a device, usually a hard disk partition. The device argument is either the device name (e.g. /dev/hda1, /dev/sdb2), or a regular file that shall contain the filesystem. The size argument is the number of blocks to be used for the filesystem.

  • mkfs

  • mkfs.btrfs

  • mkfs.cramfs

  • mkfs.ext2

  • mkfs.ext3

  • mkfs.ext4

  • mkfs.fat

  • mkfs.minix

  • mkfs.msdos

  • mkfs.vfat

  • mkfs.xfs

mkfs.xfs - construct an XFS filesystem

mkfs.xfs /dev/vgdata/lvdata

df

df displays the amount of disk space available on the file system containing each file name argument. If no file name is given, the space available on all currently mounted file systems is shown. Disk space is shown in 1K blocks by default, unless the environment variable POSIXLY_CORRECT is set, in which case 512-byte blocks are used.

[XXX ~]# df
Filesystem              1K-blocks     Used Available Use% Mounted on
devtmpfs                 32695216        0  32695216   0% /dev
tmpfs                    32707128        0  32707128   0% /dev/shm
tmpfs                    32707128    15196  32691932   1% /run
tmpfs                    32707128        0  32707128   0% /sys/fs/cgroup
/dev/mapper/centos-root  94346240 71135956  23210284  76% /
/dev/sda1                 1038336   239152    799184  24% /boot
/dev/mapper/centos-home  20961280    70960  20890320   1% /home
overlay                  94346240 71135956  23210284  76% /var/lib/docker/overlay2/6a50cb93df20ae90c6443af765cf1e7e4f657f27b25aa37c442de7f435994c4c/merged

The Device mapper

Is an interface to linux kernel that allows logical volums and other kind of volumes and software to communicate with the kernel.

Device mapper devices are identified as /dev/dm-X.

We cann identify these devices through more convinient names using the following symbolic links

/dev/{VirtualGroup}/{LogicalVolumeName}
[XXX ~]# ls -l /dev/centos/
total 0
lrwxrwxrwx. 1 root root 7 May 14 17:30 home -> ../dm-2
lrwxrwxrwx. 1 root root 7 May 14 17:30 root -> ../dm-0
lrwxrwxrwx. 1 root root 7 May 14 17:30 swap -> ../dm-1
lrwxrwxrwx. 1 root root 7 May 14 22:14 vm1 -> ../dm-3
/dev/mapper/{VirtualGroup}-{LogicalVolumeName}
[XXX ~]# ls -l  /dev/mapper/
total 0
lrwxrwxrwx. 1 root root       7 May 14 17:30 centos-home -> ../dm-2
lrwxrwxrwx. 1 root root       7 May 14 17:30 centos-root -> ../dm-0
lrwxrwxrwx. 1 root root       7 May 14 17:30 centos-swap -> ../dm-1
lrwxrwxrwx. 1 root root       7 May 14 22:14 centos-vm1 -> ../dm-3

Use cases

Create new partition

To creante new partition unallocated space need to be used. Otherwise we need to delte a partition and recreate it with less size

Create new volum group

To create a VG we need an unassigned Phisiscal partition

Otherwise following error appears:

vgcreate vgtest /dev/sda2
  Physical volume '/dev/sda2' is already in volume group 'currentVG'
  Unable to add physical volume '/dev/sda2' to volume group 'currentVG'
  /dev/sda2: physical volume not initialized.

Create new logical volume

To create and LV we need o have free space in the belonging logical group

[XXX ~]#vgs
  VG     #PV #LV #SN Attr   VSize    VFree
  centos   1   5   0 wz--n- <930.51g <688.20g

[XXX ~]#lvcreate -n lvtest -L 1G centos
  Logical volume "lvtest" created.

An lvtest LV has been created in the VG centos. Its device name is dm-4

[XXX ~]# ll /dev/centos/
total 0
lrwxrwxrwx. 1 root root 7 May 14 17:30 home -> ../dm-2
lrwxrwxrwx. 1 root root 7 May 24 13:00 lvtest -> ../dm-4
lrwxrwxrwx. 1 root root 7 May 14 17:30 root -> ../dm-0
lrwxrwxrwx. 1 root root 7 May 14 17:30 swap -> ../dm-1
lrwxrwxrwx. 1 root root 7 May 14 22:14 vm1 -> ../dm-3

Its allocated space is 1G:

[XXX ~]# lvs centos/lvtest
  LV     VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lvtest centos -wi-a----- 1.00g

Create filesystem

I will create an xfs filesystem on the new lvtest:

[XXX ~]# mkfs.xfs /dev/centos/lvtest
meta-data=/dev/centos/lvtest     isize=512    agcount=4, agsize=65536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=262144, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

now I mount it to see that it works:

[XXX ~]# mkdir /mnt/lvtest-mount-point
[XXX ~]# mount /dev/centos/lvtest /mnt/lvtest-mount-point/
[XXX ~]# touch /mnt/lvtest-mount-point/file-on-mounted-lvtest
[XXX ~]# ls -l /mnt/lvtest-mount-point/
  total 0
  -rw-r--r--. 1 root root 0 May 24 13:11 file-on-mounted-lvtest
[XXX ~]# umount /mnt/lvtest-mount-point
[XXX ~]# ls -l /mnt/lvtest-mount-point/
  total 0
[XXX ~]# mount /dev/centos/lvtest /mnt/lvtest-mount-point/
[XXX ~]# ls -l /mnt/lvtest-mount-point/
  total 0
  -rw-r--r--. 1 root root 0 May 24 13:11 file-on-mounted-lvtest
[XXX ~]# df -h |grep lvtest
/dev/mapper/centos-lvtest 1014M   33M  982M   4% /mnt/lvtest-mount-point

We see also the current fs allocated space.

Extend filsesytem size

To add more space to the file system we need to extend the logical partition:

[XXX ~]# lvresize -L +1g /dev/centos/lvtest
  Size of logical volume centos/lvtest changed from 2.00 GiB (512 extents) to 3.00 GiB (768 extents).
  Logical volume centos/lvtest successfully resized.

The filesystem has not yet taken the new size:

[XXX ~]# df -h |grep lvtest
/dev/mapper/centos-lvtest  987K   29K  816K   4% /mnt/lvtest-mount-point

so we need to extend the filesystem:

[XXX ~]# xfs_growfs /mnt/lvtest-mount-point/
meta-data=/dev/mapper/centos-lvtest isize=512    agcount=4, agsize=131072 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=524288, 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

[XXX ~]# df -h |grep lvtest
/dev/mapper/centos-lvtest  3.0G   33M  3.0G   2% /mnt/lvtest-mount-point

Alternatively, it is also possible to resize the filesystem with the partition using the parameter -r

[XXX ~]# lvresize -r -L +1g /dev/centos/lvtest
  Size of logical volume centos/lvtest changed from 3.00 GiB (768 extents) to 4.00 GiB (1024 extents).
  Logical volume centos/lvtest successfully resized.
meta-data=/dev/mapper/centos-lvtest isize=512    agcount=6, agsize=131072 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=786432, 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 786432 to 1048576


[XXX ~]# df -h |grep lvtest
/dev/mapper/centos-lvtest  4.0G   33M  4.0G   1% /mnt/lvtest-mount-point

Remove logical partition

lvremove /dev/centos/home

Backup and restore filesystem

xfsdump -l 0 -f /home-image /dev/mapper/centos-home

xfsrestore -f /home-image /home

Add new device. Create partition, PV, VG, LV and filesystem

  1. Create partition: fdisk /dev/sda

    • option n: add a new partition

    • option t: change a partition’s system id and select type 8e Linux LVM

    • option w: write table to disk and exit

  2. Create phisical volume: vgcreate --clustered n ${VOLUME_GROUP_NAME} /dev/sda1

  3. Create volume group: vgcreate --clustered n ${VOLUME_GROUP_NAME} /dev/sda1

    • Non clustered since only local devices are being used

  4. Create a 50G logical volume: lvcreate -n ${LOGICAL_VOLUME_NAME} -L 50G ${VOLUME_GROUP_NAME}

  5. Create XFS file system: mkfs.xfs /dev/${VOLUME_GROUP_NAME}/${LOGICAL_VOLUME_NAME}

  6. Give 50G to file system: lvresize -r -L +50G /dev/${VOLUME_GROUP_NAME}/${LOGICAL_VOLUME_NAME}