Thursday, May 26, 2011

Hot-removing a SATA drive, and copying partition tables

In a multi-disk setup, one sometimes needs to replace a drive. If one has a hot-swap bay, this is surprisingly easy, but before actually pulling the drive out, one MUST tell Linux that one is about to do so:

# echo 1 > /sys/block/sdX/device/delete

You can then pull the drive out and replace it with a new one. Note, though, that the new drive will generally get a new device name (e.g., /dev/sdZ), until the next reboot.

Now, say that you want the drive to have identical partitions to another drive (say, sdY) in your system, then you simple copy the partition tables:

# sfdisk -d /dev/sdY | sfdisk /dev/sdZ

This can all be done without ever rebooting the system :-)

1 comment:

Unknown said...

Of course, if the drive in question was part of a RAID array (say, /dev/mdA), then one would first issue:

# mdadm /dev/mdA --fail /dev/sdXn
# mdadm /dev/mdA --remove /dev/sdXn

Then, one would replace the drive as noted in the post, after which one would add the partition on the new drive back into the array:

# mdadm /dev/mdA --add /dev/sdZn