0

I am creating a partition with sfdisk under linux. After partition creation I want to create a filesystem.

This fails with mkfs.ext3 not finding the partition:

echo ",,L" | sfdisk $DEVICE
mkfs.ext3 $PARTITION

But this works:

echo ",,L" | sfdisk $DEVICE
sleep 1
mkfs.ext3 $PARTITION

I suspect, either the kernel takes some time to update it's internal data structures, or my script gets scheduled again, before udev had a chance to create the device node.

How do I wait exactly until the device node is created? With every constant time, there may be a system under load where this time is not eneugh.

1

There's always the low-tech solution, if you know in advance the device name:

while [ ! -e /dev/whatever ] ; do 
  sleep .1 # you can sleep for less than a second with coreutils sleep
  # optionally add a counter & timeout
done
1
  • polling is not the nicest concept, but so far nobody came up with something better – johannes May 9 '12 at 10:03
5

Later, but better than never. For completeness.

udevadm settle --timeout=15 --exit-if-exists=$PARTITION
1

Try using inotifywait (from inotify-tools) on the device node. the /dev directory.

6
  • inotifywait faìls when called for non existing file – johannes May 8 '12 at 12:43
  • 1
    @johannes: It will work when called for /dev, though. – user1686 May 8 '12 at 12:52
  • 1
    what if the file gets created before inotywait starts? – johannes May 8 '12 at 13:07
  • Check if file exists with if [[ -e /dev/whatever ]]; then ...; fi before invoking inotifywait. – LawrenceC May 8 '12 at 13:52
  • 2
    what if the file gets created inbetwen the -e and the invokation of inotifywait? – johannes May 8 '12 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.