用fdisk命令對磁盤分區進行管理

一、給主機添加一塊新硬盤sdb(也可用原有的sda進行操作)

二、步驟
1.查看現有分區

[19:34:36 root@centos7 ~]#lsblk
NAME   MAJ:MIN RM     SIZE       RO   TYPE     MOUNTPOINT
sda             8:0       0       200G        0      disk 
├─sda1      8:1       0           1G        0      part              /boot
├─sda2      8:2       0       100G        0      part              /
├─sda3      8:3       0         50G        0      part              /data
├─sda4      8:4       0           1K         0      part 
└─sda5      8:5       0           3G         0      part           [SWAP]
sdb             8:16     0         20G         0      disk 
sdc             8:32     0         20G         0      disk 
sr0             11:0      1         10G       0    rom  /run/media/root/CentOS 7 x86_64

2.查看fdisk命令幫助

[08:48:17 root@centos7 ~]#fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): m  //輸入m查看幫助
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition   //刪除分區
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
    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 partitions 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)

3.開始創建分區

Command (m for help): n  //創建新分區
Partition type:   //分區類型
   p   primary (0 primary, 0 extended, 4 free) 
   e   extended
Select (default p):  //默認主分區
Using default response p
Partition number (1-4, default 1):   //分區編號,默認1
First sector (2048-41943039, default 2048):  //起始字節數
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +1G 
//創建1G大小的新分區
Partition 1 of type Linux and of size 1 GiB is set

Command (m for help): P  //查看創建的分區

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xfd655652

   Device Boot      Start         End           Blocks    Id  System
/dev/sdb1            2048     2099199     1048576   83  Linux
//分區已建好
Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): 
Using default response p
Partition number (2-4, default 2): 
First sector (2099200-41943039, default 2099200): 2300000
                                                                                 //此處跳過起始字節號
Last sector, +sectors or +size{K,M,G} (2300000-41943039, default 41943039): 2500000  //終止字節號
Partition 2 of type Linux and of size 97.7 MiB is set

Command (m for help): n //繼續創建新分區
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): 
Using default response p
Partition number (3,4, default 3): 
First sector ('2099200'-41943039, default 2099200): //此處起始字節已有變化
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-2299999, default 2299999):
+2G //此處要創建的分區大小已被限制,掉進前面跨過的"夾縫"
Value out of range.      //顯示超出範圍,但磁盤空間還剩很多
Last sector, +sectors or +size{K,M,G} (2099200-2299999, default 2299999): 
Using default value 2299999
Partition 3 of type Linux and of size 98 MiB is set

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xfd655652

   Device Boot      Start         End            Blocks        Id  System
/dev/sdb1            2048        2099199      1048576     83  Linux
/dev/sdb2         2300000     2500000      100000+     83  Linux
/dev/sdb3         2099200     2299999      100400       83  Linux

Partition table entries are not in disk order //分區表條目不按磁盤順序排列

若要避免這種情況發生,創建分區時儘量使字節號連續,中間不要有跨越
4.保存退出,查看分區已建立好

Command (m for help): w  //保存並退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[08:53:53 root@centos7 ~]#lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0  100G  0 part /
├─sda3   8:3    0   50G  0 part /data
├─sda4   8:4    0    1K  0 part 
└─sda5   8:5    0    3G  0 part [SWAP]
sdb      8:16   0   20G  0 disk 
├─sdb1   8:17   0    1G  0 part 
├─sdb2   8:18   0 97.7M  0 part     //分區已創建成功
└─sdb3   8:19   0   98M  0 part 
sdc      8:32   0   20G  0 disk 
sr0     11:0    1   10G  0 rom  /run/media/root/CentOS 7 x86_64
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章