掛載文件系統
作者:尹正傑
版權聲明:原創作品,謝絕轉載!否則將追究法律責任。
一.博主推薦閱讀
硬盤結構類型概述: https://www.cnblogs.com/yinzhengjie/p/12340062.html 分區表概述: https://www.cnblogs.com/yinzhengjie/p/12347792.html 文件系統概述: https://www.cnblogs.com/yinzhengjie/p/12369809.html
二.用mount命令臨時掛載文件系統案例
1>.mount命令概述
mount常用命令選項如下: -t vsftype: 指定要掛載的設備上的文件系統類型,通常情況下無需指定,因為mount命令會自適應您所掛載分區對應的文件系統類型。 -r,readonly: 以只讀方式掛載。 -w,read and write: 以讀寫方式掛載,默認就用該參數。 -n: 表示不更新/etc/mtab文件,即使用mount命令不可見,起到了隱藏掛載的作用,可以通過查看"/proc/mounts"該文件追蹤內核掛載情況。
注意:該選項在CentOS 6.x支持良好,但在CentOS7.x支持的並不友好。我在CentOS 7.6版本測試時發現基於UUID方式掛載,依舊會修改"/etc/mtab"文件。 -a: 自動掛載所有支持自動掛載的設備(定義在了/etc/fstab文件中,且掛載選項中有auto功能) -L,'LABEL': 以卷標指定掛載設備,這種方式在RedHat 5.x系列使用相對較多,目前生產環境中不推薦大家使用基於卷標的方式掛載,因為卷標無法唯一確定一個設備。 -U 'UUID': 以UUID指定要掛載的設備,生產環境中強烈推薦基於UUID的方式進行掛載,因為UUID可以唯一標識一塊設備。 -B, --bind: 綁定一個目錄到另一個目錄上,本質上掛載到后者所對應的分區,但使用df命令並不會顯示,我們查看"/etc/mtab"或者"mount"命令均可以看到。 -o options:
指定掛載文件系統的選項,多個選項使用逗號分隔,常見選項如下所示: async:
異步模式,效率更快,數據會先放在buffer中,得帶buffer內存達到指定閾值(比如按時間,或者內存使用百分比)時會溢寫到磁盤,但存在數據丟失的風險。
sync同步模式:
內存更改時,同時寫磁盤,效率較低,但安全性更強。因為內存要遠遠大於磁盤I/O速度。 atime/noatime:
包含目錄和文件的訪問(讀)時間,對於訪問Web文件沒有必要記錄訪問時間,雖然數據不大,但隨着QPS的增大頻繁進行I/O操作也是一筆不小的開銷,因此建議使用noatime參數進行掛載。 diratime/nodiratime:
目錄的訪問時間戳,同上,對於訪問呢Web文件建議使用nodiratime。我在部署大數據集群時通常會將這兩個參數不記錄訪問時間。 auto/noauto:
是否支持自動掛載,是否支持-a選項 exec/noexec:
是否支持在文件系統上運行應用程序,使用"noexec"參數進行掛載的文件系統,盡管文件有執行權限也無法執行。 dev/nodev:
是否支持在此文件系統上使用設備文件 suid/nosuid:
是否支持suid和sgid權限 remount:
重新掛載,基本上大部分目錄都是可以卸載再重新掛載的,但是對於根目錄是無法卸載的,此時我們就可以使用remount來重新掛載相關參數。 ro:
只讀功能
rw:
讀寫功能 user/nouser:
是否允許普通用戶掛載此設備,與"/etc/fstab"文件配合使用。 acl:
啟用此文件系統上的acl功能,在CentOS 6.x的下默認是啟用acl功能的,因此需要手動添加acl功能,而在CentOS 7.x則無需考慮,因為默認掛載就支持acl喲~ loop:
使用loop設備。即使用普通文件作為文件系統。在CentOS 6.x版本是需要加該參數的,但是在CentOS 7.x中就無需加該參數了,因為CentOS7.x版本相對智能。
CentOS 6.x默認有8個loop設備,如果想要使用更多的loop設備需要修改內核參數("/boot/grub/grub.conf"),在kernel那一行行中添加"max_loop=100"你要初始化的loop設備數量。
在CentOS 7.x我們掛載loop設備也無需指定"-loop"選項,CentOS 7.x會自動生成loop設備。 defaults:
如果不使用"-o"選項,則會使用默認選項,相當於rw, suid, dev, exec, auto, nouser, async。

[root@yinzhengjie.com ~]# mount -help Usage: mount [-lhV] mount -a [options] mount [options] [--source] <source> | [--target] <directory> mount [options] <source> <directory> mount <operation> <mountpoint> [<target>] Options: -a, --all mount all filesystems mentioned in fstab -c, --no-canonicalize don't canonicalize paths -f, --fake dry run; skip the mount(2) syscall -F, --fork fork off for each device (use with -a) -T, --fstab <path> alternative file to /etc/fstab -h, --help display this help text and exit -i, --internal-only don't call the mount.<type> helpers -l, --show-labels lists all mounts with LABELs -n, --no-mtab don't write to /etc/mtab -o, --options <list> comma-separated list of mount options -O, --test-opts <list> limit the set of filesystems (use with -a) -r, --read-only mount the filesystem read-only (same as -o ro) -t, --types <list> limit the set of filesystem types --source <src> explicitly specifies source (path, label, uuid) --target <target> explicitly specifies mountpoint -v, --verbose say what is being done -V, --version display version information and exit -w, --rw, --read-write mount the filesystem read-write (default) -h, --help display this help and exit -V, --version output version information and exit Source: -L, --label <label> synonym for LABEL=<label> -U, --uuid <uuid> synonym for UUID=<uuid> LABEL=<label> specifies device by filesystem label UUID=<uuid> specifies device by filesystem UUID PARTLABEL=<label> specifies device by partition label PARTUUID=<uuid> specifies device by partition UUID <device> specifies device by path <directory> mountpoint for bind mounts (see --bind/rbind) <file> regular file for loopdev setup Operations: -B, --bind mount a subtree somewhere else (same as -o bind) -M, --move move a subtree to some other place -R, --rbind mount a subtree and all submounts somewhere else --make-shared mark a subtree as shared --make-slave mark a subtree as slave --make-private mark a subtree as private --make-unbindable mark a subtree as unbindable --make-rshared recursively mark a whole subtree as shared --make-rslave recursively mark a whole subtree as slave --make-rprivate recursively mark a whole subtree as private --make-runbindable recursively mark a whole subtree as unbindable For more details see mount(8). [root@yinzhengjie.com ~]#
2>.基於UUID方式掛載設備

[root@yinzhengjie.com ~]# lsblk /dev/sdb NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb 8:16 0 600G 0 disk ├─sdb1 8:17 0 100G 0 part ├─sdb2 8:18 0 200G 0 part ├─sdb3 8:19 0 1K 0 part ├─sdb5 8:21 0 100G 0 part └─sdb6 8:22 0 80G 0 part [root@yinzhengjie.com ~]#

[root@yinzhengjie.com ~]# mkfs.ext4 /dev/sdb5 mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 6553600 inodes, 26214400 blocks 1310720 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2174746624 800 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, 7962624, 11239424, 20480000, 23887872 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done [root@yinzhengjie.com ~]#

[root@yinzhengjie.com ~]# blkid /dev/sdb5 /dev/sdb5: UUID="075b12f8-0a1f-4aef-a393-3f8fb6240e43" TYPE="ext4" [root@yinzhengjie.com ~]#

[root@yinzhengjie.com ~]# ll /mnt/ total 0 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# mkdir /mnt/sdb5/ #創建一個空目錄,用於掛載指定分區 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# ll /mnt/ -R /mnt/: total 0 drwxr-xr-x 2 root root 6 May 27 15:22 sdb5 /mnt/sdb5: total 0 [root@yinzhengjie.com ~]#

[root@yinzhengjie.com ~]# ll /mnt/ -R /mnt/: total 0 drwxr-xr-x 2 root root 6 May 27 15:22 sdb5 /mnt/sdb5: total 0 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# blkid /dev/sdb5 /dev/sdb5: UUID="075b12f8-0a1f-4aef-a393-3f8fb6240e43" TYPE="ext4" [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# mount -U 075b12f8-0a1f-4aef-a393-3f8fb6240e43 /mnt/sdb5 #生產環境中推薦大家基於UUID的方式進行掛載,雖然可讀性較差,但穩定性很強。 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# ll /mnt/ -R /mnt/: total 4 drwxr-xr-x 3 root root 4096 May 27 15:19 sdb5 /mnt/sdb5: total 16 drwx------ 2 root root 16384 May 27 15:19 lost+found #如果有"lost+found"目錄,說明掛載成功啦~ /mnt/sdb5/lost+found: total 0 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]#

[root@yinzhengjie.com ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 500G 3.5G 497G 1% / devtmpfs 2.0G 0 2.0G 0% /dev tmpfs 2.0G 0 2.0G 0% /dev/shm tmpfs 2.0G 12M 2.0G 1% /run tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup /dev/sda2 1014M 133M 882M 14% /boot /dev/mapper/centos-yinzhengjie 1.6T 416M 1.6T 1% /yinzhengjie tmpfs 394M 0 394M 0% /run/user/0 /dev/sdb5 99G 61M 94G 1% /mnt/sdb5 [root@yinzhengjie.com ~]#
3>.以只讀的方式掛載(一般用於保護分區被寫入)

[root@yinzhengjie.com ~]# df -h | grep /dev/sdb5 /dev/sdb5 99G 61M 94G 1% /mnt/sdb5 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# mount | grep /dev/sdb5 #很明顯,默認是以讀寫("rw")的方式掛載 /dev/sdb5 on /mnt/sdb5 type ext4 (rw,relatime,data=ordered) [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# umount /mnt/sdb5 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# mount -r -U 075b12f8-0a1f-4aef-a393-3f8fb6240e43 /mnt/sdb5 #以只讀的方式掛載~ [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# mount | grep /dev/sdb5 #下面的參數有"ro",說明的確是以只讀方式掛載 /dev/sdb5 on /mnt/sdb5 type ext4 (ro,relatime,data=ordered) [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# df -h | grep /dev/sdb5 /dev/sdb5 99G 61M 94G 1% /mnt/sdb5 [root@yinzhengjie.com ~]#
4>.將一個目錄綁定在另一個目錄掛載的設備

[root@yinzhengjie.com ~]# grep /dev/sda2 /etc/mtab /dev/sda2 /boot xfs rw,relatime,attr2,inode64,noquota 0 0 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# mkdir /mnt/boot [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# ls /mnt/boot/ [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# mount -B /boot/ /mnt/boot/ #將/mnt/boot目錄掛載到/boot目錄,其本質是將/boot目錄對應的分區掛載到/mnt/boot上 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# ls /mnt/boot/ config-3.10.0-957.el7.x86_64 grub2 symvers-3.10.0-957.el7.x86_64.gz vmlinuz-3.10.0-957.el7.x86_64 efi initramfs-0-rescue-d637a9e4c24d4d11bed0c09151ac78e2.img System.map-3.10.0-957.el7.x86_64 grub initramfs-3.10.0-957.el7.x86_64.img vmlinuz-0-rescue-d637a9e4c24d4d11bed0c09151ac78e2 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# grep /dev/sda2 /etc/mtab /dev/sda2 /boot xfs rw,relatime,attr2,inode64,noquota 0 0 /dev/sda2 /mnt/boot xfs rw,relatime,attr2,inode64,noquota 0 0 [root@yinzhengjie.com ~]#
5>.禁止在文件系統上運行程序

[root@yinzhengjie.com ~]# grep /dev/sdb5 /etc/mtab /dev/sdb5 /mnt/sdb5 ext4 rw,relatime,data=ordered 0 0 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# vim /mnt/sdb5/test.sh [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# cat /mnt/sdb5/test.sh #!/bin/bash echo "HostName : `hostname` " echo "Blog Address: https://www.cnblogs.com/yinzhengjie/" [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# chmod +x /mnt/sdb5/test.sh [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# /mnt/sdb5/test.sh #重新掛載之前,我們是可以執行該腳本的 HostName : yinzhengjie.com Blog Address: https://www.cnblogs.com/yinzhengjie/ [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# mount -o remount,noexec /dev/sdb5 /mnt/sdb5 #我們重新掛載磁盤並在掛載時指定了禁止在文件系統上運行程序 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# ll /mnt/sdb5/ total 20 drwx------ 2 root root 16384 May 27 15:19 lost+found -rwxr-xr-x 1 root root 103 May 27 16:56 test.sh #該文件是由執行權限的 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# /mnt/sdb5/test.sh #盡管有執行權限,也無法運行,因為我們在掛載時指定了noexec參數。 -bash: /mnt/sdb5/test.sh: Permission denied [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# grep /dev/sdb5 /etc/mtab /dev/sdb5 /mnt/sdb5 ext4 rw,noexec,relatime,data=ordered 0 0 [root@yinzhengjie.com ~]#
6>.允許在文件系統上運行程序

[root@yinzhengjie.com ~]# grep /dev/sdb5 /etc/mtab /dev/sdb5 /mnt/sdb5 ext4 rw,noexec,relatime,data=ordered 0 0 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# ll /mnt/sdb5 total 20 drwx------ 2 root root 16384 May 27 15:19 lost+found -rwxr-xr-x 1 root root 103 May 27 16:56 test.sh [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# /mnt/sdb5/test.sh -bash: /mnt/sdb5/test.sh: Permission denied [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# mount -o remount,exec /dev/sdb5 /mnt/sdb5 #掛載時允許在文件系統執行應用程序,如果不指定默認情況下是允許的。 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# /mnt/sdb5/test.sh HostName : yinzhengjie.com Blog Address: https://www.cnblogs.com/yinzhengjie/ [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]#
7>.使用loop設備案例

[root@yinzhengjie.com ~]# mkdir /yinzhengjie/data [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# ll -h /yinzhengjie/data/ total 0 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# dd if=/dev/zero of=/yinzhengjie/data/ext4file bs=1M count=1024 #創建一個1G的普通文件 1024+0 records in 1024+0 records out 1073741824 bytes (1.1 GB) copied, 9.3032 s, 115 MB/s [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# ll -h /yinzhengjie/data/ total 1.0G -rw-r--r-- 1 root root 1.0G May 27 18:20 ext4file [root@yinzhengjie.com ~]#

[root@yinzhengjie.com ~]# mkfs.ext4 /yinzhengjie/data/ext4file mke2fs 1.42.9 (28-Dec-2013) /yinzhengjie/data/ext4file is not a block special device. Proceed anyway? (y,n) y Discarding device blocks: done Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 65536 inodes, 262144 blocks 13107 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=268435456 8 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376 Allocating group tables: done Writing inode tables: done Creating journal (8192 blocks): done Writing superblocks and filesystem accounting information: done [root@yinzhengjie.com ~]#

[root@yinzhengjie.com ~]# ll /yinzhengjie/data/ext4file -rw-r--r-- 1 root root 1073741824 May 27 18:27 /yinzhengjie/data/ext4file [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# blkid /yinzhengjie/data/ext4file #查看塊設備設備屬性信息,也可以看到該文件被格式化成了一個EXT4文件系統 /yinzhengjie/data/ext4file: UUID="dd7d317c-7f87-4360-a803-2b31eca4955f" TYPE="ext4" [root@yinzhengjie.com ~]#

[root@yinzhengjie.com ~]# mkdir /mnt/ext4 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# ll /mnt/ext4/ total 0 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# mount /yinzhengjie/data/ext4file /mnt/ext4 #將格式化的文件掛載到操作系統上 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# ll /mnt/ext4/ total 16 drwx------ 2 root root 16384 May 27 18:27 lost+found #該目錄用來存放文件系統崩潰時的文件 [root@yinzhengjie.com ~]#

[root@yinzhengjie.com ~]# df -h | grep /mnt/ext4 /dev/loop0 976M 2.6M 907M 1% /mnt/ext4 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# losetup -a #查看loop設備 /dev/loop0: [64769]:495 (/yinzhengjie/data/ext4file) [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# ll /dev/loop* brw-rw---- 1 root disk 7, 0 May 27 18:46 /dev/loop0 #該loop文件是自動創建的,在CentOS 6.x操作系統中,默認會創建8個loop文件喲~ crw-rw---- 1 root disk 10, 237 May 27 18:46 /dev/loop-control [root@yinzhengjie.com ~]#

[root@yinzhengjie.com ~]# ll /mnt/ext4/ total 16 drwx------ 2 root root 16384 May 27 18:27 lost+found [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# cp /etc/fstab /etc/issue /mnt/ext4/ #我們使用掛載目錄了,當您卸載該掛載點,可以loop設備文件拷貝到其它服務器節點進行掛載,數據並不會丟失喲~ [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# ll /mnt/ext4/ total 24 -rw-r--r-- 1 root root 490 May 27 18:53 fstab -rw-r--r-- 1 root root 23 May 27 18:53 issue drwx------ 2 root root 16384 May 27 18:27 lost+found [root@yinzhengjie.com ~]#
三.查看掛載目錄信息
1>.查看掛載情況

[root@yinzhengjie.com ~]# findmnt /mnt/sdb5 TARGET SOURCE FSTYPE OPTIONS /mnt/sdb5 /dev/sdb5 ext4 rw,relatime,data=ordered [root@yinzhengjie.com ~]#
2>.查看正在訪問指定文件系統的進程

[root@yinzhengjie.com ~]# lsof /mnt/sdb5/ COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME bash 6962 root cwd DIR 8,21 4096 2 /mnt/sdb5 [root@yinzhengjie.com ~]#

[root@yinzhengjie.com ~]# fuser -v /mnt/sdb5/ USER PID ACCESS COMMAND /mnt/sdb5: root kernel mount /mnt/sdb5 root 6962 ..c.. bash [root@yinzhengjie.com ~]#
3>.終止所有在正訪問指定的文件系統的進程

[root@yinzhengjie.com ~]# fuser -v /mnt/sdb5 USER PID ACCESS COMMAND /mnt/sdb5: root kernel mount /mnt/sdb5 root 6962 ..c.. bash [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# fuser -km /mnt/sdb5 /mnt/sdb5: 6962c [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# fuser -km /mnt/sdb5 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]#
四.用umount命令臨時卸載文件系統案例
1>.基於分區卸載

[root@yinzhengjie.com ~]# cat /etc/mtab | grep /dev/sdb5 /dev/sdb5 /mnt/sdb5 ext4 ro,relatime,data=ordered 0 0 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# mount | grep /dev/sdb5 /dev/sdb5 on /mnt/sdb5 type ext4 (ro,relatime,data=ordered) [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# umount /dev/sdb5 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# cat /etc/mtab | grep /dev/sdb5 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# mount | grep /dev/sdb5 [root@yinzhengjie.com ~]#
2>.基於掛載點卸載

[root@yinzhengjie.com ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 500G 3.5G 497G 1% / devtmpfs 2.0G 0 2.0G 0% /dev tmpfs 2.0G 0 2.0G 0% /dev/shm tmpfs 2.0G 12M 2.0G 1% /run tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup /dev/sda2 1014M 133M 882M 14% /boot /dev/mapper/centos-yinzhengjie 1.6T 448M 1.6T 1% /yinzhengjie /dev/sdb5 99G 61M 94G 1% /mnt/sdb5 /dev/loop0 976M 2.6M 907M 1% /mnt/ext4 tmpfs 394M 0 394M 0% /run/user/0 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# umount /mnt/sdb5 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 500G 3.5G 497G 1% / devtmpfs 2.0G 0 2.0G 0% /dev tmpfs 2.0G 0 2.0G 0% /dev/shm tmpfs 2.0G 12M 2.0G 1% /run tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup /dev/sda2 1014M 133M 882M 14% /boot /dev/mapper/centos-yinzhengjie 1.6T 448M 1.6T 1% /yinzhengjie /dev/loop0 976M 2.6M 907M 1% /mnt/ext4 tmpfs 394M 0 394M 0% /run/user/0 [root@yinzhengjie.com ~]#
五.文件掛載配置文件("/etc/fstab")
1>."/etc/fstab"概述
前面我們介紹了mount和fask等命令的使用方式,但這些命令在服務器重啟后之前的掛載信息也會面臨丟失。為了解決這個問題,需要將掛載信息寫入到配置文件中。
Linux操作系統默認使用"/etc/fstab"來保留系統重啟時文件系統體系。在操作系統重啟后會自動使用"mount -a"命令掛載"/etc/fstab"中的所有文件系統。
2>."/etc/fstab"定義格式
"/etc/fstab"每行定義一個要掛載的文件系統,其中每行被分為六列,每列使用一個或多個空格隔開,接下來我們來一起探討一下每一列代表的意義。
第一列:
要掛載的設備或偽文件系統,支持以下幾種類型(推薦大家使用基於UUID的方式進行掛載):
(1)設備文件,比如:"/dev/sdb5";
(2)卷標名稱,比如:"LABEL=/mnt/sdb5";
(3)UUID,比如:"UUID=1865a93f-6113-4097-89dc-8c4ea5fdf68c";
(4)偽文件系統名稱,比如:"proc,sysfs等";
第二列:
指定掛載點.比如:"/boot",需要注意的是,掛載點必須存在,若掛載點不存在則會報錯。
第三列:
指定文件系統類型,比如"ext4,xfs,nfs,none等"
第四列:
指定掛載選項,比如:"defaults,acl,bind等"
第五列:
指定轉存頻率,有以下3個選項:
"0":
表示不做備份,生產環境中建議配置為0,若要備份數據建議手動備份。
"1":
表示每天轉儲,需要結合其它軟件使用。
"2":
每隔一天轉儲
第六列:
fsck檢查的文件系統順序,允許的數字是0,1,2。
"0":
表示開機不自檢該設備。如果設置非"0"則會調用"fack"命令進行自檢。
"1":
首先自檢,一般只有rootfs才用。
"2":
非rootfs使用。
溫馨提示:
生產環境中強烈建議大家配置第5列和第6列均為"0"。
第五列配置為"0"表示不做備份:
若配置非"0"表示做備份,但其備份是需要您結合其它軟件實現備份功能,這無疑增加您的學習成本。
生產環境中我們運維人員都會手動備份重要的數據,因此強烈建議大家將改列配置為"0"
第六列配置為"0"表示不自檢:
若配置非"0"表示自檢,當操作系統啟動時會自檢該文件系統,且先不說自檢消費時間,當自檢失敗時會直接到此操作系統啟動失敗。
操作系統啟動失敗意味着無法遠程該服務器解決問題,此時你的服務器若沒有購買遠程管理卡那你就得去機房處理問題了。
設置為"0"的好處不僅僅是加快開啟啟動速度,最主要的還是當文件系統被破壞時操作系統照樣可以正常啟動,這樣您就可以基於IP方式連接服務器使用fack命令去修復該文件系統了。
3>.編寫配置文件掛載案例
[root@yinzhengjie.com ~]# cat /etc/fstab # # /etc/fstab # Created by anaconda on Mon Jan 20 04:18:36 2020 # # 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=1865a93f-6113-4097-89dc-8c4ea5fdf68c /boot xfs defaults 0 0 #Add by yinzhengjie /dev/mapper/centos-yinzhengjie /yinzhengjie xfs defaults,noatime,nodiratime 0 0 UUID=075b12f8-0a1f-4aef-a393-3f8fb6240e43 /mnt/sdb5 ext4 defaults,noexec 0 0 /boot /mnt/boot none bind 0 0 /yinzhengjie/data/ext4file /mnt/ext4 ext4 loop 0 0
/dev/sr0 /mnt/cdrom iso9660 defaults 0 0 [root@yinzhengjie.com ~]#

[root@yinzhengjie.com ~]# cat /var/log/boot.log [ OK ] Started Show Plymouth Boot Screen. [ OK ] Reached target Paths. [ OK ] Started Forward Password Requests to Plymouth Directory Watch. [ OK ] Reached target Basic System. [ OK ] Found device /dev/mapper/centos-root. Starting File System Check on /dev/mapper/centos-root... [ OK ] Started dracut initqueue hook. [ OK ] Reached target Remote File Systems (Pre). [ OK ] Reached target Remote File Systems. [ OK ] Started File System Check on /dev/mapper/centos-root. Mounting /sysroot... [ OK ] Mounted /sysroot. [ OK ] Reached target Initrd Root File System. Starting Reload Configuration from the Real Root... [ OK ] Started Reload Configuration from the Real Root. [ OK ] Reached target Initrd File Systems. [ OK ] Reached target Initrd Default Target. Starting dracut pre-pivot and cleanup hook... [ OK ] Started dracut pre-pivot and cleanup hook. Starting Cleaning Up and Shutting Down Daemons... [ OK ] Stopped target Timers. Starting Plymouth switch root service... [ OK ] Stopped Cleaning Up and Shutting Down Daemons. [ OK ] Stopped dracut pre-pivot and cleanup hook. [ OK ] Stopped target Remote File Systems. [ OK ] Stopped target Remote File Systems (Pre). [ OK ] Stopped dracut initqueue hook. [ OK ] Stopped target Initrd Default Target. [ OK ] Stopped target Basic System. [ OK ] Stopped target Paths. [ OK ] Stopped target Slices. [ OK ] Stopped target Sockets. [ OK ] Stopped target System Initialization. [ OK ] Stopped target Local File Systems. Stopping udev Kernel Device Manager... [ OK ] Stopped Apply Kernel Variables. [ OK ] Stopped udev Coldplug all Devices. [ OK ] Stopped target Swap. [ OK ] Stopped udev Kernel Device Manager. [ OK ] Stopped dracut pre-udev hook. [ OK ] Stopped dracut cmdline hook. [ OK ] Stopped Create Static Device Nodes in /dev. [ OK ] Stopped Create list of required static device nodes for the current kernel. [ OK ] Closed udev Kernel Socket. [ OK ] Closed udev Control Socket. Starting Cleanup udevd DB... [ OK ] Started Cleanup udevd DB. [ OK ] Reached target Switch Root. [ OK ] Started Plymouth switch root service. Starting Switch Root... Welcome to CentOS Linux 7 (Core)! [ OK ] Stopped Switch Root. [ OK ] Stopped Journal Service. Starting Journal Service... [ OK ] Reached target Remote File Systems. [ OK ] Set up automount Arbitrary Executable File Formats File System Automount Point. [ OK ] Reached target Swap. Mounting Debug File System... [ OK ] Listening on /dev/initctl Compatibility Named Pipe. [ OK ] Stopped target Switch Root. [ OK ] Stopped target Initrd Root File System. [ OK ] Started Forward Password Requests to Wall Directory Watch. [ OK ] Created slice system-selinux\x2dpolicy\x2dmigrate\x2dlocal\x2dchanges.slice. [ OK ] Stopped File System Check on Root Device. [ OK ] Created slice system-getty.slice. [ OK ] Stopped target Initrd File Systems. [ OK ] Listening on Delayed Shutdown Socket. [ OK ] Listening on LVM2 metadata daemon socket. Starting Create list of required static device nodes for the current kernel... Starting Apply Kernel Variables... Starting Remount Root and Kernel File Systems... Mounting POSIX Message Queue File System... Starting Read and set NIS domainname from /etc/sysconfig/network... [ OK ] Listening on LVM2 poll daemon socket. [ OK ] Reached target Local Encrypted Volumes. Mounting Huge Pages File System... [ OK ] Created slice User and Session Slice. [ OK ] Reached target Slices. [ OK ] Listening on udev Control Socket. [ OK ] Reached target Paths. [ OK ] Listening on Device-mapper event daemon FIFOs. Starting Monitoring of LVM2 mirrors, snapshots etc. using dmeventd or progress polling... [ OK ] Listening on udev Kernel Socket. [ OK ] Mounted Debug File System. [ OK ] Mounted POSIX Message Queue File System. [ OK ] Mounted Huge Pages File System. [ OK ] Started Journal Service. [ OK ] Started Create list of required static device nodes for the current kernel. [ OK ] Started Apply Kernel Variables. [ OK ] Started Remount Root and Kernel File Systems. [ OK ] Started Read and set NIS domainname from /etc/sysconfig/network. [ OK ] Started LVM2 metadata daemon. Starting udev Coldplug all Devices... Starting Configure read-only root support... Starting Create Static Device Nodes in /dev... Starting Flush Journal to Persistent Storage... [ OK ] Started Flush Journal to Persistent Storage. [ OK ] Started Create Static Device Nodes in /dev. [ OK ] Started Configure read-only root support. [ OK ] Started udev Coldplug all Devices. Starting Load/Save Random Seed... Starting udev Kernel Device Manager... [ OK ] Started Load/Save Random Seed. [ OK ] Started udev Kernel Device Manager. [ OK ] Found device VMware_Virtual_S 2. [ OK ] Found device VMware_Virtual_IDE_CDROM_Drive. [ OK ] Created slice system-lvm2\x2dpvscan.slice. Starting LVM2 PV scan on device 8:3... [ OK ] Found device VMware_Virtual_S 5. [ OK ] Started Monitoring of LVM2 mirrors, snapshots etc. using dmeventd or progress polling. [ OK ] Reached target Local File Systems (Pre). Mounting /mnt/sdb5... Mounting /boot... Mounting /mnt/cdrom... [FAILED] Failed to mount /mnt/cdrom. See 'systemctl status mnt-cdrom.mount' for details. [DEPEND] Dependency failed for Local File Systems. [DEPEND] Dependency failed for Migrate local SELinux p... old store structure to the new structure. [DEPEND] Dependency failed for Relabel all filesystems, if necessary. [ OK ] Reached target Timers. [ OK ] Reached target Login Prompts. [ OK ] Reached target Network. [ OK ] Reached target Network is Online. Starting Import network configuration from initramfs... Starting Tell Plymouth To Write Out Runtime Data... [ OK ] Reached target Sockets. [ OK ] Started Tell Plymouth To Write Out Runtime Data. [ OK ] Mounted /mnt/sdb5. [ OK ] Started Import network configuration from initramfs. [ OK ] Started Emergency Shell. [ OK ] Reached target Emergency Mode. Starting Create Volatile Files and Directories... [ OK ] Created slice system-systemd\x2drfkill.slice. Starting Load/Save RF Kill Switch Status of rfkill0... [ OK ] Reached target Bluetooth. [ OK ] Started Load/Save RF Kill Switch Status of rfkill0. [ OK ] Started Create Volatile Files and Directories. Starting Security Auditing Service... [ OK ] Mounted /boot. Mounting /mnt/boot... [ OK ] Mounted /mnt/boot. [ OK ] Started Security Auditing Service. Starting Update UTMP about System Boot/Shutdown... [ OK ] Started Update UTMP about System Boot/Shutdown. Starting Update UTMP about System Runlevel Changes... [ OK ] Started Update UTMP about System Runlevel Changes. [ OK ] Reached target Sound Card. [ OK ] Found device /dev/mapper/centos-yinzhengjie. Mounting /yinzhengjie... [ OK ] Started LVM2 PV scan on device 8:3. [ OK ] Mounted /yinzhengjie. Mounting /mnt/ext4... [ OK ] Mounted /mnt/ext4. [ OK ] Started Show Plymouth Boot Screen. [ OK ] Started Forward Password Requests to Plymouth Directory Watch. [ OK ] Reached target Paths. [ OK ] Reached target Basic System. [ OK ] Found device /dev/mapper/centos-root. Starting File System Check on /dev/mapper/centos-root... [ OK ] Started dracut initqueue hook. [ OK ] Reached target Remote File Systems (Pre). [ OK ] Reached target Remote File Systems. [ OK ] Started File System Check on /dev/mapper/centos-root. Mounting /sysroot... [ OK ] Mounted /sysroot. [ OK ] Reached target Initrd Root File System. Starting Reload Configuration from the Real Root... [ OK ] Started Reload Configuration from the Real Root. [ OK ] Reached target Initrd File Systems. [ OK ] Reached target Initrd Default Target. Starting dracut pre-pivot and cleanup hook... [ OK ] Started dracut pre-pivot and cleanup hook. Starting Cleaning Up and Shutting Down Daemons... Starting Plymouth switch root service... [ OK ] Stopped target Timers. [ OK ] Stopped Cleaning Up and Shutting Down Daemons. [ OK ] Stopped dracut pre-pivot and cleanup hook. [ OK ] Stopped target Remote File Systems. [ OK ] Stopped target Remote File Systems (Pre). [ OK ] Stopped target Initrd Default Target. [ OK ] Stopped target Basic System. [ OK ] Stopped target Sockets. [ OK ] Stopped target System Initialization. [ OK ] Stopped target Swap. Stopping udev Kernel Device Manager... [ OK ] Stopped Apply Kernel Variables. [ OK ] Stopped target Local File Systems. [ OK ] Stopped target Slices. [ OK ] Stopped target Paths. [ OK ] Stopped dracut initqueue hook. [ OK ] Stopped udev Coldplug all Devices. [ OK ] Stopped udev Kernel Device Manager. [ OK ] Stopped dracut pre-udev hook. [ OK ] Stopped dracut cmdline hook. [ OK ] Stopped Create Static Device Nodes in /dev. [ OK ] Stopped Create list of required static device nodes for the current kernel. [ OK ] Closed udev Control Socket. [ OK ] Closed udev Kernel Socket. Starting Cleanup udevd DB... [ OK ] Started Cleanup udevd DB. [ OK ] Reached target Switch Root. [ OK ] Started Plymouth switch root service. Starting Switch Root... Welcome to CentOS Linux 7 (Core)! [ OK ] Stopped Switch Root. [ OK ] Stopped Journal Service. Starting Journal Service... [ OK ] Stopped File System Check on Root Device. Mounting POSIX Message Queue File System... [ OK ] Reached target Remote File Systems. [ OK ] Listening on /dev/initctl Compatibility Named Pipe. [ OK ] Listening on LVM2 poll daemon socket. [ OK ] Listening on udev Kernel Socket. [ OK ] Reached target Local Encrypted Volumes. [ OK ] Listening on LVM2 metadata daemon socket. Starting Remount Root and Kernel File Systems... [ OK ] Listening on Device-mapper event daemon FIFOs. [ OK ] Created slice system-selinux\x2dpolicy\x2dmigrate\x2dlocal\x2dchanges.slice. [ OK ] Listening on udev Control Socket. Mounting Huge Pages File System... [ OK ] Stopped target Switch Root. [ OK ] Stopped target Initrd Root File System. [ OK ] Created slice system-getty.slice. Mounting Debug File System... Starting Create list of required static device nodes for the current kernel... [ OK ] Reached target Swap. [ OK ] Created slice User and Session Slice. [ OK ] Reached target Slices. Starting Read and set NIS domainname from /etc/sysconfig/network... [ OK ] Set up automount Arbitrary Executable File Formats File System Automount Point. Starting Apply Kernel Variables... [ OK ] Listening on Delayed Shutdown Socket. [ OK ] Stopped target Initrd File Systems. Starting Monitoring of LVM2 mirrors, snapshots etc. using dmeventd or progress polling... [ OK ] Started Forward Password Requests to Wall Directory Watch. [ OK ] Reached target Paths. [ OK ] Mounted Debug File System. [ OK ] Mounted POSIX Message Queue File System. [ OK ] Mounted Huge Pages File System. [ OK ] Started Journal Service. [ OK ] Started Remount Root and Kernel File Systems. [ OK ] Started Create list of required static device nodes for the current kernel. [ OK ] Started Read and set NIS domainname from /etc/sysconfig/network. [ OK ] Started Apply Kernel Variables. [ OK ] Started LVM2 metadata daemon. Starting Create Static Device Nodes in /dev... Starting Configure read-only root support... Starting udev Coldplug all Devices... Starting Flush Journal to Persistent Storage... [ OK ] Started Create Static Device Nodes in /dev. [ OK ] Started Configure read-only root support. Starting Load/Save Random Seed... Starting udev Kernel Device Manager... [ OK ] Started Flush Journal to Persistent Storage. [ OK ] Started udev Coldplug all Devices. [ OK ] Started Load/Save Random Seed. [ OK ] Started udev Kernel Device Manager. [ OK ] Created slice system-lvm2\x2dpvscan.slice. Starting LVM2 PV scan on device 8:3... [ OK ] Found device VMware_Virtual_S 2. [ OK ] Found device VMware_Virtual_S 5. [ OK ] Started Monitoring of LVM2 mirrors, snapshots etc. using dmeventd or progress polling. [ OK ] Reached target Local File Systems (Pre). Mounting /boot... Mounting /mnt/sdb5... [ OK ] Mounted /mnt/sdb5. [ OK ] Mounted /boot. Mounting /mnt/boot... [ OK ] Mounted /mnt/boot. [ OK ] Reached target Bluetooth. [ OK ] Created slice system-systemd\x2drfkill.slice. Starting Load/Save RF Kill Switch Status of rfkill0... [ OK ] Started Load/Save RF Kill Switch Status of rfkill0. [ OK ] Reached target Sound Card. [ OK ] Found device /dev/mapper/centos-yinzhengjie. Mounting /yinzhengjie... [ OK ] Started LVM2 PV scan on device 8:3. [ OK ] Mounted /yinzhengjie. Mounting /mnt/ext4... [ OK ] Mounted /mnt/ext4. [ OK ] Reached target Local File Systems. Starting Tell Plymouth To Write Out Runtime Data... Starting Import network configuration from initramfs... [ OK ] Started Tell Plymouth To Write Out Runtime Data. [ OK ] Started Import network configuration from initramfs. Starting Create Volatile Files and Directories... [ OK ] Started Create Volatile Files and Directories. Starting Security Auditing Service... [ OK ] Started Security Auditing Service. Starting Update UTMP about System Boot/Shutdown... [ OK ] Started Update UTMP about System Boot/Shutdown. [ OK ] Reached target System Initialization. [ OK ] Listening on D-Bus System Message Bus Socket. Starting Docker Socket for the API. [ OK ] Started Daily Cleanup of Temporary Directories. [ OK ] Reached target Timers. [ OK ] Listening on Docker Socket for the API. [ OK ] Reached target Sockets. [ OK ] Reached target Basic System. Starting Login Service... Starting Permit User Sessions... Starting LSB: Bring up/down networking... [ OK ] Started D-Bus System Message Bus. Starting Dump dmesg to /var/log/dmesg... Starting Authorization Manager... [ OK ] Started irqbalance daemon. [ OK ] Started Permit User Sessions. Starting Wait for Plymouth Boot Screen to Quit... Starting Terminate Plymouth Boot Screen... [ OK ] Started Command Scheduler. [ OK ] Started Login Service. May 28 08:28:16 yinzhengjie NET[5005]: /etc/sysconfig/network-scripts/ifup-post : updated /etc/resolv.conf [root@yinzhengjie.com ~]#
六.swap分區掛載
1>.配置swap的流程
交換分區是系統RAM的補充
基本設置包括: 創建交換分區或者文件 使用mkswap寫入特殊簽名 在/etc/fstab文件中添加適當的條目 使用swapon-a 激活交換空間
2>.SWAP的優先級
swapon(啟用swap分區) swapon[OPTION]... [DEVICE] -a:
激活所有的交換分區 -p PRIORITY:
指定優先級 在"/etc/fstab"中指定優先級可以修改第四列:"pri=value" swapoff(禁用swap分區):
swapoff[OPTION]... [DEVICE] 溫馨提示: (1)可以指定swap分區0到32767的優先級,值越大優先級越高 (2)如果用戶沒有指定,那么核心會自動給swap指定一個優先級,這個優先級從-1開始,每加入一個新的沒有用戶指定優先級的swap,會給這個優先級減一 (3)先添加的swap的缺省優先級比較高,除非用戶自己指定一個優先級,而用戶指定的優先級(是正數)永遠高於核心缺省指定的優先級(是負數) (3)優化性能:分布存放,高性能磁盤存放
3>.swap分區使用注意事項
我本人並不推薦大家使用swap分區,因為它會降低服務器性能。 早期由於工業原因內存相對較貴,因此很多軟件在設計之初都考慮盡可能的使用磁盤來代替內存,但是磁盤的I/O性能要和內存的I/O性能完全是天壤之別。比如在大數據領域Hadoop的一個MapReduce組件,該計算框架就盡可能使用磁盤,這是導致它計算速度很慢的一個原因,這也是為什么后來Spark和Flink崛起埋下伏筆。 因此,在生產環境中我們應該盡量禁用虛擬內存,比如阿里雲的服務器默認就是禁用虛擬內存的,我在生產環境中也是直接禁用虛擬內存的。但有的服務器內存相對較小,比如8G,擔心程序發生OOM.於是為了留一手才被迫使用虛擬內存。 如果非要使用虛擬內存建議參考以下幾點: (1)盡可能使用較塊的設備,比如固態硬盤; (2)如果使用磁盤建議使用扇區靠外的分區,這樣在查找數據時速度相對較快(雖然可以使用其它文件系統的文件來代替swap分區,但性能沒有直接分配的swap分區性能高); (3)生產環境中虛擬磁盤不建議超過8G; 博主推薦閱讀: https://www.cnblogs.com/yinzhengjie/p/9994207.html
七.移動介質掛載
掛載意味着使外來的文件系統看起來如同是主目錄樹的一部分
訪問前、介質必須被掛載
摘除時,介質必須被卸載
按照默認設置,非root用戶只能掛載某些設備(光盤、DVD、軟盤、USB等等)
掛載點通常在/media 或/mnt下
1>.使用光盤
在圖形環境下自動啟動掛載/run/media/<user>/<label>,否則就必須被手工掛載 mount /dev/cdrom/mnt/
命令卸載或彈出磁盤:
eject #彈出 eject -t #彈入
創建ISO文件: cp/dev/cdrom/root/centos7.iso mkisofs -r -o /root/etc.iso /etc #將"/etc"下的所有文件打包成一個文件,其路徑為"/root/etc.iso"
刻錄光盤: wodim–v –eject centos.iso
2>.掛載USB介質
查看USB設備是否識別 lsusb
被內核探測為SCSI設備 /dev/sdaX、/dev/sdbX、或類似的設備文件
在圖形環境中自動掛載 圖標在[計算機]窗口中創建 掛載在/run/media/<user>/<label>
手動掛載 mount /dev/sdb1 /mnt
八.遷移"/home"目錄到新分區案例
1>.模擬根文件系統被使用殆盡

[root@yinzhengjie.com ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 500G 3.5G 497G 1% / devtmpfs 2.0G 0 2.0G 0% /dev tmpfs 2.0G 0 2.0G 0% /dev/shm tmpfs 2.0G 12M 2.0G 1% /run tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup /dev/sdb5 99G 61M 94G 1% /mnt/sdb5 /dev/sda2 1014M 133M 882M 14% /boot /dev/mapper/centos-yinzhengjie 1.6T 448M 1.6T 1% /yinzhengjie /dev/loop0 976M 2.6M 907M 1% /mnt/ext4 tmpfs 394M 0 394M 0% /run/user/0 [root@yinzhengjie.com ~]#

[root@yinzhengjie.com ~]# su -l yinzhengjie [yinzhengjie@yinzhengjie.com ~]$ [yinzhengjie@yinzhengjie.com ~]$ ll total 0 [yinzhengjie@yinzhengjie.com ~]$ [yinzhengjie@yinzhengjie.com ~]$ dd if=/dev/zero of=bigfile bs=1G count=30 30+0 records in 30+0 records out 32212254720 bytes (32 GB) copied, 394.759 s, 81.6 MB/s [yinzhengjie@yinzhengjie.com ~]$ [yinzhengjie@yinzhengjie.com ~]$ ll -h total 30G -rw-rw-r-- 1 yinzhengjie yinzhengjie 30G May 28 17:11 bigfile [yinzhengjie@yinzhengjie.com ~]$ [yinzhengjie@yinzhengjie.com ~]$ exit logout [root@yinzhengjie.com ~]#

[root@yinzhengjie.com ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 500G 34G 467G 7% / devtmpfs 2.0G 0 2.0G 0% /dev tmpfs 2.0G 0 2.0G 0% /dev/shm tmpfs 2.0G 12M 2.0G 1% /run tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup /dev/sdb5 99G 61M 94G 1% /mnt/sdb5 /dev/sda2 1014M 133M 882M 14% /boot /dev/mapper/centos-yinzhengjie 1.6T 448M 1.6T 1% /yinzhengjie /dev/loop0 976M 2.6M 907M 1% /mnt/ext4 tmpfs 394M 0 394M 0% /run/user/0 [root@yinzhengjie.com ~]#
2>.准備獨立分區

[root@yinzhengjie.com ~]# lsblk /dev/sdc NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdc 8:32 0 400G 0 disk └─sdc1 8:33 0 100G 0 part [root@yinzhengjie.com ~]#

[root@yinzhengjie.com ~]# fdisk /dev/sdc WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion. Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): p Disk /dev/sdc: 429.5 GB, 429496729600 bytes, 838860800 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: gpt Disk identifier: C4A5426E-BE89-46EE-B842-3CAC96156388 # Start End Size Type Name 1 2048 209717247 100G Linux filesyste Linux filesystem Command (m for help): n Partition number (2-128, default 2): First sector (34-838860766, default 209717248): Last sector, +sectors or +size{K,M,G,T,P} (209717248-838860766, default 838860766): +200G Created partition 2 Command (m for help): p Disk /dev/sdc: 429.5 GB, 429496729600 bytes, 838860800 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: gpt Disk identifier: C4A5426E-BE89-46EE-B842-3CAC96156388 # Start End Size Type Name 1 2048 209717247 100G Linux filesyste Linux filesystem 2 209717248 629147647 200G Linux filesyste Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@yinzhengjie.com ~]#

[root@yinzhengjie.com ~]# lsblk /dev/sdc NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdc 8:32 0 400G 0 disk ├─sdc1 8:33 0 100G 0 part └─sdc2 8:34 0 200G 0 part [root@yinzhengjie.com ~]#

[root@yinzhengjie.com ~]# mkfs.xfs /dev/sdc2 meta-data=/dev/sdc2 isize=512 agcount=4, agsize=13107200 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=52428800, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=25600, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 [root@yinzhengjie.com ~]#

[root@yinzhengjie.com ~]# blkid /dev/sdc2 /dev/sdc2: UUID="2307c9c5-f7b1-4a2b-b9e9-46d89a9c7c81" TYPE="xfs" PARTUUID="e23adba5-81f3-401a-a651-d46c6bff6ccb" [root@yinzhengjie.com ~]#

[root@yinzhengjie.com ~]# mkdir /mnt/home [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# mount /dev/sdc2 /mnt/home [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# ll /mnt/home/ total 0 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 500G 34G 467G 7% / devtmpfs 2.0G 0 2.0G 0% /dev tmpfs 2.0G 0 2.0G 0% /dev/shm tmpfs 2.0G 12M 2.0G 1% /run tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup /dev/sdb5 99G 61M 94G 1% /mnt/sdb5 /dev/sda2 1014M 133M 882M 14% /boot /dev/mapper/centos-yinzhengjie 1.6T 448M 1.6T 1% /yinzhengjie /dev/loop0 976M 2.6M 907M 1% /mnt/ext4 tmpfs 394M 0 394M 0% /run/user/0 /dev/sdc2 200G 33M 200G 1% /mnt/home [root@yinzhengjie.com ~]#
3>.拷貝數據

[root@yinzhengjie.com ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 500G 34G 467G 7% / devtmpfs 2.0G 0 2.0G 0% /dev tmpfs 2.0G 0 2.0G 0% /dev/shm tmpfs 2.0G 12M 2.0G 1% /run tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup /dev/sdb5 99G 61M 94G 1% /mnt/sdb5 /dev/sda2 1014M 133M 882M 14% /boot /dev/mapper/centos-yinzhengjie 1.6T 448M 1.6T 1% /yinzhengjie /dev/loop0 976M 2.6M 907M 1% /mnt/ext4 tmpfs 394M 0 394M 0% /run/user/0 /dev/sdc2 200G 33M 200G 1% /mnt/home [root@yinzhengjie.com ~]#

[root@yinzhengjie.com ~]# cp -av /home/. /mnt/home/ #將"/home"目錄下的所有文件拷貝到我們新設備中 ‘/home/./yinzhengjie’ -> ‘/mnt/home/./yinzhengjie’ ‘/home/./yinzhengjie/.bash_logout’ -> ‘/mnt/home/./yinzhengjie/.bash_logout’ ‘/home/./yinzhengjie/.bash_profile’ -> ‘/mnt/home/./yinzhengjie/.bash_profile’ ‘/home/./yinzhengjie/.bashrc’ -> ‘/mnt/home/./yinzhengjie/.bashrc’ ‘/home/./yinzhengjie/bigfile’ -> ‘/mnt/home/./yinzhengjie/bigfile’ ‘/home/./yinzhengjie/.bash_history’ -> ‘/mnt/home/./yinzhengjie/.bash_history’ [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# du -sh /home/ 31G /home/ [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# du -sh /mnt/home/ 31G /mnt/home/ [root@yinzhengjie.com ~]#

[root@yinzhengjie.com ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 500G 34G 467G 7% / devtmpfs 2.0G 0 2.0G 0% /dev tmpfs 2.0G 0 2.0G 0% /dev/shm tmpfs 2.0G 12M 2.0G 1% /run tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup /dev/sdb5 99G 61M 94G 1% /mnt/sdb5 /dev/sda2 1014M 133M 882M 14% /boot /dev/mapper/centos-yinzhengjie 1.6T 448M 1.6T 1% /yinzhengjie /dev/loop0 976M 2.6M 907M 1% /mnt/ext4 tmpfs 394M 0 394M 0% /run/user/0 /dev/sdc2 200G 31G 170G 16% /mnt/home [root@yinzhengjie.com ~]#
4>.永久掛載文件系統

[root@yinzhengjie.com ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 500G 34G 467G 7% / devtmpfs 2.0G 0 2.0G 0% /dev tmpfs 2.0G 0 2.0G 0% /dev/shm tmpfs 2.0G 12M 2.0G 1% /run tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup /dev/sdb5 99G 61M 94G 1% /mnt/sdb5 /dev/sda2 1014M 133M 882M 14% /boot /dev/mapper/centos-yinzhengjie 1.6T 448M 1.6T 1% /yinzhengjie /dev/loop0 976M 2.6M 907M 1% /mnt/ext4 tmpfs 394M 0 394M 0% /run/user/0 /dev/sdc2 200G 31G 170G 16% /mnt/home [root@yinzhengjie.com ~]#

[root@yinzhengjie.com ~]# blkid /dev/sdc2 /dev/sdc2: UUID="2307c9c5-f7b1-4a2b-b9e9-46d89a9c7c81" TYPE="xfs" PARTUUID="e23adba5-81f3-401a-a651-d46c6bff6ccb" [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# tail -2 /etc/fstab #Add by yinzhengjie for mount "/home". UUID=2307c9c5-f7b1-4a2b-b9e9-46d89a9c7c81 /home xfs defaults 0 0 [root@yinzhengjie.com ~]#

[root@yinzhengjie.com ~]# mount -a [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 500G 34G 467G 7% / devtmpfs 2.0G 0 2.0G 0% /dev tmpfs 2.0G 0 2.0G 0% /dev/shm tmpfs 2.0G 12M 2.0G 1% /run tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup /dev/sdb5 99G 61M 94G 1% /mnt/sdb5 /dev/sda2 1014M 133M 882M 14% /boot /dev/mapper/centos-yinzhengjie 1.6T 448M 1.6T 1% /yinzhengjie /dev/loop0 976M 2.6M 907M 1% /mnt/ext4 tmpfs 394M 0 394M 0% /run/user/0 /dev/sdc2 200G 31G 170G 16% /home [root@yinzhengjie.com ~]#
5>.掛在成功后,測試數據有效性
[root@yinzhengjie.com ~]# su -l yinzhengjie #可以正常切換 Last login: Thu May 28 17:49:22 CST 2020 on pts/0 [yinzhengjie@yinzhengjie.com ~]$ [yinzhengjie@yinzhengjie.com ~]$ ll -h #數據也正常,生產環境建議先不要立即刪除之前的數據,盡管測試成功了也暫時先不要刪除,保險起見可以先觀察2~3天 total 30G -rw-rw-r-- 1 yinzhengjie yinzhengjie 30G May 28 17:11 bigfile [yinzhengjie@yinzhengjie.com ~]$ [yinzhengjie@yinzhengjie.com ~]$ exit logout [root@yinzhengjie.com ~]#
6>.刪除根文件系統中的數據

[root@yinzhengjie.com ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 500G 34G 467G 7% / devtmpfs 2.0G 0 2.0G 0% /dev tmpfs 2.0G 0 2.0G 0% /dev/shm tmpfs 2.0G 12M 2.0G 1% /run tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup /dev/sdb5 99G 61M 94G 1% /mnt/sdb5 /dev/sda2 1014M 133M 882M 14% /boot /dev/mapper/centos-yinzhengjie 1.6T 448M 1.6T 1% /yinzhengjie /dev/loop0 976M 2.6M 907M 1% /mnt/ext4 tmpfs 394M 0 394M 0% /run/user/0 /dev/sdc2 200G 31G 170G 16% /home [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]#

[root@yinzhengjie.com ~]# umount /home/ [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 500G 34G 467G 7% / devtmpfs 2.0G 0 2.0G 0% /dev tmpfs 2.0G 0 2.0G 0% /dev/shm tmpfs 2.0G 12M 2.0G 1% /run tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup /dev/sdb5 99G 61M 94G 1% /mnt/sdb5 /dev/sda2 1014M 133M 882M 14% /boot /dev/mapper/centos-yinzhengjie 1.6T 448M 1.6T 1% /yinzhengjie /dev/loop0 976M 2.6M 907M 1% /mnt/ext4 tmpfs 394M 0 394M 0% /run/user/0 /dev/sdc2 200G 31G 170G 16% /mnt/home [root@yinzhengjie.com ~]#

[root@yinzhengjie.com ~]# ll /home/ total 0 drwx------ 2 yinzhengjie yinzhengjie 98 May 28 17:18 yinzhengjie [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# rm -rf /home/* #上一步驟驗證數據有效后,就可以自己酌情考慮刪除原來根目錄下的數據啦。 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# ll /home/ total 0 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]#

[root@yinzhengjie.com ~]# df -h #刪除后可以明顯看出跟文件系統的孔家被釋放了部分空間。 Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 500G 3.5G 497G 1% / devtmpfs 2.0G 0 2.0G 0% /dev tmpfs 2.0G 0 2.0G 0% /dev/shm tmpfs 2.0G 12M 2.0G 1% /run tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup /dev/sdb5 99G 61M 94G 1% /mnt/sdb5 /dev/sda2 1014M 133M 882M 14% /boot /dev/mapper/centos-yinzhengjie 1.6T 448M 1.6T 1% /yinzhengjie /dev/loop0 976M 2.6M 907M 1% /mnt/ext4 tmpfs 394M 0 394M 0% /run/user/0 /dev/sdc2 200G 31G 170G 16% /mnt/home [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]#
7>.重新掛載"/home"

[root@yinzhengjie.com ~]# ll /home/ total 0 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# su -l yinzhengjie Last login: Thu May 28 17:49:27 CST 2020 on pts/0 su: warning: cannot change directory to /home/yinzhengjie: No such file or directory -bash-4.2$ -bash-4.2$ exit logout [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# mount -a [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# ll /home/ total 0 drwx------ 2 yinzhengjie yinzhengjie 98 May 28 17:18 yinzhengjie [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# su -l yinzhengjie Last login: Thu May 28 17:55:47 CST 2020 on pts/0 [yinzhengjie@yinzhengjie.com ~]$ [yinzhengjie@yinzhengjie.com ~]$ ll total 31457280 -rw-rw-r-- 1 yinzhengjie yinzhengjie 32212254720 May 28 17:11 bigfile [yinzhengjie@yinzhengjie.com ~]$ [yinzhengjie@yinzhengjie.com ~]$ exit logout [root@yinzhengjie.com ~]#

[root@yinzhengjie.com ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 500G 3.5G 497G 1% / devtmpfs 2.0G 0 2.0G 0% /dev tmpfs 2.0G 0 2.0G 0% /dev/shm tmpfs 2.0G 12M 2.0G 1% /run tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup /dev/sdb5 99G 61M 94G 1% /mnt/sdb5 /dev/sda2 1014M 133M 882M 14% /boot /dev/mapper/centos-yinzhengjie 1.6T 448M 1.6T 1% /yinzhengjie /dev/loop0 976M 2.6M 907M 1% /mnt/ext4 tmpfs 394M 0 394M 0% /run/user/0 /dev/sdc2 200G 31G 170G 16% /home [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]#
8>.思考
既然"/home"目錄可以遷移,那根"/"文件系統能否遷移呢?
答:遷移根文件系統還不如重新安裝操作系統呢,因為遷移根文件系統需要進入到救援模式,這意味着你得重啟服務器喲~
綜上所述,分區策略有一個致命的弱點就是不支持動態變化,雖然類似於"/home"目錄是可以遷移的,但是根文件系統遷移就比較麻煩了,因此就引入了一個新的技術叫邏輯卷。
9>.小試牛刀
(1)創建一個2G的文件系統,塊大小為2048byte,預留1%可用空間,文件系統ext4,卷標為TEST,要求此分區開機后自動掛載至/test目錄,且默認有acl掛載選項 (2)寫一個腳本,完成如下功能: (a)列出當前系統識別到的所有磁盤設備 (b)如磁盤數量為1,則顯示其空間使用信息,否則,則顯示最后一個磁盤上的空間使用信息 (3)將CentOS6的CentOS-6.10-x86_64-bin-DVD1.iso和CentOS-6.10-x86_64-bin-DVD2.iso兩個文件,合並成一個CentOS-6.10-x86_64-Everything.iso文件,並將其配置為yum源。