磁盘分区:
- 说明:
- 磁盘的分区主要分为基本分区(primary partion)和扩充分区(extension partion)两种,基本分区和扩充分区的数目之和不能大于四个。且基本分区可以马上被使用但不能再分区。扩充分区必须再进行分区后才能使用,也就是说它必须还要进行二次分区。那么由扩充分区再分下去的是什么呢?它就是逻辑分区(logical partion),况且逻辑分区没有数量上限制。
-
- 在 Linux 中,每一个硬件设备都映射到一个系统的文件,对于硬盘、光驱等 IDE 或 SCSI 设备也不例外。Linux把各种 IDE 设备分配了一个由 hd 前缀组成的文件;而对于各种 SCSI 设备,则分配了一个由 sd 前缀组成的文件。
- 对于ide硬盘,驱动器标识符为“hdx~”,其中“hd”表明分区所在设备的类型,这里是指ide硬盘了。“x”为盘号(a为基本盘,b为基本从属盘,c为辅助主盘,d为辅助从属盘),“~”代表分区,前四个分区用数字1到4表示,它们是主分区或扩展分区,从5开始就是逻辑分区。例,hda3表示为第一个ide硬盘上的第三个主分区或扩展分区,hdb2表示为第二个ide硬盘上的第二个主分区或扩展分区。对于scsi硬盘则标识为“sdx~”,scsi硬盘是用“sd”来表示分区所在设备的类型的,其余则和ide硬盘的表示方法一样,不在多说。
- 在 Linux 中规定,每一个硬盘设备最多能有 4个主分区(其中包含扩展分区)构成,任何一个扩展分区都要占用一个主分区号码,也就是在一个硬盘中,主分区和扩展分区一共最多是 4 个。
- Linux 规定了主分区(或者扩展分区)占用 1 至 16 号码中的前 4 个号码。以第一个 IDE 硬盘为例说明,主分区(或者扩展分区)占用了 hda1、hda2、hda3、hda4,而逻辑分区占用了 hda5 到 hda16 等 12 个号码。
- 因此,Linux 下面每一个硬盘总共最多有 16 个分区。IDE硬盘最多有64个分区。
-
- fdisk -l 查看系统分区详细信息
- [root@test4 ~]# fdisk -l
- Disk /dev/sda: 21.4 GB, 21474836480 bytes
- 255 heads, 63 sectors/track, 2610 cylinders
- Units = cylinders of 16065 * 512 = 8225280 bytes
- Device Boot Start End Blocks Id System
- /dev/sda1 * 1 13 104391 83 Linux
- /dev/sda2 14 2610 20860402+ 8e Linux LVM
-
- 注释:这个硬盘的大小是21.4GB,有255个磁面,63个扇区,2610磁柱(cylinders)
- 每个cylinder(磁柱)的容量是 8225280 bytes=8225.280 K(约为)=8.225280M(约为);
- Device Boot Start End Blocks Id System
- /dev/sda1 * 1 13 104391 83 Linux
- /dev/sda2 14 1305 10377990 8e Linux LVM
-
- id和System 表示的是一个意思,id看起来不太直观,我们要在fdisk 一个分区时,通过指定id来确认分区类型;比如 7表示的就NTFS 分区;这个在fdisk 中要通过t功能来指定。83是linux。下面的部分会提到;
- 说明:硬盘分区的表示:在Linux 是通过hd*x 或 sd*x 表示的:
- 其中* 表示的是a、b、c ... ...
- 另外x 表示的数字 1、2、3 ... ...
-
- hd大多是IDE硬盘;sd大多是SCSI或移动存储;引导(Boot):表示引导分区,在上面的例子中sda1 是引导分区;
-
- Start (开始):表示的一个分区从Xcylinder(磁柱)开始;
-
- End (结束):表示一个分区到 Ycylinder(磁柱)结束;
-
- fdisk 对磁盘进行分区
-
- [root@test4 ~]# fdisk /dev/sda //对sda磁盘进行分区
- Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
- Building a new DOS disklabel. Changes will remain in memory only,
- until you decide to write them. After that, of course, the previous
- content won't be recoverable.
- The number of cylinders for this disk is set to 2597.
- There is nothing wrong with that, but this is larger than 1024,
- and could in certain setups cause problems with:
- 1) software that runs at boot time (e.g., old versions of LILO)
- 2) booting and partitioning software from other OSs
- (e.g., DOS FDISK, OS/2 FDISK)
- Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
- Command (m for help): m //输出帮助信息
- Command action
- a toggle a bootable flag //设置启动分区
- b edit bsd disklabel //编辑分区标签
- c toggle the dos compatibility flag
- d delete a partition //删除一个分区
- l list known partition types //列出分区类型
- m print this menu //输出帮助信息
- n add a new partition //建立一个新的分区
- o create a new empty DOS partition table //创建一个新的空白DOS分区表
- p print the partition table //打印分区表
- q quit without saving changes //退出不保存设置
- s create a new empty Sun disklabel
- t change a partition's system id //改变分区的ID
- u change display/entry units //改变显示的单位
- v verify the partition table //检查验证分区表
- w write table to disk and exit //保存分区表
- x extra functionality (experts only)
- Command (m for help):n
- Command action
- e extended //e是扩展分区
- p primary partition (1-4) //p是主分区
- Partition number (1-4): 1 //定义分区数量 --主分区最多只能有四个
- First cylinder (1-2597, default 1): 1
- Last cylinder or +size or +sizeM or +sizeK (1-2597, default 2597): +100M
-
- 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 22: 无效的参数.
- The kernel still uses the old table.
- The new table will be used at the next reboot.
- Syncing disks.
-
- [root@test6 ~]# fdisk /dev/sda
- The number of cylinders for this disk is set to 2610.
- There is nothing wrong with that, but this is larger than 1024,
- and could in certain setups cause problems with:
- 1) software that runs at boot time (e.g., old versions of LILO)
- 2) booting and partitioning software from other OSs
- (e.g., DOS FDISK, OS/2 FDISK)
- Command (m for help): n
- First cylinder (1710-2610, default 1710):
- Using default value 1710
- Last cylinder or +size or +sizeM or +sizeK (1710-2610, default 2610): +100M
- 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: 设备或资源忙.
- The kernel still uses the old table.
- The new table will be used at the next reboot.
- Syncing disks.
- [root@test6 ~]# partprobe /dev/sda //对硬盘进行更新
- [root@test6 ~]# fdisk -l
-
- Disk /dev/sda: 21.4 GB, 21474836480 bytes
- 255 heads, 63 sectors/track, 2610 cylinders
- Units = cylinders of 16065 * 512 = 8225280 bytes
- Device Boot Start End Blocks Id System
- /dev/sda1 * 1 13 104391 83 Linux
- /dev/sda2 14 274 2096482+ 82 Linux swap / Solaris
- /dev/sda3 275 404 1044225 8e Linux LVM
- /dev/sda4 405 2610 17719695 5 Extended
- /dev/sda5 405 1709 10482381 83 Linux
- /dev/sda6 1710 1722 104391 83 Linux
- [root@test6 ~]# mkfs.ext3 /dev/sda6 //需要先进行格式化,才能使用 。mkfs -t ext3
- /dev/sda5 (mkfs.ext3 一样效果 -t 指定类型 -b 指定block大小·)
- mke2fs 1.39 (29-May-2006)
- Filesystem label=
- OS type: Linux
- Block size=1024 (log=0)
- Fragment size=1024 (log=0)
- 26104 inodes, 104388 blocks
- 5219 blocks (5.00%) reserved for the super user
- First data block=1
- Maximum filesystem blocks=67371008
- 13 block groups
- 8192 blocks per group, 8192 fragments per group
- 2008 inodes per group
- Superblock backups stored on blocks:
- 8193, 24577, 40961, 57345, 73729
-
- Writing inode tables: done
- Creating journal (4096 blocks): done
- Writing superblocks and filesystem accounting information: done
-
- This filesystem will be automatically checked every 23 mounts or
- 180 days, whichever comes first. Use tune2fs -c or -i to override.
-
- [root@test6 ~]# mount /dev/sda6 /mnt/sda7/ //进行挂载使用
- anaconda-ks.cfg install.log install.log.syslog
- [root@test6 ~]# df -h
- 文件系统 容量 已用 可用 已用% 挂载点
- /dev/sda5 9.7G 1.1G 8.1G 12% /
- /dev/sda1 99M 12M 83M 13% /boot
- tmpfs 252M 0 252M 0% /dev/shm
- /dev/hdc 3.9G 3.9G 0 100% /mnt/cdrom
- /dev/sda6 99M 5.6M 89M 6% /mnt/sda7
- 1.磁盘管理常用命令
- 1.1 ls -i 文件名 查看文件存储在哪个innode中
- 1 [root@test6 ~]# ls -i lstest
- 2 1179659 lstest
- 1.2 ls -id 文件夹名 查看文件夹存储在哪个innode中
-
- 1 [root@test6 ~]# ls -id lstest1
- 2 1179661 lstest1
- 1.3 filefrag -v 文件名 查看文件存储block具体位置
-
- 1 [root@test6 ~]# filefrag -v lstest
- 2 Checking lstest
- 3 Filesystem type is: ef53
- 4 Filesystem cylinder groups is approximately 78
- 5 Blocksize of file lstest is 4096
- 6 File size of lstest is 5 (1 blocks)
- 7 First block: 1183744
- 8 Last block: 1183744
- 9 lstest: 1 extent found
-
- 2.硬盘工具:
- 2.1 dumpe2fs /dev/sda1 查看分区superblock 和blockgroup等详细信息
- View Code
- 2.2 fsck /dev/sda 检查硬盘情况 -C 显示检查过程 -f 强制检查
- 2.3 badblocks /dev/sda5 检查硬盘坏道 -sv 显示进度和结果
- df 查看文件系统(-h 以合适的单位示系统大小 -T 显示系统类型)
- du 查看文件夹大小,包含文件夹里面文件
- du -s 仅仅查看文件夹大小
- du -h 以M方式显示大小,方便查看
-
- fuser -mv /mnt 当显示 设备正在忙的时候,可以使用这个命令查看使用进程
- tune2fs -l /dev/sda 查看磁盘参数(-h 查看可选选项,进行选项更改)
-
- 1 [root@test6 ~]# tune2fs -l /dev/sda5
- 2 tune2fs 1.39 (29-May-2006)
- 3 Filesystem volume name: / //卷标
- 4 Last mounted on: <not available>
- 5 Filesystem UUID: 4b64bcce-0acc-40f2-85e6-07e198152c20
- 6 Filesystem magic number: 0xEF53
- 7 Filesystem revision #: 1 (dynamic)
- 8 Filesystem features: has_journal ext_attr resize_inode dir_index filetype needs_recovery sparse_super large_file
- 9 Default mount options: user_xattr acl
- 10 Filesystem state: clean
- 11 Errors behavior: Continue
- 12 Filesystem OS type: Linux
- 13 Inode count: 2621440
- 14 Block count: 2620595
- 15 Reserved block count: 131029
- 16 Free blocks: 2257390
- 17 Free inodes: 2582833
- 18 First block: 0
- 19 Block size: 4096
- 20 Fragment size: 4096
- 21 Reserved GDT blocks: 639
- 22 Blocks per group: 32768
- 23 Fragments per group: 32768
- 24 Inodes per group: 32768
- 25 Inode blocks per group: 1024
- 26 Filesystem created: Tue Mar 28 07:57:02 2017
- 27 Last mount time: Tue Mar 28 08:29:56 2017
- 28 Last write time: Tue Mar 28 08:29:56 2017
- 29 Mount count: 3
- 30 Maximum mount count: -1 //超过最大次数,则需要对分区进行自检
- 31 Last checked: Tue Mar 28 07:57:02 2017
- 32 Check interval: 0 (<none>)
- 33 Reserved blocks uid: 0 (user root)
- 34 Reserved blocks gid: 0 (group root)
- 35 First inode: 11
- 36 Inode size: 128
- 37 Journal inode: 8
- 38 First orphan inode: 163883
- 39 Default directory hash: tea
- 40 Directory Hash Seed: 8633f5c3-e870-428e-8ce3-7d636f0ef80a
- 41 Journal backup: inode blocks