一、磁盘基础知识
机械式硬盘接口类型:
并口:
IDE: 133MB/s
SCSI:640MB/s
串口:
SATA: 6Gbps
SAS: 6Gbps
USB: 460MB/s
Linux上的磁盘设备文件命名方式:
/dev/DEV_FILE:
IDE:/dev/hd
SCSI,SATA,SAS,USB: /dev/sd
不同设备a-z
/dev/sda,/dev/sdb....
同一设备上的不同分区:1,2....
二、简单的磁盘空间使用情况查看命令
df、du命令的使用
du:概述磁盘使用情况
常用选项:
-s:指定文件和目录中所有文件的
-h:以K,M,G为单位,提高信息的可读性。
示例:
[root@Lab6-1 ~]# du -h /tmp 4.0K/tmp/.ICE-unix 4.0K/tmp/pear/temp 8.0K/tmp/pear 68K/tmp [root@Lab6-1 ~]# du -sh /etc/ 30M/etc/ [root@Lab6-1 ~]# du -s /etc/ 30072/etc/
df:检查文件系统的磁盘空间占用情况
-i:显示inode节点使用信息。
-h:以K,M,G为单位,提高信息的可读性。
-P:输出格式为POSIX,以整齐的格式进行输出。
示例:
[root@Lab6-1 ~]# df /dev/sda Filesystem 1K-blocks Used Available Use% Mounted on - 491000 192 490808 1% /dev [root@Lab6-1 ~]# df -i /dev/sda Filesystem Inodes IUsed IFree IUse% Mounted on - 122750 701 122049 1% /dev [root@Lab6-1 ~]# df -ih /dev/sda Filesystem Inodes IUsed IFree IUse% Mounted on - 120K 701 120K 1% /dev [root@Lab6-1 ~]# df -ihP /dev/sda Filesystem Inodes IUsed IFree IUse% Mounted on - 120K 701 120K 1% /dev
二、磁盘的分区及格式化管理
fdisk、mke2fs、tune2fs、fsck、e2fsck、mkswap、swapon、swapoff
1)fdisk:观察硬盘使用情况与磁盘分区功能。
-l [device...]: 显示默认或所有的磁盘设备
示例:
[root@Lab6-1 ~]# fdisk -l Disk /dev/sda: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0x000f3537 Device Boot Start End Blocks Id System /dev/sda1 * 1 64 512000 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 64 2611 20458496 8e Linux LVM Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0x00000000
直接管理管理磁盘并对磁盘分区操作如下,根据需求在Command(m for help)下键入信息
创建完磁盘分区之后记得 partx -a DEVICE 否则系统不会识别
[root@Lab6-1 ~]# fdisk /dev/sda [root@Lab6-1 ~]# fdisk /dev/sdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x898da2f9. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won‘t be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) 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 // 新建分区 Command action e extended // 此处是扩展分区 p primary partition (1-4) // 此处是主分区 p // 创建主分区 Partition number (1-4): 1 // 因为我们是给新的磁盘所以选择1 First cylinder (1-2610, default 1): // 这里默认,直接回车即可 Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +5G // 单位可以是K,M,G Command (m for help): P // 显示当前磁盘分区情况 Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0x898da2f9 Device Boot Start End Blocks Id System /dev/sdb1 1 654 5253223+ 83 Linux // 查看/dev/sdb1已经创建成功 Command (m for help): w // 这里千万不要忘,要记得保存,否则无法生效 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
1. 输入 m 显示所有命令列示。
2. 输入 p 显示硬盘分割情形。
3. 输入 a 设定硬盘启动区。
4. 输入 n 设定新的硬盘分割区。
4.1. 输入 e 硬盘为[延伸]分割区(extend)。
4.2. 输入 p 硬盘为[主要]分割区(primary)。
5. 输入 t 改变硬盘分割区属性。
6. 输入 d 删除硬盘分割区属性。
7. 输入 q 结束不存入硬盘分割区属性。
8. 输入 w 结束并写入硬盘分割区属性
2) mkfs:用来在一个设备上构建Linux支持的相关文件系统,也可称之为格式化工具
-t: 指定文件系统类型
mkfs命令有两种方式:
1) mkfs.FS_TYPE /dev/DEVICE
# 常用类型: ext4,xfs,brtfs,vfat
2) mkfs -t FS_TYPE /dev/DEVICE
示例:
[root@Lab6-1 ~]# mkfs.ext3 /dev/sdb1 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 328656 inodes, 1313305 blocks 65665 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=1346371584 41 block groups 32768 blocks per group, 32768 fragments per group 8016 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 30 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@Lab6-1 ~]# mkfs -t ext2 /dev/sdb1 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 328656 inodes, 1313305 blocks 65665 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=1346371584 41 block groups 32768 blocks per group, 32768 fragments per group 8016 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736 Writing inode tables: done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 22 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
3) mke2fs:用于创建磁盘分区上的“ext2/ext3/ext4”文件系统的专有命令
常用选项:
-t {ext2|ext3|ext4}::指定etx系列的文件系统的类型
-j:开启日志功能,也就是格式化为ext3系列文件系统类型
-m:设定超级块的的百分比
-b:指定block块的大小 1024/2048/4096/
-L ‘LABEL‘:给磁盘设定卷标
-i #:数据空间每多少个字节创建一个inode,此大小不应该小block的大小
-n #:为数据空间创建多少个inode
示例:
[root@Lab6-1 ~]# mke2fs -t etx4 -b 2048 -L ‘sdb_one‘ -m 3 /dev/sdb1 mke2fs 1.41.12 (17-May-2010) Filesystem label=sdb_one OS type: Linux Block size=2048 (log=1) Fragment size=2048 (log=1) Stride=0 blocks, Stripe width=0 blocks 328440 inodes, 2626610 blocks 78798 blocks (3.00%) reserved for the super user First data block=0 Maximum filesystem blocks=540016640 161 block groups 16384 blocks per group, 16384 fragments per group 2040 inodes per group Superblock backups stored on blocks: 16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816, 1327104, 2048000 Writing inode tables: done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 36 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
4) blkid:块设备属性信息查看
常用选项:
-U UUID:根据指定的UUID来查找对应的设备
-L LABEL: 很据指定的LABEL来查找对应的设备
示例:
[root@Lab6-1 ~]# blkid /dev/sdb1 /dev/sdb1: UUID="6cae6626-1b85-498f-a424-0c34a3e2e256" TYPE="ext2" LABEL="sdb_one" [root@Lab6-1 ~]# blkid -L ‘sdb_one‘ /dev/sdb1
5)tune2fs:重新设定ext系类文件系统可调整参数的值
常用选项:
-l:查看指定文件系统超级块信息
-L ‘LABEL‘ :修改卷标
-m # :修改留给管理员的空间百分比
-j:ext2升级为ext3,开启日志功能
-o: 调整文件系统的默认挂载选项
-u UUID:修改UUID号
示例:
[root@Lab6-1 ~]# tune2fs -l /dev/sda1 tune2fs 1.41.12 (17-May-2010) Filesystem volume name: <none> Last mounted on: /boot Filesystem UUID: e7b261b7-858c-44d8-bf72-66ad58a3a3b7 Filesystem magic number: 0xEF53 Filesystem revision #: 1 (dynamic) Filesystem features: has_journal ext_attr resize_inode dir_index filetype needs_recovery extent flex_bg sparse_super huge_file uninit_bg dir_nlink extra_isize Filesystem flags: signed_directory_hash Default mount options: user_xattr acl Filesystem state: clean Errors behavior: Continue Filesystem OS type: Linux Inode count: 128016 Block count: 512000 Reserved block count: 25600 Free blocks: 459278 Free inodes: 127978 First block: 1 Block size: 1024 Fragment size: 1024 Reserved GDT blocks: 256 Blocks per group: 8192 Fragments per group: 8192 Inodes per group: 2032 Inode blocks per group: 254 Flex block group size: 16 Filesystem created: Thu Sep 17 03:28:07 2015 Last mount time: Tue Sep 22 21:11:28 2015 Last write time: Tue Sep 22 21:11:28 2015 Mount count: 9 Maximum mount count: -1 Last checked: Thu Sep 17 03:28:07 2015 Check interval: 0 (<none>) Lifetime writes: 50 MB Reserved blocks uid: 0 (user root) Reserved blocks gid: 0 (group root) First inode: 11 Inode size: 128 Journal inode: 8 Default directory hash: half_md4 Directory Hash Seed: b41acf8c-bd3f-4803-a824-19661b67741a Journal backup: inode blocks
[root@Lab6-1 ~]# tune2fs -L ‘data_db1‘ /dev/sdb1 tune2fs 1.41.12 (17-May-2010) [root@Lab6-1 ~]# blkid /dev/sdb1 /dev/sdb1: UUID="6cae6626-1b85-498f-a424-0c34a3e2e256" TYPE="ext2" LABEL="data_db1"
6)mkswap:创建交换分区
-L ‘LABEL‘: 设定磁盘卷标
示例:
[root@Lab6-1 ~]# mkswap -L ‘swap_data‘ /dev/sdb2 Setting up swapspace version 1, size = 1060284 KiB LABEL=swap_data, UUID=c49b7121-26fa-4904-a446-7f77aa5104cb
7)swapon swapoff 开启、关闭交换分区
/dev/sdb2的磁盘大小为1G,注意查看开启和关闭交换分区之后free项的大小变化,
[root@Lab6-1 ~]# free -g total used free shared buffers cached Mem: 0 0 0 0 0 0 -/+ buffers/cache: 0 0 Swap: 1 0 1 [root@Lab6-1 ~]# swapon /dev/sdb2 [root@Lab6-1 ~]# free -g total used free shared buffers cached Mem: 0 0 0 0 0 0 -/+ buffers/cache: 0 0 Swap: 2 0 2 [root@Lab6-1 ~]# swapoff /dev/sdb2 [root@Lab6-1 ~]# free -g total used free shared buffers cached Mem: 0 0 0 0 0 0 -/+ buffers/cache: 0 0 Swap: 1 0 1
8)fsck:文件系统检测
常用选项:
-a: 自动修复
-f: 强制修复 // 如果文件系统无错不会进行检查修复的,所以需要强制检查修复
-r: 交互式问答修复
示例:
[root@Lab6-1 ~]# fsck -f /dev/sdb1 fsck from util-linux-ng 2.17.2 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 data_db1: 11/328440 files (0.0% non-contiguous), 47063/2626610 blocks [root@Lab6-1 ~]# fsck -a /dev/sdb1 fsck from util-linux-ng 2.17.2 data_db1: clean, 11/328440 files, 47063/2626610 blocks [root@Lab6-1 ~]# fsck -r /dev/sdb1 fsck from util-linux-ng 2.17.2 e2fsck 1.41.12 (17-May-2010) data_db1: clean, 11/328440 files, 47063/2626610 blocks [root@Lab6-1 ~]#
9) e2fsck:ext系列文件专用的检测修复工具
常用选项:
-y: 交互式问答修复时,自动回答为yes
-f:强制修复;
示例:
[root@Lab6-1 ~]# e2fsck -f /dev/sdb1 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 data_db1: 11/328440 files (0.0% non-contiguous), 47063/2626610 blocks
完。
原文:http://corazon.blog.51cto.com/9658694/1699569