Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to script sfdisk or parted for multiple partitions?

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
}
like image 970
LabRat Avatar asked Aug 27 '12 21:08

LabRat


People also ask

What is sfdisk command used for?

sfdisk has four (main) uses: list the size of a partition, list the partitions on a device, check the partitions on a device, and - very dangerous - repartition a device. sfdisk doesn't understand GUID Partition Table (GPT) and it is not designed for large partitions. In particular case use more advanced GNU parted(8).

What is Sgdisk?

sgdisk is a command-line program for manipulating GPT partition tables. It is intended to be used in scripts, and is also useful for experts making quick changes.


2 Answers

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
like image 78
Stephane Rouberol Avatar answered Oct 19 '22 03:10

Stephane Rouberol


sfdisk

sfdisk is a Scripted version of fdisk

It is part of util-linux, just like fdisk, so availability should be the same.

A partition table with a single partition that takes the whole disk can be created with:

echo 'type=83' | sudo sfdisk /dev/sdX

and more complex partition tables are explained below.

To generate an example script, get the setup of one of your disks:

sudo sfdisk -d /dev/sda > sda.sfdisk

Sample output on my Lenovo T430 Windows 7 / Ubuntu dual boot:

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

Once you have the script saved to a file, you can apply it to sdX with:

sudo sfdisk /dev/sdX < sda.sfdisk

For sfdisk input, you can just omit the device names, and use lines of type:

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

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

Some explanations:

  • header lines: all optional:
    • label: type of partition table. dos (MBR) is the old an widely supported one, gpt the new shiny thing.
    • unit: only sector is supported. 1 sector usually equals 512 bytes. Find with cat /sys/block/sda/queue/hw_sector_size See also: https://unix.stackexchange.com/questions/2668/finding-the-sector-size-of-a-partition
    • device: informative only I think
  • partition lines:

    • start: offset inside the disk at which the partition starts.

      start has very good defaults, and can often be ommited:

      • on the first line, start is 2048, i.e. 1Mb (2048 + 512), which is a sane default for disk compatibility
      • further start default to the first unallocated position
    • size: man sfdisk says: The default value of size indicates "as much as possible". So to fill the disk with a single partition use: /dev/sda : start=2048, type=83

    • type: magic byte stored on the boot sector for each partition entry. Possible values: https://en.wikipedia.org/wiki/Partition_type On this example we observe:
      • 7 (sda1, 2 and 3): filesystems that Windows supports. Preinstalled Windows stuff and Lenovo recovery partitions. sudo blkid labels help identify them.
      • 5 (sda4): extended primary partition, which will contain other logical partitions (because we can only have 4 primary partitions with MBR)
      • 83(sda5, 7, and 8): partitions which Linux supports. For me one home, and two roots with different Ubuntu versions
      • 82 (sd6): swap

fdisk can also read sfdisk scripts with the I command, which "sources" them during an interactive fdisk session, allowing you further customization before writing the partition.

Tested on Ubuntu 16.04, sfdisk 2.27.1.

Format and populate the partitions an image file without sudo

This is a good way to learn to use sfdisk without blowing up your hard disks: How to create a multi partition SD disk image without root privileges?