LVM and File System

Page content

How to think Linux volume system

Here is the coprehensive image from Wikipedia.

image alt text
From Wikipedia

  • There are several physical volumes (PVs) in a server.
  • Linux group the physical volumes in a volume group (a VG).
  • Each of PVs contains physical partitions (PPs).
  • We can aggregate the PVs in a logical volume (a LV).
  • On a LV, we can decide a file system (FS).
  • From Linux side, we can mount a Linux directory to the FS. The directory is called a mounting point (MP).

File system

This YouTube video made me happy :)

  • When peoples talk about “file system”, one the one hand they intend “Linux Filesystem Hierarchy Structure (FHS)”, and on the other hand they intend “file storage device structure.” I learned about the later in this video.
  • We should decide one file system on a partition.

ext4 file system structure

  • The first “block” of the partition is called super block.
  • There is a block called “(block) bitmap block.” This block
    • A block, group of sectors, is an unit of storage in ext4. The size is 1KiB ~ 64KiB.
    • The number of sectors in a block is 2^n.
    • One bit represents the usage status of one data block.

Inode

  • In ext4 partition, there is a block called “inode table”.
  • Inode is the data structure without name and actual data (like a pointer).
  • Directory is a file with two fields, one is file name and the other is inode number.
  • The root / is anonimous master inode (first inode) in ext4.
  • Inode contains timestamp, owner, permission, and links to data.
    • ls -li shows inode information.
  • There are two types of link, direct link and indirect link, a.k.a., hard link and symbolic link. “Direct” means “direct pointer to the actual data in data block.”
  • stat {{ file }} commands returns human readable information about the inode.
  • rm command deletes the links.

This video was also good (from Udacity):

https://youtu.be/tMVj22EWg6A

Good for review quickly.

See super block:

$ sudo dumpe2fs /dev/sda1 | head -n 30
dumpe2fs 1.44.1 (24-Mar-2018)
Filesystem volume name:   <none>
Last mounted on:          /boot
Filesystem UUID:          0f642e82-73d9-48d2-b83f-913d843399db
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      ext_attr resize_inode dir_index filetype sparse_super large_file
Filesystem flags:         signed_directory_hash
Default mount options:    user_xattr acl
Filesystem state:         not clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              121920
Block count:              487424
Reserved block count:     24371
Free blocks:              46284
Free inodes:              121595
First block:              1
Block size:               1024
Fragment size:            1024
Reserved GDT blocks:      256
Blocks per group:         8192
Fragments per group:      8192
Inodes per group:         2032
Inode blocks per group:   254
Filesystem created:       Fri Aug 18 04:01:26 2017
Last mount time:          Thu Apr 15 22:06:31 2021
Last write time:          Thu Apr 15 22:06:31 2021
Mount count:              13
Maximum mount count:      -1
Last checked:             Thu Feb  6 19:58:56 2020

$ sudo dumpe2fs /dev/mapper/vg0-root | head -n 30
dumpe2fs 1.44.1 (24-Mar-2018)
Filesystem volume name:   <none>
Last mounted on:          /
Filesystem UUID:          7551d7c4-f4fa-46f4-af5c-c3a8ab054a13
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery extent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
Filesystem flags:         signed_directory_hash
Default mount options:    user_xattr acl
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              1250928
Block count:              4999168
Reserved block count:     249958
Free blocks:              1192695
Free inodes:              304342
First block:              0
Block size:               4096
Fragment size:            4096
Reserved GDT blocks:      1022
Blocks per group:         32768
Fragments per group:      32768
Inodes per group:         8176
Inode blocks per group:   511
Flex block group size:    16
Filesystem created:       Fri Aug 18 04:01:26 2017
Last mount time:          Thu Apr 15 22:06:23 2021
Last write time:          Thu Apr 15 22:06:20 2021
Mount count:              12
Maximum mount count:      -1

Others:

  • sync: Cache to disk.
  • fsck: Check file system. Some Linux distributions run this command at boot time automatically.
  • tune2fs: To tune ext4 file system.

Check disk partition and LVM

  • lvscan: Check logical volumes.
  • fdisk -l: Get physical disk information
  • lsblk: Lists information about all block devices in a Linux system.
  • pvscan: List all physical volumes.
  • vgscan: List all volume groups.
  • lvdisplay: Allows you to see the attributes of a logical volume like size, read/write status, snapshot information etc.
  • lvremove {{ Logical Volume }}: Delete the logical volume.

Delete partition (LVM) and assign to the other partition

  1. df -h and check mount point and lvm partition.
  2. umount {{ mount_point }}
  3. lvremove {{ Logical_Volume }}
  4. vim /etc/fstab and delete the corresponding line.
  5. lvextend -l +100%FREE {{ Logical_Volume_you_want_to_extend }}
  6. resize2fs {{ Logical_Volume_you_want_to_extend }}

Shrink the partition

Rezie the filesystem first, and shrink the logical volume. Otherwise your Linux couldn’t boot correctly, should do trouble shooting with rescue mode.

# Unmount the partition
umount /dev/vg/my-lvm

# Check the filesystem
e2fsck /dev/vg/my-lvm

# Resize file system to 100GB
resize2fs /dev/vg/my-lvm 100GB

# Reduce the LVM to 10GB
lvreduce -L 100G /dev/vg/disk-name