用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

转载于:https://blog.51cto.com/14230230/2368581

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值