![LVM2:从路径获取lv和vg名称(卷组名称和逻辑卷名称)](https://linux22.com/image/21850/LVM2%EF%BC%9A%E4%BB%8E%E8%B7%AF%E5%BE%84%E8%8E%B7%E5%8F%96lv%E5%92%8Cvg%E5%90%8D%E7%A7%B0%EF%BC%88%E5%8D%B7%E7%BB%84%E5%90%8D%E7%A7%B0%E5%92%8C%E9%80%BB%E8%BE%91%E5%8D%B7%E5%90%8D%E7%A7%B0%EF%BC%89.png)
假设我们有 lv 路径:
lvpath=/dev/mapper/vg2-Music
比我lvdisplay -c
用剪切分割:
vgname="$(lvdisplay -c "$lvpath" | cut -d ':' -f 2 )"
lvname="$(basename "$(lvdisplay -c "$lvpath" | cut -d ':' -f 1 )" )"
结果:
$ echo VG: $vgname LV: $lvname
VG: vg2 LV: Music
问题:
有更方便的方法吗?
我是否可以依赖lvdisplay -c
始终以表单形式提供路径/dev/VGname/LVname
?(如果使用/dev/mapper/VGname-LVname
sed 交换 basename)
LVM2 软件包中是否有工具可以从路径获取 LVname?
答案1
lvs
与选项一起使用-o
来自定义显示的字段。
set $(lvs --noheadings -o lv_name,vg_name)
lv_name=$1 vg_name=$2
请注意,您不能直接写入,lv_name=$(lvs --noheadings -o lv_name)
因为lvs
在值周围放置了额外的空格。该代码片段是安全的,因为卷名称不允许包含 shell 特殊字符。
答案2
这些非常适合在 bash 脚本中使用 LVM。
VG名称:
这对于获取 VG 名称(卷组名称)作为没有空格的单个值非常有效:
sudo vgs --noheadings -o vg_name | tr -d ' '
LV 名称:
对于 LV 名称(逻辑卷名称),“通常”不止一个,并且您通常需要使用语法来引用它/dev/<vg_name>/<lv_name>
。不知道你想要什么输出格式。下面将为您提供 LV 名称的列表,其中每个值均以换行符“\n”分隔:
sudo lvscan | cut -d "'" -f 2
或者,如果您只想要 LV 名称<lv_name>
:
sudo lvs --noheadings -o lv_name | tr -d ' '
PV 名称:
你没有问,但当我这样做时,这就是你获取 PV 名称(物理卷名称)的方式:
sudo pvs --noheadings -o pv_name | tr -d ' '
答案3
其他答案都是正确的。如果你需要LV怎么办小路来自LV名字本身?我们假设 LV 名称是唯一的,即任何其他 VG 中都不存在具有该名称的其他 LV。比如“音乐”。您只需一步即可获得路径:
lvs --noheadings -o lv_path -S lv_name=Musik
您的输出将是一个可在后续 LV 命令中使用的令牌(LV 路径)。
如果lv_name
不是唯一的,那么如果不额外小心,这将无法工作。
答案4
也许有点晚了,但我让你看一下我制作的一个脚本:
#!/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 "|--------------------------|----------------------|----------------------|----------------------|--------------------------------|----------------------|-----------------|-----------------|--------------------------------|"
更新
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 |
|--------------------------|----------------------|----------------------|----------------------|--------------------------------|----------------------|-----------------|-----------------|--------------------------------|
欢迎任何改进!