Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How to properly unplug (remove) and later plug (insert) removable (hot-plug, usb etc) storage device with LVM2 on it ?

I've found this note

Remove (unplug, eject):

lvchange -an /dev/your_volume_group_name
vgexport -a

Insert (plug):

vgimport -a
lvchange -ay /dev/your_volume_group_name

but I'm unsure if using "all" option -a isn't and overkill ?

Another thing, I wonder if there might be in some circumstances need of using vgscan pvscan in such scenario ? I'm thinking about automatized solution.

share|improve this question
    
You mean auto configure of LVM device when the PV is available ? –  warl0ck Apr 4 '12 at 7:22

2 Answers 2

You don't have to export the VG, that's used to migrate a VG from one system to another.

Simply vgchange -an vgname to deactivate all logical volumes on the volume group you wish to unplug.

Later, after plugging the device back in, vgchange -ay vgname will reactivate all logical volumes in your vgname VG and then you can mount LVs and use.

Device recognition is dependent on hardware and kernel modules, etc. but this should work with most recent kit.

share|improve this answer
    
"You don't have to export the VG, that's used to migrate a VG from one system to another" -> Don't you think that failure of System A, and reverting data from backup on VG on System B is like exporting from A and importing to B ? That's why I have a feeling that export/import is needed when, it's about using LVM2 for backup solution. –  Grzegorz Wierzowiecki Apr 4 '12 at 11:32
2  
I've only used export/import in the context where two machines shared a device ala differential scsi. A VG could only exist in one, so for failover/HA we'd export and import. To simply powerdown a VG device collection while not in use, VG export is not necessary, though it doesn't hurt either –  bdowning Apr 4 '12 at 15:43
    
vgchange or lvchange? I use lvchange. –  Martin Schröder Apr 8 '12 at 23:55
    
vgchange -an will deactivate all logical volumes in VG –  bdowning Apr 9 '12 at 2:00

I use a script similar like this one:

  • unmount all filesystems in the volume group vg_name
  • if all filesystems in vg_name are unmounted, set the VG to unavailable
  • don't forget to sync memory buffers to disk ...
  • sispmctl is used to power off my external disks
#!/bin/bash    
echo "Unmounting filesystem"
mount | grep vg_name && {
    mount | grep vg_name | cut -d' ' -f3 | xargs umount -v
}

mount | grep vg_name || {
    echo "Set VG unavailable"
    /sbin/vgchange -an /dev/vg_name && {
        echo "Sync disks"
        sync; sync && {
            echo "Power off disks"
            /usr/local/bin/sispmctl -D 01:01:4b:54:e5 -f 1
        }
    }
}

When the disk is attached, the kernel will find it automatically when proper modules are available on the system (this is usually the case with main stream distributions)

export/import is only required when the PV's/VG's/LV's are changed and the VG is attached to different systems. vgexport would go after vgchange in that case.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.