source: MondoRescue/branches/3.2/MondoRescue/lib/MondoRescue/Disk.pm@ 3553

Last change on this file since 3553 was 3553, checked in by Bruno Cornec, 8 years ago

Help Fix #790

  • Fix perl functions to handle disk type (MBR vs GPT) on RHEL7 and unify output format
  • Adds perl function to list all disks on the system
  • Adds perl program to unit test these functions
  • which_partition_format now uses mr-disk-type
File size: 3.0 KB
Line 
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;
15use MondoRescue::File;
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);
25our @EXPORT = qw(mr_disk_list mr_disk_type mr_device_mounted);
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
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
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
80my $type = "MBR";
81
82my $fdisk = pb_check_req("fdisk",0);
83
84open(FDISK, "LANG=C $fdisk -l $device 2>/dev/null |") || die "Unable to read from $fdisk";
85while (<FDISK>) {
86 if (($_ =~ /EFI GPT/) || ($_ =~ / GPT /) || ($_ =~ /Disk[\s]*label type: gpt/)) {
87 $type= "GPT";
88 last;
89 }
90}
91close(FDISK);
92pb_log(2,"Found a $type partition format\n");
93return ($type);
94}
95
96
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
107return(1) if ((not defined $device) || ($device =~ "^/sys|^/proc"));
108
109my $h = mr_file_read_all_link($device);
110# Preserve that hash locally before earsing it to reuse another one
111my $h2 = mr_file_copy_and_erase_hash($h);
112
113foreach 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
138return(undef);
139}
Note: See TracBrowser for help on using the repository browser.