磁盘分区时需要使用以下两命令:

RHEL6基础二十五之磁盘分区、格式化、大于2T分区创建、文件系统状态_RHEL基础

RHEL6基础二十五之磁盘分区、格式化、大于2T分区创建、文件系统状态_RHEL基础_02

一、查看目前磁盘使用情况

RHEL6基础二十五之磁盘分区、格式化、大于2T分区创建、文件系统状态_磁盘分区_03

一个已经分好区的磁盘sda和一个未使用的磁盘sdb,

二、创建磁盘分区

以硬盘设备文件名为参数执行fdisk命令,进入到交互式的分区管理界面中,就可以对相应的硬盘进行创建、删除、更改分区等操作

[root@justin ~]# 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 0x9adeb999.
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):

在操作界面中的“Command (m for help):”提示符后,用户可以输入特定的分区操作指令,完成各项分区管理任务。例如输入“m”指令后,可以查看各种操作指令的帮助信息。

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
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system 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):

创建一个新的分区

Command (m for help): n            ;创建一个新的分区
Command action
   e   extended                    ;扩展分区
   p   primary partition (1-4)     ;主分区,主分区最多只能4个
p                                  ;创建主分区
Partition number (1-4): 1          ;指定主分区号
First cylinder (1-1044, default 1): ;指定第一个柱面,直接回车使用默认
Using default value 1               ;
Last cylinder, +cylinders or +size{K,M,G} (1-1044, default 1044): +3G  ;指定分区大小
Command (m for help):

创建好分区后可以通过命令p查看分区情况

Command (m for help): p
Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 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: 0x9adeb999
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         393     3156741   83  Linux
Command (m for help):

通过以上步骤可以继续创建其他主分区,但主分区最多为4个,

创建逻辑分区

创建扩展分区:

创建逻辑分区前必须创建扩展分区,且此时需要把所有空间分配给扩展分区

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e
Partition number (1-4): 4
First cylinder (394-1044, default 394):
Using default value 394
Last cylinder, +cylinders or +size{K,M,G} (394-1044, default 1044):
Using default value 1044
Command (m for help): p
Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 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: 0x9adeb999
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         393     3156741   83  Linux
/dev/sdb4             394        1044     5229157+   5  Extended
Command (m for help):

创建逻辑分区:

扩展分区创建好之后,接着就可以创建逻辑分区。在创建逻辑分区的时候不需要指定分区编号,系统将会自动从5开始顺序编号

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (394-1044, default 394):
Using default value 394
Last cylinder, +cylinders or +size{K,M,G} (394-1044, default 1044): +3G
Command (m for help): p
Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 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: 0x9adeb999
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         393     3156741   83  Linux
/dev/sdb4             394        1044     5229157+   5  Extended
/dev/sdb5             394         786     3156741   83  Linux
Command (m for help):

继续创建一个逻辑分区

Command (m for help): p
Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 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: 0x9adeb999
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         393     3156741   83  Linux
/dev/sdb4             394        1044     5229157+   5  Extended
/dev/sdb5             394         786     3156741   83  Linux
/dev/sdb6             787        1044     2072353+  83  Linux
Command (m for help):

分区创建好需要保存退出(w)或者放弃编辑退出(q)

Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@justin ~]#

新分区需要重启启动或者执行partprobe命令使其生效

[root@justin ~]# partprobe /dev/sdb
[root@justin ~]#

格式化、挂载分区

创建好的分区需要格式化和挂载才能正常使用

格式化分区

[root@justin ~]# mkfs -V -t ext4 -c /dev/sdb1
[root@justin ~]# mkfs -t ext4 /dev/sdb5
[root@justin ~]# mkfs -t vfat /dev/sdb6

挂载分区

在安装Linux系统的过程中,自动建立或识别的分区通常会由系统自动完成挂载,如“/”分区、“boot”分区等,对于后来新增加的硬盘分区、优盘、光盘等设备,就必须由管理员手动进行挂载。挂载一个分区时,必须为其指定一个目录作为挂载点,用户通过这个目录就可以访问设备中的文件、目录数据

[root@justin ~]# mkdir /mnt/{sdb1,sdb5,sdb6}
[root@justin ~]# mount /dev/sdb1 /mnt/sdb1
[root@justin ~]# mount /dev/sdb5 /mnt/sdb5
[root@justin ~]# mount /dev/sdb6 /mnt/sdb6
[root@justin ~]# df -Th
文件系统    类型      容量  已用  可用 已用%% 挂载点
/dev/sda2     ext4    9.9G  2.7G  6.7G  29% /
tmpfs        tmpfs    504M     0  504M   0% /dev/shm
/dev/sda1     ext4    194M   27M  158M  15% /boot
/dev/sda5     ext4    7.7G  147M  7.2G   2% /home
/dev/sdb1     ext4    3.0G   69M  2.8G   3% /mnt/sdb1
/dev/sdb5     ext4    3.0G   69M  2.8G   3% /mnt/sdb5
/dev/sdb6     vfat    2.0G  4.0K  2.0G   1% /mnt/sdb6
[root@justin ~]# vim /etc/fstab
proc                    /proc                   proc    defaults        0 0
/dev/sdb1               /mnt/sdb1               ext4    defaults        0 0
/dev/sdb5               /mnt/sdb5               ext4    defaults        0 0
/dev/sdb6               /mnt/sdb6               vfat    defaults        0 0

Tips:如果需要删除已创建好的分区,可以在fdisk命令操作界面中使用“d”指令将指定的分区删除,根据提示输入需要删除的分区序号即可。在删除时建议从最后一个分区开始进行删除,以免fdisk识别的分区序号发生紊乱。另外,如果扩展分区被删除,则扩展分区之下的逻辑分区也将同时被删除


Tips:格式化分区时候出现以下报错,是因为扩展分区不能格式化,需要在创建成逻辑分区

[root@localhost ~]# mkfs.ext4 /dev/sdb4 
mke2fs 1.41.12 (17-May-2010)
mkfs.ext4: inode_size (128) * inodes_count (0) too big for a
	filesystem with 0 blocks, specify higher inode_ratio (-i)
	or lower inode count (-N).

[root@localhost ~]# mkfs.ext4 /dev/sdb4 
mke2fs 1.41.12 (17-May-2010)
mkfs.ext4: inode_size (128) * inodes_count (0) too big for a
	filesystem with 0 blocks, specify higher inode_ratio (-i)
	or lower inode count (-N).

[root@localhost ~]#

大于2T分区创建

[root@localhost ~]# fdisk -l

Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 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: 0x00047e01

   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        2115    16777216   82  Linux swap / Solaris
/dev/sda3            2115       13055    87874560   83  Linux

Disk /dev/sdb: 2089.1 GB, 2089072092160 bytes
255 heads, 63 sectors/track, 253981 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

[root@localhost ~]# parted /dev/sdb   #parted>select sdb 假设磁盘为sdb  
GNU Parted 2.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt #将MBR磁盘格式化为GPT,mklabel msdos 将GPT磁盘转化为MBR磁盘  
(parted) print                                                            
Model: VMware Virtual disk (scsi)
Disk /dev/sdb: 2089GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start  End  Size  File system  Name  Flags

(parted) mkpart primary 0 2089GB #划分一个起始位置为0,全部的2089G的主分区,mkpart primary 0 -1 将整块磁盘分成一个分区    
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? I                                                          
(parted) print
Model: VMware Virtual disk (scsi)
Disk /dev/sdb: 2089GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End     Size    File system  Name     Flags
 1      17.4kB  2089GB  2089GB               primary

(parted) quit                                                             
Information: You may need to update /etc/fstab.                           

[root@localhost ~]# df -Th 
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/sda3      ext4    83G  3.4G   75G   5% /
tmpfs          tmpfs  3.9G   72K  3.9G   1% /dev/shm
/dev/sda1      ext4   190M   39M  142M  22% /boot
[root@localhost ~]# fdisk -l

Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 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: 0x00047e01

   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        2115    16777216   82  Linux swap / Solaris
/dev/sda3            2115       13055    87874560   83  Linux

WARNING: GPT (GUID Partition Table) detected on '/dev/sdb'! The util fdisk doesn't support GPT. Use GNU Parted.


Disk /dev/sdb: 2089.1 GB, 2089072092160 bytes
255 heads, 63 sectors/track, 253981 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

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1      253982  2040109464+  ee  GPT
[root@localhost ~]# mkfs.ext4 -F /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
127508480 inodes, 510027357 blocks
25501367 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
15565 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
	4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 
	102400000, 214990848

Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 31 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@localhost ~]# df -Th
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/sda3      ext4    83G  3.4G   75G   5% /
tmpfs          tmpfs  3.9G   72K  3.9G   1% /dev/shm
/dev/sda1      ext4   190M   39M  142M  22% /boot
[root@localhost opt]# mkdir /app
[root@localhost opt]# mount /dev/sdb
sdb   sdb1  
[root@localhost opt]# mount /dev/sdb1 /opt/
[root@localhost opt]# df -Th
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/sda3      ext4    83G  3.4G   75G   5% /
tmpfs          tmpfs  3.9G   72K  3.9G   1% /dev/shm
/dev/sda1      ext4   190M   39M  142M  22% /boot
/dev/sdb1      ext4   1.9T   67M  1.8T   1% /opt
[root@localhost opt]# cd /opt/
[root@localhost opt]# ls
lost+found
[root@localhost opt]# umount /opt
umount: /opt: device is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))
[root@localhost opt]# cd ..
[root@localhost /]# cd ..
[root@localhost /]# umount /opt
[root@localhost /]# cd /opt/
[root@localhost opt]# ls
rh
[root@localhost opt]# mount -t ext4 /dev/sdb1 /app/
[root@localhost opt]# df -Th
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/sda3      ext4    83G  3.4G   75G   5% /
tmpfs          tmpfs  3.9G   72K  3.9G   1% /dev/shm
/dev/sda1      ext4   190M   39M  142M  22% /boot
/dev/sdb1      ext4   1.9T   67M  1.8T   1% /app
[root@localhost opt]# blki
blkid     blkiomon  
[root@localhost opt]# blkid /dev/sdb1 
/dev/sdb1: UUID="452d68c5-58e3-41aa-979c-2ce38b31050f" TYPE="ext4" 
[root@localhost opt]# cat /etc/fstab 
UUID="452d68c5-58e3-41aa-979c-2ce38b31050f" /app  ext4    defaults        0 0
[root@localhost opt]# df -Th
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/sda3      ext4    83G  3.5G   75G   5% /
tmpfs          tmpfs  3.9G   72K  3.9G   1% /dev/shm
/dev/sda1      ext4   190M   39M  142M  22% /boot
/dev/sdb1      ext4   1.9T   67M  1.8T   1% /app
[root@localhost opt]#



dumpe2fs用来查询文件系统状态

[root@zhaolei-10 ~]# dumpe2fs -h /dev/mapper/VolGroup-LogVol00
dumpe2fs 1.41.12 (17-May-2010)
Filesystem volume name:   <none>
Last mounted on:          /
Filesystem UUID:          649401be-04d1-4535-9797-aadc1a500437
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 large_file 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:              31645696
Block count:              126563328
Reserved block count:     6326236
Free blocks:              68305331
Free inodes:              6694476
First block:              0
Block size:               4096
Fragment size:            4096
Reserved GDT blocks:      993
Blocks per group:         32768
Fragments per group:      32768
Inodes per group:         8192
Inode blocks per group:   512
Flex block group size:    16
Filesystem created:       Thu Jun 26 19:34:35 2014
Last mount time:          Sat Jul  7 11:44:47 2018
Last write time:          Sun Jul  3 17:21:36 2016
Mount count:              33
Maximum mount count:      -1
Last checked:             Thu Jun 26 19:34:35 2014
Check interval:           0 (<none>)
Lifetime writes:          1788 GB
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:      7c444e29-c6bb-40b5-8cc7-647af7e07773
Journal backup:           inode blocks
Journal features:         journal_incompat_revoke
Journal size:             128M
Journal length:           32768
Journal sequence:         0x01b7599a
Journal start:            28573

[root@zhaolei-10 ~]#

选项     描述
-b     打印文件系统中的坏块
-o     不常用,检查严重损坏文件系统时指定
-f     强制显示所有信息,即便dumpe2fs对有些文件系统功能标识不能识别。
- i     显示image文件系统信息。device指定image文件的路径
-h     只显示超级块信息
-x     将已分组的块的数量用十六进制显示
-v     显示dumpe2fs的版本号并推出


df和du用来查文件系统大小,fsck文件系统修复,dumpe2fs查看文件系统状态