How to run fdisk in non-interactive mode
fdisk is a command-line utility for disk partition management.If you are going to create one or two partition then its okay to go with manual user intracetion method of fdisk. But if the partition list is a bit longer or we can say its repitative for several machines then need to seek some automation , here we go for probable two solutions for this : -
Solution - I : Using below shell script
#!/bin/sh
sdd="/dev/sdb /dev/sdc /dev/sdd"
for i in $sdd
do
echo "n
p
1
t
8e
w
"|fdisk $i;pvcreate "$i"1;done
n = Create new partition
p = Primary partition
1 = 1st partition of primary type
t = Partition type
8e = Linux LVM
Note: two consicuitve blank lines between 1 and t .
Solution - II : Using sfdisk Utility
This tool allows you to create/change partition tables according to the disk partitioning specification read from standard input. It also allows you to export the partition table specification of a device to a file, so that sfdisk can read the file back in to apply the same partitioning to a different device.
Solution - I : Using below shell script
#!/bin/sh
sdd="/dev/sdb /dev/sdc /dev/sdd"
for i in $sdd
do
echo "n
p
1
t
8e
w
"|fdisk $i;pvcreate "$i"1;done
n = Create new partition
p = Primary partition
1 = 1st partition of primary type
t = Partition type
8e = Linux LVM
Note: two consicuitve blank lines between 1 and t .
Solution - II : Using sfdisk Utility
This tool allows you to create/change partition tables according to the disk partitioning specification read from standard input. It also allows you to export the partition table specification of a device to a file, so that sfdisk can read the file back in to apply the same partitioning to a different device.
The basic usage of sfdisk for non-interactive disk partitioning is as follows.
To prepare a disk partitioning specification:
$ sudo sfdisk -d /dev/sda > my.layout
The exported disk layout information looks as follows.
$ cat my.layout
# partition table of /dev/sda
unit: sectors
/dev/sda1 : start= 2048, size= 497664, Id=83, bootable
/dev/sda2 : start= 501758, size=1953021954, Id= 5
/dev/sda3 : start= 0, size= 0, Id= 0
/dev/sda4 : start= 0, size= 0, Id= 0
/dev/sda5 : start= 501760, size=1953021952, Id=8e
To apply the same disk partitioning and layout to another device:
$ sudo sfdisk /dev/sdb < my.layout
Hope , this will make your miserable life a bit easy.
Cheers !!
AJ
Comments
Post a Comment