fdisk利用脚本自动分区,可以用于快速部署系统, 或者需要复制一个硬盘的分区格式到另外的硬盘。
在sfdisk有个很好用的-d参数,以前十多年以来,我们一直这么干:
sfdisk -d /dev/sdx |sfdisk /dev/sdy
把分区从x盘复制到y盘
但sfdisk有2个弊端, 由于sfdisk不是核心工具,在debian的各版本中,它会时有时无,并且sfdisk不支持GPT
最近发现linux的核心工具fdisk从sfdisk吸收了这个思路,可以用O命令输出类似于sfdisk的分区表文本备份文件,并且可以用I命令导入,并且支持GPT
虽然fdisk不能用命令行来做分区表的自动导入导出。我们可以用重定向功能来实现脚本自动。这可以用在自动快速安装linux系统上
详细如下(红字为键盘输入内容,灰底是需要注意部分):
root@voip:/bak/lims# fdisk /dev/sda
Welcome to fdisk (util-linux 2.29.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): O
Enter script file name: sda.txt
Script successfully saved.
Command (m for help): q
root@voip:/bak/lims# cat sda.txt
label: dos
label-id: 0x000ab7fb
device: /dev/sda
unit: sectors
/dev/sda1 : start= 63, size= 58593750, type=83, bootable
/dev/sda2 : start= 58593813, size= 1894926252, type=f
/dev/sda5 : start= 58593876, size= 7812500, type=82
/dev/sda6 : start= 66406377, size= 1887113688, type=83
root@voip:/bak/lims# cat sde.txt
label: gpt
label-id: 123
device: /dev/sde
unit: sectors
first-lba: 2048
/dev/sde1 : size=200M
/dev/sde2 : size=30G
/dev/sde3 : size=5G
/dev/sde4 : size=5G
/dev/sde5 : size=5G
root@voip:/bak/lims# cat 1.txt
I
sde.txt
w
root@voip:/bak/lims# fdisk /dev/sde <1.txt
Welcome to fdisk (util-linux 2.29.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help):
Enter script file name:
Created a new GPT disklabel (GUID: BDA9433C-5E68-410C-B169-55BE40D21304).
Created a new partition 1 of type 'Linux filesystem' and of size 200 MiB.
Created a new partition 2 of type 'Linux filesystem' and of size 30 GiB.
Created a new partition 3 of type 'Linux filesystem' and of size 5 GiB.
Created a new partition 4 of type 'Linux filesystem' and of size 5 GiB.
Created a new partition 5 of type 'Linux filesystem' and of size 5 GiB.
Script successfully applied.
Command (m for help): The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
root@voip:/bak/lims#