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_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_type>
|
---|
42 |
|
---|
43 | This function uses fdisk to determine the type of disk whose device is passed as a parameter
|
---|
44 | It returns either msdos for MBR type or gpt for GPT type
|
---|
45 |
|
---|
46 | =cut
|
---|
47 |
|
---|
48 | sub mr_disk_type {
|
---|
49 |
|
---|
50 | my $device = shift;
|
---|
51 | # MBR by default
|
---|
52 | my $type = "msdos";
|
---|
53 |
|
---|
54 | my $fdisk = pb_check_req("fdisk",0);
|
---|
55 |
|
---|
56 | open(FDISK, "$fdisk -l $device 2>/dev/null |") || die "Unable to read from $fdisk";
|
---|
57 | while (<FDISK>) {
|
---|
58 | if (($_ =~ /EFI GPT/) || ($_ =~ / GPT /)) {
|
---|
59 | $type= "gpt";
|
---|
60 | last;
|
---|
61 | }
|
---|
62 | }
|
---|
63 | close(FDISK);
|
---|
64 | pb_log(2,"Found a $type partition format\n");
|
---|
65 | return ($type);
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | =item B<mr_device_mounted>
|
---|
70 |
|
---|
71 | This function returns the mount point if a device is mounted and undef if not.
|
---|
72 |
|
---|
73 | =cut
|
---|
74 |
|
---|
75 | sub mr_device_mounted {
|
---|
76 |
|
---|
77 | my $device = shift;
|
---|
78 |
|
---|
79 | return(1) if ((not defined $device) || ($device =~ "^/sys|^/proc"));
|
---|
80 |
|
---|
81 | my $h = mr_file_read_all_link($device);
|
---|
82 | # Preserve that hash locally before earsing it to reuse another one
|
---|
83 | my $h2 = mr_file_copy_and_erase_hash($h);
|
---|
84 |
|
---|
85 | foreach my $f (keys %$h2) {
|
---|
86 | pb_log(2,"Working on device $f\n");
|
---|
87 | # TODO: get them from a conf file - FreeBSD needs swapinfo for swap request
|
---|
88 | foreach my $dev ("mount","cat /proc/swaps") {
|
---|
89 | # Look for the device in the command result
|
---|
90 | open(MOUNT,"$dev|") || die "Unable to execute the $dev command";
|
---|
91 | while (<MOUNT>) {
|
---|
92 | my ($mntp,$void) = split('\s',$_,2);
|
---|
93 | $h = mr_file_read_all_link($mntp);
|
---|
94 | foreach my $m (keys %$h) {
|
---|
95 | pb_log(2,"== Working on mounted device $m\n");
|
---|
96 | if ($m eq $f) {
|
---|
97 | # Find the mountpoint and return it
|
---|
98 | my ($void1,$mountpoint,$void2) = split('\s',$void);
|
---|
99 | pb_log(2,"*** Found $m on $mountpoint\n") if (defined $mountpoint);
|
---|
100 | return($mountpoint);
|
---|
101 | }
|
---|
102 | }
|
---|
103 | my $h3 = mr_file_copy_and_erase_hash($h);
|
---|
104 | }
|
---|
105 | close(MOUNT);
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | # Not found
|
---|
110 | return(undef);
|
---|
111 | }
|
---|