sfdisk without --no-reread is likely to cause race conditions

Bug #942788 reported by Scott Moser on 2012-02-28
This bug affects 2 people
Affects Status Importance Assigned to Milestone
util-linux (Ubuntu)

Bug Description

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:
  sfdisk --re-read $DEV
  blockdev --rereadpt $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
 PATH=(custom, user)
SourcePackage: util-linux
UpgradeStatus: No upgrade log present (probably fresh install)

Related Bugs:
  * bug 937352: cloud-initramfs-tools root partition may not be grown by growpart

Scott Moser (smoser) wrote :
Steve Langasek (vorlon) on 2012-02-28
Changed in util-linux (Ubuntu):
status: New → Triaged
importance: Undecided → Medium
Scott Moser (smoser) wrote :

Per Steve's request, I'm attaching the log created with:
  udevadm monitor -e >out.log 2>&1 &
  printf "1,,L,*\n" | sudo sfdisk /dev/vdb
  kill %1

Steve Langasek (vorlon) wrote :

So it seems that --no-reread is the only sensible way to invoke sfdisk. Means it probably ought to be the default.

Scott Moser (smoser) on 2012-02-28
description: updated
description: updated
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers