For QA purposes, I should be able to split the disk using a bash script with up to 30 or more partitions for RHEL and SLES.
I tried to do this in bash using fdisk through a "here document" that works, but as you can guess, it inflates at different stages. I assume that this is due to the fact that input commands are executed at inopportune times and go out of sync. 1 out of 10 times my script will work correctly.
I looked through the section and sfdisk and don’t understand how to use these tools.
I just used fdisk.
My problem is that with fdisk you can specify something like “new section + 1gb” over and over again, and this works for me because in my script I don’t have to track previous sections or remaining space or do any calculations . Each time I run this function, it just makes an extra 1gb partition from any unused space.
Is there a way to use parted or sfdisk (or any other tool that would already be part of these distributions) so that I can script a cycle from 1 to x operations without having to take into account the remaining space? Does anyone have examples to share?
Update
Here is an example of one of my functions. At the beginning of the script, we ask the user for the number of partitions, their size (this is static for everyone) and the FS type, if any. These functions create section 1 through 3, and another function processes the extended (4th), and the other handles from 5 to nn.
As I said, this script is fully functional; my problem is that from time to time the commands sent to fdisk seem to get into the wrong time, which then breaks the whole script, thereby disrupting any automation.
So, the commands are sent as follows:
n p 1 +1M w
I read fdisk and realized that it is not suitable for scripts, so I see that when fdisk can request p in script mode, my script already thinks that this is the time to send 1 .
As for fdisk, which worked for me, it’s that after you specify the partition number, it has already calculated the next free sector , so all I need to do at this point is send an empty line for my launch and then + 1M for my total size. Parted and sfdisk don't seem to work the way I can tell, and I'm still very new to this to figure out how to automate these 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 }