Linux 磁盘与文件系统管理(二)
3、磁盘的分区、格式化、检验与挂载
如果我们想要在系统里边新增一块磁盘,应该有哪些动作:
- 对磁盘进行分区,以建立可用的 partition;
- 对该 partition 进行格式化(format),以建立系统可用的 filesystem;
- 若想要仔细一点,则可对刚刚建立好的 filesystem 进行检验;
- 在 Linux 系统上,需要建立挂载点(亦即是目录),并将它挂载上来。
3.1、观察磁盘分区状态
-
lsblk 列出系统上的所有磁盘列表
lsblk 是 “list block device” 的缩写。
[root@server ~]# lsblk [-dfimpt] [device] 选项与参数: -d :仅列出磁盘本身,并不会列出该磁盘的分区数据 -f :同时列出该磁盘内的文件系统名称 -i :使用ASCII的线段输出 -m :同时输出该设备在/dev底下的权限数据 -p :列出该设备的完整文件名 -t :列出该磁盘的详细信息
范例一:列出本系统下的所有磁盘与磁盘内的分区信息 [root@MiWiFi-R4A-srv ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda1 8:1 0 300M 0 part /boot ├─sda2 8:2 0 2G 0 part [SWAP] └─sda3 8:3 0 17.7G 0 part / sdb 8:16 0 2G 0 disk sr0 11:0 1 1024M 0 rom 范例二:仅列出 /dev/sda 设备内的所有数据的完整文件名 [root@MiWiFi-R4A-srv ~]# lsblk -ip /dev/sda NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT /dev/sda 8:0 0 20G 0 disk |-/dev/sda1 8:1 0 300M 0 part /boot |-/dev/sda2 8:2 0 2G 0 part [SWAP] `-/dev/sda3 8:3 0 17.7G 0 part /
-
blkid 列出设备的 UUID 等参数
UUID 是全局单一标识符(university unique identifier),Linux 会将系统内所有的设备都给予一个独一无二的标识符,这个标识符就可以拿来挂载或者是使用这个设备/文件系统之用。
[root@MiWiFi-R4A-srv ~]# blkid /dev/sda1: UUID="f87b1ed0-3a1b-450c-8c27-a96ca507ffd9" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="f6405047-01" /dev/sda2: UUID="ec371279-dd44-4802-8b93-1e3f256f79ec" TYPE="swap" PARTUUID="f6405047-02" /dev/sda3: UUID="5a3894d9-7791-4691-aebb-16c899803e43" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="f6405047-03"
-
parted 列出磁盘的分区表类型与分区信息
[root@server ~]# parted device_name print
范例一:列出 /dev/sda 磁盘的相关数据 [root@MiWiFi-R4A-srv ~]# parted /dev/sda print Model: VMware, VMware Virtual S (scsi) Disk /dev/sda: 21.5GB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 1049kB 316MB 315MB primary xfs boot 2 316MB 2463MB 2147MB primary linux-swap(v1) 3 2463MB 21.5GB 19.0GB primary xfs
3.2、磁盘分区:gdisk/fdisk
MBR 分区表请使用 fdisk 分区,GPT 分区表请使用 gdisk 分区。
-
gdisk
[root@MiWiFi-R4A-srv ~]# gdisk 设备名
范例: [root@MiWiFi-R4A-srv ~]# gdisk /dev/sdb GPT fdisk (gdisk) version 1.0.3 Partition table scan: MBR: not present BSD: not present APM: not present GPT: not present Creating new GPT entries. Command (? for help): ? b back up GPT data to a file c change a partition's name d delete a partition <== 删除一个分区 i show detailed information on a partition l list known partition types n add a new partition <== 增加一个分区 o create a new empty GUID partition table (GPT) p print the partition table <== 打印分区表 q quit without saving changes <== 不存储分区就离开 gdisk r recovery and transformation options (experts only) s sort partitions t change a partition's type code v verify disk w write table to disk and exit <== 存储分区再离开 gdisk x extra functionality (experts only) ? print this menu
[root@MiWiFi-R4A-srv ~]# gdisk /dev/sdb GPT fdisk (gdisk) version 1.0.3 Partition table scan: MBR: not present BSD: not present APM: not present GPT: not present Creating new GPT entries. Command (? for help): p Disk /dev/sdb: 4194304 sectors, 2.0 GiB <== 磁盘文件名/扇区数与总容量 Model: VMware Virtual S Sector size (logical/physical): 512/512 bytes <== 单一扇区大小为 512bytes Disk identifier (GUID): CC2B80BB-DED2-4DC8-83E6-BD7743503DA3 <== 磁盘的 GPT 标识符 Partition table holds up to 128 entries Main partition table begins at sector 2 and ends at sector 33 First usable sector is 34, last usable sector is 4194270 Partitions will be aligned on 2048-sector boundaries Total free space is 4194237 sectors (2.0 GiB) Number Start (sector) End (sector) Size Code Name
使用的 “设备名” 请不要加上数字,因为 partition 是针对 “整个磁盘设备” 而不是某个 partition。
-
用 gdisk 新增分区
[root@MiWiFi-R4A-srv ~]# gdisk /dev/sdb GPT fdisk (gdisk) version 1.0.3 Partition table scan: MBR: not present BSD: not present APM: not present GPT: not present Creating new GPT entries. Command (? for help): p Disk /dev/sdb: 4194304 sectors, 2.0 GiB Model: VMware Virtual S Sector size (logical/physical): 512/512 bytes Disk identifier (GUID): 4E6E5D56-2141-4B54-9565-77B9E188EE14 Partition table holds up to 128 entries Main partition table begins at sector 2 and ends at sector 33 First usable sector is 34, last usable sector is 4194270 Partitions will be aligned on 2048-sector boundaries Total free space is 4194237 sectors (2.0 GiB) Number Start (sector) End (sector) Size Code Name Command (? for help): n <== n 为新增磁盘 Partition number (1-128, default 1): 1 First sector (34-4194270, default = 2048) or {+-}size{KMGTP}: 2048 <== 可以默认 Last sector (2048-4194270, default = 4194270) or {+-}size{KMGTP}: +1G <== 谨慎!!! Current type is 'Linux filesystem' Hex code or GUID (L to show codes, Enter = 8300): Changed type of partition to 'Linux filesystem' Command (? for help): p Disk /dev/sdb: 4194304 sectors, 2.0 GiB Model: VMware Virtual S Sector size (logical/physical): 512/512 bytes Disk identifier (GUID): 4E6E5D56-2141-4B54-9565-77B9E188EE14 Partition table holds up to 128 entries Main partition table begins at sector 2 and ends at sector 33 First usable sector is 34, last usable sector is 4194270 Partitions will be aligned on 2048-sector boundaries Total free space is 2097085 sectors (1024.0 MiB) Number Start (sector) End (sector) Size Code Name 1 2048 2099199 1024.0 MiB 8300 Linux filesystem
最后的分区如下:
Command (? for help): p Disk /dev/sdb: 4194304 sectors, 2.0 GiB Model: VMware Virtual S Sector size (logical/physical): 512/512 bytes Disk identifier (GUID): 88D27160-13F3-4599-AE64-CF23784B7339 Partition table holds up to 128 entries Main partition table begins at sector 2 and ends at sector 33 First usable sector is 34, last usable sector is 4194270 Partitions will be aligned on 2048-sector boundaries Total free space is 4061 sectors (2.0 MiB) Number Start (sector) End (sector) Size Code Name 1 2048 2097152 1023.0 MiB 8300 Linux filesystem 2 2099200 3123199 500.0 MiB 0700 Microsoft basic data 3 3123200 4194270 523.0 MiB 8200 Linux swap
如果一切的分区状态都正常的话,那么就直接写入磁盘分区表:
Command (? for help): w Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!! Do you want to proceed? (Y/N): y OK; writing new GUID partition table (GPT) to /dev/sdb. The operation has completed successfully.
-
partprobe 更新 Linux 核心的分区表信息
[root@li ~]# partprobe [-s] #如果不加 -s,屏幕将不会出现信息
[root@MiWiFi-R4A-srv ~]# partprobe -s /dev/sda: msdos partitions 1 2 3 /dev/sdb: gpt partitions 1 2 3 [root@MiWiFi-R4A-srv ~]# lsblk /dev/sdb NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb 8:16 0 2G 0 disk ├─sdb1 8:17 0 1023M 0 part ├─sdb2 8:18 0 500M 0 part └─sdb3 8:19 0 523M 0 part [root@MiWiFi-R4A-srv ~]# cat /proc/partitions major minor #blocks name 8 0 20971520 sda 8 1 307200 sda1 8 2 2097152 sda2 8 3 18566144 sda3 8 16 2097152 sdb 8 17 1047552 sdb1 8 18 512000 sdb2 8 19 535535 sdb3 11 0 1048575 sr0
-
用 gdisk 删除一个分区
[root@MiWiFi-R4A-srv ~]# gdisk /dev/sdb GPT fdisk (gdisk) version 1.0.3 Partition table scan: MBR: protective BSD: not present APM: not present GPT: present Found valid GPT with protective MBR; using GPT. Command (? for help): p Disk /dev/sdb: 4194304 sectors, 2.0 GiB Model: VMware Virtual S Sector size (logical/physical): 512/512 bytes Disk identifier (GUID): 88D27160-13F3-4599-AE64-CF23784B7339 Partition table holds up to 128 entries Main partition table begins at sector 2 and ends at sector 33 First usable sector is 34, last usable sector is 4194270 Partitions will be aligned on 2048-sector boundaries Total free space is 4061 sectors (2.0 MiB) Number Start (sector) End (sector) Size Code Name 1 2048 2097152 1023.0 MiB 8300 Linux filesystem 2 2099200 3123199 500.0 MiB 0700 Microsoft basic data 3 3123200 4194270 523.0 MiB 8200 Linux swap Command (? for help): d Partition number (1-3): 3 Command (? for help): p Disk /dev/sdb: 4194304 sectors, 2.0 GiB Model: VMware Virtual S Sector size (logical/physical): 512/512 bytes Disk identifier (GUID): 88D27160-13F3-4599-AE64-CF23784B7339 Partition table holds up to 128 entries Main partition table begins at sector 2 and ends at sector 33 First usable sector is 34, last usable sector is 4194270 Partitions will be aligned on 2048-sector boundaries Total free space is 1075132 sectors (525.0 MiB) Number Start (sector) End (sector) Size Code Name 1 2048 2097152 1023.0 MiB 8300 Linux filesystem 2 2099200 3123199 500.0 MiB 0700 Microsoft basic data Command (? for help): w Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!! Do you want to proceed? (Y/N): y OK; writing new GUID partition table (GPT) to /dev/sdb. The operation has completed successfully.
3.3、磁盘格式化(建立文件系统)
-
XFS 文件系统 mkfs.xfs
[root@li ~]# mkfs.xfs [-b bsize] [-d parms] [-i parms] [-l parms] [-L label] [-f] [-r parms] 设备名称 选项与参数: -b :后面接的是 block 容量,可由 512 到 64k,不过最大容量限制为 Linux 的 4k -d :后面接的是重要的 data section 的相关参数,主要的值有: agcount=数值:设定需要几个存储群组的意思(AG),通常与 CPU 有关 agsize=数值:每个 AG 设定为多少容量的意思,通常 agcount/agsize 只选一个设定即可 file:指的是“格式化的设备是个文件而不是个设备”的意思 size=数值:data section 的容量,也就是说你可以不将全部容量用完 su=数值:当有 RAID 时,那个 stripe 数值的意思,于底下的 sw 搭配使用 sw=数值:当有 RAID 时,用于存储数据的磁盘数量 sunit=数值:与 su 相当,不过单位使用的是“几个 sector(512bytes大小)”的意思 swidth=数值:就是 su*sw 的意思 -f :如果设备内已经有文件系统,就用 -f 强制格式化 -i :与 inode 相关的参数: size=数值:最小是 256bytes 最大是 2k internal=[011]:log设备是否内建 logdev=device:log设备为后面的设备,需设定 internal=0 才可 size=数值:指定登录区的容量,通常最小有 512 个 block,大约 2M 以上才行 -L :这个文件系统的标头名称 Label Name的意思 -r :指定 realtime section 的相关设定值,常见的有: extsize=数值:就是那个重要的 extent 数值,一般不须设定,但有 RAID 时,最好设定与 swidth 的数值相同较佳
范例:将前面分区出来的 /dev/sdb1 格式化为 xfs 文件系统 [root@MiWiFi-R4A-srv ~]# mkfs.xfs /dev/sdb1 meta-data=/dev/sdb1 isize=512 agcount=4, agsize=65472 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=1, sparse=1, rmapbt=0 = reflink=1 data = bsize=4096 blocks=261888, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0, ftype=1 log =internal log bsize=4096 blocks=1566, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 [root@MiWiFi-R4A-srv ~]# blkid /dev/sdb1 /dev/sdb1: UUID="02689c66-af06-4b0b-b2a2-0c33247afb04" BLOCK_SIZE="512" TYPE="xfs" PARTLABEL=" 2 2099200 3123199" PARTUUID="a81d69ea-73c6-4423-9ef3-8c14aeb7a143"
因为 xfs 可以使用多个数据流来读写系统,以增加速度,因此那个 agcount 可以跟 CPU 的核心数来做搭配!
[root@MiWiFi-R4A-srv ~]# grep 'processor' /proc/cpuinfo processor : 0 [root@MiWiFi-R4A-srv ~]# mkfs.xfs -f -d agcount=2 /dev/sdb1 meta-data=/dev/sdb1 isize=512 agcount=2, agsize=130944 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=1, sparse=1, rmapbt=0 = reflink=1 data = bsize=4096 blocks=261888, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0, ftype=1 log =internal log bsize=4096 blocks=1986, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0
-
EXT4 文件系统 mkfs.ext4
[root@MiWiFi-R4A-srv ~]# mkfs.ext4 [-b size] [-L label1] 设备名
范例:将 /dev/sdb2 格式化为 ext4 [root@MiWiFi-R4A-srv ~]# mkfs.ext4 /dev/sdb2 mke2fs 1.45.6 (20-Mar-2020) Creating filesystem with 512000 1k blocks and 128016 inodes Filesystem UUID: 13751b1f-2a15-4729-ba6b-f0b02eccf011 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 [root@MiWiFi-R4A-srv ~]# dumpe2fs -h /dev/sdb2 dumpe2fs 1.45.6 (20-Mar-2020) Filesystem volume name: <none> Last mounted on: <not available> Filesystem UUID: 13751b1f-2a15-4729-ba6b-f0b02eccf011 Filesystem magic number: 0xEF53 Filesystem revision #: 1 (dynamic) Filesystem features: has_journal ext_attr resize_inode dir_index filetype extent 64bit flex_bg sparse_super large_file huge_file dir_nlink extra_isize metadata_csum Filesystem flags: signed_directory_hash Default mount options: user_xattr acl Filesystem state: clean Errors behavior: Continue Filesystem OS type: Linux Inode count: 128016 Block count: 512000 Reserved block count: 25600 Free blocks: 485316 Free inodes: 128005 First block: 1 Block size: 1024 Fragment size: 1024 Group descriptor size: 64 Reserved GDT blocks: 256 Blocks per group: 8192 Fragments per group: 8192 Inodes per group: 2032 Inode blocks per group: 254 Flex block group size: 16 Filesystem created: Fri Aug 27 19:56:20 2021 Last mount time: n/a Last write time: Fri Aug 27 19:56:20 2021 Mount count: 0 Maximum mount count: -1 Last checked: Fri Aug 27 19:56:20 2021 Check interval: 0 (<none>) Lifetime writes: 284 kB Reserved blocks uid: 0 (user root) Reserved blocks gid: 0 (group root) First inode: 11 Inode size: 128 Journal inode: 8 Default directory hash: half_md4 Directory Hash Seed: 46ee8114-78ef-4169-ba11-51e59990c853 Journal backup: inode blocks Checksum type: crc32c Checksum: 0x1479c7cf Journal features: (none) Journal size: 8M Journal length: 8192 Journal sequence: 0x00000001 Journal start: 0
-
其他文件系统 mkfs
[root@MiWiFi-R4A-srv ~]# mkfs[tab][tab] mkfs mkfs.ext2 mkfs.ext4 mkfs.minix mkfs.vfat mkfs.cramfs mkfs.ext3 mkfs.fat mkfs.msdos mkfs.xfs
3.4、文件系统的检测
-
xfs_repair 处理 XFS 文件系统
[root@li ~]# xfs_repair [-fnd] 设备 选项与参数: -f :后面接的是文件 -n :单纯检查并不维修文件系统 -d :通常用在单人维护模式下,针对根目录(/)进行检查与修复的动作!很危险!不要随意使用
[root@MiWiFi-R4A-srv ~]# xfs_repair /dev/sdb1 Phase 1 - find and verify superblock... Phase 2 - using internal log - zero log... - scan filesystem freespace and inode maps... - found root inode chunk Phase 3 - for each AG... - scan and clear agi unlinked lists... - process known inodes and perform inode discovery... - agno = 0 - agno = 1 - process newly discovered inodes... Phase 4 - check for duplicate blocks... - setting up duplicate extent list... - check for inodes claiming duplicate blocks... - agno = 0 - agno = 1 Phase 5 - rebuild AG headers and trees... - reset superblock... Phase 6 - check inode connectivity... - resetting contents of realtime bitmap and summary inodes - traversing filesystem ... - traversal finished ... - moving disconnected inodes to lost+found ... Phase 7 - verify and correct link counts... done
-
fsck.ext4 处理 ext4 文件系统
[root@MiWiFi-R4A-srv ~]# fsck.ext4 /dev/sdb2 e2fsck 1.45.6 (20-Mar-2020) /dev/sdb2: clean, 11/128016 files, 26684/512000 blocks
执行 xfs_repair 或 fsck.ext4 时,被检查的 partition 务必不可挂载在系统上。
3.5、文件系统的挂载与卸载
在挂载之前,最好确定几件事:
- 单一文件系统不应该被重复挂载在不同的挂载点(目录)中;
- 单一目录不应该重复挂载多个文件系统;
- 要作为挂载点的目录,理论上都是空目录才是。
如果目录非空,那么挂载了文件系统后,原目录底下的东西就会暂时消失。等分区卸载后,就会再次显示出来。
[root@MiWiFi-R4A-srv ~]# mount [-hV]
[root@MiWiFi-R4A-srv ~]# mount -a [-fFnrsvw] [-t vfstype]
[root@MiWiFi-R4A-srv ~]# mount [-fnrsvw] [-o options [,...]] device | dir
[root@MiWiFi-R4A-srv ~]# mount [-fnrsvw] [-t vfstype] [-o options] device dir
选项与参数:
-V :显示程序版本
-h :显示辅助讯息
-v :显示较讯息,通常和 -f 用来除错。
-a :将 /etc/fstab 中定义的所有档案系统挂上。
-F :这个命令通常和 -a 一起使用,它会为每一个 mount 的动作产生一个行程负责执行。在系统需要挂上大量 NFS 档案系统时可以加快挂上的动作。
-f :通常用在除错的用途。它会使 mount 并不执行实际挂上的动作,而是模拟整个挂上的过程。通常会和 -v 一起使用。
-n :一般而言,mount 在挂上后会在 /etc/mtab 中写入一笔资料。但在系统中没有可写入档案系统存在的情况下可以用这个选项取消这个动作。
-s-r :等于 -o ro
-w :等于 -o rw
-L :将含有特定标签的硬盘分割挂上。
-U :将档案分割序号为 的档案系统挂下。-L 和 -U 必须在/proc/partition 这种档案存在时才有意义。
-t :指定档案系统的型态,通常不必指定。mount 会自动选择正确的型态。
-o async :打开非同步模式,所有的档案读写动作都会用非同步模式执行。
-o sync :在同步模式下执行。
-o atime、-o noatime :当 atime 打开时,系统会在每次读取档案时更新档案的『上一次调用时间』。当我们使用 flash 档案系统时可能会选项把这个选项关闭以减少写入的次数。
-o auto、-o noauto :打开/关闭自动挂上模式。
-o defaults :使用预设的选项 rw, suid, dev, exec, auto, nouser, and async.
-o dev、-o nodev-o exec、-o noexec允许执行档被执行。
-o suid、-o nosuid:
允许执行档在 root 权限下执行。
-o user、-o nouser :使用者可以执行 mount/umount 的动作。
-o remount :将一个已经挂下的档案系统重新用不同的方式挂上。例如原先是唯读的系统,现在用可读写的模式重新挂上。
-o ro :用唯读模式挂上。
-o rw :用可读写模式挂上。
-o loop= :使用 loop 模式用来将一个档案当成硬盘分割挂上系统。
CentOS 是怎么找出文件系统类型的?由于文件系统几乎都有 superblock,我们的 Linux 可以通过分析 superblock 搭配 Linux 自己的驱动程序去测试挂载,如果成功地套和了,就立刻自动地使用该类型的文件系统挂载起来。那么系统有没有指定哪些类型的文件系统才需要进行上述的挂载测试呢?主要是参考以下两个文件:
- /etc/filesystems :系统指定的测试挂载文件系统类型的优先级;
- /proc/filesyss :Linux 系统已经加载的文件系统类型。
那我怎么知道我的 Linux 有没有相关文件系统类型的驱动程序呢?我们 Linux 支持的文件系统驱动程序都写在如下的目录中:
- /lib/modules/$(uname -r)/kernell/fs/
3.5.1、挂载 xfs/ext4/vfat 等文件系统
范例:找出 /dev/sdb2 的 UUID 后,用该 UUID 来挂载文件系统到 /data/xfs
[root@MiWiFi-R4A-srv ~]# blkid /dev/sdb1
/dev/sdb1: UUID="31cdde7a-c59c-43b8-bdb5-8e41ff0aed68" BLOCK_SIZE="512" TYPE="xfs" PARTLABEL=" 2 2099200 3123199" PARTUUID="a81d69ea-73c6-4423-9ef3-8c14aeb7a143"
[root@MiWiFi-R4A-srv ~]# mount UUID="31cdde7a-c59c-43b8-bdb5-8e41ff0aed68" /data/xfs
mount: /data/xfs: mount point does not exist.
[root@MiWiFi-R4A-srv ~]# mkdir /data/xfs
mkdir: cannot create directory ‘/data/xfs’: No such file or directory
[root@MiWiFi-R4A-srv ~]# mkdir -p /data/xfs
[root@MiWiFi-R4A-srv ~]# mount UUID="31cdde7a-c59c-43b8-bdb5-8e41ff0aed68" /data/xfs
[root@MiWiFi-R4A-srv ~]# df /data/xfs/
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sdb1 1039608 40168 999440 4% /data/xfs
范例:使用同样的方式,将 /dev/sdb1 挂载到 /data/ext4
[root@MiWiFi-R4A-srv ~]# blkid /dev/sdb2
/dev/sdb2: UUID="13751b1f-2a15-4729-ba6b-f0b02eccf011" BLOCK_SIZE="1024" TYPE="ext4" PARTLABEL="Microsoft basic data" PARTUUID="fe277584-9a67-4e71-ad91-3155cb38c77a"
[root@MiWiFi-R4A-srv ~]# mkdir /data/ext4
[root@MiWiFi-R4A-srv ~]# mount UUID="13751b1f-2a15-4729-ba6b-f0b02eccf011" /data/ext4
[root@MiWiFi-R4A-srv ~]# df /data/ext4
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sdb2 487634 2318 455620 1% /data/ext4
3.5.2、umount(将设备卸载)
[root@li ~]# umount [-fn] 设备文件名或挂载点
选项与参数:
-f:强制卸载!
-l:立刻卸载文件系统,比 -f 还强!
-n:不更新 /etc/mtab 情况下卸载
[root@MiWiFi-R4A-srv ~]# umount /data/xfs
[root@MiWiFi-R4A-srv ~]# umount /data/ext4
3.6、磁盘/文件系统参数修订
-
mknod
在 Linux 底下所有的设备都以文件来代表的,但是文件是如何代表设备呢?就是通过文件的 major 和 minor 数值来代替的。
[root@MiWiFi-R4A-srv ~]# ll /dev/sd* brw-rw----. 1 root disk 8, 0 Aug 28 08:48 /dev/sda brw-rw----. 1 root disk 8, 1 Aug 28 08:48 /dev/sda1 brw-rw----. 1 root disk 8, 2 Aug 28 08:48 /dev/sda2 brw-rw----. 1 root disk 8, 3 Aug 28 08:48 /dev/sda3 brw-rw----. 1 root disk 8, 16 Aug 28 08:48 /dev/sdb brw-rw----. 1 root disk 8, 17 Aug 28 08:48 /dev/sdb1 brw-rw----. 1 root disk 8, 18 Aug 28 08:48 /dev/sdb2
常见的磁盘文件名 /dev/sda 与 /dev/loop0 设备代码如下:
磁盘文件明 Major Minor /dev/sda 8 0-15 /dev/sdb 8 16-31 /dev/loop0 7 0 /dev/loop1 7 1 [root@MiWiFi-R4A-srv ~]# mknod 设备文件名 [bcp] [Major] [Minor] 选项与参数: 设备种类: b : 设定设备名称为一个周边存储设备文件,例如磁盘等; c : 设定设备名称成为一个周边输入设备文件,例如鼠标/键盘等; p : 设定设备名称成为一个 FIFO 文件; Major : 主要设备代码 Minor : 次要设备代码
范例:建立 /dev/vda10 为 252 10 [root@MiWiFi-R4A-srv ~]# mknod /dev/vda10 b 252 10 [root@MiWiFi-R4A-srv ~]# ll /dev/vda10 brw-r--r--. 1 root root 252, 10 Aug 28 09:22 /dev/vda10 范例:建立一个 FIFO 文件,文件名为 /tmp/testpipe [root@MiWiFi-R4A-srv ~]# mknod /tmp/testpipt p [root@MiWiFi-R4A-srv ~]# ll /tmp/testpipt prw-r--r--. 1 root root 0 Aug 28 09:23 /tmp/testpipt # 这个文件可不是一般文件,建议删除 [root@MiWiFi-R4A-srv ~]# rm /tmp/testpipt rm: remove fifo '/tmp/testpipt'? y [root@MiWiFi-R4A-srv ~]# rm /dev/vda10 rm: remove block special file '/dev/vda10'? y
-
xfs_admin 修改 XFS 文件系统的 UUID 与 Label name
[root@MiWiFi-R4A-srv ~]# xfs_admin [-lu] [-L label] [-U UUID] 设备名 选项与参数: -l : 列出这个设备的 label name -u : 列出这个设备的 UUID -L : 设定这个设备的 Label name -U : 设定这个设备的 UUID
范例:设定 /dev/sdb1 的 label name 为 li_xfs,并测试挂载 [root@MiWiFi-R4A-srv ~]# xfs_admin -L li_xfs /dev/sdb1 writing all SBs new label = "li_xfs" [root@MiWiFi-R4A-srv ~]# xfs_admin -l /dev/sdb1 label = "li_xfs" [root@MiWiFi-R4A-srv ~]# mount Label=li_xfs /data/xfs mount: /data/xfs: special device Label=li_xfs does not exist. [root@MiWiFi-R4A-srv ~]# mount LABEL=li_xfs /data/xfs 范例:利用 uuidgen 产生新 UUID 来设定 /dev/sdb2,并挂载 [root@MiWiFi-R4A-srv ~]# umount /data/xfs [root@MiWiFi-R4A-srv ~]# uuidgen 2fe61dd8-070d-45e4-9d65-0b971dba6c90 [root@MiWiFi-R4A-srv ~]# xfs_admin -u /dev/sdb1 UUID = 31cdde7a-c59c-43b8-bdb5-8e41ff0aed68 [root@MiWiFi-R4A-srv ~]# xfs_admin -U 2fe61dd8-070d-45e4-9d65-0b971dba6c90 /dev/sdb1 Clearing log and setting UUID writing all SBs new UUID = 2fe61dd8-070d-45e4-9d65-0b971dba6c90 [root@MiWiFi-R4A-srv ~]# mount UUID="2fe61dd8-070d-45e4-9d65-0b971dba6c90" /data/xfs
-
tune2fs 修改 ext4 的 label name 与 UUID
[root@MiWiFi-R4A-srv ~]# tune2fs [-l] [-L label] [-U UUID] 设备名 选项与参数: -l : 类似 dumpe2fs -h 的功能一样~将 superblock 内的数据列出来 -L : 修改 LABEL name -U : 修改 UUID
范例:列出 /dev/sdb2 的 label name,并改为 li_ext4 [root@MiWiFi-R4A-srv ~]# dumpe2fs -h /dev/sdb2 | grep name dumpe2fs 1.45.6 (20-Mar-2020) Filesystem volume name: <none> <== 没有设定 [root@MiWiFi-R4A-srv ~]# tune2fs -L li_ext4 /dev/sdb2 tune2fs 1.45.6 (20-Mar-2020) [root@MiWiFi-R4A-srv ~]# dumpe2fs -h /dev/sdb2 | grep name dumpe2fs 1.45.6 (20-Mar-2020) Filesystem volume name: li_ext4 [root@MiWiFi-R4A-srv ~]# mount LABEL=li_ext4 /data/ext4/
4、设定开机挂载
4.1、开机挂载 /etc/fstab 及 /etc/mtab
先要说明一下系统挂载的限制:
- 根目录 / 是必须挂载的,而且一定要先于其他 mount point 被挂载出来;
- 其他 mount point 必须为已建立的目录,可任意指定,但一定要遵守必须的系统目录架构原则(FHS);
- 所有 mount point 在同一时间之内,只能挂载一次;
- 所有mount point 在同一时间之内,只能挂载一次;
- 如若进行卸载,您必须先将工作目录移到 mount point(及其子目录)之外。
先查阅一下 /etc/fstab 的内容:
[root@MiWiFi-R4A-srv ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue Aug 10 08:24:18 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.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
UUID=5a3894d9-7791-4691-aebb-16c899803e43 / xfs defaults 0 0
UUID=f87b1ed0-3a1b-450c-8c27-a96ca507ffd9 /boot xfs defaults 0 0
UUID=ec371279-dd44-4802-8b93-1e3f256f79ec none swap defaults 0 0
[设备/UUID等] [挂载点] [文件系统] [文件系统参数] [dump] [fsck]
六个字段很重要:
-
第一栏:磁盘设备文件名/UUID/Label Name
-
第二栏:挂载点
-
第三栏:文件系统
-
第四栏:文件系统参数
文件系统参数 说明 async/sync 设定磁盘是否以异步方式运作!预设为async(效能较佳) auto/noauto 当下达 mount -a 时,此文件系统是否会被主动测试挂载。预设为 auto rw/ro 如果你想要分享数据而不给用户随便更改,可以设定为只读!则不论在此文件系统上的文件是否设定为w,都无法写入! exec/noexec 同上 user/nouser 是否允许用户使用 mount 指令挂载。一般不允许一般用户使用 mount,所以设定为 nouser suid/nosuid 该文件系统是否允许 SUID 的存在 defaults 同时具有 rw,suid,exec,auto,nouser,async等 -
第五栏:是否被 dump 备份指令作用
-
第六栏:是否以 fsck 检验扇区