一、总结整理磁盘管理及文件系统管理中设计的各种命令的使用,并附注一定的示例;
1、mkfs命令使用
mkfs.type -t /dev/sda...
mkfs -t ext2 = mkfs.ext2
mkfs -t ext3 = mkfs.ext3
2、mke2fs命令:管理ext系列文件系统
mke2fs [OPTION]...DEVICE
-t:指定格式化格式{ext2|ext3|ext4}
-b BLOCK_SIZE:指定块大小,默认4096k,也可指定1024或2048
-j:等于mkfs.ext3
-L LABEL:指定分区卷标
-m #:指定预留给超级用户的块数百分比,以便后期管理
-i #:用于指定为多少字节的空间创建一个inode,默认为8192;这里给出的数值应该为块大小的2^n倍;
-N:指定inode个数
-F:强制创建文件系统
3、blkid命令:查看磁盘设备相关属性
blkid DEVICE
UUID:唯一识别ID
TYPE:分区
LABEL:卷标
<实例>:格式化分区/dev/sda5,要求格式为ext4,块大小为2048,卷标为DATA01
[root@cwc01 ~]# mke2fs -t ext4 -b 2048 -L DATA01 /dev/sda5
[root@cwc01 ~]# blkid /dev/sda5 /dev/sda5: LABEL="DATA01" UUID="4fa47b37-5e3c-4e42-a837-b559d8aaf3c1" TYPE="ext4"
4、e2lable命令:用于查看或定义卷标
[root@cwc01 ~]# e2lable -L DATA02 /dev/sda5 [root@cwc01 ~]# blkid /dev/sda5 /dev/sda5: LABEL="DATA02" UUID="4fa47b37-5e3c-4e42-a837-b559d8aaf3c1" TYPE="ext4"
5、tune2fs命令:调整文件系统相关属性
-l:显示超级块中的信息;显示整个文件的属性及布局等相关信息
-j:不损害源数据,将ext2升级为ext3
-L LABEL:设定或修改卷标
-m # :调整预留百分比
-r:指定预留块数
-O:文件系统属性的启动或关闭
-o:文件系统默认挂载选项的启用或关闭
[root@cwc01 ~]# tune2fs -l /dev/sda5 tune2fs 1.41.12 (17-May-2010) Filesystem volume name: DATA02 Last mounted on:Filesystem UUID: 4fa47b37-5e3c-4e42-a837-b559d8aaf3c1 Filesystem magic number: 0xEF53 Filesystem revision #: 1 (dynamic) Filesystem features: has_journal ext_attr resize_inode dir_index filetype extent flex_bg sparse_super huge_file uninit_bg dir_nlink extra_isize Filesystem flags: signed_directory_hash Default mount options: (none) Filesystem state: clean Errors behavior: Continue Filesystem OS type: Linux Inode count: 328440 Block count: 2626610 Reserved block count: 131330 Free blocks: 2546779 Free inodes: 328429 First block: 0 Block size: 2048 Fragment size: 2048 Reserved GDT blocks: 512 Blocks per group: 16384 Fragments per group: 16384 Inodes per group: 2040 Inode blocks per group: 255 Flex block group size: 16 Filesystem created: Mon Sep 7 20:45:30 2015 Last mount time: n/a Last write time: Mon Sep 7 20:59:08 2015 Mount count: 0 Maximum mount count: 34 Last checked: Mon Sep 7 20:45:30 2015 Check interval: 15552000 (6 months) Next check after: Sat Mar 5 20:45:30 2016 Lifetime writes: 146 MB Reserved blocks uid: 0 (user root) Reserved blocks gid: 0 (group root) First inode: 11 Inode size: 256 Required extra isize: 28 Desired extra isize: 28 Journal inode: 8 Default directory hash: half_md4 Directory Hash Seed: 4c2e4df0-2183-4e87-8c94-4881c235cae5 Journal backup: inode blocks [root@cwc01 ~]# tune2fs -r 130000 /dev/sda5 tune2fs 1.41.12 (17-May-2010) Setting reserved blocks count to 130000
6、dumpe2fs命令:显示文件属性信息
-h :只显示超级块中的信息
[root@cwc01 ~]# dumpe2fs -h /dev/sda5 dumpe2fs 1.41.12 (17-May-2010) Filesystem volume name: DATA02 Last mounted on:Filesystem UUID: 4fa47b37-5e3c-4e42-a837-b559d8aaf3c1 Filesystem magic number: 0xEF53 Filesystem revision #: 1 (dynamic) Filesystem features: has_journal ext_attr resize_inode dir_index filetype extent flex_bg sparse_super huge_file uninit_bg dir_nlink extra_isize Filesystem flags: signed_directory_hash Default mount options: (none) Filesystem state: clean Errors behavior: Continue Filesystem OS type: Linux Inode count: 328440 Block count: 2626610 Reserved block count: 130000 Free blocks: 2546779 Free inodes: 328429 First block: 0 Block size: 2048 Fragment size: 2048 Reserved GDT blocks: 512 Blocks per group: 16384 Fragments per group: 16384 Inodes per group: 2040 Inode blocks per group: 255 Flex block group size: 16 Filesystem created: Mon Sep 7 20:45:30 2015 Last mount time: n/a Last write time: Mon Sep 7 21:12:55 2015 Mount count: 0 Maximum mount count: 34 Last checked: Mon Sep 7 20:45:30 2015 Check interval: 15552000 (6 months) Next check after: Sat Mar 5 20:45:30 2016 Lifetime writes: 146 MB Reserved blocks uid: 0 (user root) Reserved blocks gid: 0 (group root) First inode: 11 Inode size: 256 Required extra isize: 28 Desired extra isize: 28 Journal inode: 8 Default directory hash: half_md4 Directory Hash Seed: 4c2e4df0-2183-4e87-8c94-4881c235cae5 Journal backup: inode blocks Journal features: (none) Journal size: 64M Journal length: 32768 Journal sequence: 0x00000001 Journal start: 0
7、fsck命令:检查并修复linux文件系统
-t:指定文件系统类型
-a:自动修复
-r:交互式修复
-f:强制修复
[root@cwc01 ~]# fsck -a /dev/sda5 fsck from util-linux-ng 2.17.2 DATA02: clean, 11/328440 files, 79831/2626610 blocks
8、e2fsck命令:专用于ext系列文件系统修复工具
-f:强制修复
-p:自动修复
[root@cwc01 ~]# e2fsck -f /dev/sda5 e2fsck 1.41.12 (17-May-2010) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information DATA02: 11/328440 files (0.0% non-contiguous), 79831/2626610 blocks
9、mount命令:挂载文件系统
mount:通过读取/etc/mtab文件来显示当前系统所有已经挂载的设备
mount命令格式:
mount [options] [-o options] 设备 挂载点
设备:
(1) 设备文件:/dev/sda...
(2) 卷标:LABEL="xxx"
(3) UUID: UUID="xxx"
(4) 伪文件系统名称
挂载点:目录
(1) 此目录没有被其他进程使用
(2) 此目录得事先存在
(3) 目录中的原有文件将会暂时隐藏
常规options:
-a:表示挂载/etc/fstab中文件中定义的所有文件系统
-t:指定挂载的文件系统类型,不使用此选项,mount会调用blkid命令查看其文件系统类型
-r:以"只读"方式挂载文件系统(光盘)
-w:以"读写"方式挂载此文件系统
-n:默认mount命令挂载的设备,都会将信息写入/etc/mtab 文件中,加上-n选项,挂载时则不写入此文件
,此时想查看挂载的所有文件系统,使用cat /proc/mounts
-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
[root@cwc01 ~]# mkdir data [root@cwc01 ~]# mount /dev/sda5 /root/data/ [root@cwc01 ~]# mount /dev/mapper/luks-f301c527-9123-46cf-b67a-4cb8d4208bb7 on / type ext4 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) tmpfs on /dev/shm type tmpfs (rw) /dev/sda1 on /boot type ext4 (rw) /dev/mapper/vg_cwcedu-LogVol01 on /usr type ext4 (rw) /dev/mapper/vg_cwcedu-LogVol02 on /var type ext4 (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other) /dev/sda5 on /root/data type ext4 (rw)
10、umount命令:卸载
umount DEVICE
umount 挂载点
注意:卸载时挂载的设备需要没有进程使用
查看正在访问指定挂载点的进程
fuser -v mount_point
终止所有正在访问指定挂载点的进程
fuser -km mount_point
11、交换分区swap
(1)Linux内存分配机制中,优先使用物理内存
(2)默认X86架构32位系统,系统初始会创建4G的虚拟地址空间(线性地址),部分给内核使用(1G),进程会默认以为自己有3G内存给自己使用
(3)需要将一部分硬盘空间转换成映射到内存窗口的交换空间,如果有些进程目前状态闲置,就可以放进硬盘的交换空间,释放部分内存空间
12、内存管理相关命令free、mkswap、swapon、swapoff使用
(1)free命令:查看memory和swap的使用状态
-m:以MB为单位
-G:以GB为单位
[root@cwc01 ~]# free -m total used free shared buffers cached Mem: 988 256 732 1 20 99 -/+ buffers/cache: 135 852 Swap: 2047 0 2047
total:物理内存总大小
Used:总计内存使用量,包含(buffers 和cache),但其中可能部分缓存并未实际使用
free:未分配的内存
Shared:共享内存,一般系统用不到,为0
Buffers:缓冲区
Cached:缓存
实际可用内存大小:
free(-/+ buffers/cache)=free(mem)+ buffers(mem)+ cache(mem)
852=732+20+99
已经分配的内存大小:
used(mem)= Used(-/+ buffers/cache)+ buffers(Mem) + Cached(Mem)
256=135+20+99
物理内存总大小:
total(Mem) = used(-/+ buffers/cache) + free(-/+ buffers/cache)
988=135+852
(2)mkswap:创建交换分区
[root@cwc01 ~]# mkswap /dev/sda6 Setting up swapspace version 1, size = 1060252 KiB no label, UUID=176ebe58-b904-4d62-ae4c-de0aff9b5817
(3)swapon:启用交换分区
swapon -a :激活所有交换分区
swapon -s :查看当前swap空间(相当于cat /proc/swaps)
swapon -p PRIORITY :设定优先级
[root@cwc01 ~]# swapon -a /dev/sda6 [root@cwc01 ~]# swapon -s Filename Type Size Used Priority /dev/dm-0 partition 2097148 0 -1 /dev/sda6 partition 1060252 0 -2 [root@cwc01 ~]# free -m total used free shared buffers cached Mem: 988 260 728 1 21 102 -/+ buffers/cache: 136 851 Swap: 3083 0 3083
13、查看磁盘分区及文件系统占用情况命令df、du
(1)df命令:查看分区占用情况
-h: human-readable
-i: inode数量
-P: 以Posix兼容的格式输出
[root@cwc01 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/dm-2 38G 533M 35G 2% / tmpfs 495M 72K 495M 1% /dev/shm /dev/sda1 190M 28M 153M 16% /boot /dev/mapper/vg_cwcedu-LogVol01 9.8G 3.1G 6.2G 34% /usr /dev/mapper/vg_cwcedu-LogVol02 9.8G 189M 9.1G 2% /var
(2)du命令:查看文件或目录占用磁盘空间大小
[root@cwc01 ~]# du -sh /etc/ 40M /etc/
14、文件系统相关配置文件:/etc/fstab解析
要挂载的设备或伪文件系统 挂载点 文件系统类型 挂载选项 转储频率 自检次序
/dev/sda5 /data ext4 default 0 0
要挂载的设备:
设备文件、LABEL=、UUID=、伪文件系统名称
挂载选项:
defaults
转储频率:每多少天做一次完全备份
0: 不转储
1: 每天转储
2: 每隔一天转储
自检次序:
0:不自检
1:首先自检,通常只有/才为1
15、文件系统上的链接文件及命令ln
(1)硬链接:两个路径指向同一个inode
ln srcfile desfile
不能对目录进行;
不能跨分区进行
指向同一个inode的多个不同路径
创建文件的硬链接会增加inode的引用计数;删除硬链接仅是删除其一个访问路径,只到最后一个路径被删除;
(2)软链接:链接文件的数据指向另一个文件路径(符号链接)
ln -s srcfile desfile
可应用于目录;
可以跨文件系统;
不会增加被链接文件的链接次数;
其大小为指定的路径所包含的字符个数
[root@cwc01 ~]# cp /etc/fstab . [root@cwc01 ~]# ln fstab fstab01 [root@cwc01 ~]# ln -s fstab fstab02 [root@cwc01 ~]# ls -l total 118 -rw-------. 1 root root 1714 Aug 25 21:23 anaconda-ks.cfg drwxr-xr-x 3 root root 2048 Sep 7 20:45 data drwxr-xr-x. 2 root root 4096 Aug 25 13:39 Desktop drwxr-xr-x. 2 root root 4096 Aug 25 13:39 Documents drwxr-xr-x. 2 root root 4096 Aug 25 13:39 Downloads -rw-r--r-- 2 root root 972 Sep 7 22:53 fstab -rw-r--r-- 2 root root 972 Sep 7 22:53 fstab01 lrwxrwxrwx 1 root root 5 Sep 7 22:54 fstab02 -> fstab -rw-r--r--. 1 root root 50010 Aug 25 21:23 install.log -rw-r--r--. 1 root root 10033 Aug 25 21:19 install.log.syslog
二、创建一个10G的文件系统,类型为ext4,要求开机可自动挂载至/mydata目录
[root@cwc01 ~]# fdisk /dev/sda WARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u'). Command (m for help): n First cylinder (8513-12429, default 8513): Using default value 8513 Last cylinder, +cylinders or +size{K,M,G} (8513-12429, default 12429): +10G Command (m for help): w The partition table has been altered! [root@cwc01 ~]# partx /dev/sda # 1: 2048- 411647 ( 409600 sectors, 209 MB) # 2: 411648-126240767 (125829120 sectors, 64424 MB) # 3: 126240768-136745279 ( 10504512 sectors, 5378 MB) # 4: 136745280-199671884 ( 62926605 sectors, 32218 MB) # 5: 136745343-157726169 ( 20980827 sectors, 10742 MB) [root@cwc01 ~]# fdisk -l /dev/sda Disk /dev/sda: 128.8 GB, 128849018880 bytes 255 heads, 63 sectors/track, 15665 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x0002aa52 Device Boot Start End Blocks Id System /dev/sda1 * 1 26 204800 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 26 7859 62914560 8e Linux LVM /dev/sda3 7859 8512 5252256 83 Linux /dev/sda4 8513 12429 31463302+ 5 Extended /dev/sda5 8513 9818 10490413+ 83 Linux [root@cwc01 ~]# mkfs.ext4 /dev/sda5 mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 655776 inodes, 2622603 blocks 131130 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2688548864 81 block groups 32768 blocks per group, 32768 fragments per group 8096 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 26 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@cwc01 ~]# mkdir -pv /mydata mkdir: created directory `/mydata' [root@cwc01 ~]# vim /etc/fstab # # /etc/fstab # Created by anaconda on Tue Aug 25 21:07:35 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 # /dev/mapper/luks-f301c527-9123-46cf-b67a-4cb8d4208bb7 / ext4 defaults 1 1 UUID=956489f4-eccf-41a8-a173-a7b3a7c36f93 /boot ext4 defaults 1 2 /dev/mapper/vg_cwcedu-LogVol01 /usr ext4 defaults 1 2 /dev/mapper/vg_cwcedu-LogVol02 /var ext4 defaults 1 2 /dev/mapper/vg_cwcedu-LogVol00 swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 /dev/sda5 /mydata ext4 defaults 0 0
[root@cwc01 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/dm-2 38G 533M 35G 2% / tmpfs 495M 72K 495M 1% /dev/shm /dev/sda1 190M 28M 153M 16% /boot /dev/mapper/vg_cwcedu-LogVol01 9.8G 3.1G 6.2G 34% /usr /dev/mapper/vg_cwcedu-LogVol02 9.8G 189M 9.1G 2% /var /dev/sda5 9.8G 23M 9.3G 1% /mydata