1、总结整理磁盘管理及文件系统管理中设计的各种命令的使用,并附注一定的示例;
涉及常用命令:
fdisk,parted,mkfs,mke2fs,blkid,e2label,fsck,e2fsck,mount,free,mkswap,swapon,swapoff,df,du
fdisk
分区管理子命令:
p: 显示
n: 创建
d: 删除
t: 修改分区ID
l: 列出所有支持ID类型
w: 保存退出
q: 放弃修改并退出
m: 获取帮助
partx命令:
partx DEVICE
partx -a DEVICE
partx -a -n M:N DEVICE
M
M:
:N
创建工具:mkfs (make filesystem)
mkfs -t type DEVICE
实例:
mkfs -t ext3 /dev/sda5
ext系列文件系统的专用管理工具:
mke2fs -t {ext2|ext3|ext4} DEVICE
-b BLOCK: 1024, 2048, 4096
-L ‘LABEL‘: 设定卷标
实例:
mke2fs -t -b 2048k -L ‘TEST‘ ext3 /dev/sda5
blkid命令:查看分区信息
blkid DEVICE
LABEL, UUID, TYPE
实例:
blkid /dev/vda2 /dev/vda2: UUID="2qmMdG-5cM8-tWmk-jR5o-m2kI-STW5-8z8I6w" TYPE="LVM2_member"
e2label:修改卷标,(设备后跟LABEL名称为修改,不跟是查看设备信息)
e2lable DEVICE [LABEL]
实例:
[[email protected] ~]# e2label /dev/sda1 TEST [[email protected] ~]# e2label /dev/sda1 "NEW_TEST" [[email protected] ~]# e2label /dev/sda1 NEW_TEST
fsck: Filesystem check
fsck -t type
fsck.type
-a: 自动修复错误
-r: 交互式修复错误
-f: 强制检测
e2fsck:ext系列文件系统专用的检测修复工具;
-y: 自动回答为“yes”
-f:force
mount:通过读取/etc/mtab文件来显示当前系统所有已经挂载的设备;
mount -a:挂载/etc/fstab文件中的所有支持自动挂载的文件系统;
mount [options] [-o options] DEVICE MOUNT_POINT
[options]:命令选项
[-o options]:挂载选项
DEVICE: 要挂载的设备
(1) 设备文件:/dev/sda5
(2) 卷标:-L ‘LABEL‘
(3) UUID:-U ‘UUID‘
(4) 伪文件系统名称
MOUNT_POINT: 挂载点
常用选项:
-t type:文件系统类型
-r: 以“只读”方式挂载此文件系统
-w: 以“读写”方式挂载此文件系统
-n:每个文件系统在挂载时都会自动更新/etc/mtab文件,-n用于禁止此功能;
此时,如果想查看挂载的所有文件系统:cat /proc/mounts
-a: 自动挂载所有支持自动挂载的设备;
-B:绑定目录至另一个目录上;
-o 挂载选项:
async:异步模式
sync: 同步模式
atime/noatime:是否更新访问时间戳;
diratime/nodiratime:是否更新目录的访问时间戳;
auto/noauto: 是否允许此设备被自动挂载;
exec/noexec:是否允许执行此文件系统上应用程序;
dev/nodev:是否支持在此设备上使用设备文件;
suid/nosuid:
remount:重新挂载
ro
rw
user/nouser: 是否允许普通挂载此设备;
acl:是否支持此设备上使用facl;
defaults:rw, suid, dev, exec, auto, nouser, and async
umount:卸载命令
# umount DEVICE
# umount MOUNT_POINT
查看正在访问指定挂载点的进程:
# fuser -v MOUNT_POINT
终止所有正在访问指定的挂载点的进程:
# fuser -km MOUNT_POINT
free命令:
查看memory和swap的使用状态
-m: 以MB为单位
-g: 以GB为单位
实例:
[[email protected] ~]# free -m total used free shared buff/cache available Mem: 969 436 139 9 393 370 Swap: 4863 239 4624
交换分区:
mkswap:创建交换分区
mkswap [option] DEVICE
-L ‘LABEL‘
swapon:启用交换分区
swapon [option] [DEVICE]
-a: 激活所有交换分区
-p PRIORITY: 设定其优先级;
swapoff:禁用交换分区
swapoff [option] [DEVICE]
实例:
[[email protected] ~]# mkswap -L "S01" /dev/sda2 Setting up swapspace version 1, size = 2097148 KiB LABEL=S01, UUID=66b21e40-7f09-4c38-b62a-1d25e0bbe137 [[email protected] ~]# swapon -p -2 /dev/sda2 [[email protected] ~]# swapon -s Filename Type Size Used Priority /dev/vda3 partition 4980732 245300 -1 /dev/sda2 partition 2097148 0 -2 [[email protected] ~]# swapoff /dev/sda2
df: disk free
-h: human-readable
-i: inode数量
-P: 以Posix兼容的格式输出
实例:
[[email protected] ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/vda2 45G 3.8G 41G 9% / devtmpfs 476M 0 476M 0% /dev tmpfs 485M 84K 485M 1% /dev/shm tmpfs 485M 13M 472M 3% /run tmpfs 485M 0 485M 0% /sys/fs/cgroup /dev/vda1 297M 111M 187M 38% /boot
du: disk usage
-h: human-readable
-s: summary
实例: [[email protected] ~]# du -sh /etc/ 28M /etc/
2、创建一个10G的文件系统,类型为ext4,要求开机可自动挂载至/mydata目录
1)创建分区 fdisk -l Disk /dev/vda: 53.7 GB, 53687091200 bytes, 104857600 sectors #50G硬盘1/dev/vda Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000bb195 Device Boot Start End Blocks Id System /dev/vda1 * 2048 616447 307200 83 Linux /dev/vda2 616448 94486527 46935040 83 Linux /dev/vda3 94486528 104447999 4980736 82 Linux swap / Solaris Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors #100G硬盘2/dev/sda Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes fdisk /dev/sda #操作第二块没有分区的新硬盘 Command (m for help): p #查看分区信息 Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x34165aa2 Device Boot Start End Blocks Id System #硬盘没有分区 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): #回车默认为分区1 First sector (2048-209715199, default 2048): #回车默认从高2048开始 Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): +10G #创建为10G大小 Partition 1 of type Linux and of size 10 GiB is set Command (m for help): p #查看,下面出现了/dev/sda1证明创建成功 Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x34165aa2 Device Boot Start End Blocks Id System /dev/sda1 2048 20973567 10485760 83 Linux Command (m for help): w #保存退出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. 2,格式化分区 [[email protected] ~]# mke2fs -t ext4 -L ‘TEST01‘ -b 2048 /dev/sda1 #格式化分区/dev/sda1文件系统为ext4 卷标为TEST01 块大小为2048 mke2fs 1.42.9 (28-Dec-2013) Filesystem label=TEST01 OS type: Linux Block size=2048 (log=1) Fragment size=2048 (log=1) Stride=0 blocks, Stripe width=0 blocks 655360 inodes, 5242880 blocks 262144 blocks (5.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 [[email protected] ~]# blkid /dev/sda1 #查看/dev/sda1 /dev/sda1: LABEL="TEST01" UUID="242dca5e-7909-407c-ba25-eabb763d5f53" TYPE="ext4" 3)挂载/dev/sda1到/sda1_point [[email protected] ~]# mkdir /mydata [[email protected] ~]# mount /dev/sda1 /mydata [[email protected] ~]# mount #查看挂载是否成功(最后一行显示成功挂载),这样挂载在重启后将失效,重启后仍然挂载需要下一步的永久挂载 proc on /proc type proc (rw,nosuid,nodev,noexec,relatime) sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime) devtmpfs on /dev type devtmpfs (rw,nosuid,size=487312k,nr_inodes=121828,mode=755) ...... /dev/sda1 on /sda1_point type ext4 (rw,relatime,data=ordered) 4)永久挂载 [[email protected] ~]# vim /etc/fstab #添加最后一行 # /etc/fstab # Created by anaconda on Thu Aug 20 01:14:29 2015 # # 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 # UUID=a082897a-e1a4-4c7b-81f8-56b9f250cf60 / xfs defaults 0 0 UUID=046de458-2504-44dd-8d78-b9ac85dd3fa1 /boot xfs defaults 0 0 UUID=5341f7ce-4821-490d-936e-f3118fdc7b44 swap swap defaults 0 0 UUID=242dca5e-7909-407c-ba25-eabb763d5f53 /mydata ext4 defaults 0 0 [[email protected] ~]# mount -a #挂载fstab内可以允许自动挂载的设备 /dev/sda1 9.8G 13M 9.3G 1% /sda1_point [[email protected] ~]# df -h #-h以M为单位列出磁盘信息 Filesystem Size Used Avail Use% Mounted on /dev/vda2 45G 3.8G 41G 9% / devtmpfs 476M 0 476M 0% /dev tmpfs 485M 84K 485M 1% /dev/shm tmpfs 485M 6.9M 478M 2% /run tmpfs 485M 0 485M 0% /sys/fs/cgroup /dev/vda1 297M 111M 187M 38% /boot /dev/sda1 9.8G 13M 9.3G 1% /mydata #挂载成功 5)卸载分区 umount /mydata # #注意:如果卸载出现如下信息,证明这个挂载的目录正在被用户使用 [[email protected] mydata]# umount /dev/sda1 umount: /mydata: target is busy. (In some cases useful info about processes that use the device is found by lsof(8) or fuser(1)) [[email protected] ~]# fuser -v /mydata/ #查看正在使用的用户,发现user01正在使用这个目录 USER PID ACCESS COMMAND /mydata: root kernel mount /mydata user01 3346 ..c.. bash [[email protected] ~]# fuser -km /mydata/ #终止所有正在使用该目录的用户 /mydata: 3346c [[email protected] ~]# umount /mydata #卸载成功