Subversion Repositories zfs_utils

Rev

Rev 31 | Rev 34 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 31 Rev 33
Line 19... Line 19...
19
# If called in scalar context, returns the full output as a single string.
19
# If called in scalar context, returns the full output as a single string.
20
# If called in list context, returns the output split into lines.
20
# If called in list context, returns the output split into lines.
21
# If $merge_stderr is true (default), stderr is merged into stdout (only for scalar commands).
21
# If $merge_stderr is true (default), stderr is merged into stdout (only for scalar commands).
22
# returns empty string or empty list on failure and logs failure message.
22
# returns empty string or empty list on failure and logs failure message.
23
sub runCmd {
23
sub runCmd {
24
   my $cmd = \@_;
24
   my $cmd = join( ' ', @_ );
25
   $merge_stderr = 1 unless defined $merge_stderr;
25
   $merge_stderr = 1 unless defined $merge_stderr;
26
   my $output = '';
26
   my $output = '';
27
 
27
 
28
   if (ref $cmd eq 'ARRAY') {
28
#   if (ref $cmd eq 'ARRAY') {
29
      # Execute without a shell (safer). Note: stderr is not merged in this path.
29
#      # Execute without a shell (safer). Note: stderr is not merged in this path.
30
      logMsg( 'Running command [' . join( ' ', @$cmd ) . ']');
30
#      logMsg( 'Running command [' . join( ' ', @$cmd ) . ']');
31
      open my $fh, '-|', @{$cmd} or do {
31
#      open my $fh, '-|', @{$cmd} or do {
32
         logMsg("runCmd: failed to exec '@{$cmd}': $!");
32
#         logMsg("runCmd: failed to exec '@{$cmd}': $!");
33
         return wantarray ? () : '';
33
#         return wantarray ? () : '';
34
      };
34
#      };
35
      local $/ = undef;
35
#      local $/ = undef;
36
      $output = <$fh>;
36
#      $output = <$fh>;
37
      close $fh;
37
#      close $fh;
38
   } else {
38
#   } else {
39
      # Scalar command runs via the shell; optionally merge stderr into stdout.
39
      # Scalar command runs via the shell; optionally merge stderr into stdout.
40
      logMsg( "Scalar running command [$cmd]" );
40
      logMsg( "Running command [$cmd]" );
41
      my $c = $cmd;
-
 
42
      $c .= ' 2>&1' if $merge_stderr;
41
      $cmd .= ' 2>&1' if $merge_stderr;
43
      $output = `$c`;
42
      $output = `$cmd`;
-
 
43
if ($? == -1) {
-
 
44
    logMsg( "failed to execute: $!");
-
 
45
    return ''
-
 
46
}
-
 
47
elsif ($? & 127) { # fatal error, exit program
-
 
48
    logMsg( sprintf( "child died with signal %d, %s coredump\n", ($? & 127),  ($? & 128) ? 'with' : 'without' ) );
44
   }
49
    die;
-
 
50
}
-
 
51
else {
-
 
52
    logMsg( sprintf( "child exited with value %d\n", $? >> 8 ) );
-
 
53
}
45
 
54
 
-
 
55
#   }
46
   $output //= '';
56
   $output //= '';
47
 
57
 
48
   if (wantarray) {
58
   if (wantarray) {
49
      return $output eq '' ? () : split(/\n/, $output);
59
      return $output eq '' ? () : split(/\n/, $output);
50
   } else {
60
   } else {
Line 76... Line 86...
76
 
86
 
77
# find a drive by it's label by scanning /dev/gpt/ for $timeout seconds.
87
# find a drive by it's label by scanning /dev/gpt/ for $timeout seconds.
78
# If the drive is found, mount it on mountPath and return the mountPath.
88
# If the drive is found, mount it on mountPath and return the mountPath.
79
# If not found, return empty string.
89
# If not found, return empty string.
80
sub mountDriveByLabel {
90
sub mountDriveByLabel {
81
   my ($label, $mountPath, $timeout, $checkEvery ) = @_;
91
   my ($label, $mountPath, $timeout, $checkEvery, $filesystem, $devPath ) = @_;
82
   unless ($label) {
92
   unless ($label) {
83
      logMsg("mountDriveByLabel: No label provided");
93
      logMsg("mountDriveByLabel: No label provided");
84
      return '';
94
      return '';
85
   }
95
   }
86
   unless ( $label =~ /^[a-zA-Z0-9_\-]+$/ ) {
96
   unless ( $label =~ /^[a-zA-Z0-9_\-]+$/ ) {
Line 89... Line 99...
89
   }
99
   }
90
 
100
 
91
   logMsg("mountDriveByLabel: Looking for drive with label '$label'");
101
   logMsg("mountDriveByLabel: Looking for drive with label '$label'");
92
   # default to /mnt/label if not provided
102
   # default to /mnt/label if not provided
93
   $mountPath //= "/mnt/$label"; # this is where we'll mount it if we find it
103
   $mountPath //= "/mnt/$label"; # this is where we'll mount it if we find it
-
 
104
   $devPath //= "/dev/gpt/";
94
   $label = "/dev/gpt/$label"; #  this is where FreeBSD puts gpt labeled drives
105
   $label = "$devPath$label"; #  this is where FreeBSD puts gpt labeled drives
-
 
106
   $filesystem //= 'ufs'; # default to mounting ufs
95
   # drive already mounted, just return the path
107
   # drive already mounted, just return the path
96
   return $mountPath if ( runCmd( "mount | grep '$mountPath'" ) );
108
   return $mountPath if ( runCmd( "mount | grep '$mountPath'" ) );
97
   # default to 10 minutes (600 seconds) if not provided
109
   # default to 10 minutes (600 seconds) if not provided
98
   $timeout //= 600;
110
   $timeout //= 600;
99
   # default to checking every minute if not provided
111
   # default to checking every minute if not provided
Line 114... Line 126...
114
       unless ( -d $mountPath || make_path($mountPath) ) {
126
       unless ( -d $mountPath || make_path($mountPath) ) {
115
         logMsg("Failed to create $mountPath: $!");
127
         logMsg("Failed to create $mountPath: $!");
116
         return '';
128
         return '';
117
       }
129
       }
118
       # mount device (let mount detect filesystem)
130
       # mount device (let mount detect filesystem)
119
       unless ( system('mount', $label, $mountPath) == 0 ) {
131
       unless ( runCmd( "mount -t $filesystem $label $mountPath" ) ) {
120
         logMsg("Failed to mount $label on $mountPath: $!");
132
         logMsg("Failed to mount $label on $mountPath: $!");
121
         return '';
133
         return '';
122
       }
134
       }
123
       return $mountPath;
135
       return $mountPath;
124
    } else {
136
    } else {