4

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?

  • 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
5

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.

  • 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
3

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 '  '
0

Other answers are correct. What if you need LV path from the LV name itself? Let's assume the LV name is unique, ie, there are no other LVs with in any other VG that has that name. Such as "Musik". You could get the path with one step:

lvs --noheadings -o lv_path -S lv_name=Musik

Your output will be a single token (the LV path) that can be used in subsequent LV commands.

If the lv_name is not unique, this will not work without extra care.

  • As it's reversed problem, you may like to share this insight as separate Q&A (formulate question amd answer it by yourself) and link to it from comment under main question here – Grzegorz Wierzowiecki Mar 28 '17 at 8:39
0

Maybe a bit late, but I let you here one script I made:

#!/bin/bash
IFS=$'\n'
export IFS

phydis=$(pvdisplay | egrep "PV Name|VG Name|PV Size" | awk 'NR%3{printf "%s,",$0;next}{print;}' | sed 's/PV Name//g' | sed 's/VG Name//g' | sed 's/PV Size//g' | sed 's/ //g')
if [ -f /etc/redhat-release ];then
    if [[ `cat /etc/redhat-release | awk -F " " '{print$4}' | awk -F "." '{print$1}'` -eq 7 ]];then
        logdis=$(lvdisplay | egrep "LV Path|LV Size" | awk 'NR%2{printf "%s,",$0;next}{print;}' | sed 's/LV Path//g' | sed 's/LV Size//g' | sed 's/ //g' | sort)
    else
        logdis=$(lvdisplay | egrep "LV Name|LV Size" | awk 'NR%2{printf "%s,",$0;next}{print;}' | sed 's/LV Name//g' | sed 's/LV Size//g' | sed 's/ //g' | sort)
    fi
else
        logdis=$(lvdisplay | egrep "LV Name|LV Size" | awk 'NR%2{printf "%s,",$0;next}{print;}' | sed 's/LV Name//g' | sed 's/LV Size//g' | sed 's/ //g' | sort)
        flag="norh"
fi
phyparttrans=""

echo "|--------------------------|----------------------|----------------------|----------------------|--------------------------------|----------------------|-----------------|-----------------|--------------------------------|"
echo "| PV Name                  | PV Size              | VG Name              | VG Size              | LV Name                        | LV Size              | Used            | Avail           | Mounted on                     |"
        for phy in ${phydis};do
                phypart1=$(echo "${phy}" | awk -F "," '{print$1}')
                phypart2=$(echo "${phy}" | awk -F "," '{print$2}')
                phypart3=$(echo "${phy}" | awk -F "," '{print$3}' | cut -f1 -d"/")
                if [ "$phypart2" != "$phyparttrans" ];then
                phyparttrans="$phypart2"
                counter=1
                    for log in ${logdis};do
                        volsize=$(vgdisplay | egrep "VG Name|VG Size" | awk 'NR%2{printf "%s,",$0;next}{print;}' | sed 's/VG Name//g' | sed 's/VG Size//g' | sed 's/ //g' | grep "$phypart2" | awk -F "," '{print$2}')
                        logpart1=$(echo "${log}" | awk -F "," '{print$1}')
                        logpart2=$(echo "${log}" | awk -F "," '{print$2}')
                        logpartmatch=$(echo "${logpart1}" | awk -F "/" '{print$3}')
                        if [ $counter -eq 1 ];then
                                if [ "${phypart2}" == "${logpartmatch}" ];then
                                        mountpoint1=$(echo "$logpart1" | awk -F "/" '{print$3}')
                                        mountpoint2=$(echo "$logpart1" | awk -F "/" '{print$4}')
                                        df_output=$(df -h /dev/mapper/${mountpoint1}-${mountpoint2} | tail -1)
                                        Used=$(echo "$df_output" | awk -F " " '{print$2}')
                                        Avail=$(echo "$df_output" | awk -F " " '{print$3}')
                                        Mounted=$(echo "$df_output" | awk -F " " '{print$NF}')
                                        echo "|--------------------------|----------------------|----------------------|----------------------|--------------------------------|----------------------|-----------------|-----------------|--------------------------------|"
                                        printf '%1s %-24s %1s %-20s %1s %-20s %1s %-20s %1s %-30s %1s %-20s %1s %-15s %1s %-15s %1s %-30s %1s\n' "|" "$phypart1" "|" "$phypart3" "|" "$phypart2" "|" "$volsize" "|" "$logpart1" "|" "$logpart2" "|" "$Used" "|" "$Avail" "|" "$Mounted" "|"
                                        counter=$[$counter +1]
                                elif [[ "${flag}" == "norh" ]];then
                                        df_output=$(df -h /dev/${phypart2}/${logpart1} | tail -1)
                                        Used=$(echo "$df_output" | awk -F " " '{print$2}')
                                        Avail=$(echo "$df_output" | awk -F " " '{print$3}')
                                        Mounted=$(echo "$df_output" | awk -F " " '{print$NF}')
                                        echo "|--------------------------|----------------------|----------------------|----------------------|--------------------------------|----------------------|-----------------|-----------------|--------------------------------|"
                                        printf '%1s %-24s %1s %-20s %1s %-20s %1s %-20s %1s %-30s %1s %-20s %1s %-15s %1s %-15s %1s %-30s %1s\n' "|" "$phypart1" "|" "$phypart3" "|" "$phypart2" "|" "$volsize" "|" "$logpart1" "|" "$logpart2" "|" "$Used" "|" "$Avail" "|" "$Mounted" "|"
                                        counter=$[$counter +1]
                                fi
                        else
                                if [ "${phypart2}" == "${logpartmatch}" ];then
                                        mountpoint1=$(echo "$logpart1" | awk -F "/" '{print$3}')
                                        mountpoint2=$(echo "$logpart1" | awk -F "/" '{print$4}')
                                        df_output=$(df -h /dev/mapper/${mountpoint1}-${mountpoint2} | tail -1)
                                        Used=$(echo "$df_output" | awk -F " " '{print$2}')
                                        Avail=$(echo "$df_output" | awk -F " " '{print$3}')
                                        Mounted=$(echo "$df_output" | awk -F " " '{print$NF}')
                                        printf '%1s %-24s %1s %-20s %1s %-20s %1s %-20s %1s %-30s %1s %-20s %1s %-15s %1s %-15s %1s %-30s %1s\n' "|" " " "|" " " "|" " " "|" " " "|" "$logpart1" "|" "$logpart2" "|" "$Used" "|" "$Avail" "|" "$Mounted" "|"
                                        counter=$[$counter +1]
                                elif [[ "${flag}" == "norh" ]];then
                                        df_output=$(df -h /dev/${phypart2}/${logpart1} | tail -1)
                                        Used=$(echo "$df_output" | awk -F " " '{print$2}')
                                        Avail=$(echo "$df_output" | awk -F " " '{print$3}')
                                        Mounted=$(echo "$df_output" | awk -F " " '{print$NF}')
                                        printf '%1s %-24s %1s %-20s %1s %-20s %1s %-20s %1s %-30s %1s %-20s %1s %-15s %1s %-15s %1s %-30s %1s\n' "|" " " "|" " " "|" " " "|" " " "|" "$logpart1" "|" "$logpart2" "|" "$Used" "|" "$Avail" "|" "$Mounted" "|"
                                        counter=$[$counter +1]

                                fi
                        fi
                    done
                else
                        printf '%1s %-24s %1s %-20s %1s %-20s %1s %-20s %1s %-30s %1s %-20s %1s %-15s %1s %-15s %1s %-30s %1s\n' "|" "$phypart1" "|" "$phypart3" "|" " " "|" " " "|" " " "|" " " "|" " " "|" " " "|" " " "|"
                fi
        done
echo "|--------------------------|----------------------|----------------------|----------------------|--------------------------------|----------------------|-----------------|-----------------|--------------------------------|"

Update

Here is the script exit example:

[root@server01 /root/scripts]# ./lvm_display.sh
|--------------------------|----------------------|----------------------|----------------------|--------------------------------|----------------------|-----------------|-----------------|--------------------------------|
| PV Name                  | PV Size              | VG Name              | VG Size              | LV Name                        | LV Size              | Used            | Avail           | Mounted on                     |
|--------------------------|----------------------|----------------------|----------------------|--------------------------------|----------------------|-----------------|-----------------|--------------------------------|
| /dev/cciss/c0d2          | 1.64TB               | VolGroup01           | 1.64TB               | /dev/VolGroup01/backup_files   | 50.00GB              | 132M            | 50G             | /data/backup_files             |
|                          |                      |                      |                      | /dev/VolGroup01/data           | 1000.00GB            | 591G            | 410G            | /data                          |
|                          |                      |                      |                      | /dev/VolGroup01/ftp            | 100.00GB             | 74G             | 27G             | /data/ftp                      |
|                          |                      |                      |                      | /dev/VolGroup01/dir1           | 50.00GB              | 15G             | 36G             | /prod/app/dir1                 |
|                          |                      |                      |                      | /dev/VolGroup01/nfs_test       | 12.00GB              | 127G            | 224K            | /dev                           |
|                          |                      |                      |                      | /dev/VolGroup01/testing        | 200.00GB             | 39G             | 162G            | /data/testing                  |
|                          |                      |                      |                      | /dev/VolGroup01/shared         | 125.00GB             | 15G             | 111G            | /data/shared                   |
|--------------------------|----------------------|----------------------|----------------------|--------------------------------|----------------------|-----------------|-----------------|--------------------------------|
| /dev/cciss/c0d0p2        | 558.78GB             | VolGroup00           | 558.75GB             | /dev/VolGroup00/LogVol00       | 10.00GB              | 7.0G            | 2.3G            | /                              |
|                          |                      |                      |                      | /dev/VolGroup00/LogVol01       | 32.00GB              | 127G            | 224K            | /dev                           |
|                          |                      |                      |                      | /dev/VolGroup00/LogVol02       | 120.00GB             | 57G             | 54G             | /prod                          |
|                          |                      |                      |                      | /dev/VolGroup00/LogVol03       | 10.00GB              | 160M            | 9.1G            | /tmp                           |
|                          |                      |                      |                      | /dev/VolGroup00/LogVol05       | 336.75GB             | 205G            | 133G            | /data                          |
|--------------------------|----------------------|----------------------|----------------------|--------------------------------|----------------------|-----------------|-----------------|--------------------------------|

Any improvement is welcome!

  • 1
    Seems like a very long script for such a short question.  Also, when an answer contains so much code, we prefer an explanation. P.S. You hardly ever have to pipe grep into awk, and you can use grep -E instead of egrep. – Scott Nov 20 '18 at 20:56
  • You right Scott! I think the best way to explain what is doing the script is to show the exit: I'll update my comment with it. – Marcos Arroyo Nov 21 '18 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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