You are not logged in.

#1 2014-12-21 13:46:06

satori
Member
Registered: 2013-01-21
Posts: 29

Fdisk used in script gives "Device or resource busy" without sleep

Hello!

I'm using fdisk in bash function to create few partitions automatically using a script.

createPartition()
{
    local disk="$1"         # e.g. /dev/sda
    local partType="$2"     # "p" for prtimary, "e" for extented
    local partNb="$3"       # e.g. "1" for "/dev/sda1"
    local partSize="$4"     # e.g. "+1G" for 1GiB, "" for remaining space
    local partCode="$5"     # e.g. "82" for swap, "83" for Linux, etc.
    local partCodeNb=""     # No partition nb for code setting for 1st partition

    # For first partition, provide partition number when entering
    # partition code
    if [[ $partNb -ne 1 ]]; then
        partCodeNb=$partNb
    fi

    cat <<-EOF | fdisk $disk
    n
    $partType
    $partNb
    
    $partSize
    t
    $partCodeNb
    $partCode
    w
    EOF

    err "$?" "$FUNCNAME" "failed to create partition"
}

The function gets called few times in a row, once for each partition.
Unless I add sleep 5 after each createPartition call, I get following report:

Re-reading the partition table failed: Device or resource busy

Having a sleep is not universal solution as for some older disks time might vary, etc.
I tried to replace the sleep command with sync and partprobe but it doesn't help.

Is there an elegant way to ensure disk operations are finished before the script proceeds to next partition creation?

Thanks in advance

Offline

#2 2014-12-21 14:50:22

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,331
Website

Re: Fdisk used in script gives "Device or resource busy" without sleep

Could you gather all the data first, then just have one call to fdisk?

If not, then just use `sync` instead of sleep.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2014-12-21 22:06:13

satori
Member
Registered: 2013-01-21
Posts: 29

Re: Fdisk used in script gives "Device or resource busy" without sleep

Thanks for quick reply.
The function is supposed to be a utility. I don't know how many partitions will be needed on given machine upfront, so providing all data in one run is not an option here.
The sync command does not help - I still get the report after using it instead of sleep. Same for partprobe.

What I'm looking for is some way to block the script until all disk operations are finished and next partition can be created.

Offline

Board footer

Powered by FluxBB