How to script sfdisk or parted for multiple partitions?

bash, partitioning

Solution

Not sure to get what you really want, but you may be interested by the fact that `sfdisk` can dump a partition layout and use this layout to partition other disks. For instance:

sfdisk -d /dev/sda > mydiskpartitionslayout

Then in your script (take care of course) you can specify

sfdisk /dev/sdx < mydiskpartitionslayout

Problem

For QA purposes I need to be able to partition a drive via a bash script up to 30 or more partitions for both RHEL and SLES. I have attempted to do this in BASH with fdisk via a "here document" which works but as you can guess blows up in various steps. I assume this is because of timing of the input commands occurring at the wrong times and getting out of sync. 1 out of 10 times my script will work correctly. I have looked at parted and sfdisk and don't really understand how to use these tools. I have only ever used fdisk. My issue is that with fdisk you can state something like "new partition +1gb" over and over and this works for me because in my script I don't need to keep track of prior partitions or remaining space or do any calculations. Every time I run this function this just makes an additional 1gb partition from any unused space. Is there a way to use parted or sfdisk (or any other tool that would already be a part of these distros) so that I could script a loop from 1 to x operations without having to do this accounting of remaining space? Does anyone have any examples they could share? Update Here is an example of one of my functions. At the start of the script we ask the user for the number of partitions to create, their size (this is static for all), and type of FS if any. This functions creates partition 1 thru 3 then a different function handles the extended (4th) and another handles 5th to nn. As I said before, this script is fully functional; my problem is that at times the commands being sent to fdisk seem to arrive at the wrong timing, which then breaks the entire script thereby breaking any automation. So the commands are being sent like this: ``` n p 1 +1M w ``` I have been reading up on fdisk and have learned it is not suited well for scripting so what I am seeing is that when in script mode, fdisk might be asking for `p` my script already thinks it's time to send the `1`. The thing about fdisk that worked for me is that after you specify the partition number it already calculated the next free sector so all I have to do at this point is send a blank line for my start and then the +1M for my total size. Parted and sfdisk don't appear to work this way from what I can tell and I am still very new at this to understand how to automate those tools at this time. ``` Create1to3Primary_Func() { Size=\+$partSize\MB for i in {1..3} do echo " this loop i= $i" echo "Creating Partition $i on $targetFull as $targetFull$i using Create1to3Primary_Func()" rm -f /tmp/myScript echo -e "n" >> /tmp/myScript echo -e "p" >> /tmp/myScript echo -e "$i" >> /tmp/myScript echo -e " " >> /tmp/myScript echo -e "$Size" >> /tmp/myScript echo -e "w" >> /tmp/myScript echo -e "EOF" >> /tmp/myScript fdisk $targetFull < /tmp/myScript echo " sleeping Create1to3Primary_Func()" sleep 4s if [ "$RawOrFs" == "f" ]; then mkfsCMD="mkfs.$fsType" mkfsFullTarget="$targetFull$i" cmdline="$mkfsCMD $mkfsFullTarget -L 'Partition$i'" echo "Creating $fsType File System on $mkfsFullTarget" $cmdline fi void="/mnt/mymnt$i" if [ ! -d $void ] ; then echo "Creating Mount Point /mnt/mymnt$i" void="/mnt/mymnt$i" mkdir $void fi echo "Part Probe on $targetFull " partprobe $targetFull ; sleep 4s done } ```

Original source

Related problems