99

Sometimes, when resizing or otherwise mucking about with partitions on a disk, cfdisk will say:

Wrote partition table, but re-read table failed. Reboot to update table.

(This also happens with other partitioning tools, so I'm thinking this is a Linux issue rather than a cfdisk issue.) Why is this, and why does it only happens sometimes, and what can I do to avoid it?

Note: Please assume that none of the partitions I am actually editing are opened, mounted or otherwise in use.


Update:

cfdisk uses ioctl(fd, BLKRRPART, NULL) to tell Linux to reread the partition table. Two of the other tools recommended so far (hdparm -z DEVICE, sfdisk -R DEVICE) does exactly the same thing. The partprobe DEVICE command, on the other hand, seems to use a new ioctl called BLKPG, which might be better; I don't know. (It also falls back on BLKRRPART if BLKPG fails.)

BLKPG seems to be a "this partition has changed; here is the new size" operation, and it looked like partprobe called it individually on all the partitions on the device passed, so it should work if the individual partitions are unused. However, I have not had the opportunity to try it.

Teddy
  • 5,424

18 Answers18

98

IMHO the most reliable/best answer is

partprobe /dev/sdX
knweiss
  • 4,205
25

Rereading partition table information doesn't always work, but try

hdparm -z /dev/sda

or

sfdisk -R /dev/sda

If it works the values in /proc/partitions will change.

ko-dos
  • 1,397
23

I (the original questioner) had a situation a few days ago when none of the other answers (including partprobe /dev/sdX, currently the accepted and highest-voted answer) worked. What did work, however, was this:

blockdev --rereadpt /dev/sdX

(I don't know why this worked and the others didn't, but I'm happy it did work, as it saved me a reboot on a busy server.)

Teddy
  • 5,424
16

On Centos7 :

According to https://access.redhat.com/solutions/199573

You should try :

partx -u <partition>

It worked for me.

Edredon
  • 171
9

When a command like blockdev --rereadpt /dev/sdX fails with

blockdev: ioctl error on BLKRRPART: Device or resource busy

this usually means that some (old) partition is indeed still somehow used by the kernel.

Possible causes/fixes:

  1. an sdX partition - say sdX1 - is still mounted - check with mount and umount it
  2. /dev/sdX1 is part of a software raid - check cat /proc/mdstat and possibly stop the relevant arrays, e.g. mdadm --stop /dev/md126
  3. /dev/sdX1 is part of an LVM physical volume - check with pvdisplay/vgdisplay and possibly deactivate with vgchange
  4. /dev/sdX1 is part of some device mapping - e.g. via cryptsetup - check /dev/mapper and lsblk and possibly remove the mapping (e.g. cryptsetup luksClose)
  5. Race condition with some udev probing - check running processes with ps and possibly kill one

If one tool - say blockdev --rereadpt fails usually similar ones like (partx -uv, kpartx, partprobe, kpartprobe) fail in a similar way until the root cause is eliminated.

maxschlepzig
  • 1,224
8

Note: Please assume that none of the partitions I am actually editing are opened, mounted or otherwise in use.

Given that assumption, the partition table can be successfully rescanned, and the issue won't arise. If you're getting that error, it's because the partition table is currently in use, and hence can't be re-scanned without creating inconsistencies.

Giacomo1968
  • 3,553
  • 29
  • 42
womble
  • 98,245
6

It is not based on partition that you are editing.

Suppose you have only one harddisk (/dev/sda) and two partitions (/dev/sda1, /dev/sda2) and you have mounted only one partition (/dev/sda1). If you delete or change anything about other partition which is not even mounted (/dev/sda2) you will get the error that re-reading of partition table failed and kernel will use old table.

But if you have two harddisks (/dev/sda, /dev/sdb) and none of the partitions of (/dev/sdb) are in use. Then you can add / delete / resize /edit partitions of /dev/sdb and they will be re-read without any problem. But even if one partition of /dev/sdb was mounted during change. Then kernel will keep using old table.

Giacomo1968
  • 3,553
  • 29
  • 42
5

With all mount points unmounted, running Yocto 2.4:

partprobe /dev/sda 

Still failed to re-load the partition table after partitions had been deleted on the device. Also tried -- and failed were:

udevadm trigger --subsystem-match=block; udevadm settle
hdparm -z /dev/sda
blockdev --rereadpt /dev/sda

All reported similar "BLKRRPART failed : device or resource busy..." errors instructing me to reboot. Is this failure of previously working methods possibly due to the fact that udev is now under systemd control? Thinking along those lines I tried:

systemctl restart systemd-udevd.service

And suddenly my disk is available again, without a reboot!

4

You can also try:

echo 1 > /sys/block/sdX/device/rescan

(But might not work, see the comment below)

bogdano
  • 165
4

i'm on centos 6.5 x64 ; kernel 2.6.32 . and i'm testing the fdisk trick to resize.

/dev/sda1 /boot
/dev/sda2 /

All the following commands did not make kernel reread partition :

  • partprobe /dev/sda (warning : kernel failed to reread ....)
  • hdparm -z /dev/sda (BLKRRPART failed : device or resource busy)
  • blockdev -rereadpt /dev/sda (BLKRRPART failed : device or resource busy)
  • sfdisk -R /dev/sda (BLKRRPART failed : device or resource busy)

i still need a reboot to make it work

Max
  • 492
3

in Alpine-Linux netboot mode , new partitions were shown in dmesg, main disks /dev/sda were present but no /dev/sdaX partition device files , tried all tricks above , finally


mknod -m 660 ${chroot_dir}/dev/sda1 b 8 1
mknod -m 660 ${chroot_dir}/dev/sda2 b 8 2

worked

https://wiki.alpinelinux.org/wiki/Alpine_Linux_in_a_chroot

2

I was having a very similar issue in Alpine Linux (not chrooted) where partprobe did nothing. It was using mdev to manage the /dev and /sys mounts, and so mdev -s to trigger a re-scan after imaging the mmcblk0 device worked.

Paul Ridgway
  • 139
  • 1
  • 8
1

For me neither partprobe or blockdev solution worked. Although, this one works:

udevadm settle --exit-if-exists=/dev/sdb1
Sibi
  • 111
0
fdisk /dev/sdX

and exit by "w"

0

In case if you want to ask kernel to "reread partition table", or update the size of specific partition to its actual size on Oracle Linux 6, these instructions could be helpful.

Let's say you have a machine with Oracle Linux 6, relatively new kernel 4.1.12-124.28.3.el6uek.x86_64, you just changed the size of your virtual disk, and now you're in situation that your /dev/sdc is 2.1 TiB but its own only MOUNTED partition sdc1 is 2 TiB, and you want to resize it to 2.1 TiB without unmounting it or rebooting. lsblk will show you something like that:

sdc                              8:32   0  2.1T  0 disk
`-sdc1                           8:33   0    2T  0 part

What I've found is that there is no ready-to-use application that you can install from package repository to help you with the issue: all programs in repository are too old and in the best case they will rewrite your partition table to disk (it's still required), but will use BLKRRPART to notify kernel about changes that won't work with mounted/"busy" partitions.

What you can do instead is to build your own simple C program that will run newer BLKPG ioctl to do the trick. Fortunately, you don't need to write it from scratch: just download this C file, modify it, build and run:

wget https://raw.githubusercontent.com/gportay/blkpg-part/master/blkpg-part.c
sed -i 's/BLKPG_RESIZE_PARTITION/3/' blkpg-part.c
make blkpg-part

Why you need to replace BLKPG_RESIZE_PARTITION with hardcoded value 3? It's because Oracle Linux 6 ships with the old conservative UAPI that is not synchronized with actual Linux kernel API, and therefore BLKPG_RESIZE_PARTITION constant from include/uapi/linux/blkkg.h is not available, even though the feature is present in kernel (you can check it by grepping the Linux kernel source code with BLKPG_RESIZE_PARTITION).

Okay, so you have your blkpg-part built, now you can check that it's working. Let's start with running it "dry-run": just ask it to resize the partition /dev/sdc1 with its own current size (reminder: the size of partition from kernel's point of view is still the "old" one, therefore /sys/class/block/sdc1/size will return the "old" size, not the one that is written in the new partition table):

./blkpg-part resize /dev/sdc 1 $(( $(cat /sys/class/block/sdc1/start) * 512 )) $(( $(cat /sys/class/block/sdc1/size) * 512 ))

If it runs without errors, it means that this userspace application succeeded with running BLKPG_RESIZE_PARTITION and interacting with kernel; so, we are close to actually ask kernel to change the size of the partition.

Now you need to run blkpg-part with the new partition size in bytes that is written in the new partition table. parted /dev/sdc unit B print can be used to identify the actual size of the partition, you'll get something like that:

Number  Start   End             Size            File system  Name     Flags
 1      20480B  2254857813503B  2254857793024B               primary  lvm

So, the new size of partition is 2254857793024, and you can finally run this command below:

./blkpkg-part3 resize /dev/sdc 1 $(( $(cat /sys/class/block/sdc1/start) * 512 )) 2254857793024

Immediately after that kernel will update the state of the partition, and in lsblk you'll see:

sdc                              8:32   0  2.1T  0 disk
`-sdc1                           8:33   0  2.1T  0 part

Useful links:

0

kpartx -a <partition> can be run two times on newly created partition .... instead of rebooting the system.

HBruijn
  • 84,206
  • 24
  • 145
  • 224
0

Remember to check udev service is running. This is especially useful when partprobe, hdparm, blockdev, and various other commands do not seem to make any difference what device files are available in /dev/ directory.

kerolasa
  • 121
-5

If you read the manpage for 'man oracleasm-scandisks' you will note the text below. oracleasm is using /proc/partitions as the source of all scanning it performs. You must get your raw devices listed in /proc/partitions before you can do a scandisk. The Scanorder and Scanexclude parameters you place in /etc/sysconfig/oracleasm relate to the names found in /proc/partitions (!!!!).

---------- man oracleasm-scandisks ------ ...

HOW SCANNING HAPPENS The scan proceeds in four basic stages.

   First, the list of disks to scan is created. If disks were specified on the command line, this is the list.
   If not, /proc/partitions is read, and each block device is added, subject to the -o and -x options.

   Second, the partition tables of each disk in the scan are reloaded unless the -s option was specified. Any
   disks that no longer exist are dropped.

   Third, the list of disks is recreated based on the new partition tables.

   Finally, each disk in the list is checked to see if it is marked for ASM use. Disks that are marked are
   instantiated.