1 | #!/usr/bin/perl -w
|
---|
2 | #
|
---|
3 | # Disk subroutines brought by the MondoRescue project
|
---|
4 | #
|
---|
5 | # $Id$
|
---|
6 | #
|
---|
7 | # Copyright B. Cornec 2008-2015
|
---|
8 | # Provided under the GPL v2
|
---|
9 |
|
---|
10 | package MondoRescue::Disk;
|
---|
11 |
|
---|
12 | use strict 'vars';
|
---|
13 | use ProjectBuilder::Base;
|
---|
14 | use English;
|
---|
15 | use MondoRescue::File;
|
---|
16 |
|
---|
17 | # Inherit from the "Exporter" module which handles exporting functions.
|
---|
18 |
|
---|
19 | use Exporter;
|
---|
20 |
|
---|
21 | # Export, by default, all the functions into the namespace of
|
---|
22 | # any code which uses this module.
|
---|
23 |
|
---|
24 | our @ISA = qw(Exporter);
|
---|
25 | our @EXPORT = qw(mr_disk_list mr_disk_type mr_device_mounted);
|
---|
26 |
|
---|
27 | =pod
|
---|
28 |
|
---|
29 | =head1 NAME
|
---|
30 |
|
---|
31 | MondoRescue::Disk, part of the mondorescue.org
|
---|
32 |
|
---|
33 | =head1 DESCRIPTION
|
---|
34 |
|
---|
35 | This modules provides disk related functions for the Mondorescue project
|
---|
36 |
|
---|
37 | =head1 USAGE
|
---|
38 |
|
---|
39 | =over 4
|
---|
40 |
|
---|
41 | =item B<mr_disk_list>
|
---|
42 |
|
---|
43 | This function uses fdisk to determine the list of all the disks on the system
|
---|
44 | It returns an array of strings giving all the devices
|
---|
45 |
|
---|
46 | =cut
|
---|
47 |
|
---|
48 | sub mr_disk_list {
|
---|
49 |
|
---|
50 | my $fdisk = pb_check_req("fdisk",0);
|
---|
51 | my @disks;
|
---|
52 |
|
---|
53 | open(FDISK, "LANG=C $fdisk -l 2>/dev/null |") || die "Unable to read from $fdisk";
|
---|
54 | while (<FDISK>) {
|
---|
55 | chomp($_);
|
---|
56 | my $i = $_;
|
---|
57 | if ($i =~ /^Disk \//) {
|
---|
58 | pb_log(2,"Found disk line: $i\n");
|
---|
59 | $i =~ s|^Disk /([A-z0-9/_-]+):.*|/$1|;
|
---|
60 | pb_log(2,"Pushing $i\n");
|
---|
61 | push @disks,$i;
|
---|
62 | }
|
---|
63 | }
|
---|
64 | close(FDISK);
|
---|
65 | pb_log(2,"Found the following disks: @disks\n");
|
---|
66 | return (@disks);
|
---|
67 | }
|
---|
68 |
|
---|
69 | =item B<mr_disk_type>
|
---|
70 |
|
---|
71 | This function uses fdisk to determine the type of disk whose device is passed as a parameter
|
---|
72 | It returns either msdos for MBR type or gpt for GPT type
|
---|
73 |
|
---|
74 | =cut
|
---|
75 |
|
---|
76 | sub mr_disk_type {
|
---|
77 |
|
---|
78 | my $device = shift;
|
---|
79 | # MBR by default
|
---|
80 | my $type = "MBR";
|
---|
81 |
|
---|
82 | my $fdisk = pb_check_req("fdisk",0);
|
---|
83 |
|
---|
84 | open(FDISK, "LANG=C $fdisk -l $device 2>/dev/null |") || die "Unable to read from $fdisk";
|
---|
85 | while (<FDISK>) {
|
---|
86 | if (($_ =~ /EFI GPT/) || ($_ =~ / GPT /) || ($_ =~ /Disk[\s]*label type: gpt/)) {
|
---|
87 | $type= "GPT";
|
---|
88 | last;
|
---|
89 | }
|
---|
90 | }
|
---|
91 | close(FDISK);
|
---|
92 | pb_log(2,"Found a $type partition format\n");
|
---|
93 | return ($type);
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | =item B<mr_device_mounted>
|
---|
98 |
|
---|
99 | This function returns the mount point if a device is mounted and undef if not.
|
---|
100 |
|
---|
101 | =cut
|
---|
102 |
|
---|
103 | sub mr_device_mounted {
|
---|
104 |
|
---|
105 | my $device = shift;
|
---|
106 |
|
---|
107 | return(1) if ((not defined $device) || ($device =~ "^/sys|^/proc"));
|
---|
108 |
|
---|
109 | my $h = mr_file_read_all_link($device);
|
---|
110 | # Preserve that hash locally before earsing it to reuse another one
|
---|
111 | my $h2 = mr_file_copy_and_erase_hash($h);
|
---|
112 |
|
---|
113 | foreach my $f (keys %$h2) {
|
---|
114 | pb_log(2,"Working on device $f\n");
|
---|
115 | # TODO: get them from a conf file - FreeBSD needs swapinfo for swap request
|
---|
116 | foreach my $dev ("mount","cat /proc/swaps") {
|
---|
117 | # Look for the device in the command result
|
---|
118 | open(MOUNT,"$dev|") || die "Unable to execute the $dev command";
|
---|
119 | while (<MOUNT>) {
|
---|
120 | my ($mntp,$void) = split('\s',$_,2);
|
---|
121 | $h = mr_file_read_all_link($mntp);
|
---|
122 | foreach my $m (keys %$h) {
|
---|
123 | pb_log(2,"== Working on mounted device $m\n");
|
---|
124 | if ($m eq $f) {
|
---|
125 | # Find the mountpoint and return it
|
---|
126 | my ($void1,$mountpoint,$void2) = split('\s',$void);
|
---|
127 | pb_log(2,"*** Found $m on $mountpoint\n") if (defined $mountpoint);
|
---|
128 | return($mountpoint);
|
---|
129 | }
|
---|
130 | }
|
---|
131 | my $h3 = mr_file_copy_and_erase_hash($h);
|
---|
132 | }
|
---|
133 | close(MOUNT);
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | # Not found
|
---|
138 | return(undef);
|
---|
139 | }
|
---|