Comment 0 for bug 942788

Revision history for this message
Scott Moser (smoser) wrote :

If you invoke sfdisk without --no-reread, you're almost guaranteed to have race conditions involved.

Run the following as root:
  DEV=/dev/vdb # or some disk you don't have data on
  printf "1,,L,*\n" > sfdisk.in
  sudo udevadm settle;
  for((i=0;i<100;i++)); do sfdisk $DEV <sfdisk.in > out 2>&1 ||
     { echo "FAILED: $i"; cat out; break; } ; echo -n .; udevadm settle; done

You'll see failure at some point.

The problem is that unless you pass '--no-reread', sfdisk checks if the device is busy by issuing a BLKRRPART ioctl on the device. That, in turn, causes a flurry of udev events that open the disk (such as 'blkid'). If those udev events don't finish before sfdisk writes the partition table and calls BLKRRPART again, the second call will fail.

To prove that a single run is destined for race condition, turn udevadm debug on with:
  udevadm control --log-priority=debug
and then just run:
  printf "1,,L,*\n" | sudo sfdisk $DEV

That will show you output in /var/log/syslog that shows udev responding to events.

ProblemType: Bug
DistroRelease: Ubuntu 12.04
Package: util-linux 2.20.1-1ubuntu2
ProcVersionSignature: User Name 3.2.0-17.27-virtual 3.2.6
Uname: Linux 3.2.0-17-virtual x86_64
ApportVersion: 1.93-0ubuntu2
Architecture: amd64
Date: Tue Feb 28 17:36:40 2012
Ec2AMI: ami-00000000
Ec2AMIManifest: FIXME
Ec2AvailabilityZone: nova
Ec2InstanceType: m1.small
Ec2Kernel: unavailable
Ec2Ramdisk: unavailable
ProcEnviron:
 TERM=screen
 PATH=(custom, user)
 LANG=en_US.UTF-8
 SHELL=/bin/bash
SourcePackage: util-linux
UpgradeStatus: No upgrade log present (probably fresh install)