File system management
The act of establishing an association between an additional file system and an existing directory in the root file system so that this directory can be used as an access to other files is called mounting; The process of releasing this association is called unloading.
Associate the device with the mount point: mount; During unloading, the equipment or overload point can be used: umount
Note: the original files under the mount point will be temporarily hidden after the overload is completed
Mounting method: mount DEVICE MOUNT_POINT
mount: display all devices mounted on the current system by viewing the / etc/mtab file
mount [-fnrsvw] [-t vfstype] [-o options] device dir
Device: indicates the device to mount
(1) equipment documents:
(2) volume label:
(3)UUID,-U 'UUID':
(4) pseudo file system name:
dir: mount point
Pre existing, it is recommended to use an empty directory; The device in use by the process cannot be uninstalled
Common command options:
- t vsftype: Specifies the file system type on the device to mount
- r: read only mount
- w: read write mount
- n: do not update / etc/mtab
- a: automatically mount all devices that support automatic mount (defined in the / etc/fstab file, with the "auto mount" function in the mount option)
- L 'LABEL': specify the mounted device with the volume LABEL
- U 'UUID': specify the device to be mounted with UUID
- B --bind: bind directory to another directory
Note: view all mounted devices tracked by the kernel: cat /proc/mounts
- o options: (options for mounting the file system)
async: asynchronous mode
sync: synchronization mode
Atime / noaatime: the timestamp (including directory and file) is updated every time the file is accessed
Diratime / nodiraatime: access timestamp of the directory
auto/noauto: whether auto mount is supported
exec/noexec: does it support running applications on the file system as processes
dev/nodev: does it support the use of device files on this file system
suid/nosuid:
Remount: remount
ro:
rw:
user/nouser: whether to allow ordinary users to mount this device
acl: enables acl functionality on this file system
Note: the above options can be used at the same time, separated by commas;
Default mount options: rw, suid, dev, exec, auto, nouser, and async
Uninstall command:
# umount DEVICE
# umount MOUNT_POINT
View the processes accessing the specified file system: # fuser -v MOUNT_POINT
Terminates all processes accessing the specified file system: # fuser -km MOUNT_POINT
Mount switch partition:
Enable: swapon
swapon [OPTION]... [DEVICE]
-a: Activate all switched partitions
-p PRIORITY: specify priority
Disable: swapoff [OPTION]... [DEVICE]
Memory space usage status:
free [OPTION]
-m: In MB
-g: In GB
Viewing tools for file system space occupation and other information:
df:
-h: human-readable
-i: inodes instead of blocks
-P: Output in Posix compatible format;
To view the overall space usage status of a directory:
du:
du [OPTION]... DIR
-h: human-readable
-s: summary
File the mounted configuration file: / etc/fstab. The system initialization script will read this file and try to use the mount command to analyze each line. Each line is used to define a file system to be mounted
Note: the mount point of swap is called swap. There is no special mount directory, and the swap device is activated with the swap on command rather than mounted with the mount command
[root@localhost ~]# cat /etc/fstab # Device or pseudo file system to mount Mount point File system type Mount options Dump frequency Self checking sequence # # /etc/fstab # Created by anaconda on Mon Aug 30 01:57:41 2021 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/mapper/centos-root / xfs defaults 0 0 UUID=1f63fd17-4235-44d3-bb4e-bf002ace7f7c /boot xfs defaults 0 0 /dev/mapper/centos-swap swap swap defaults 0 0
Device or pseudo file system to mount:
Device file, LABEL(LABEL = ""), UUID(UUID = ""), pseudo file system name (proc, sysfs)
Overload option: defaults
Dump frequency:
0: no backup
1: dump every day
2: dump every other day
Self inspection sequence: 0, no self inspection; 1. Self check first. Generally, only rootfs adopts 1
Other concepts on the file system:
Super block is used to describe the organization of the large picture structure of the entire file system, how many block groups, Mount options and other information, that is, the information that can be seen using tune2fs -l DEVICE (information in super block). If dump2fs device is used, it can display not only the super block information of its own file system, but also the information of each group, These group information is stored in the description block of the group called the group itself.
Inode: Index Node
Address pointer:
Direct pointer:
Indirect pointer:
Tertiary pointer:
inode bitmap: status information identifying whether each inode is idle or not
Linked files:
Hard link: two file paths point to the same inode number
The base name of the path of any file to be accessed must be in the same partition as the inode, so hard links cannot be made across partitions
The directory cannot be referenced, which will cause circular reference
Multiple different paths to the same inode; Creating a hard link to a file creates a new reference path for the inode, thus increasing its reference count
Symbolic links:
You can edit the directory
Can span partitions
It refers to the path of another file. Its size is the length of the path string pointed to. It does not increase or decrease the reference count of the inode of the target file
ln [-sv] SRC DEST
-s: symbolic link
-v: verbose
practice:
1. Create a 20G file system with a block size of 2048, a file system of ext4 and a volume label of TEST. This partition is required to be automatically mounted to the / testing directory after startup, and the acl mount option is available by default;
(1) Create 20G partition;
(2) Format:
mke2fs -t ext4 -b 2048 -L 'TEST' /dev/DEVICE
(3) Edit the / etc/fstab file
LABEL='TEST' /testing ext4 defaults,acl 0 0
2. Create a 5G file system with the volume label HUGE. This partition is required to be automatically mounted to the / mogdata directory after startup, and the file system type is ext3;
3. Write a script to complete the following functions:
(1) list all disk devices recognized by the current system;
(2) if the number of disks is 1, its space usage information is displayed;
Otherwise, the space usage information on the last disk is displayed;
if [ $disks -eq 1 ]; then
fdisk -l /dev/[hs]da
else
fdisk -l $(fdisk -l /dev/[sh]d[a-z] | grep -o "^Disk /dev/[sh]d[a-]" | tail -1 | cut -d' ' -f2)
fi
User interaction of bash script programming:
read [option]... [name ...]
-p 'PROMPT'
-t TIMEOUT
bash -n /path/to/some_script
Detect syntax errors in scripts
bash -x /path/to/some_script
Debug execution
Example:
#!/bin/bash # Version: 0.0.1 # Author: MageEdu # Description: read testing read -p "Enter a disk special file: " diskfile [ -z "$diskfile" ] && echo "Fool" && exit 1 if fdisk -l | grep "^Disk $diskfile" &> /dev/null; then fdisk -l $diskfile else echo "Wrong disk special file." exit 2 fi
RAID
Improve IO capability: disk parallel read and write;
Improve durability: disk redundancy
Level: multiple disks work differently when organized together
RAID implementation method:
External disk array: provide adaptation capability through expansion card (hardware mode)
Internal Raid: integrated RAID controller on motherboard (hardware mode)
SoftWare RAID (software mode)
Level:
RAID-0: improved read and write performance; Available space: available space: N*min(S1,S2,...); No fault tolerance; Minimum number of disks: 2,2+
It is a disk organization structure that organizes multiple hard disks in parallel as one hard disk to realize I/O parallel organization. The data is striped and stored in multiple hard disks organized.
RAID-1: improved read performance and slightly decreased write performance; Available space: 1*min(S1,S2,...); Redundancy capability; Minimum number of disks: 2, 2+
At least two hard disks, and one copy of data is accessed in each disk
RAID-4
Organize 3 or more hard disks into RAID. When storing data, one disk is used to store check code, and the other two disks store data.
A single disk is specially used to verify the disk. Due to high access pressure, it is easy to become a performance bottleneck, but it is allowed to damage a disk and work in a degraded manner.
RAID-5: improved read / write performance; Available space: (N-1)*min(S1,S2,...); Fault tolerance: 1 disk; Minimum number of disks: 3, 3+
Mechanism of using cycle as calibration disk
RAID-6: improved read / write performance; Available space: (N-2)*min(S1,S2,...); Fault tolerance: 2 disks; Minimum number of disks: 4, 4+
Use two disks as the calibration disk, and do cyclic calibration
Mixed type
RAID-10: improved read / write performance; Available space: N*min(S1,S2,...) / 2; Fault tolerance: at most one image in each group can be broken; Minimum number of disks: 4, 4+
First make RAID-1 in pairs, and then make a pile of RAID-1 into RAID-0
RAID-01:
First, divide the hard disk into two groups, and each group is made into RAID-0; Make each group RAID-1
RAID-50,RAID-7
JBOD: available space: sum(S1,S2,...)
Function: combine the space of multiple disks into a large continuous space;
Implementation of software RAID on CentOS 6:
Combined with md(multi devices) in the kernel
mdadm: a patterned tool
Syntax format of command: mdadm [mode] < raiddevice > [options] < component devices >
Supported RAID levels: LINEAR, RAID-0, RAID-1, RAID-4, RAID-5, RAID-6, RAID-10;
Mode:
Create: - C
Assembly: - A
Monitoring: - F
Management: - f, -r, -a
<raiddevice>: /dev/md#
< component devices >: any block device
- C: create mode
- n #: use # block devices to create this RAID;
- l #: indicates the level of RAID to be created;
- a {yes|no}: automatically create the device file of the target RAID device;
-c CHUNK_SIZE: indicates the block size;
- x #: indicates the number of free disks;
For example: create a RAID-5 with 10G free space;
- D: display the details of raid;
mdadm -D /dev/md#
Management mode:
- f: mark the specified disk as damaged;
- a: add disk
- r: remove disk
Observe the status of md:
cat /proc/mdstat
Stop md device:
mdadm -S /dev/md#
watch command:
- n #: refresh interval, in seconds;
watch -n# 'COMMAND'
Exercise 1: create a RAID1 device with 10G free space. It is required that its chunk size is 128k, the file system is ext4, there is a free disk, and it can be automatically mounted to the / backup directory after startup;
Exercise 2: create a RAID-10 device with 10G free space. It is required that its chunk size is 256k, the file system is ext4, and it can be automatically mounted to the / mydata directory after startup;
LVM2
dm: device mapper, which organizes one or more underlying block devices into a module of a logical device;
/dev/dm-#
/dev/mapper/VG_NAME-LV_NAME
/dev/mapper/vol0-root
/dev/VG_NAME/LV_NAME
/dev/vol0/root
pv management tools:
pvs: brief pv information display
pvdisplay: displays details of pv
pvcreate /dev/DEVICE: create pv
vg management tools:
vgs
vgdisplay
vgcreate [-s #[kKmMgGtTpPeE]] VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]
vgextend VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]
vgreduce VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]
Do pvmove first
vgremove
lv management tools:
lvs
lvdisplay
lvcreate -L #[mMgGtT] -n NAME VolumeGroup
lvremove /dev/VG_NAME/LV_NAME
Extended logical volumes:
lvextend -L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME
resize2fs /dev/VG_NAME/LV_NAME / / reset the size of Lv. You don't need to mount it again during expansion
Reduce logical volumes:
umount /dev/VG_NAME/LV_NAME
e2fsck -f /dev/VG_NAME/LV_NAME
resize2fs /dev/VG_NAME/LV_NAME #[mMgGtT]
lvreduce -L [-]#[mMgGtT] /dev/VG_NAME/LV_NAME
mount
Snapshots: snapshot s
lvcreate -L #[mMgGtT] -p r -s -n snapshot_lv_name original_lv_name
Exercise 1: create a VG named testvg with a size of 20G composed of at least two PV S; The PE size is required to be 16MB, and then a 5G logical volume testlv is created in the volume group; Mount to / users directory;
Exercise 2: create a new user, archlinux, whose home directory is / users/archlinux, then su switch to the archlinux user and copy the / etc/pam.d directory to your home directory;
Exercise 3: extend testlv to 7G, requiring that the files of archlinux users cannot be lost;
Exercise 4: shrink testlv to 3G, and require archlinux users' files not to be lost;
Exercise 5: create a snapshot of testlv, try to back up data based on the snapshot, and verify the function of the snapshot;
File system mount usage:
Mount optical device:
CD device files:
IDE: /dev/hdc
SATA: /dev/sr0
Symbolic link file:
/dev/cdrom
/dev/cdrw
/dev/dvd
/dev/dvdrw
mount -r /dev/cdrom /media/cdrom
umount /dev/cdrom
dd command: convert and copy a file
Usage:
dd if=/PATH/FROM/SRC of=/PATH/TO/DEST
bs = #: block size, copy unit size;
count = #: how many BSS are copied;
Disk copy:
dd if=/dev/sda of=/dev/sdb
Backup MBR
dd if=/dev/sda of=c/tmp/mbr.bak bs=512 count=1
Destroy bootloader in MBR:
dd if=/dev/zero of=/dev/sda bs=256 count=1
Two special devices:
/ dev/null: Data black hole;
/ dev/zero: Zero spitting machine;
LVM simple steps
1,Create a zone and adjust the zone type to 8 e [root@localhost ~]# fdisk /dev/sdb Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): e // Create extended partition directly Partition number (1-4, default 1): First sector (2048-10485759, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759): Using default value 10485759 Partition 1 of type Extended and of size 5 GiB is set Command (m for help): n Partition type: p primary (0 primary, 1 extended, 3 free) l logical (numbered from 5) Select (default p): l // Create logical partitions 5,6,7,8,9 on the extended partition Adding logical partition 6 First sector (1030144-10485759, default 1030144): Using default value 1030144 Last sector, +sectors or +size{K,M,G} (1030144-10485759, default 10485759): +500M Partition 6 of type Linux and of size 500 MiB is set Command (m for help): t // Adjust partition type Partition number (1,5-9, default 9): 5 Hex code (type L to list all codes): 8e Changed type of partition 'Linux' to 'Linux LVM' Command (m for help): p Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x5372fd24 Device Boot Start End Blocks Id System /dev/sdb1 2048 10485759 5241856 5 Extended /dev/sdb5 4096 1028095 512000 8e Linux LVM /dev/sdb6 1030144 2054143 512000 8e Linux LVM /dev/sdb7 2056192 3080191 512000 8e Linux LVM /dev/sdb8 3082240 5179391 1048576 8e Linux LVM /dev/sdb9 5181440 10485759 2652160 8e Linux LVM Command (m for help): w // Save exit The partition table has been altered! 2,Refresh partition [root@localhost ~]# partprobe /dev/sdb [root@localhost ~]# lsblk /dev/sdb NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb 8:16 0 5G 0 disk ├─sdb1 8:17 0 1K 0 part ├─sdb5 8:21 0 500M 0 part ├─sdb6 8:22 0 500M 0 part ├─sdb7 8:23 0 500M 0 part ├─sdb8 8:24 0 1G 0 part └─sdb9 8:25 0 2.5G 0 part 3,establish pv [root@localhost ~]# pvcreate /dev/sdb{5,6,7} Physical volume "/dev/sdb5" successfully created. Physical volume "/dev/sdb6" successfully created. Physical volume "/dev/sdb7" successfully created. [root@localhost ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 centos lvm2 a-- <19.00g 0 /dev/sdb5 lvm2 --- 500.00m 500.00m /dev/sdb6 lvm2 --- 500.00m 500.00m /dev/sdb7 lvm2 --- 500.00m 500.00m 4,establish VG [root@localhost ~]# vgcreate myvg /dev/sdb{5,6} Volume group "myvg" successfully created [root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree centos 1 2 0 wz--n- <19.00g 0 myvg 2 0 0 wz--n- 992.00m 992.00m [root@localhost ~]# vgextend myvg /dev/sdb7 // Extended vg Volume group "myvg" successfully extended 5,establish LV [root@localhost ~]# Lvcreate - L 400m - n mylv myvg / / - L size - n lv_name vg_name Logical volume "mylv" created. [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert root centos -wi-ao---- <17.00g swap centos -wi-ao---- 2.00g mylv myvg -wi-a----- 400.00m 6,Create file system [root@localhost ~]# mke2fs -t ext4 -b 1024 -L MYLV /dev/myvg/mylv mke2fs 1.42.9 (28-Dec-2013) Filesystem label=MYLV OS type: Linux Block size=1024 (log=0) Fragment size=1024 (log=0) Stride=0 blocks, Stripe width=0 blocks 102400 inodes, 409600 blocks 20480 blocks (5.00%) reserved for the super user First data block=1 Maximum filesystem blocks=34078720 50 block groups 8192 blocks per group, 8192 fragments per group 2048 inodes per group Superblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409 Allocating group tables: done Writing inode tables: done Creating journal (8192 blocks): done Writing superblocks and filesystem accounting information: done 7,mount [root@localhost ~]# mount /dev/myvg/mylv /mnt/mylv.mount/ [root@localhost mapper]# lsblk /dev/sdb NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb 8:16 0 5G 0 disk ├─sdb1 8:17 0 1K 0 part ├─sdb5 8:21 0 500M 0 part │ └─myvg-mylv 253:2 0 400M 0 lvm /mnt/mylv.mount ├─sdb6 8:22 0 500M 0 part ├─sdb7 8:23 0 500M 0 part ├─sdb8 8:24 0 1G 0 part └─sdb9 8:25 0 2.5G 0 part