SI/system.pm
branchstable
changeset 33 6432e90cc34d
parent 26 39421a9700c5
parent 32 8f9e45f83a65
child 34 28090de7d005
equal deleted inserted replaced
26:39421a9700c5 33:6432e90cc34d
     1 package SI::system;
       
     2 
       
     3 use strict;
       
     4 use warnings;
       
     5 use File::Temp qw(tempdir);
       
     6 use if $ENV{DEBUG} ~~ qw(system) => "Smart::Comments";
       
     7 use SI::tools;
       
     8 
       
     9 $ENV{LC_ALL} = "C";
       
    10 
       
    11 sub id() {
       
    12 
       
    13     # hope it's eth* or wlan*
       
    14     local $_ = (sort grep { /^(eth|wlan)/ } `ifconfig -a`)[0];
       
    15     /^(?<dev>\S+)\s.*HWaddr\s+(?<mac>[\da-f:]+)\s*$/i
       
    16       and return $+{mac};
       
    17     die "ERR: Can't get system identification (MAC address)\n";
       
    18 }
       
    19 
       
    20 sub hostname(\%) {
       
    21     my $devs = shift;
       
    22     my $mnt = tempdir(CLEANUP => 1);
       
    23     my $h;
       
    24 
       
    25     foreach my $fs (
       
    26         grep {
       
    27             exists $devs->{volume}{$_}{type}
       
    28               and $devs->{volume}{$_}{type} =~ /^ext/i
       
    29         } @{ $devs->{volumes} }
       
    30       )
       
    31     {
       
    32         run("mount -r $fs $mnt");
       
    33         if (-f "$mnt/etc/hostname") {
       
    34             chomp($h = cat("$mnt/etc/hostname"));
       
    35         }
       
    36         run("umount $mnt");
       
    37         return $h if defined $h;
       
    38     }
       
    39 
       
    40     return $h;
       
    41 }
       
    42 
       
    43 sub fsck(\%) {
       
    44     my $devs = shift;
       
    45 
       
    46     foreach
       
    47       my $v (grep { exists $devs->{volume}{type} } keys %{ $devs->{volume} })
       
    48     {
       
    49         my $volume = $devs->{volume}{$v};
       
    50         next if $volume->{type} !~ /^ext/i;
       
    51 
       
    52         system("fsck -C0 $v");
       
    53         die "ERR: fsck failed\n" if $? > 2;
       
    54     }
       
    55 }
       
    56 
       
    57 1;
       
    58 
       
    59 # vim:sts=4 sw=4 aw ai si: