0

The situation, where I have unknown number of volume groups and their names with unknown number of disks assigned to them. Example :

pvs -o pv_name,vg_name
  PV         VG
  /dev/vdd   appvg01
  /dev/vdb   appvg01
  /dev/vdf3  vg00
  /dev/vdh   testvg

 vgs --noheadings | awk '{print $1}'| while read line ; do echo $line;vgs --noheadings -o pv_name $line; done
appvg01
  /dev/vdd
  /dev/vdb
testvg
  /dev/vdh
vg00
  /dev/vdf3

At the final stage I'd like to mirror each volume with new disk that I'll add manually :

for i in `/sbin/lvs| /bin/awk '{if ($2 ~ /appvg01/) print $1}'`; do
  /sbin/lvconvert -b -m0 appvg01/$i /dev/vde
done

but, I don't know what volume name should I correlate with, as it might be any other name. what is the best approach for this structure.

Thanks

2
  • 1
    FYI -- see mywiki.wooledge.org/DontReadLinesWithFor Commented Jul 22, 2014 at 18:30
  • 1
    ...beyond that, though, I really don't understand the question. What do you mean by "correlate"? Why do you need to know a name? Etc. Commented Jul 22, 2014 at 18:31

1 Answer 1

2

The correct data structure to store this kind of information in bash is associative arrays:

declare -A pvs
{
  read # skip the header
  while read -r pv vg; do
     pvs[$pv]=$vg
  done
} < <(pvs -o pv_name,vg_name)

Thereafter, you can iterate and do lookups:

for pv in "${!pvs[@]}"; do
  vg="${pvs[$pv]}"
  echo "vg $vg is backed by pv $pv"
done
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Charles, thanks for the quick response. sorry, if I didn't was clear enough. I need to know a name of volume to be able to use this name whenever I mirror it. At the end of the procedure I have to un-mirror mirrored volume and I need to know the name again: for i in /sbin/lvs| /bin/awk '{if ($2 ~ /appvg01/) print $1}'; do /bin/nice -n 19 /usr/bin/ionice -c 3 /sbin/lvconvert -b -m1 --corelog appvg01/$i $NewDisks; done for i in /sbin/lvs| /bin/awk '{if ($2 ~ /appvg01/) print $1}'; do /sbin/lvconvert -b -m0 appvg01/$i $OldDisks; done
...well, I've showed you how to do an associative array lookup; store what you want in an associative array (in other languages, this might be called a map or hashmap), and thereafter getting back to the name should be a straightforward exercise.
@user1607856, ...if you want a better answer than that, questions where it's easy to tell if an answer is correct are going to get much less handwavey responses. See sscce.org, or stackoverflow.com/help/mcve -- build a reproducer that doesn't require LVM, and it'll be easier to help you here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.