fish: Allow '-' prefix on command line to override exit on error (RHBZ#578407).
[libguestfs.git] / regressions / test-lvm-mapping.pl
1 #!/usr/bin/perl
2 # Copyright (C) 2010 Red Hat Inc.
3 #
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.
8 #
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.
13 #
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.
17
18 # Test the discovery of relationships between LVM PVs, VGs and LVs.
19
20 use strict;
21 use warnings;
22
23 use Sys::Guestfs;
24
25 my $testimg = "test.img";
26
27 unlink $testimg;
28 open FILE, ">$testimg" or die "$testimg: $!";
29 truncate FILE, 256*1024*1024 or die "$testimg: truncate: $!";
30 close FILE or die "$testimg: $!";
31
32 my $g = Sys::Guestfs->new ();
33
34 #$g->set_verbose (1);
35 #$g->set_trace (1);
36
37 $g->add_drive ($testimg);
38 $g->launch ();
39
40 # Create an arrangement of PVs, VGs and LVs.
41 $g->sfdiskM ("/dev/sda", [",127", "128,"]);
42
43 $g->pvcreate ("/dev/sda1");
44 $g->pvcreate ("/dev/sda2");
45 $g->vgcreate ("VG", ["/dev/sda1", "/dev/sda2"]);
46
47 $g->lvcreate ("LV1", "VG", 32);
48 $g->lvcreate ("LV2", "VG", 32);
49 $g->lvcreate ("LV3", "VG", 32);
50
51 # Now let's get the arrangement.
52 my @pvs = $g->pvs ();
53 my @lvs = $g->lvs ();
54
55 my %pvuuids;
56 foreach my $pv (@pvs) {
57     my $uuid = $g->pvuuid ($pv);
58     $pvuuids{$uuid} = $pv;
59 }
60 my %lvuuids;
61 foreach my $lv (@lvs) {
62     my $uuid = $g->lvuuid ($lv);
63     $lvuuids{$uuid} = $lv;
64 }
65
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");
70
71 my @pvs_in_VG;
72 foreach my $uuid (@pvuuids_in_VG) {
73     push @pvs_in_VG, $pvuuids{$uuid};
74 }
75 @pvs_in_VG = sort @pvs_in_VG;
76
77 my @lvs_in_VG;
78 foreach my $uuid (@lvuuids_in_VG) {
79     push @lvs_in_VG, $lvuuids{$uuid};
80 }
81 @lvs_in_VG = sort @lvs_in_VG;
82
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"
87 }
88
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"
95 }
96
97 undef $g;
98
99 unlink $testimg or die "$testimg: unlink: $!";