source: MondoRescue/branches/3.3/MondoRescue/lib/MondoRescue/Disk.pm@ 3742

Last change on this file since 3742 was 3742, checked in by Bruno Cornec, 4 years ago

Fix non working mr-device-mounted

File size: 3.1 KB
RevLine 
[3365]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
10package MondoRescue::Disk;
11
12use strict 'vars';
13use ProjectBuilder::Base;
14use English;
[3447]15use MondoRescue::File;
[3365]16
17# Inherit from the "Exporter" module which handles exporting functions.
18
19use Exporter;
20
21# Export, by default, all the functions into the namespace of
22# any code which uses this module.
23
24our @ISA = qw(Exporter);
[3553]25our @EXPORT = qw(mr_disk_list mr_disk_type mr_device_mounted);
[3365]26
27=pod
28
29=head1 NAME
30
31MondoRescue::Disk, part of the mondorescue.org
32
33=head1 DESCRIPTION
34
35This modules provides disk related functions for the Mondorescue project
36
37=head1 USAGE
38
39=over 4
40
[3553]41=item B<mr_disk_list>
42
43This function uses fdisk to determine the list of all the disks on the system
44It returns an array of strings giving all the devices
45
46=cut
47
48sub mr_disk_list {
49
50my $fdisk = pb_check_req("fdisk",0);
51my @disks;
52
53open(FDISK, "LANG=C $fdisk -l 2>/dev/null |") || die "Unable to read from $fdisk";
54while (<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}
64close(FDISK);
65pb_log(2,"Found the following disks: @disks\n");
66return (@disks);
67}
68
[3365]69=item B<mr_disk_type>
70
71This function uses fdisk to determine the type of disk whose device is passed as a parameter
72It returns either msdos for MBR type or gpt for GPT type
73
74=cut
75
76sub mr_disk_type {
77
78my $device = shift;
79# MBR by default
[3553]80my $type = "MBR";
[3365]81
82my $fdisk = pb_check_req("fdisk",0);
83
[3553]84open(FDISK, "LANG=C $fdisk -l $device 2>/dev/null |") || die "Unable to read from $fdisk";
[3365]85while (<FDISK>) {
[3553]86 if (($_ =~ /EFI GPT/) || ($_ =~ / GPT /) || ($_ =~ /Disk[\s]*label type: gpt/)) {
87 $type= "GPT";
[3365]88 last;
89 }
90}
91close(FDISK);
[3369]92pb_log(2,"Found a $type partition format\n");
[3365]93return ($type);
94}
[3432]95
96
[3447]97=item B<mr_device_mounted>
98
99This function returns the mount point if a device is mounted and undef if not.
100
101=cut
102
103sub mr_device_mounted {
104
105my $device = shift;
106
[3742]107return("") if ((not defined $device) || ($device =~ "^/sys|^/proc"));
[3447]108
[3742]109# Beware of how hashes are handled in these functions
[3447]110my $h = mr_file_read_all_link($device);
111# Preserve that hash locally before earsing it to reuse another one
112my $h2 = mr_file_copy_and_erase_hash($h);
113
114foreach my $f (keys %$h2) {
115 pb_log(2,"Working on device $f\n");
116 # TODO: get them from a conf file - FreeBSD needs swapinfo for swap request
[3742]117 foreach my $dev ("mount","grep -Ev '^Filename' /proc/swaps") {
[3447]118 # Look for the device in the command result
119 open(MOUNT,"$dev|") || die "Unable to execute the $dev command";
120 while (<MOUNT>) {
[3742]121 my ($mntp,$v1,$dir,$v3) = split('\s',$_,4);
[3447]122 $h = mr_file_read_all_link($mntp);
123 foreach my $m (keys %$h) {
124 pb_log(2,"== Working on mounted device $m\n");
125 if ($m eq $f) {
126 # Find the mountpoint and return it
[3742]127 pb_log(2,"*** Found $m on $dir\n") if (defined $dir);
128 # Clean before returning
129 mr_file_erase_hash($h);
130 mr_file_erase_hash($h2);
131 return($dir);
[3447]132 }
133 }
[3742]134 mr_file_erase_hash($h);
[3447]135 }
136 close(MOUNT);
137 }
138}
139
140# Not found
141return(undef);
142}
Note: See TracBrowser for help on using the repository browser.