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.

Let's assume we have lv path:

lvpath=/dev/mapper/vg2-Music

Than I split lvdisplay -c with cut:

vgname="$(lvdisplay -c "$lvpath" | cut -d ':' -f 2 )"
lvname="$(basename "$(lvdisplay -c "$lvpath" | cut -d ':' -f 1 )" )"

result:

$ echo VG: $vgname LV: $lvname
VG: vg2 LV: Music

question:

Is there more convenient way of doing this ?

Can I rely on lvdisplay -c that will always provide path in form /dev/VGname/LVname ? (In case of /dev/mapper/VGname-LVname basename sould be exchanged with sed)

Is there tool from LVM2 package to obtain LVname from path?

share|improve this question
    
There are many tools and switches for obtaining LVM info. Could you give an example of a use case for your need? Just from the path itself, you can't be certain that a lv doesn't have an embedded hyphen in it's name. –  bdowning Mar 24 '12 at 16:06
add comment

2 Answers 2

up vote 2 down vote accepted

Use lvs with the -o option to customize the displayed fields.

set $(lvs --noheadings -o lv_name,vg_name)
lv_name=$1 vg_name=$2

Note that you can't just write lv_name=$(lvs --noheadings -o lv_name) because lvs puts extra whitespace around the value. The snippet is safe because volume names aren't allowed to contain shell special characters.

share|improve this answer
    
Is lvname="$(echo `lvs --noheadings -o lv_name "$lvpath"`)" safe in your opinion ? –  Grzegorz Wierzowiecki Mar 26 '12 at 14:38
    
@GrzegorzWierzowiecki It's safe but looks awfully complicated. –  Gilles Mar 26 '12 at 16:49
    
I know, I just don't want to overwrite parameters $1 , $2 . On the other hand I can encapsulate your solution into function to prevent set of changing global parameters. Thanks :) –  Grzegorz Wierzowiecki Mar 26 '12 at 19:46
add comment

Have you tried lvs (logical volume scan)? I added how the ouput looks after running lvs and some of the man page on lvs.

LV VG Attr LSize Origin Snap% Move Log Copy% Convert

lv_home vg_testing -wi-ao 113.75g

MAN info:

LVS(8) LVS(8)

NAME lvs - report information about logical volumes

SYNOPSIS lvs [-a|--all] [--aligned] [-d|--debug] [-h|-?|--help] [--ignorelock- ingfailure] [--nameprefixes] [--noheadings] [--nosuffix] [-o|--options [+]Field[,Field]] [-O|--sort [+|-]Key1[,[+|-]Key2[,...]]] [-P|--par- tial] [--rows] [--segments] [--separator Separator] [--unbuffered] [--units hHbBsSkKmMgGtTpPeE] [--unquoted] [-v|--verbose] [--version] [VolumeGroupName [VolumeGroupName...]]

share|improve this answer
    
I looked at your script are you really trying to get the path or just the name of the logical volume? –  Iman Mar 24 '12 at 14:51
    
I have no idea what you're trying to say in your answer, beyond “use lvs”, which is true but incomplete, and not useful the way you show it. –  Gilles Mar 26 '12 at 0:29
    
lvs is one of most basic LVM commands –  Grzegorz Wierzowiecki Mar 26 '12 at 14:32
add comment

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.