File: checkmount.pl

package info (click to toggle)
libldm 0.2.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 780 kB
  • sloc: ansic: 3,832; xml: 654; makefile: 183; perl: 60; sh: 16
file content (96 lines) | stat: -rwxr-xr-x 2,125 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/perl

# libldm
# Copyright 2012 Red Hat Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

use strict;
use warnings;

use File::Temp qw(tempdir);
use JSON::PP;

my @loops;
my @dm_devices;

my $mount = tempdir(CLEANUP => 1);
my $mounted = 0;

my $builddir = shift @ARGV;
my $dg = shift @ARGV;
my $vol = shift @ARGV;

sub umount
{
    my $dir = shift;

    my $tries = 0;
    do {
        return if (system('umount', $mount) == 0);
        sleep(1);
        $tries++;
    } while ($tries < 3)
}

END {
    my $err = $?;

    umount($mount) if (defined($mount) && $mounted);

    while(@dm_devices) {
        system('dmsetup', 'remove', pop(@dm_devices));
    }

    while(@loops) {
        system('losetup', '-d', pop(@loops));
    }

    $? = $err;
}

my @loop_args;
foreach my $arg (@ARGV) {
    my $loop = `losetup -f --show $arg`;
    chomp($loop);
    push(@loops, $loop);
    push(@loop_args, '-d', $loop);
}

my $json = JSON::PP->new();
open(JSON, '-|', "$builddir/ldmtool", @loop_args, 'create', 'volume', $dg, $vol)
    or die("Unable to run ldmtool");
while(<JSON>) {
    $json->incr_parse($_);
}
close(JSON);

my $tested = 0;
foreach my $device (@{$json->incr_parse}) {
    system('mount', "/dev/mapper/$device", $mount);
    $mounted = 1;

    open(TEST, '<', "$mount/test.txt") or die "Test file missing";
    my $line = <TEST>;
    close(TEST);
    umount($mount);
    $mounted = 0;

    `$builddir/ldmtool remove all`;

    exit(1) unless $line eq 'Filesystem test';
    $tested = 1;
}

exit($tested ? 0 : 1);