How to script sfdisk or split into several sections?

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 } 
+6
source share
4 answers

I'm not sure to get what you really want, but you might be interested in the fact that sfdisk can unload the partition layout and use this layout to separate other disks. For instance:

 sfdisk -d /dev/sda > mydiskpartitionslayout 

Then in the script (note, of course) you can specify

 sfdisk /dev/sdx < mydiskpartitionslayout 
+13
source

sfdisk explained example

Received from my Lenovo T430 Windows 7 / Ubuntu dual boot:

 sfdisk -d /dev/sda 

as described in Stephen's Answer . Exit:

 label: dos label-id: 0x7ddcbf7d device: /dev/sda unit: sectors /dev/sda1 : start= 2048, size= 3072000, type=7, bootable /dev/sda2 : start= 3074048, size= 195430105, type=7 /dev/sda3 : start= 948099072, size= 28672000, type=7 /dev/sda4 : start= 198504446, size= 749594626, type=5 /dev/sda5 : start= 198504448, size= 618891264, type=83 /dev/sda6 : start= 940277760, size= 7821312, type=82 /dev/sda7 : start= 817397760, size= 61437952, type=83 /dev/sda8 : start= 878837760, size= 61437500, type=83 

To enter sfdisk you can simply omit device names and use strings like:

 start= 2048, size= 3072000, type=7, bootable 

They are simply ignored if they are present, and the device name is taken from the command line argument.

Structure:

  • header lines: all optional:
  • section lines:
    • size : man sfdisk says: The default value of size indicates "as much as possible" . Therefore, to fill the disk with one partition, use: /dev/sda : start=2048, type=83
    • type : magic byte stored in the boot sector for each record . Possible values: https://en.wikipedia.org/wiki/Partition_type In this example, we observe:
      • 7 ( sda1 , 2 and 3 ): file systems supported by Windows. Preinstalled Windows partitions and Lenovo recovery partitions. sudo blkid tags help identify them.
      • 5 ( sda4 ): an extended primary partition that will contain other logical partitions (because we can only have 4 primary partitions with MBR)
      • 83 ( sda5 , 7 and 8 ): partitions supported by Linux. For me, one home and two roots with different versions of Ubuntu
      • 82 ( sd6 ): swap

Tested on Ubuntu 16.04, sfdisk 2.27.1.

+1
source

The approach that I like (which I saw in this article ) refers to the "script" input file of fdisk directly, since it is smarter than sfdisk about creating a partition "to the end of the disk" or "2 GB large." Example:

 echo "d 1 d 2 d 3 n p 1 +2G n p 2 w " | fdisk /dev/sda 

This script deletes up to 3 existing partitions, creates a 2 GB partition (for example, a swap), and then creates a partition that will extend to the remaining disk space.

In contrast, if the partition layout was created and used in sfdisk, the script will not cover the entire disk if more space is available.

0
source

Automating a recurring task is the norm in the life of automation, and we need a method to automatically provide answers to these programs if we want to include them in our script.

A program called "Pending" is automated here. For Red Hat-based systems, run the following command to install Pending

yum install expect

For Debian or Ubuntu, run the following command.

apt-get install expect

below is the wait for the script to create the / dev / sdc partition


! / USR / BIN / planner

log_file -a "/tmp/expect.log"

set timeout 600

spawn / sbin / fdisk / dev / sdc

expect "Command (m for reference):" {send "n \ r"}

expect "p primary section (1-4)"

expect "{send" p \ r "}

expect "Section number (1-4):" {send "1 \ r"}

expect "First cylinder (1-133544, default 1):" {send "1 \ r"}

expect ":" {send "\ r"}

expect "Command (m for reference):" {send "w \ r"}

interact

0
source

Source: https://habr.com/ru/post/923915/


All Articles