File Coverage

lib/Rex/Commands/Mkfs.pm
Criterion Covered Total %
statement 20 39 51.2
branch 0 12 0.0
condition 0 15 0.0
subroutine 7 8 87.5
pod 1 1 100.0
total 28 75 37.3


line stmt bran cond sub pod time code
1              
2             =head1 NAME
3              
4             Rex::Commands::Mkfs - Create filesystems
5              
6             =head1 DESCRIPTION
7              
8             With this module you can create filesystems on existing partitions and logical volumes.
9              
10             =head1 SYNOPSIS
11              
12             use Rex::Commands::Mkfs;
13              
14             =head1 EXPORTED FUNCTIONS
15              
16             =cut
17              
18             package Rex::Commands::Mkfs;
19              
20 1     1   8 use warnings;
  1         6  
  1         31  
21 1     1   15 use v5.12.5;
  1         4  
22              
23             our $VERSION = '1.14.3'; # VERSION
24              
25             require Rex::Exporter;
26 1     1   6 use base qw(Rex::Exporter);
  1         2  
  1         95  
27 1     1   7 use vars qw(@EXPORT);
  1         6  
  1         67  
28              
29             @EXPORT = qw(mkfs);
30              
31 1     1   6 use Rex::Helper::Run;
  1         2  
  1         56  
32 1     1   6 use Rex::Commands::Run;
  1         2  
  1         7  
33 1     1   6 use Carp;
  1         3  
  1         394  
34              
35             =head2 mkfs($devname, %option)
36              
37             Create a filesystem on device $devname.
38              
39             mkfs "sda1",
40             fstype => "ext2",
41             label => "mydisk";
42              
43             mkfs "sda2",
44             fstype => "swap";
45              
46             =cut
47              
48             sub mkfs {
49 0     0 1   my ( $devname, %option ) = @_;
50              
51 0 0         if ( $devname !~ m/^\// ) {
52 0           $devname = "/dev/$devname";
53             }
54              
55 0           my $add_opts = "";
56              
57 0 0 0       unless ( exists $option{fstype} && defined $option{fstype} ) {
58 0           croak("Missing or undefined fstype.");
59             }
60              
61 0 0         if ( grep { $option{fstype} eq $_ } ( "non-fs", "none", "" ) ) {
  0            
62 0           Rex::Logger::debug("Skip creating a filesystem of type '$option{fstype}'");
63 0           return;
64             }
65              
66 0 0 0       if ( ( exists $option{label} && $option{label} )
      0        
      0        
67             || ( exists $option{lable} && $option{lable} ) )
68             {
69 0   0       my $label = $option{label} || $option{lable};
70 0           $add_opts .= " -L $label ";
71             }
72              
73 0 0         if ( $option{fstype} eq "swap" ) {
    0          
74 0           Rex::Logger::info("Creating swap space on $devname");
75 0           i_run "mkswap $add_opts -f $devname";
76             }
77             elsif ( can_run("mkfs.$option{fstype}") ) {
78 0           Rex::Logger::info("Creating filesystem $option{fstype} on $devname");
79              
80 0           i_run "mkfs.$option{fstype} $add_opts $devname";
81             }
82              
83 0           return;
84             }
85              
86             1;