磁盘分区介绍

一、fdisk命令介绍

1)、fdisk -l 查看本机磁盘信息(查的本机有两块硬盘,分别是sda 128.8G,sdb21.5G,sda这块硬盘有两个分区,分别是sda1,sda2)

[root@station78 ~]# fdisk -lDisk /dev/sda: 128.8 GB, 128849018880 bytes255 heads, 63 sectors/track, 15665 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00057501   Device Boot      Start         End      Blocks   Id  System/dev/sda1   *           1          26      204800   83  LinuxPartition 1 does not end on cylinder boundary./dev/sda2              26        7859    62914560   8e  Linux LVMDisk /dev/sdb: 21.5 GB, 21474836480 bytes255 heads, 63 sectors/track, 2610 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x12ae388b   Device Boot      Start         End      Blocks   Id  System

2)、对/dev/sdb这块硬盘进行基本操作

3)、创建分区,查看分区,保存分区信息并退出(设置一个5G的主分区)

[root@station78 ~]# fdisk  /dev/sdbWARNING: 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): nCommand action   e   extended   p   primary partition (1-4)pPartition number (1-4): 1First cylinder (1-2610, default 1):Using default value 1Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +5GCommand (m for help): pDisk /dev/sdb: 21.5 GB, 21474836480 bytes255 heads, 63 sectors/track, 2610 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x12ae388b   Device Boot      Start         End      Blocks   Id  System/dev/sdb1               1         654     5253223+  83  LinuxCommand (m for help): wThe partition table has been altered!Calling ioctl() to re-read partition table.Syncing disks.[root@station78 ~]# fdisk -l /dev/sdbDisk /dev/sdb: 21.5 GB, 21474836480 bytes255 heads, 63 sectors/track, 2610 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x12ae388b   Device Boot      Start         End      Blocks   Id  System/dev/sdb1               1         654     5253223+  83  Linux[root@station78 ~]#

4)、磁盘分区之后,使用的内核读到硬盘分区信息然后才能格式化分区(让内核重新读取硬盘的分区数)使用以下命令查看是否出现了sdb1的信息,从一下结构中很容易得知内核已经读到了硬盘信息

[root@station78 ~]# cat /proc/partitionsmajor minor  #blocks  name   8        0  125829120 sda   8        1     204800 sda1   8        2   62914560 sda2   8       16   20971520 sdb   8       17    5253223 sdb1 253        0   20971520 dm-0 253        1    2097152 dm-1 253        2   10485760 dm-2 253        3   20971520 dm-3[root@station78 ~]#

注意:如果内核没有读取信息,则使用以下命令,然后在查看

[root@station78 ~]# partx  -a /dev/sdb1 /dev/sdb[root@station78 ~]#

二、硬盘的格式化查看磁盘类型并修改块大小  

  1)、使用mkfs 命令格式化硬盘

[root@station78 ~]# mkfs -t ext4 /dev/sdb1mke2fs 1.41.12 (17-May-2010)Filesystem label=OS type: LinuxBlock size=4096 (log=2)Fragment size=4096 (log=2)Stride=0 blocks, Stripe width=0 blocks328656 inodes, 1313305 blocks65665 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=134637158441 block groups32768 blocks per group, 32768 fragments per group8016 inodes per groupSuperblock backups stored on blocks:    32768, 98304, 163840, 229376, 294912, 819200, 884736Writing inode tables: done                          Creating journal (32768 blocks): doneWriting superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 30 mounts or180 days, whichever comes first.  Use tune2fs -c or -i to override.[root@station78 ~]#

 2)、查看文件类型以及分区的唯一标识UUID(UUID用于区分硬盘)

[root@station78 ~]# blkid /dev/sdb1/dev/sdb1: UUID="f2263aef-fbc0-4dab-abc1-ccda473dc20a" TYPE="ext4"[root@station78 ~]#

 3)、修改硬盘的块大小,默认的块大小是4096字节,如果我们存储的数据文件都是小文件的时候,那么我们需要将块调小一些,这样更加合理的利用磁盘空间

mke2fs 不仅可以修改块大小,还能够设置磁盘的类型,设定卷标等

Mke2fs 命令常用选项 -b {1024|2048|4096 } bytes   设置块大小 -t {ext2|ext3|ext4}           指定文件类型 -L    指定卷标

 4)使用mke2fs 设定/dev/sdb1的文件类型,块大小并指定卷标

[root@station78 ~]# mke2fs -t ext4 -b 1024 -L MYDAT /dev/sdb1mke2fs 1.41.12 (17-May-2010)Filesystem label=MYDATOS type: LinuxBlock size=1024 (log=0)Fragment size=1024 (log=0)Stride=0 blocks, Stripe width=0 blocks328704 inodes, 5253220 blocks262661 blocks (5.00%) reserved for the super userFirst data block=1Maximum filesystem blocks=72613888642 block groups8192 blocks per group, 8192 fragments per group512 inodes per groupSuperblock backups stored on blocks:    8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409, 663553,    1024001, 1990657, 2809857, 5120001Writing inode tables: done                          Creating journal (32768 blocks): doneWriting superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 27 mounts or180 days, whichever comes first.  Use tune2fs -c or -i to override.[root@station78 ~]#

 5)、查看硬盘信息

[root@station78 ~]# blkid /dev/sdb1/dev/sdb1: UUID="4bc881bb-8e96-4c10-9541-1169b01f28e5" TYPE="ext4" LABEL="MYDAT"[root@station78 ~]#

注意:设定硬盘块大小时候必须格式化硬盘

三、硬盘格式化之后查看硬盘属性并调整(使用 tune2fs 命令)

  1)、查看设备属性

[root@station78 ~]# tune2fs -l /dev/sdb1tune2fs 1.41.12 (17-May-2010)Filesystem volume name:   MYDATLast mounted on:          
Filesystem UUID: 4bc881bb-8e96-4c10-9541-1169b01f28e5Filesystem magic number: 0xEF53Filesystem 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_isizeFilesystem flags: signed_directory_hashDefault mount options: (none)Filesystem state: cleanErrors behavior: ContinueFilesystem OS type: LinuxInode count: 328704Block count: 5253220Reserved block count: 262661Free blocks: 5133085Free inodes: 328693First block: 1Block size: 1024Fragment size: 1024Reserved GDT blocks: 256Blocks per group: 8192Fragments per group: 8192Inodes per group: 512Inode blocks per group: 128Flex block group size: 16Filesystem created: Wed Jul 17 07:33:29 2013Last mount time: n/aLast write time: Wed Jul 17 07:33:31 2013Mount count: 0Maximum mount count: 27Last checked: Wed Jul 17 07:33:29 2013Check interval: 15552000 (6 months)Next check after: Mon Jan 13 07:33:29 2014Lifetime writes: 113 MBReserved blocks uid: 0 (user root)Reserved blocks gid: 0 (group root)First inode: 11Inode size: 256Required extra isize: 28Desired extra isize: 28Journal inode: 8Default directory hash: half_md4Directory Hash Seed: 16ed53d0-29bf-4fd8-b50e-d98a5d87984bJournal backup: inode blocks[root@station78 ~]#shan

  2)、通过硬盘属性查看硬盘预留空间的块数(设置预留空间的块数的原因是防止硬盘数据存满以后,管理员无法在硬盘中任何操作,比如说当硬盘数据100%的时候,管理员无法使用任何命令操作。)

[root@station78 ~]# tune2fs -l /dev/sdb1 | grep "Reserved"Reserved block count:     262661                  块数Reserved GDT blocks:      256Reserved blocks uid:      0 (user root)          管理员    Reserved blocks gid:      0 (group root)[root@station78 ~]#

 3)、调整预留空间的块大小(防止当硬盘分区较大时浪费磁盘容量)

[root@station78 ~]# tune2fs -m 3 /dev/sdb1   设置预留块数为3%tune2fs 1.41.12 (17-May-2010)Setting reserved blocks percentage to 3% (157596 blocks)[root@station78 ~]# tune2fs -l /dev/sdb1 | grep "Reserved" 查看属性Reserved block count:     157596                       变成157596Reserved GDT blocks:      256Reserved blocks uid:      0 (user root)Reserved blocks gid:      0 (group root)[root@station78 ~]#

四、硬盘的挂载和卸载

  1)、挂载与卸载