8.1 fdisk:磁盘分区工具

fdisk

  是Linux下常用的磁盘分区工具。受mbr分区表的限制,fdisk工具只能给小于2TB的磁盘划分分区。如果使用fdisk对大于2TB的磁盘进行分区,虽然可以分区,但其仅识别2TB的空间,所以磁盘容量若超过2TB,就要使用parted分区工具(后面会讲)进行分区。
fdisk [选项] [设备名] 
-l    显示所有磁盘分区的信息
 
[root@cs6 ~]# fdisk -l #<==查看当前系统所有磁盘的分区信息。
 
Disk /dev/sda: 32.2 GB, 32212254720 bytes  #<==磁盘/dev/sda的大小。
255 heads, 63 sectors/track, 3916 cylinders #<==255个虚拟磁头,63个扇区/磁道,3916 个柱面。
Units = cylinders of 16065 * 512 = 8225280 bytes #<==一个柱面大小8225280 bytes。
Sector size (logical/physical): 512 bytes / 512 bytes #<==每个扇区的字节数。
I/O size (minimum/optimal): 512 bytes / 512 bytes  #<==每次读写的字节数。
Disk identifier: 0x0001038a
 
   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        3917    30944256   8e  Linux LVM
 
Disk /dev/mapper/vg_cs6-lv_root: 28.5 GB, 28462546944 bytes
255 heads, 63 sectors/track, 3460 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
 
 
Disk /dev/mapper/vg_cs6-lv_swap: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 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:block 块数量;
Id:分区类型Id;
System:分区类型。
 

在虚拟机(VMware Workstation Pro)模拟磁盘分区实战

步骤1:先在虚拟机关机状态下添加一块1GB硬盘,若是在开机状态下添加,则需要重启系统。
步骤2:重启系统后查看添加的磁盘。 
[root@cs6 ~]# fdisk -l
 
Disk /dev/sdb: 1073 MB, 1073741824 bytes #<==刚刚新添加的硬盘名为sdb,是第二块盘。
255 heads, 63 sectors/track, 130 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@cs6 ~]# fdisk -l /dev/sdb
 
Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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 

步骤3:交互式分区实践。

 
[root@cs6 ~]# ls /dev/sd* #<=需查看分区前设备的状态。
/dev/sda  /dev/sda1  /dev/sda2  /dev/sdb
 
[root@cs6 ~]# 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 0x34d8eef3.
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  #<==提示使用-c关闭dos兼容模式。
         sectors (command 'u').       #<==提示-u参数使用扇区为单位分区。
                            
Command (m for help): m                #<= m是帮助,需要人工输入m后回车,
Command action
   a   toggle a bootable flag                #<==设置引导扇区。
   b   edit bsd disklabel                    #<==编辑bsd卷标。
   c   toggle the dos compatibility flag     #<==设置dos兼容扇区。
   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      #<==创建新的空 sun卷标。
   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     #<==新建一个分区,需要人工输入n后回车。
Command action
   e   extended                    #<==创建扩展分区。
   p   primary partition (1-4)     #<==创建主分区(编号1-4)。
p
Partition number (1-4): 1            #<==设置分区编号为1,需要人工输入1后回车。
First cylinder (1-130, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-130, default 130): +100M
#<==设置结束柱面(130)或分区大小(+100M),因为要划分出指定大小的分区,所以常用+100M这种方法,如果分区时使用fdisk -cu /dev/sdb,则这里就会使用扇区为单位来进行分区。
Command (m for help): p
 
Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0x34d8eef3
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          14      112423+  83  Linux
 
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
2
Invalid partition number for type `2'
Command action
   e   extended
   p   primary partition (1-4)
e
Partition number (1-4): 2
First cylinder (15-130, default 15):
Using default value 15
Last cylinder, +cylinders or +size{K,M,G} (15-130, default 130):
Using default value 130    #<==按回车键,默认设置结束柱而号130。
 
Command (m for help): p
 
Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0x34d8eef3
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          14      112423+  83  Linux
/dev/sdb2              15         130      931770    5  Extended
 
Command (m for help): n
Command action
   l   logical (5 or over)        #<=分了扩展分区,这里自动变为逻辑分区。
   p   primary partition (1-4)
p
Partition number (1-4): 3
No free sectors available     #<==不能再创建主分区,没有磁盘空间了。
                              #<==再新建一个分区。
Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (15-130, default 15): #<=按回车键,开始柱面号为15。
Using default value 15
Last cylinder, +cylinders or +size{K,M,G} (15-130, default 130): +400M
                        #<==设置分区大小为400MB
Command (m for help): p
 
Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0x34d8eef3
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          14      112423+  83  Linux
/dev/sdb2              15         130      931770    5  Extended
/dev/sdb5              15          66      417658+  83  Linux
 
Command (m for help): n #<==新建一个分区。
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l    #<==再新建一个逻辑分区。
First cylinder (67-130, default 67):
Using default value 67
Last cylinder, +cylinders or +size{K,M,G} (67-130, default 130):
Using default value 130
 
Command (m for help): p    #<==打印分区表。
 
Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0x34d8eef3
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          14      112423+  83  Linux
/dev/sdb2              15         130      931770    5  Extended
/dev/sdb5              15          66      417658+  83  Linux
/dev/sdb6              67         130      514048+  83  Linux
 
Command (m for help): w    #<==将操作写入分区表生效并退出程序。
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
Syncing disks.
[root@cs6 ~]# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sdb  /dev/sdb1  /dev/sdb2  /dev/sdb5  /dev/sdb6
[root@cs6 ~]#
[root@cs6 ~]# yum -y install parted
[root@cs6 ~]# partprobe /dev/sdb    #<==执行该命令通知内核分区表已更改,此步是不重启让分区表生效的命令。
步骤4:格式化磁盘。
 
[root@cs6 ~]# mkfs.ext4 /dev/sdb1    #<==只有格式化后的磁盘才能挂载到系统中使用,后面将会详细讲解mkfs.ext4命令。
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=1024 (log=0)
分块大小=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
28112 inodes, 112420 blocks
5621 blocks (5.00%) reserved for the super user
第一个数据块=1
Maximum filesystem blocks=67371008
14 block groups
8192 blocks per group, 8192 fragments per group
2008 inodes per group
Superblock backups stored on blocks:
    8193, 24577, 40961, 57345, 73729
 
正在写入inode表: 完成                            
Creating journal (4096 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
 
This filesystem will be automatically checked every 20 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
 
 
[root@cs6 ~]# tune2fs -c -1 /dev/sdb1    #<=执行这个命令可以避免磁盘挂载自动检查磁盘,后面将会讲解tune2fs命令。
tune2fs 1.41.12 (17-May-2010)
Setting maximal mount count to -1

 步骤5:挂载磁盘分区。

[root@cs6 ~]# vi /etc/fstab    #<==最后一行加入,要开机自动挂载磁盘就要加入/etc/fstab或将上面的mount命令放入/etc/rc.local中。
 
/dev/sdb1              /mnt                     ext4       defaults     0  0
 
[root@cs6 ~]# vi /etc/rc.local    #<==或者编辑/etc/rc.local,最后一行加入,两种方法二选一。
mount  /dev/sdb1  /mnt

 步骤6:其他事项。

    用交互指令d删除分区时要小心,看好分区的序号,如果删除了扩展分区,那么扩展分区之下的逻辑分区都会删除,所以操作时一定要小心。如果不小心操作错了,直接使用交互指令q不保存退出,这样先前的操作就会无效。如果输入w(保存指令)则会保存所有修改。
[root@cs6 ~]# fdisk /dev/sdb
 
Command (m for help): d
Partition number (1-6): 2
 
Command (m for help): p
 
Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0x34d8eef3
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          14      112423+  83  Linux

fdsik 非交互式分区(批量分区案例)

 
 
以下是实现非交互式分区的代码。
fdisk /dev/sdb <<EOF    #<==也可以将下面的内容写入文本文件,然后读文本执行。
n
P
1

+100M n e 2

n l +400 n l
p
w EOF
 
 
 
 
-
posted @ 2019-05-11 11:24  Fadewalk  阅读(2417)  评论(0编辑  收藏  举报