Linux file system and LVM

Posted by Namadoor on Mon, 17 Feb 2020 08:35:31 +0100

1, Manage file system

1. Creating an EXT4 file system

The mkfs (Make Filesystem) command is used to create a file system (format). The basic syntax format of the mkfs command is as follows:

1) mkfs application example

Format the / dev/sdb1 partition as an EXT4 file system.

         <! -- self planning zone -- >
[root@centos01 ~]# fdisk /dev/sdb   
Welcome to fdisk (util Linux 2.23.2).

Changes stay in memory until you decide to write them to disk.
Think twice before using the write command.

Command (enter m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
 Partition number (1-4, default 1):
Start sector (2048-83886079, default 2048):
The default value 2048 will be used
 Last sector, + sector or +size{K,M,G} (2048-83886079, default is 83886079): + 5G      
Partition 1 is set to Linux type and size is set to 5 GiB

Command (enter m for help): p

Disk / dev/sdb: 42.9 GB, 42949672960 bytes, 83886080 sectors
 Units = sector of 1 * 512 = 512 bytes
 Sector size (logical / physical): 512 bytes / 512 bytes
 I/O size (minimum / best): 512 bytes / 512 bytes
 Disk label type: dos
 Disk identifier: 0xd459c569

   Device boot start end blocks ID system
/dev/sdb1            2048    10487807     5242880   83  Linux

Command (enter m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Synchronizing disks.

[root @ centos01 ~] (mkfs - t EXT4 / dev/sdb1 <! -- format / dev/sdb1 as EXT4 file system -- >
[root @ centos01 ~] (mkfs. Ext4 / dev / sdb1 <! -- or execute the second method -- >
mke2fs 1.42.9 (28-Dec-2013)
File system label=
OS type: Linux
 Block size = 4096 (log=2)
Block size = 4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
 First block =0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736

Allocating group tables: complete                            
Writing inode table: done                            
Creating journal (32768 blocks): done
 Writing superblocks and filesystem accounting information: done

2. Creating a Swap file system

Use the mkswap command to create an exchange file system; the basic syntax format is as follows:

1) mkswap application example

Create the / dev/sdb2 partition as a swap partition.

[root@centos01 ~]# Mkswap / dev / sdb2 <! -- format sdb2 as a swap partition -- >
mkswap: /dev/sdb2: warning: wiping old swap signature.
//Setting swap space version 1, size = 5242876 KiB
//No label, UUID=a86ecd38-3058-4d92-9688-346cb9407915
[root@centos01 ~]# Cat / proc / meminfo | grep swaptotal <! -- View swap partition size -- >
SwapTotal:       4194300 kB
[root@centos01 ~]# Swap on / dev / sdb2 <! -- extend swap partition -- >
[root@centos01 ~]# cat /proc/meminfo | grep SwapTotal  
      <!--View extended swap Partition size-->
SwapTotal:       9437176 kB
[root@centos01 ~]# Swapoff / dev / sdb2 <! -- cancel expanding swap partition -- >
[root@centos01 ~]# cat /proc/meminfo | grep SwapTotal   
       <!--View the swap Partition size-->
SwapTotal:       4194300 kB

3. Mount and unmount file system

1) Attach hard disk partition

Mount command mounts file system and ISO image; the basic syntax format of mount is as follows:

Application examples:

[root@centos01 ~]# MKDIR / sdb1 <! -- create mount directory -- >
[root@centos01 ~]# Mount / dev/sdb1 / sdb1 / <! -- mount / dev/sdb1 partition -- >
[root@centos01 ~]# Mount <! -- View mount partition -- >
............      <!--Some parts are omitted here-->
/dev/sdb1 on /sdb1 type ext4 (rw,relatime,data=ordered) 

[root@centos01 ~]# DF - th <! -- or execute this command to view -- >
//Filesystem type capacity used% free used mount point
/dev/sda3      xfs        76G  3.6G   73G    5% /
devtmpfs       devtmpfs  474M     0  474M    0% /dev
tmpfs          tmpfs     489M     0  489M    0% /dev/shm
tmpfs          tmpfs     489M  7.0M  482M    2% /run
tmpfs          tmpfs     489M     0  489M    0% /sys/fs/cgroup
/dev/sda1      xfs       197M  136M   61M   70% /boot
tmpfs          tmpfs      98M     0   98M    0% /run/user/0
/dev/sdb1      ext4      4.8G   20M  4.6G    1% /sdb1

2) Unmount the attached hard disk partition

[root@centos01 ~]# Umount / sdb1 / <! -- unmount the mounted file system -- >
[root@centos01 ~]# DF - th <! -- verify uninstall succeeded -- >
//Filesystem type capacity used% free used mount point
/dev/sda3      xfs        76G  3.7G   73G    5% /
devtmpfs       devtmpfs  474M     0  474M    0% /dev
tmpfs          tmpfs     489M     0  489M    0% /dev/shm
tmpfs          tmpfs     489M  7.0M  482M    2% /run
tmpfs          tmpfs     489M     0  489M    0% /sys/fs/cgroup
/dev/sda1      xfs       197M  136M   61M   70% /boot
tmpfs          tmpfs      98M     0   98M    0% /run/user/0

3) Mount ISO image

[root@centos01 ~]# ls
anaconda-ks.cfg  ansible.iso  initial-setup-ks.cfg
[root@centos01 ~]# mount -o loop ansible.iso /mnt/ 
           <!--mount ISO Mirror to specified directory-->
mount: /dev/loop0 Write protected, will be mounted read-only

[root@centos01 ~]# Mount <! -- view the mounted ISO image -- >
............      <!--Some contents are omitted here-->
/root/ansible.iso on /mnt type iso9660 (ro,relatime)
[root@centos01 ~]# DF - th <! -- check if the ISO image is successfully mounted -- >
//Filesystem type capacity used% free used mount point
/dev/sda3      xfs        76G  3.7G   73G    5% /
devtmpfs       devtmpfs  474M     0  474M    0% /dev
tmpfs          tmpfs     489M     0  489M    0% /dev/shm
tmpfs          tmpfs     489M  7.0M  482M    2% /run
tmpfs          tmpfs     489M     0  489M    0% /sys/fs/cgroup
/dev/sda1      xfs       197M  136M   61M   70% /boot
tmpfs          tmpfs      98M     0   98M    0% /run/user/0
/dev/loop0     iso9660    83M   83M     0  100% /mnt

4) Unmount the mounted ISO image

[root@centos01 ~]# Umount / MNT / <! -- unmount the mounted ISO image -- >
[root@centos01 ~]# DF - th <! -- verify uninstall succeeded -- >
//Filesystem type capacity used% free used mount point
/dev/sda3      xfs        76G  3.7G   73G    5% /
devtmpfs       devtmpfs  474M     0  474M    0% /dev
tmpfs          tmpfs     489M     0  489M    0% /dev/shm
tmpfs          tmpfs     489M  7.0M  482M    2% /run
tmpfs          tmpfs     489M     0  489M    0% /sys/fs/cgroup
/dev/sda1      xfs       197M  136M   61M   70% /boot
tmpfs          tmpfs      98M     0   98M    0% /run/user/0

4. Set up automatic mount of file system

/The etc/fstab configuration file contains the file system records that need to be mounted automatically after power on. The example is as follows:

1) Auto mount hard disk partition

[root@centos01 ~]# mkfs.ext4 /dev/sdb1
         <!--take/dev/sdb1 Format partition as EXT4 file system-->
[root@centos01 ~]# VIM / etc / fstab <! -- edit boot auto mount configuration file -- >
............
/dev/sdb1                                /sdb1                    ext4    defaults        0 0         <!--Add auto mount hard disk partition and mount directory-->
[root@centos01 ~]# DF - th <! -- restart the computer to see if it is automatically mounted -- >
//Filesystem type capacity used% free used mount point
/dev/sda3      xfs        76G  3.6G   73G    5% /
devtmpfs       devtmpfs  474M     0  474M    0% /dev
tmpfs          tmpfs     489M     0  489M    0% /dev/shm
tmpfs          tmpfs     489M  7.0M  482M    2% /run
tmpfs          tmpfs     489M     0  489M    0% /sys/fs/cgroup
/dev/sdb1      ext4      4.8G   20M  4.6G    1% /sdb1
/dev/sda1      xfs       197M  136M   61M   70% /boot
tmpfs          tmpfs      98M     0   98M    0% /run/user/0
[root@centos01 ~]# cat /proc/meminfo | grep SwapTotal  
                  <!--Auto Mount swap partition-->
SwapTotal:       4194300 kB
[root@centos01 ~]# VIM / etc / fstab <! -- edit boot auto mount configuration file -- >
............
/dev/sdb1                                 /sdb1                   ext4    defaults        0 0   
/dev/sdb2                                 swap                    swap    defaults        0 0       <!--Add this trip-->
[root@centos01 ~]# cat /proc/meminfo | grep SwapTotal   
            <!--Restart the computer to verify that the mount was successful-->
SwapTotal:       9437176 kB

2) Auto mount ISO image

[root@centos01 ~]# ls
anaconda-ks.cfg  ansible.iso  initial-setup-ks.cfg
[root@centos01 ~]# Mount - O loop ansible.iso / MNT / <! -- mount ISO image -- >
mount: /dev/loop0 Write protected, will be mounted read-only
[root@centos01 ~]# Mount <! -- View mount information -- >
............
/root/ansible.iso on /mnt type iso9660 (ro,relatime)  <!--View device types-->
[root@centos01 ~]# Umount / MNT / <! -- unmount the mounted ISO image -- >
[root@centos01 ~]# VIM / etc / fstab <! -- edit boot auto mount configuration file -- >
............
/dev/sdb1                                 /sdb1                   ext4    defaults        0 0   
/dev/sdb2                                 swap                    swap    defaults        0 0
/root/ansible.iso                         /mnt                   iso9660  defaults        0 0       <!--Add this trip-->
[root@centos01 ~]# df -Th       
         <!--Restart computer verification ISO Whether the image is automatically mounted successfully-->
//Filesystem type capacity used% free used mount point
/dev/sda3      xfs        76G  3.7G   73G    5% /
devtmpfs       devtmpfs  474M     0  474M    0% /dev
tmpfs          tmpfs     489M     0  489M    0% /dev/shm
tmpfs          tmpfs     489M  7.0M  482M    2% /run
tmpfs          tmpfs     489M     0  489M    0% /sys/fs/cgroup
/dev/loop0     iso9660    83M   83M     0  100% /mnt
/dev/sdb1      ext4      4.8G   20M  4.6G    1% /sdb1
/dev/sda1      xfs       197M  136M   61M   70% /boot
tmpfs          tmpfs      98M     0   98M    0% /run/user/0

5. View disk usage

The basic syntax format of the df command is as follows:

The application examples are as follows:

[root@centos01 ~]# df -Th
//Filesystem type capacity used% free used mount point
/dev/sda3      xfs        76G  3.7G   73G    5% /
devtmpfs       devtmpfs  474M     0  474M    0% /dev
tmpfs          tmpfs     489M     0  489M    0% /dev/shm
tmpfs          tmpfs     489M  7.0M  482M    2% /run
tmpfs          tmpfs     489M     0  489M    0% /sys/fs/cgroup
/dev/sda1      xfs       197M  136M   61M   70% /boot
tmpfs          tmpfs      98M     0   98M    0% /run/user/0

2, Manage LVM logical volumes

1. LVM overview

Logical Volume Manager, logical volume management.

1) LVM action

Adjust the disk capacity dynamically to improve the flexibility of disk management. Note: the / boot partition is used to store boot files and cannot be created based on LVM.

2) GUI management tools

system-config-lvm

2. The basic concept of LVM mechanism

PV (Physical Volume): the whole hard disk, or common partition established by fdisk and other tools; including many PE (physical extension, basic unit) with default 4MB size.
VG (Volume Group): a combination of one or more physical volumes.
LV (Logical Volume): a piece of space separated from a volume group, used to establish a file system.

3. LVM management command

1) Common LVM management commands

2) Main commands for managing LVM

4. LVM application case

1) Add two hard disks by yourself, use fdisk command to plan two partitions, and set the type to "8e"

[root@centos01 ~]# fdisk -l /dev/sdb /dev/sdc | grep "LVM"
/dev/sdb1            2048    83886079    41942016   8e  Linux LVM
/dev/sdc1            2048    83886079    41942016   8e  Linux LVM

2) Create physical volume pvcreate

[root@centos01 ~]# pvcreate /dev/sdb1 /dev/sdc1
  Physical volume "/dev/sdb1" successfully created.
  Physical volume "/dev/sdc1" successfully created.

3) Create volume group vgcreate

[root@centos01 ~]# vgcreate mail_store /dev/sdb1 /dev/sdc1
  Volume group "mail_store" successfully created

4) Create logical volume lvcreate

[root@centos01 ~]# lvcreate -L 20G -n mbox mail_store
  Logical volume "mbox" created.
[root@centos01 ~]# mkfs -t ext4 /dev/mail_store/mbox
mke2fs 1.42.9 (28-Dec-2013)
//File system label=
OS type: Linux
//Block size = 4096 (log=2)
//Block size =4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes, 5242880 blocks
262144 blocks (5.00%) reserved for the super user
//First block = 0
Maximum filesystem blocks=2153775104
160 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
    4096000

Allocating group tables: complete                            
//Writing inode table: done                            
Creating journal (32768 blocks): complete
Writing superblocks and filesystem accounting information: complete   

5) Expand logical volumes

[root@centos01 ~]# lvextend -L +5G /dev/mail_store/mbox 
  Size of logical volume mail_store/mbox changed from 20.00 GiB (5120 extents) to 25.00 GiB (6400 extents).
  Logical volume mail_store/mbox successfully resized.
[root@centos01 ~]# 
[root@centos01 ~]# resize2fs /dev/mail_store/mbox 
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/mail_store/mbox to 6553600 (4k) blocks.
The filesystem on /dev/mail_store/mbox is now 6553600 blocks long.

————————Thank you for reading————————

Topics: Linux ansible vim Anaconda