挂载文件系统
挂载文件系统
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.博主推荐阅读
硬盘结构类型概述: 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源。
本文来自博客园,作者:尹正杰,转载请注明原文链接:https://www.cnblogs.com/yinzhengjie/p/12828901.html,个人微信: "JasonYin2020"(添加时请备注来源及意图备注,有偿付费)
当你的才华还撑不起你的野心的时候,你就应该静下心来学习。当你的能力还驾驭不了你的目标的时候,你就应该沉下心来历练。问问自己,想要怎样的人生。