Friday, March 14, 2008

How to eject a usb harddrive that contains LVM?

Documentation seems to be lacking a bit on the common task on how to eject a normal USB harddrive with LVM on it, so here it goes, without guarantee for correctness:
  1. umount all the directories on the drive as usual
  2. Get rid of the volume groups on the drive via: vgchange -a n /dev/storage_usbhd
  3. Spin the drive down via sdparm -C stop /dev/sde
  4. Pull the USB-Plug
  5. Done
Some more notes:
  • neither eject nor eject -s work, they don't spin the drive down or remove LVM VG
  • when you don't do a vgchange -a n the Volume Group will stay active and you won't be able to plug the drive back in, because the device names will change and your Kernel will get pretty confused, so don't do it
  • you of course have to adjust the device and volume group names to fit your setting
  • spinning the drive down might not be strictly needed, but gives a warm and fluffy feeling
  • you can reactivate the drive via vgchange -a y or by pluging it out and back in, it should spin up automatically
  • if your drive goes into auto sleep a while true; do touch /file/on/usbhd will work around the problem, you can't adjust auto-sleep with hdparm
  • I have no idea if it is save to pull the powercord without first pulling the usb cord

2 comments:

nickname said...

Thanks for posting these instructions. Little correction: vgchange -a n /dev/sdX didn't work for me, I had to give the volume group name instead of device name (can be found by running 'vgs' command).

jartsa said...

There is a new tool specificly for this task, lvmeject. It tries to handle also encrypted volumes and unmanaged partitions. Please take a look at the project page at https://launchpad.net/lvmeject

Ubuntu package can be found at https://launchpad.net/~yartsa/+archive/lvmeject