Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I was looking for a way to refresh the scsi bus (or any other bus, for that matter) that would allow my kernel ( 2.6.18-194-el5 on CentOS 5.5) to know about the partitions on a drive, and I couldn't find a way. partprobe did it instantly. How?

Since it's important, here's what I was doing:

I wanted to get some practice in partition-specific things like resizing, backing up the MBR and restoring it, and so on. I had created a 20GB partition on /dev/sdb, backed up the MBR:

dd if=/dev/sdb of=sdb.mbr bs=512 count=1

then I went into fdisk, deleted the partition, wrote it, and exited.

ls /dev/sdb*

showed that there was no partition, and

fdisk -l /dev/sdb'

matched, so I figured I was good.

I then reversed the dd:

dd if=sdb.mbr of=/dev/sdb bs=512 count=1

Of course, I didn't do anything at that point, so

ls /dev/sdb*

didn't list any partitions, but

fdisk -l

showed the partition, presumably because it reads the first 512 bytes on the disk and doesn't rely on the kernel. I knew that I'd have to refresh the bus, so I went into into /sys/class/scsi_host/host1, and did

echo "- - -" > scan

and doing

ls /dev/sdb

didn't show anything new, so then I went to /sys/bus/scsi/devices and for each of the listed devices, I did

echo 1 > rescan

and that didn't work.

I then did more research on the problem, and came across 'partprobe', which comes with parted. I ran it, and it worked instantly.

If I don't get a likely answer here, I'm probably going to just go get the source and look it up, but I figure there are wizards here beyond me, so I thought I'd appeal to you all.

share|improve this question
1  
+1 Awesomely detailed question. –  pjc50 Feb 15 '11 at 16:22
add comment

1 Answer

up vote 9 down vote accepted

Partprobe calls the BLKRRPART ioctl, which is documented in, err, include/linux/fs.h, and beyond that the kernel source (the meat is in rescan_partitions()):

#define BLKRRPART  _IO(0x12,95) /* re-read partition table */

The easiest way to find this out is to run strace -e raw=ioctl -e open,ioctl partprobe /dev/sdb.

I think what you tried with /sys/*/*scan tells the kernel to check if there's been a change of drive. That doesn't help if the drive hasn't changed (or has been hotswapped in a way that the kernel doesn't detect?) but the partition structure on it has changed.

share|improve this answer
    
Awesome answer. Thanks Gilles –  Matt Simmons Nov 14 '10 at 22:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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