How to add/update partition table without reboot

Jephe Wu - http://linuxtechres.blogspot.com

Objective:  update partition table without Linux OS reboot
Environment:  RHEL 6.X
Tools: partprobe, partx -a , kpartx -a, blockdev --rereadpt /dev/sdX, sfdisk -R /dev/sdX, hdparm -z /dev/sdX

Concepts: create/update partition for a block device while Linux is running

Explanation: 

partprobe

In RHEL 6, it will only trigger the OS to update the partitions on a disk that none of its partitions are in use (e.g. mounted). If any partition on a disk is in use, partprobe will not trigger the OS to update partitions in the system because it is considered unsafe in some situations.

Partx
# partx -v -a /dev/sdb
# partx -l /dev/sdb

kpartx

kpartx -a /file/path/system.img

mount /dev/loop0p1 /mnt/somewhere
Some raw images contains multiple partitions (e.g. full HD image). In this case, it's necessary to specify a starting offset for each partition

Some raw images contains multiple partitions (e.g. full HD image). In this case, it's necessary to specify a starting offset for each partition. offset is the bytes which is the starting sectors*512.


[root@ovm01 xen]# file system.img 
system.img: x86 boot sector, GRand Unified Bootloader (0.94); partition 1: ID=0x83, active, starthead 0, startsector 2048, 208896 sectors; partition 2: ID=0x83, starthead 0, startsector 210944, 20760576 sectors; partition 3: ID=0x82, starthead 63, startsector 20971520, 4194304 sectors, code offset 0x48

# mount -t vfat -o loop,offset=$((2048*512)),ro,noexec img.dd /mnt/tmp_1
# mount -t vfat -o loop,offset=$((210944*512),ro,noexec img.dd /mnt/tmp_2




# kpartx -v -a rawimage.dd
creates these mappings

/dev/mapper/loop0p1
/dev/mapper/loop0p2
/dev/mapper/loop0p5

The partitions can be mounted with these commands:
# mount /dev/mapper/loop0p1 /media/suspectHD_01/ -o ro
# mount /dev/mapper/loop0p5 /media/suspectHD_02/ -o ro

References commands

  • partx -va /dev/sdX
  • kpartx -va /dev/sdX
  • hdparm -z /dev/sdX
  • sfdisk -R /dev/sdX