2 # Copyright (C) 2010 Red Hat Inc.
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 # Test the discovery of relationships between LVM PVs, VGs and LVs.
25 my $testimg = "test.img";
28 open FILE, ">$testimg" or die "$testimg: $!";
29 truncate FILE, 256*1024*1024 or die "$testimg: truncate: $!";
30 close FILE or die "$testimg: $!";
32 my $g = Sys::Guestfs->new ();
37 $g->add_drive_opts ($testimg, format => "raw");
40 # Create an arrangement of PVs, VGs and LVs.
41 $g->sfdiskM ("/dev/sda", [",127", "128,"]);
43 $g->pvcreate ("/dev/sda1");
44 $g->pvcreate ("/dev/sda2");
45 $g->vgcreate ("VG", ["/dev/sda1", "/dev/sda2"]);
47 $g->lvcreate ("LV1", "VG", 32);
48 $g->lvcreate ("LV2", "VG", 32);
49 $g->lvcreate ("LV3", "VG", 32);
51 # Now let's get the arrangement.
56 foreach my $pv (@pvs) {
57 my $uuid = $g->pvuuid ($pv);
58 $pvuuids{$uuid} = $pv;
61 foreach my $lv (@lvs) {
62 my $uuid = $g->lvuuid ($lv);
63 $lvuuids{$uuid} = $lv;
66 # In this case there is only one VG, called "VG", but in a real
67 # program you'd want to repeat these steps for each VG that you found.
68 my @pvuuids_in_VG = $g->vgpvuuids ("VG");
69 my @lvuuids_in_VG = $g->vglvuuids ("VG");
72 foreach my $uuid (@pvuuids_in_VG) {
73 push @pvs_in_VG, $pvuuids{$uuid};
75 @pvs_in_VG = sort @pvs_in_VG;
78 foreach my $uuid (@lvuuids_in_VG) {
79 push @lvs_in_VG, $lvuuids{$uuid};
81 @lvs_in_VG = sort @lvs_in_VG;
83 unless (@pvs_in_VG == 2 &&
84 $pvs_in_VG[0] eq "/dev/vda1" && $pvs_in_VG[1] eq "/dev/vda2") {
85 die "unexpected set of PVs for volume group VG: [",
86 join (", ", @pvs_in_VG), "]\n"
89 unless (@lvs_in_VG == 3 &&
90 $lvs_in_VG[0] eq "/dev/VG/LV1" &&
91 $lvs_in_VG[1] eq "/dev/VG/LV2" &&
92 $lvs_in_VG[2] eq "/dev/VG/LV3") {
93 die "unexpected set of LVs for volume group VG: [",
94 join (", ", @lvs_in_VG), "]\n"
99 unlink $testimg or die "$testimg: unlink: $!";