note_8.4_挂载
文件系统的使用:
首先要“挂载”:mount命令和umount命令
根文件系统这外的其它文件系统要想能够被访问,都必须通过“关联”至根文件系统上的某个目录来实现
此关联操作即为“挂载”
此目录即为“挂载点”;
挂载点:mount_point,用于作为另一个文件系统的访问入口;
1. 事先存在;
2. 应该使用未被或不会被其它进程使用到的目录;
3. 挂载点下原有的文件将会被隐藏;
mount命令:
mount [-nrw] [-t vfstype] [-o options] device dir
命令选项:
-r:readonly,只读挂载
-w:read and write, 读写挂载
-n:默认情况下,设备挂载或卸载的操作会同步更新至/etc/mtab文件中;-n用于禁止此特性
-t vfstype:指明要挂载的设备上的文件系统的类型;多数情况下可省略,此时mount会通过blkid来判断要挂载的设备的文件系统类型
-L LABEL:挂载时以卷标的方式指明设备
mount -L LABEL dir
-U UUID:挂载时以UUID的方式指明设备
mount -U UUID dir
-o options:挂载选项
sync/async:同步/异步操作;
atime/noatime:文件或目录在被访问时是否更新其访问时间戳;
diratime/nodiratime:目录在被访问时是否更新其访问时间戳;
remount:重新挂载;
acl:支持使用facl功能;
# mount -o acl device dir
# tune2fs -o acl device
ro:只读
rw:读写
dev/nodev:此设备上是否允许创建设备文件;
exec/noexec:是否允许运行此设备上的程序文件;
auto/noauto:
user/nouser:是否允许普通用户挂载此文件系统;
suid/nosuid:是否允许程序文件上的suid和sgid特殊权限生效;
defaults:Use default options: rw, suid, dev, exec, auto, nouser, async, and relatime.
一个使用技巧:
可以实现将目录绑定至另一个目录上,作为其临时访问入口;
mount --bind 源目录 目标目录
-
查看当前系统所有已挂载的设备:
mount
cat /etc/mtab
cat /proc/mounts
-
挂载光盘:
mount -r /dev/cdrom mount_point
光盘设备文件:/dev/cdrom, /dev/dvd -
挂载U盘:
事先识别U盘的设备文件; -
挂载本地的回环设备:
mount -o loop /PATH/TO/SOME_LOOP_FILE MOUNT_POINT
umount命令:
umount device|dir
注意:正在被进程访问到的挂载点无法被卸载;
查看被哪个或哪些进程所战用:
lsof MOUNT_POINT
fuser -v MOUNT_POINT
终止所有正在访问某挂载点的进程:
fuser -km MOUNT_POINT
交换分区的启用和禁用:
创建交换分区的命令:mkswap
启用:swapon
swapon [OPTION] [DEVICE]
-a:定义在/etc/fstab文件中的所有swap设备
禁用:swapoff
swapoff DEVICE
/etc/fstab文件
设定除/
文件系统以外的其它文件系统能够开机时自动挂载
每行定义一个要挂载的文件系统及相关属性:
6个字段:
1. 要挂载的设备:
设备文件;
LABEL
UUID
伪文件系统:如sysfs, proc, tmpfs等
2. 挂载点
swap类型的设备的挂载点为swap;
3. 文件系统类型;
4. 挂载选项
defaults:使用默认挂载选项;
如果要同时指明多个挂载选项,彼此间以事情分隔;
defaults,acl,noatime,noexec
5. 转储频率
0:从不备份;
1:每天备份;
2:每隔一天备份;
6. 自检次序
0:不自检;
1:首先自检,通常只能是根文件系统可用1;
2:次级自检
...
mount -a:可自动挂载定义在此文件中的所支持自动挂载的设备
两个命令:df和du
-
df命令:report file system disk space usage
df [OPTION]... [FILE]...
-l:仅显示本地文件的相关信息;
-h:human-readable
-i:显示inode的使用状态而非blocks -
du命令:estimate file space usage
du [OPTION]... [FILE]...
-s: sumary
-h: human-readable
练习
1、创建一个10G的分区,并格式化为ext4文件系统;
(1) block大小为2048;预留空间为2%,卷标为MYDATA;
(2) 挂载至/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳;
(3) 可开机自动挂载;
2、创建一个大小为1G的swap分区,并启动之;
创建10G分区
[root@localhost ~]# fdisk /dev/sdb
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.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x13ceef16.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +10G
Partition 1 of type Linux and of size 10 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
重读磁盘分区表
[root@localhost ~]# partx -a /dev/sdb1
partx: /dev/sdb: error adding partition 1
[root@localhost ~]# cat /proc/partitions
major minor #blocks name
2 0 4 fd0
8 16 20971520 sdb
8 17 10485760 sdb1
8 0 125829120 sda
8 1 1048576 sda1
8 2 124779520 sda2
11 0 4481024 sr0
253 0 52428800 dm-0
253 1 2097152 dm-1
253 2 70246400 dm-2
[root@localhost ~]#
格式化磁盘分区
[root@localhost ~]# mke2fs.ext4 -b 2048 -m 2 -L MYDATA /dev/sdb1
-bash: mke2fs.ext4: command not found
[root@localhost ~]# mke2fs -t ext4 -b 2048 -m 2 -L MYDATA /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=MYDATA
OS type: Linux
Block size=2048 (log=1)
Fragment size=2048 (log=1)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 5242880 blocks
104857 blocks (2.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=273678336
320 block groups
16384 blocks per group, 16384 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816, 1327104,
2048000, 3981312
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
挂载
[root@localhost ~]# mkdir /mydata
[root@localhost ~]# ls /
bin boot dev etc home lib lib64 media mnt mydata opt proc root run sbin srv sys tmp usr var
[root@localhost ~]# mount -o noexec,noatime -L MYDATA /mydata
设置开机自动挂载
[root@localhost ~]# vim /etc/fstab
[root@localhost ~]# tail -1 /etc/fstab
/dev/sdb1 /mydata ext4 defaults,noatime,noexec 0 0
[root@localhost ~]# ls /mydata/
lost+found
创建swap分区
[root@localhost ~]# fdisk /dev/sdb
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): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): e
Partition number (2-4, default 2):
First sector (20973568-41943039, default 20973568):
Using default value 20973568
Last sector, +sectors or +size{K,M,G} (20973568-41943039, default 41943039):
Using default value 41943039
Partition 2 of type Extended and of size 10 GiB is set
Command (m for help): n
Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (20975616-41943039, default 20975616):
Using default value 20975616
Last sector, +sectors or +size{K,M,G} (20975616-41943039, default 41943039): +1G
Partition 5 of type Linux and of size 1 GiB is set
Command (m for help): t
Partition number (1,2,5, default 5):
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
重读磁盘分区表
[root@localhost ~]# partx -a /dev/sdb
partx: /dev/sdb: error adding partition 1
[root@localhost ~]# partx -a /dev/sdb
partx: /dev/sdb: error adding partitions 1-2
partx: /dev/sdb: error adding partition 5
[root@localhost ~]# cat /proc/partitions
major minor #blocks name
2 0 4 fd0
8 16 20971520 sdb
8 17 10485760 sdb1
8 18 1 sdb2
8 21 1048576 sdb5
8 0 125829120 sda
8 1 1048576 sda1
8 2 124779520 sda2
11 0 4481024 sr0
253 0 52428800 dm-0
253 1 2097152 dm-1
253 2 70246400 dm-2
启动swap分区
[root@localhost ~]# mkswap /dev/sdb5
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=245eb860-e845-47f2-9d2e-9c444a7589f5
[root@localhost ~]# free
total used free shared buff/cache available
Mem: 1005656 140556 703660 7644 161440 690796
Swap: 2097148 0 2097148
[root@localhost ~]# swapon /dev/sdb5
[root@localhost ~]# free
total used free shared buff/cache available
Mem: 1005656 140660 703552 7644 161444 690692
Swap: 3145720 0 3145720