📄 linux-lib.pl
字号:
# linux-lib.pl# Mount table functions for linuxif (&has_command("amd")) { local $amd = &read_amd_conf(); $amd_support = $amd =~ /\[\s*global\s*\]/i ? 2 : 1; }$autofs_support = &has_command("automount");if (&has_command("mount.smb")) { $smbfs_support = 3; }elsif (&has_command("smbmount")) { $smbfs_support = `smbmount -v` =~ /Version\s+2/i ? 2 : 1; }$swaps_support = -r "/proc/swaps";# Return information about a filesystem, in the form:# directory, device, type, options, fsck_order, mount_at_boot# If a field is unused or ignored, a - appears instead of the value.# Swap-filesystems (devices or files mounted for VM) have a type of 'swap',# and 'swap' in the directory fieldsub list_mounts{local(@rv, @p, @o, $_, $i, $j); $i = 0;# Get /etc/fstab mountsopen(FSTAB, $config{fstab_file});while(<FSTAB>) { local(@o, $at_boot); chop; s/#.*$//g; if (!/\S/ || /\signore\s/) { next; } @p = split(/\s+/, $_); if ($p[2] eq "proc") { $p[0] = "proc"; } if ($p[2] eq "auto") { $p[2] = "*"; } $rv[$i] = [ $p[1], $p[0], $p[2] ]; $rv[$i]->[5] = "yes"; @o = split(/,/ , $p[3] eq "defaults" ? "" : $p[3]); if (($j = &indexof("noauto", @o)) >= 0) { # filesytem is not mounted at boot splice(@o, $j, 1); $rv[$i]->[5] = "no"; } $rv[$i]->[3] = (@o ? join(',' , @o) : "-"); $rv[$i]->[4] = (@p >= 5 ? $p[5] : 0); $i++; }close(FSTAB);if ($amd_support == 1) { # Get old automounter configuration, as used by redhat local $amd = &read_amd_conf(); if ($amd =~ /MOUNTPTS='(.*)'/) { @p = split(/\s+/, $1); for($j=0; $j<@p; $j+=2) { $rv[$i++] = [ $p[$j], $p[$j+1], "auto", "-", 0, "yes" ]; } } }elsif ($amd_support == 2) { # Guess what? There's now a *new* amd config file format, introduced # in redhat 6.1 and caldera 2.3 local @amd = &parse_amd_conf(); local @sp = split(/:/, $amd[0]->{'opts'}->{'search_path'}); local ($am, $sp); foreach $am (@amd) { local $mn = $am->{'opts'}->{'map_name'}; if ($mn !~ /^\//) { foreach $sp (@sp) { if (-r "$sp/$mn") { $mn = "$sp/$mn"; last; } } } $rv[$i++] = [ $am->{'dir'}, $mn, "auto", $am->{'opts'}, 0, "yes" ] if ($am->{'dir'} ne 'global'); } }# Get kernel automounter configurationif ($autofs_support) { open(AUTO, $config{'autofs_file'}); while(<AUTO>) { chop; s/#.*$//g; if (/^\s*(\S+)\s+(\S+)\s*(.*)$/) { $rv[$i++] = [ $1, $2, "autofs", ($3 ? &autofs_options($3) : "-"), 0, "yes" ]; } } close(AUTO); }return @rv;}# create_mount(directory, device, type, options, fsck_order, mount_at_boot)# Add a new entry to the fstab file, old or new automounter filesub create_mount{local(@mlist, @amd, $_); local($opts);if ($_[2] eq "auto") { if ($amd_support == 1) { # Adding an old automounter mount local $amd = &read_amd_conf(); local $m = "$_[0] $_[1]"; if ($amd =~ /MOUNTPTS=''/) { $amd =~ s/MOUNTPTS=''/MOUNTPTS='$m'/; } else { $amd =~ s/MOUNTPTS='(.*)'/MOUNTPTS='$1 $m'/; } &write_amd_conf($amd); } elsif ($amd_support == 2) { # Adding a new automounter mount open(AMD, ">>$config{'auto_file'}"); print AMD "\n"; print AMD "[ $_[0] ]\n"; print AMD "map_name = $_[1]\n"; close(AMD); } }elsif ($_[2] eq "autofs") { # Adding a new automounter mount open(AUTO, ">> $config{'autofs_file'}"); print AUTO "$_[0] $_[1]"; if ($_[3]) { print AUTO " ",&autofs_args($_[3]); } print AUTO "\n"; close(AUTO); }else { # Adding a normal mount to the fstab file open(FSTAB, ">> $config{fstab_file}"); print FSTAB "$_[1] $_[0] $_[2]"; $opts = $_[3] eq "-" ? "" : $_[3]; if ($_[5] eq "no") { $opts = join(',' , (split(/,/ , $opts) , "noauto")); } if ($opts eq "") { print FSTAB " defaults"; } else { print FSTAB " $opts"; } print FSTAB " 0 "; print FSTAB $_[4] eq "-" ? "0\n" : "$_[4]\n"; close(FSTAB); }}# change_mount(num, directory, device, type, options, fsck_order, mount_at_boot)# Change an existing permanent mountsub change_mount{local($i, @fstab, $line, $opts, $j, @amd);$i = 0;# Update fstab fileopen(FSTAB, $config{fstab_file});@fstab = <FSTAB>;close(FSTAB);open(FSTAB, "> $config{fstab_file}");foreach (@fstab) { chop; ($line = $_) =~ s/#.*$//g; if ($line =~ /\S/ && $line !~ /\signore\s/ && $i++ == $_[0]) { # Found the line to replace print FSTAB "$_[2] $_[1] $_[3]"; $opts = $_[4] eq "-" ? "" : $_[4]; if ($_[6] eq "no") { $opts = join(',' , (split(/,/ , $opts) , "noauto")); } if ($opts eq "") { print FSTAB " defaults"; } else { print FSTAB " $opts"; } print FSTAB " 0 "; print FSTAB $_[5] eq "-" ? "0\n" : "$_[5]\n"; } else { print FSTAB $_,"\n"; } }close(FSTAB);if ($amd_support == 1) { # Update older amd configuration local $amd = &read_amd_conf(); if ($amd =~ /MOUNTPTS='(.*)'/) { # found mount points line.. local @mpts = split(/\s+/, $1); for($j=0; $j<@mpts; $j+=2) { if ($i++ == $_[0]) { $mpts[$j] = $_[1]; $mpts[$j+1] = $_[2]; } } local $mpts = join(" ", @mpts); $amd =~ s/MOUNTPTS='(.*)'/MOUNTPTS='$mpts'/; } &write_amd_conf($amd); }elsif ($amd_support == 2) { # Update new amd configuration local @amd = &parse_amd_conf(); local $lref = &read_file_lines($config{'auto_file'}); foreach $am (@amd) { next if ($am->{'dir'} eq 'global'); if ($i++ == $_[0]) { local @nl = ( "[ $_[1] ]" ); local %opts = %{$am->{'opts'}}; $opts->{'map_name'} = $_[2]; foreach $o (keys %opts) { push(@nl, "$o = $opts{$o}"); } splice(@$lref, $am->{'line'}, $am->{'eline'} - $am->{'line'} + 1, @nl); } } &flush_file_lines(); }# Update autofs configurationif ($autofs_support) { open(AUTO, $config{'autofs_file'}); @auto = <AUTO>; close(AUTO); open(AUTO, "> $config{'autofs_file'}"); foreach (@auto) { chop; ($line = $_) =~ s/#.*$//g; if ($line =~ /\S/ && $i++ == $_[0]) { print AUTO "$_[1] $_[2]"; if ($_[4]) { print AUTO " ",&autofs_args($_[4]); } print AUTO "\n"; } else { print AUTO $_,"\n"; } } close(AUTO); }}# delete_mount(index)# Delete an existing permanent mountsub delete_mount{local($i, @fstab, $line, $opts, $j, @amd);$i = 0;# Update fstab fileopen(FSTAB, $config{fstab_file});@fstab = <FSTAB>;close(FSTAB);open(FSTAB, "> $config{fstab_file}");foreach (@fstab) { chop; ($line = $_) =~ s/#.*$//g; if ($line !~ /\S/ || $line =~ /\signore\s/ || $i++ != $_[0]) { # Don't delete this line print FSTAB $_,"\n"; } }close(FSTAB);if ($amd_support == 1) { # Update older amd configuration local $amd = &read_amd_conf(); if ($amd =~ /MOUNTPTS='(.*)'/) { # found mount points line.. local @mpts = split(/\s+/, $1); for($j=0; $j<@mpts; $j+=2) { if ($i++ == $_[0]) { splice(@mpts, $j, 2); } } local $mpts = join(" ", @mpts); $amd =~ s/MOUNTPTS='(.*)'/MOUNTPTS='$mpts'/; } &write_amd_conf($amd); }elsif ($amd_support == 2) { # Update new amd configuration local @amd = &parse_amd_conf(); local $lref = &read_file_lines($config{'auto_file'}); foreach $am (@amd) { next if ($am->{'dir'} eq 'global'); if ($i++ == $_[0]) { splice(@$lref, $am->{'line'}, $am->{'eline'} - $am->{'line'} + 1); } } &flush_file_lines(); }# Update AMD fileif ($amd_support) { open(AMD, $config{auto_file}); @amd = <AMD>; close(AMD); open(AMD, "> $config{auto_file}"); foreach (@amd) { if (/MOUNTPTS='(.*)'/) { # found mount points line.. @mpts = split(/\s+/, $1); for($j=0; $j<@mpts; $j+=2) { if ($i++ != $_[0]) { push(@nmpts, $mpts[$j]); push(@nmpts, $mpts[$j+1]); } } print AMD "MOUNTPTS='".join(' ', @nmpts)."'\n"; } else { print AMD $_; } } close(AMD); }# Update autofs fileif ($autofs_support) { open(AUTO, $config{'autofs_file'}); @auto = <AUTO>; close(AUTO); open(AUTO, "> $config{'autofs_file'}"); foreach (@auto) { chop; ($line = $_) =~ s/#.*$//g; if ($line !~ /\S/ || $i++ != $_[0]) { # keep this line print AUTO $_,"\n"; } } close(AUTO); }}# list_mounted()# Return a list of all the currently mounted filesystems and swap files.# The list is in the form:# directory device type optionssub list_mounted{local(@rv, @p, @o, $mo, $_, %smbopts);&read_smbopts();open(MTAB, "/etc/mtab");while(<MTAB>) { chop; s/#.*$//g; if (!/\S/) { next; } @p = split(/\s+/, $_); if ($p[2] eq "auto" || $p[0] =~ /^\S+:\(pid\d+\)$/) { # Automounter map.. turn the map= option into the device @o = split(/,/ , $p[3]); ($mo) = grep {/^map=/} (@o); $mo =~ /^map=(.*)$/; $p[0] = $1; $p[3] = join(',' , grep {!/^map=/} (@o)); $p[2] = "auto"; } elsif ($p[2] eq "autofs") { # Kernel automounter map.. use the pid to find the map $p[0] =~ /automount\(pid(\d+)\)/ || next; $out = `ps hwwww $1`; $out =~ /automount\s+(.*)\s*(\S+)\s+(file|program|yp)(,\S+)?\s+(\S+)/ || next; $p[0] = $5; $p[3] = $1 ? &autofs_options($1) : "-"; } elsif ($p[2] eq "smbfs") { # Change from //FOO/BAR to \\foo\bar $p[0] =~ s/\//\\/g; $p[0] = lc($p[0]); $p[3] = $smbopts{$p[1]}; } elsif ($p[2] eq "proc") { # The source for proc mounts is always proc $p[0] = "proc"; } push(@rv, [ $p[1], $p[0], $p[2], $p[3] ]); }close(MTAB);open(SWAPS, "/proc/swaps");while(<SWAPS>) { chop; if (/^(\/\S+)\s+/) { push(@rv, [ "swap", $1, "swap", "-" ]); } }close(SWAPS);return @rv;}# mount_dir(directory, device, type, options)# Mount a new directory from some device, with some options. Returns 0 if ok,# or an error string if failedsub mount_dir{local($out, $opts, $shar, %options, %smbopts);if ($_[2] eq "swap") { # Use swapon to add the swap space.. $out = `swapon $_[1] 2>&1`; if ($out =~ /Invalid argument/) { # looks like this swap partition isn't ready yet.. set it up $out = `mkswap $_[1] 2>&1`; if ($?) { return "mkswap failed : <pre>$out</pre>"; } $out = `swapon $_[1] 2>&1`;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -