Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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. – bsd Mar 24 '12 at 16:06
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

These are great for working with LVM in bash scripts.

VG Name:
This works nicely for getting the VG name (Volume Group name) as a single value with no whitespaces:

sudo vgs --noheadings -o vg_name | tr -d '  '


LV Name(s):
For LV names (Logical Volume names) there are "usually" more than just one AND you typically need to reference it with the syntax /dev/<vg_name>/<lv_name>. Don't know what output format you want. The following will give you a list of the LV names with each value separated with a new line "\n":

sudo lvscan | cut -d "'" -f 2

Or if you just want the LV names only <lv_name>:

sudo lvs --noheadings -o lv_name | tr -d '  '


PV Name(s):
You didn't ask, but while I'm at it, this is how you would get PV names (Physical Volume names):

sudo pvs --noheadings -o pv_name | tr -d '  '
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.