📄 linux-lib.pl
字号:
}# check_options(type, device, directory)# Read options for some filesystem from %in, and use them to update the# %options array. Options handled by the user interface will be set or# removed, while unknown options will be left untouched.sub check_options{local($k, @rv);# Parse the common options first..if ($_[0] ne "swap" && $_[0] ne "auto" && $_[0] ne "autofs" && $_[0] ne "smbfs") { delete($options{"ro"}); delete($options{"rw"}); if ($in{lnx_ro}) { $options{"ro"} = ""; } delete($options{"sync"}); delete($options{"async"}); if ($in{lnx_sync}) { $options{"sync"} = ""; } delete($options{"dev"}); delete($options{"nodev"}); if ($in{lnx_nodev}) { $options{"nodev"} = ""; } delete($options{"exec"}); delete($options{"noexec"}); if ($in{lnx_noexec}) { $options{"noexec"} = ""; } delete($options{"suid"}); delete($options{"nosuid"}); if ($in{lnx_nosuid}) { $options{"nosuid"} = ""; } delete($options{"user"}); delete($options{"nouser"}); if ($in{lnx_user}) { $options{"user"} = ""; } }if ($_[0] eq "nfs") { # NFS has a few specific options.. delete($options{"bg"}); delete($options{"fg"}); if ($in{nfs_bg}) { $options{"bg"} = ""; } delete($options{"soft"}); delete($options{"hard"}); if ($in{nfs_soft}) { $options{"soft"} = ""; } delete($options{"timeo"}); if (!$in{nfs_timeo_def}) { $options{"timeo"} = $in{nfs_timeo}; } delete($options{"nfsvers"}); if (!$in{nfs_vers_def}) { $options{"nfsvers"} = $in{nfs_vers}; } delete($options{"port"}); if (!$in{nfs_port_def}) { $options{"port"} = $in{nfs_port}; } delete($options{"intr"}); delete($options{"nointr"}); if ($in{nfs_intr}) { $options{"intr"} = ""; } delete($options{"tcp"}); delete($options{"udp"}); if ($in{nfs_tcp}) { $options{"tcp"} = ""; } }elsif ($_[0] eq "ext2") { # More options for ext2.. delete($options{"check"}); delete($options{"nocheck"}); if ($in{ext2_check} ne "normal") { $options{"check"} = $in{ext2_check}; } delete($options{"errors"}); if ($in{ext2_errors} ne "default") { $options{"errors"} = $in{ext2_errors}; } delete($options{"grpid"}); delete($options{"bsdgroups"}); delete($options{"sysvgroups"}); delete($options{"nogrpid"}); if ($in{ext2_grpid}) { $options{"grpid"} = ""; } delete($options{"resuid"}); delete($options{"resgid"}); if ($in{'ext2_resuid'}) { $options{"resuid"} = getpwnam($in{'ext2_resuid'}); } if ($in{'ext2_resgid'}) { $options{"resgid"} = getgrnam($in{'ext2_resgid'}); } delete($options{"quota"}); delete($options{"noquota"}); delete($options{"usrquota"}); delete($options{"grpquota"}); if ($in{'ext2_quota'} == 1) { $options{'usrquota'} = ""; } elsif ($in{'ext2_quota'} == 2) { $options{'grpquota'} = ""; } elsif ($in{'ext2_quota'} == 3) { $options{'usrquota'} = $options{'grpquota'} = ""; } }elsif ($_[0] eq "fat" || $_[0] eq "vfat" || $_[0] eq "msdos" || $_[0] eq "umsdos") { # All dos-based filesystems have similar options delete($options{"uid"}); delete($options{"gid"}); if ($in{fat_uid} ne "") { $options{"uid"} = getpwnam($in{'fat_uid'}); } if ($in{fat_gid} ne "") { $options{"gid"} = getgrnam($in{'fat_gid'}); } delete($options{"check"}); if ($in{fat_check} ne "") { $options{"check"} = $in{fat_check}; } delete($options{"conv"}); if ($in{fat_conv} ne "") { $options{"conv"} = $in{fat_conv}; } delete($options{"umask"}); if (!$in{fat_umask_def}) { $in{fat_umask} =~ /^[0-7]{3}$/ || &error("'$in{fat_umask}' is not a valid octal mask"); $options{"umask"} = $in{fat_umask}; } delete($options{"quiet"}); if ($in{fat_quiet}) { $options{"quiet"} = ""; } if ($_[0] eq "vfat") { # Parse extra vfat options.. delete($options{"uni_xlate"}); if ($in{fat_uni_xlate}) { $options{"uni_xlate"} = ""; } delete($options{"posix"}); if ($in{fat_posix}) { $options{"posix"} = ""; } } }elsif ($_[0] eq "hpfs") { # OS/2 filesystem options.. delete($options{"uid"}); delete($options{"gid"}); if ($in{hpfs_uid} ne "") { $options{"uid"} = getpwnam($in{hpfs_uid}); } if ($in{hpfs_gid} ne "") { $options{"gid"} = getgrnam($in{hpfs_gid}); } delete($options{"umask"}); if (!$in{hpfs_umask_def}) { $in{hpfs_umask} =~ /^[0-7]{3}$/ || &error("'$in{hpfs_umask}' is not a valid octal mask"); $options{"umask"} = $in{hpfs_umask}; } delete($options{"conv"}); if ($in{hpfs_conv} ne "") { $options{"conv"} = $in{hpfs_conv}; } }elsif ($_[0] eq "iso9660") { # Options for iso9660 cd-roms delete($options{"uid"}); delete($options{"gid"}); if ($in{iso9660_uid} ne "") { $options{"uid"} = getpwnam($in{iso9660_uid}); } if ($in{iso9660_gid} ne "") { $options{"gid"} = getgrnam($in{iso9660_gid}); } delete($options{"norock"}); if ($in{iso9660_norock}) { $options{"norock"} = ""; } delete($options{"mode"}); $in{iso9660_mode} =~ /^[0-7]{3}$/ || &error("'$in{iso9660_mode}' is not a valid octal mask"); $options{"mode"} = $in{iso9660_mode}; }elsif ($_[0] eq "autofs") { # Options for automounter filesystems delete($options{'timeout'}); if (!$in{'autofs_timeout_def'}) { $in{'autofs_timeout'} =~ /^\d+$/ || &error("'$in{autofs_timeout}' is not a valid timeout"); $options{'timeout'} = $in{'autofs_timeout'}; } delete($options{'pid-file'}); if (!$in{'autofs_pid-file_def'}) { $in{'autofs_pid-file'} =~ /^\/\S+$/ || &error("'$in{autofs_pid-file}' is not a valid pid file"); $options{'pid-file'} = $in{'autofs_pid-file'}; } }elsif ($_[0] eq "smbfs") { # Options for smb filesystems.. delete($options{user}); if ($in{smbfs_user}) { $options{user} = $in{smbfs_user}; } delete($options{passwd}); if ($in{smbfs_passwd}) { $options{passwd} = $in{smbfs_passwd}; } if ($smbfs_support != 2) { delete($options{uid}); if ($in{smbfs_uid} ne "") { $options{uid} = getpwnam($in{smbfs_uid}); } delete($options{gid}); if ($in{smbfs_gid} ne "") { $options{gid} = getgrnam($in{smbfs_gid}); } } if ($smbfs_support == 1) { delete($options{servername}); if (!$in{smbfs_sname_def}) { $options{servername} = $in{smbfs_sname}; } } elsif ($smbfs_support == 2) { delete($options{workgroup}); if (!$in{smbfs_wg_def}) { $options{workgroup} = $in{smbfs_wg}; } } if ($smbfs_support != 3) { delete($options{clientname}); if (!$in{smbfs_cname_def}) { $options{clientname} = $in{smbfs_cname}; } delete($options{machinename}); if (!$in{smbfs_mname_def}) { $options{machinename} = $in{smbfs_mname}; } elsif (!gethostbyname($in{'smbfs_server'})) { # No hostname found for the server.. try to guess local($out, $sname); $sname = $in{'smbfs_server'}; $out = `$config{'nmblookup_path'} -d 0 $sname 2>&1`; if (!$? && $out =~ /^([0-9\.]+)\s+$sname\n/) { $options{machinename} = $1; } } } if ($smbfs_support == 1) { delete($options{fmode}); if ($in{smbfs_fmode} !~ /^[0-7]{3}$/) { &error("'$in{smbfs_fmode}' is not a valid ", "octal file mode"); } elsif ($in{smbfs_fmode} ne "755") { $options{fmode} = $in{smbfs_fmode}; } delete($options{dmode}); if ($in{smbfs_dmode} !~ /^[0-7]{3}$/) { &error("'$in{smbfs_dmode}' is not a valid ", "octal directory mode"); } elsif ($in{smbfs_dmode} ne "755") { $options{dmode} = $in{smbfs_dmode}; } } elsif ($smbfs_support == 3) { delete($options{'ro'}); delete($options{'rw'}); if ($in{'ro'}) { $options{'ro'} = ''; } } }# Add loop option if mounting a normal fileif ($_[0] ne "swap" && $_[0] ne "auto" && $_[0] ne "autofs" && $_[0] ne "smbfs" && $_[0] ne "nfs") { local(@st); @st = stat($_[1]); if (@st && ($st[2] & 0xF000) == 0x8000) { # a regular file.. add the loop option if (!$options{'loop'}) { $options{'loop'} = ""; } } }# Return options stringforeach $k (keys %options) { if ($options{$k} eq "") { push(@rv, $k); } else { push(@rv, "$k=$options{$k}"); } }return @rv ? join(',' , @rv) : "-";}# Get the smbfs options from 'smbfs_opts' file in the current directory. This# is sadly necessary because there is no way to get the current options for# an existing smbfs mount... so webmin has to save them in a file when# mounting. Blech.sub read_smbopts{local($_);open(SMBOPTS, "$module_config_directory/smbfs");while(<SMBOPTS>) { /^(\S+)\s+(\S+)$/; $smbopts{$1} = $2; }close(SMBOPTS);}sub write_smbopts{local($_);open(SMBOPTS, "> $module_config_directory/smbfs");foreach (keys %smbopts) { print SMBOPTS "$_\t$smbopts{$_}\n"; }close(SMBOPTS);}# create_swap(file, size, units)# Calls dd and mkswap to setup a swap filesub create_swap{local($out, $bl);$bl = $_[1] * ($_[2] eq "m" ? 1024 : 1);$out = `dd if=/dev/zero of=$_[0] bs=1024 count=$bl 2>&1`;if ($?) { return "dd failed : $out"; }$out = `mkswap $_[0] $bl 2>&1`;if ($?) { return "mkswap failed : $out"; }system("sync >/dev/null 2>&1");return 0;}# exports_list(host, dirarray, clientarray)# Fills the directory and client array references with exports from some# host. Returns an error string if something went wrongsub exports_list{local($dref, $cref, $out, $_);$dref = $_[1]; $cref = $_[2];$out = `showmount -e $_[0] 2>&1`;if ($?) { return $out; }foreach (split(/\n/, $out)) { if (/^(\/\S*)\s+(.*)$/) { push(@$dref, $1); push(@$cref, $2); } }return undef;}# broadcast_addr()# Returns a useable broadcast address for finding NFS serverssub broadcast_addr{local($out);$out = `ifconfig -a 2>&1`;if ($out =~ /(eth|tr)\d\s+.*\n.*Bcast:(\S+)\s+/) { return $2; }return "255.255.255.255";}# autofs_options(string)# Converts a string of options line --timeout 60 to something like timeout=60sub autofs_options{local(@options);if ($_[0] =~ /--timeout\s+(\d+)/ || $_[0] =~ /-t\s+(\d+)/) { push(@options, "timeout=$1"); }if ($_[0] =~ /--pid-file\s+(\S+)/ || $_[0] =~ /-p\s+(\d+)/) { push(@options, "pid-file=$1"); }return join(',', @options);}# autofs_args(string)# Convert a comma-separated options string into args for automountsub autofs_args{local(%options, $args);&parse_options("autofs", $_[0]);if (defined($options{'timeout'})) { $args .= " --timeout $options{'timeout'}"; }if (defined($options{'pid-file'})) { $args .= " --pid-file $options{'pid-file'}"; }return $args;}# read_amd_conf()# Returns the entire amd config file as a stringsub read_amd_conf{local $sl = $/;$/ = undef;open(AMD, $config{'auto_file'});local $rv = <AMD>;close(AMD);$/ = $sl;return $rv;}# write_amd_conf(text)sub write_amd_conf{open(AMD, ">$config{'auto_file'}");print AMD $_[0];close(AMD);}# parse_amd_conf()# Parses a new-style amd.conf file into a hashtablesub parse_amd_conf{local (@rv, $str, $lnum);open(AMD, $config{'auto_file'});while(<AMD>) { s/\r|\n//g; s/#*$//g; if (/\[\s*(\S+)\s*\]/) { $str = { 'dir' => $1, 'line' => $lnum, 'eline' => $lnum }; push(@rv, $str); } elsif (/(\S+)\s*=\s*"(.*)"/ || /(\S+)\s*=\s*(\S+)/) { $str->{'opts'}->{$1} = $2; $str->{'eline'} = $lnum; } $lnum++; }close(AMD);return @rv;}1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -