fdisk分区基于命令和echo传入命令差异

问题

正常echo命令到 fdisk管道,是需要参考实际操作过程…

env

  • rocky9.4
  • fdisk(在线扩容根分区)

⚠️:在线fdisk删除根分区,在新建分区修正尾部扇区(默认全部分配),在w保存实际不影响系统。

分析 && 解决

1.fdisk /dev/sda 新建分区提示是否移除签名

Command (m for help): n
Partition number (5-128, default 5): 
First sector (2265088-104857566, default 2265088): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2265088-104857566, default 104857566): 

Created a new partition 5 of type 'Linux filesystem' and of size 48.9 GiB.
Partition #5 contains a xfs signature.

Do you want to remove the signature? [Y]es/[N]o: 

2.因此参考写出基于echo分区命令

echo将响应传递给fdisk

echo -e 'd\n\nn\n\n\n\nY\np\n' |fdisk /dev/sda

52eebc7c0802b0d4efdb82f51a4823aa.png

提示不能识别Y命令,看过程都没提示要确认移除签名

3.查看fdisk help命令

发现有默认值 -w/-W auto 自行判断是否要移除签名

[root@vgt-rocky9-60 ~]# fdisk --help

Usage:
 fdisk [options] <disk>         change partition table
 fdisk [options] -l [<disk>...] list partition table(s)

Display or manipulate a disk partition table.

Options:
...
 -w, --wipe <mode>             wipe signatures (auto, always or never)
 -W, --wipe-partitions <mode>  wipe signatures from new partitions (auto, always or never)
...

4.移除确认y,命令顺利执行

echo -e "d\n\nn\n\n\n\np\nw\n" | fdisk /dev/sda

aad018ca0cdaf2cbc15c3c46ca031efe.png

根据分析得知,基于wipe形式,不需要人工确认是否移除分区签名(自动给处理了)

基于echo命令执行过程

echo -e "d\n\nn\n\n\n\np\nw\n" | fdisk /dev/sda

解释:
d\n  删除分区+回车
\n   默认值确认
n\n  n新建分区+回车
\n   默认值确认
\n   默认值确认
\n   默认值确认
p\n  打印分区信息 + 回车
w\n  保存分区 + 回车 

操作过程

[vagrant@vgt-rocky9-60 ~]$ sudo fdisk /dev/sda

Welcome to fdisk (util-linux 2.37.4).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

GPT PMBR size mismatch (20971519 != 104857599) will be corrected by write.
The backup GPT table is not on the end of the device. This problem will be corrected by write.
This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.


Command (m for help): p

Disk /dev/sda: 50 GiB, 53687091200 bytes, 104857600 sectors
Disk model: VBOX HARDDISK   
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 31708F37-29EF-4E85-BE83-713DF6452BB1

Device       Start      End  Sectors  Size Type
/dev/sda1     2048   206847   204800  100M EFI System
/dev/sda2   206848  2254847  2048000 1000M Linux filesystem
/dev/sda3  2254848  2263039     8192    4M PowerPC PReP boot
/dev/sda4  2263040  2265087     2048    1M BIOS boot
/dev/sda5  2265088 18649087 16384000  7.8G Linux filesystem

Command (m for help): d
Partition number (1-5, default 5): 

Partition 5 has been deleted.

Command (m for help): n
Partition number (5-128, default 5): 
First sector (2265088-104857566, default 2265088): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2265088-104857566, default 104857566): 

Created a new partition 5 of type 'Linux filesystem' and of size 48.9 GiB.
Partition #5 contains a xfs signature.

Do you want to remove the signature? [Y]es/[N]o: y

The signature will be removed by a write command.

Command (m for help): p
Disk /dev/sda: 50 GiB, 53687091200 bytes, 104857600 sectors
Disk model: VBOX HARDDISK   
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 31708F37-29EF-4E85-BE83-713DF6452BB1

Device       Start       End   Sectors  Size Type
/dev/sda1     2048    206847    204800  100M EFI System
/dev/sda2   206848   2254847   2048000 1000M Linux filesystem
/dev/sda3  2254848   2263039      8192    4M PowerPC PReP boot
/dev/sda4  2263040   2265087      2048    1M BIOS boot
/dev/sda5  2265088 104857566 102592479 48.9G Linux filesystem

Filesystem/RAID signature on partition 5 will be wiped.

Command (m for help): w
The partition table has been altered.
Syncing disks.

[vagrant@vgt-rocky9-60 ~]$ sudo fdisk /dev/sda

Welcome to fdisk (util-linux 2.37.4).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.


Command (m for help): p

Disk /dev/sda: 50 GiB, 53687091200 bytes, 104857600 sectors
Disk model: VBOX HARDDISK   
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 31708F37-29EF-4E85-BE83-713DF6452BB1

Device       Start       End   Sectors  Size Type
/dev/sda1     2048    206847    204800  100M EFI System
/dev/sda2   206848   2254847   2048000 1000M Linux filesystem
/dev/sda3  2254848   2263039      8192    4M PowerPC PReP boot
/dev/sda4  2263040   2265087      2048    1M BIOS boot
/dev/sda5  2265088 104857566 102592479 48.9G Linux filesystem