~bohwaz/blog/

Avec de vrais morceaux de 2.0 !

Replace a dying hard drive when using LVM

I'm a bad sysadmin. Not because I'm bad at managing servers (I am too, but that's not the point), but because I don't like and don't use "sysadmin" stuff. One example of this is RAID. In my experience RAID has always been bad at what it's supposed to do. I've had many bad experiences with hardware RAID cards dying and leaving you with a bunch of hard drives containing data that you can't recover because the RAID card has a data format that is actually different from the other cards (I didn't even know that was possible). That's the worst case: losing the data it's supposed to protect. But more commonly it will just fail and you'll have to spend countless hours rebuilding the RAID. Software RAID might have been a good idea, if kernel updates wouldn't be able to break things, like last time when after a security upgrade I hit a nasty bug. I was unable to boot on the RAID partition and had to move all data to a new server to extract the root partition from the RAID. Every time it's like this.

Maybe I'm just unlucky and RAID is the best solution everyone should stick to. But well, I just don't want to spend time with this.

But one awesome software that I used over the years without hitting any bug or losing any data ever is LVM. And one awesome example of this is actually now: the server hard drive is dying, sending a lot of SMART errors. So I have to change it. For this I just plug the new drive using a USB interface and sending a few commands in console. First copy the partition table (if the drives are of the same size):

# sfdisk -d /dev/sda > /tmp/table.txt
# sfdisk /dev/sdb < /tmp/table.txt

Then add the new drive to the volume group, and move data into it:

# pvcreate /dev/sdb
# vgextend vg01 /dev/sdb
# pvmove /dev/sda /dev/sdb

LVM now moves all the data from one drive to the other. At the end just remove the old drive:

# vgreduce vg01 /dev/sda

All of this without unmounting any partition, and while applications and users are still writing to the disk. Then tonight I'll just have to shutdown the server to replace the old drive with the new one, with a downtime of only 2 minutes.

How awesome is that? Thanks LVM!

Write a comment
(optional)
(optional)
(mandatory)
       _ _            
__   _(_) |_ _ __ ___ 
\ \ / / | __| '__/ _ \
 \ V /| | |_| | |  __/
  \_/ |_|\__|_|  \___|
                      
(mandatory)

URLs will create links automatically.
Allowed HTML tags: <blockquote> <cite> <pre> <code> <var> <strong> <em> <del> <ins> <kbd> <samp> <abbr>