📄 unixware-lib.pl
字号:
# unixware-lib.pl# Filesystem functions for UnixWare (works for me on 7.0.1)# 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, $_, $i); $i = 0;# List normal filesystem mountsopen(FSTAB, $config{fstab_file});while(<FSTAB>) { chop; s/#.*$//g; if (!/\S/) { next; } @p = split(/\s+/, $_); if ($p[3] eq "swap") { $p[2] = "swap"; } $rv[$i++] = [ $p[2], $p[0], $p[3], $p[6], $p[4], $p[5] ]; }close(FSTAB);# List automount pointsopen(AUTOTAB, $config{autofs_file});while(<AUTOTAB>) { chop; s/#.*$//g; if (!/\S/ || /^[+\-]/) { next; } @p = split(/\s+/, $_); if ($p[2] eq "") { $p[2] = "-"; } else { $p[2] =~ s/^-//g; } $rv[$i++] = [ $p[0], $p[1], "autofs", $p[2], "-", "yes" ]; }close(AUTOTAB);return @rv;}# create_mount(directory, device, type, options, fsck_order, mount_at_boot)# Add a new entry to the fstab file, and return the index of the new entrysub create_mount{local($len, @mlist, $fcsk, $dir);if ($_[2] eq "autofs") { # An autofs mount.. add to /etc/auto_master $len = grep { $_->[2] eq "autofs" } (&list_mounts()); open(AUTOTAB, ">> $config{autofs_file}"); print AUTOTAB "$_[0] $_[1]",($_[3] eq "-" ? "" : " -$_[3]"),"\n"; close(AUTOTAB); }else { # Add to the fstab file $len = grep { $_->[2] ne "autofs" } (&list_mounts()); open(FSTAB, ">> $config{fstab_file}"); if ($_[2] eq "ufs" || $_[2] eq "s5fs") { ($fsck = $_[1]) =~ s/\/dsk\//\/rdsk\//g; } else { $fsck = "-"; } if ($_[2] eq "swap") { $dir = "-"; } else { $dir = $_[0]; } print FSTAB "$_[1] $fsck $dir $_[2] $_[4] $_[5] $_[3]\n"; close(FSTAB); }return $len;}# delete_mount(index)# Delete some mount from the tablesub delete_mount{local(@fstab, $i, $line, $_);open(FSTAB, $config{fstab_file});@fstab = <FSTAB>;close(FSTAB);$i = 0;open(FSTAB, "> $config{fstab_file}");foreach (@fstab) { chop; ($line = $_) =~ s/#.*$//g; if ($line =~ /\S/ && $i++ == $_[0]) { # found the line not to include } else { print FSTAB $_,"\n"; } }close(FSTAB);open(AUTOTAB, $config{autofs_file});@autotab = <AUTOTAB>;close(AUTOTAB);open(AUTOTAB, "> $config{autofs_file}");foreach (@autotab) { chop; ($line = $_) =~ s/#.*$//g; if ($line =~ /\S/ && $line !~ /^[+\-]/ && $i++ == $_[0]) { # found line not to include.. } else { print AUTOTAB $_,"\n"; } }close(AUTOTAB);}# change_mount(num, directory, device, type, options, fsck_order, mount_at_boot)# Change an existing permanent mountsub change_mount{local(@fstab, @autotab, $i, $line, $fsck, $dir, $_);$i = 0;open(FSTAB, $config{fstab_file});@fstab = <FSTAB>;close(FSTAB);open(FSTAB, "> $config{fstab_file}");foreach (@fstab) { chop; ($line = $_) =~ s/#.*$//g; if ($line =~ /\S/ && $i++ == $_[0]) { # Found the line to replace if ($_[3] eq "ufs" || $_[3] eq "s5fs") { ($fsck = $_[2]) =~ s/\/dsk\//\/rdsk\//g; } else { $fsck = "-"; } if ($_[3] eq "swap") { $dir = "-"; } else { $dir = $_[1]; } print FSTAB "$_[2] $fsck $dir $_[3] $_[5] $_[6] $_[4]\n"; } else { print FSTAB $_,"\n"; } }close(FSTAB);open(AUTOTAB, $config{autofs_file});@autotab = <AUTOTAB>;close(AUTOTAB);open(AUTOTAB, "> $config{autofs_file}");foreach (@autotab) { chop; ($line = $_) =~ s/#.*$//g; if ($line =~ /\S/ && $line !~ /^[+\-]/ && $i++ == $_[0]) { # Found the line to replace print AUTOTAB "$_[1] $_[2] ", ($_[4] eq "-" ? "" : "-$_[4]"),"\n"; } else { print AUTOTAB $_,"\n"; } }close(AUTOTAB);}# list_mounted()# Return a list of all the currently mounted filesystems and swap files.# The list is in the form:# directory device type options# For swap files, the directory will be 'swap'sub list_mounted{local(@rv, @p, $_, $i, $r);foreach (split(/\n/, `swap -l`)) { if (/^(\/\S+)\s+/) { push(@rv, [ "swap", $1, "swap", "-" ]); } }foreach (split(/\n/, `cat /etc/mnttab`)) { s/#.*$//g; if (!/\S/) { next; } @p = split(/\s+/, $_); if ($p[0] =~ /:vold/) { next; } if ($p[0] =~ /^rumba-(\d+)$/) { # rumba smb mount local($args, $ps); $p[3] = "pid=$1"; $ps = (-x "/usr/ucb/ps") ? "/usr/ucb/ps auwwwwx $1" : "ps -o args -f $1"; `$ps` =~ /rumba\s+\/\/([^\/]+)\/(.*\S)\s+(\/\S+)(.*)/ || next; $serv = $1; $shar = $2; $p[2] = "rumba"; $args = $4; if ($args =~ /\s+-s\s+(\S+)/ && $1 ne $serv) { $p[0] = "\\\\$1\\$shar"; $p[3] .= ",machinename=$serv"; } else { $p[0] = "\\\\$serv\\$shar"; } if ($args =~ /\s+-c\s+(\S+)/) { $p[3] .= ",clientname=$1"; } if ($args =~ /\s+-U\s+(\S+)/) { $p[3] .= ",username=$1"; } if ($args =~ /\s+-u\s+(\S+)/) { $p[3] .= ",uid=$1"; } if ($args =~ /\s+-g\s+(\S+)/) { $p[3] .= ",gid=$1"; } if ($args =~ /\s+-f\s+(\S+)/) { $p[3] .= ",fmode=$1"; } if ($args =~ /\s+-d\s+(\S+)/) { $p[3] .= ",dmode=$1"; } if ($args =~ /\s+-C/) { $p[3] .= ",noupper"; } if ($args =~ /\s+-P\s+(\S+)/) { $p[3] .= ",password=$1"; } if ($args =~ /\s+-S/) { $p[3] .= ",readwrite"; } if ($args =~ /\s+-w/) { $p[3] .= ",readonly"; } if ($args =~ /\s+-e/) { $p[3] .= ",attr"; } } else { $p[3] = join(',' , (grep {!/^dev=/} split(/,/ , $p[3]))); } push(@rv, [ $p[1], $p[0], $p[2], $p[3] ]); }foreach $r (@rv) { if ($r->[2] eq "cachefs" && $r->[1] =~ /\.cfs_mnt_points/) { # Oh no.. a caching filesystem mount. Fiddle things so that # it looks right. for($i=0; $i<@rv; $i++) { if ($rv[$i]->[0] eq $r->[1]) { # Found the automatically mounted entry. lose it $r->[1] = $rv[$i]->[1]; splice(@rv, $i, 1); last; } } } }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 failed. If the directory is 'swap', then mount as# virtual memory.sub mount_dir{local($out, $opts);if ($_[0] eq "swap") { # Adding a swap device $out = `swap -a $_[1] 2>&1`; if ($?) { return $out; } }else { # Mounting a directory if ($_[2] eq "cachefs") { # Mounting a caching filesystem.. need to create cache first local(%options); &parse_options("cachefs", $_[3]); if (!(-r "$options{cachedir}/.cfs_resource")) { # The cache directory does not exist.. set it up if (-d $options{cachedir} && !rmdir($options{"cachedir"})) { return "The directory $options{cachedir} ". "already exists. Delete it"; } $out = `cfsadmin -c $options{cachedir} 2>&1`; if ($?) { return $out; } } } if ($_[2] eq "rumba") { # call 'rumba' to mount local(%options, $shortname, $shar, $opts, $rv); &parse_options("rumba", $_[3]); $shortname = &get_system_hostname(); if ($shortname =~ /^([^\.]+)\.(.+)$/) { $shortname = $1; } $_[1] =~ /^\\\\(.+)\\(.+)$/; $shar = "//".($options{machinename} ?$options{machinename} :$1). "/$2"; $opts = ("-s $1 "). (defined($options{'clientname'}) ? "-c $options{'clientname'} " : "-c $shortname "). (defined($options{'username'}) ? "-U $options{'username'} " : ""). (defined($options{'uid'}) ? "-u $options{'uid'} " : ""). (defined($options{'gid'}) ? "-g $options{'gid'} " : ""). (defined($options{'fmode'}) ? "-f $options{'fmode'} " : ""). (defined($options{'dmode'}) ? "-d $options{'dmode'} " : ""). (defined($options{'noupper'}) ? "-C " : ""). (defined($options{'password'}) ? "-P $options{'password'} " : "-n "). (defined($options{'readwrite'}) ? "-S " : ""). (defined($options{'readonly'}) ? "-w " : ""). (defined($options{'attr'}) ? "-e " : ""); $rv = system("rumba \"$shar\" $_[0] $opts >/tmp/$$.rumba 2>&1 </dev/null"); $out = `cat /tmp/$$.rumba`; unlink("/tmp/$$.rumba"); if ($rv) { return "<pre>$out</pre> : rumba \"$shar\" $_[0] $opts"; } } else { $opts = $_[3] eq "-" ? "" : "-o \"$_[3]\""; $out = `mount -F $_[2] $opts -- $_[1] $_[0] 2>&1`; if ($?) { return $out; } } }return 0;}# unmount_dir(directory, device, type)# Unmount a directory (or swap device) that is currently mounted. Returns 0 if# ok, or an error string if failedsub unmount_dir{if ($_[0] eq "swap") { $out = `swap -d $_[1] 2>&1`; }elsif ($_[2] eq "rumba") { # kill the process (if nobody is in the directory) $dir = $_[0]; if (`fuser -c $_[0] 2>/dev/null` =~ /\d/) { return "$_[0] is busy"; } if (`cat /etc/mnttab` =~ /rumba-(\d+)\s+$dir\s+nfs/) { kill('TERM', $1) || return "Failed to kill rumba"; } else { return "Failed to find rumba pid"; } sleep(1); }else { $out = `umount $_[0] 2>&1`; }if ($?) { return $out; }return 0;}# disk_space(type, directory)# Returns the amount of total and free space for some filesystem, or an# empty array if not appropriate.sub disk_space{if (&get_mounted($_[1], "*") < 0) { return (); }if ($_[0] eq "fd" || $_[0] eq "proc" || $_[0] eq "swap" || $_[0] eq "autofs") { return (); }`df -k $_[1]` =~ /Mounted on\n\S+\s+(\S+)\s+\S+\s+(\S+)/;return ($1, $2);}# list_fstypes()# Returns an array of all the supported filesystem types. If a filesystem is# found that is not one of the supported types, generate_location() and# generate_options() will not be called for it.sub list_fstypes{local(@fs);@fs = ("vxfs", "ufs", "nfs", "hsfs", "pcfs", "lofs", "cachefs", "swap", "tmpfs", "autofs");if (&has_command("rumba")) { push(@fs, "rumba"); }return @fs;}# fstype_name(type)# Given a short filesystem type, return a human-readable name for itsub fstype_name{local(%fsmap);%fsmap = ("vxfs", "Veritas Filesystem", "ufs","Unix Filesystem", "nfs","Network Filesystem", "hsfs","ISO9660 CD-ROM", "pcfs","MS-DOS Filesystem", "lofs","Loopback Filesystem", "cachefs","Caching Filesystem", "swap","Virtual Memory", "tmpfs","Ram Disk", "autofs","Automounter Filesystem", "proc","Process Image Filesystem", "fd","File Descriptor Filesystem", "rumba","Windows Networking Filesystem");return $config{long_fstypes} && $fsmap{$_[0]} ? $fsmap{$_[0]} : uc($_[0]);}# mount_modes(type)# Given a filesystem type, returns 4 numbers that determine how the file# system can be mounted, and whether it can be fsck'd# 0 - cannot be permanently recorded# 1 - can be permanently recorded, and is always mounted at boot# 2 - can be permanently recorded, and may or may not be mounted at boot# The second is:# 0 - mount is always permanent => mounted when saved# 1 - doesn't have to be permanent# The third is:# 0 - cannot be fsck'd at boot time# 1 - can be be fsck'd at boot time# The fourth is:# 0 - can be unmounted# 1 - cannot be unmountedsub mount_modes{if ($_[0] eq "vxfs" || $_[0] eq "ufs" || $_[0] eq "cachefs" || $_[0] eq "s5fs") { return (2, 1, 1, 0); }elsif ($_[0] eq "rumba") { return (0, 1, 0, 0); }else { return (2, 1, 0, 0); }}# multiple_mount(type)# Returns 1 if filesystems of this type can be mounted multiple times, 0 if notsub multiple_mount{return ($_[0] eq "vxfs" || $_[0] eq "nfs" || $_[0] eq "tmpfs" || $_[0] eq "cachefs" || $_[0] eq "autofs" || $_[0] eq "lofs" || $_[0] eq "rumba");}# generate_location(type, location)# Output HTML for editing the mount location of some filesystem.sub generate_location{if ($_[0] eq "nfs") { # NFS mount from some host and directory if ($_[1] =~ /^nfs:/) { $nfsmode = 2; } elsif (!$_[1] || $_[1] =~ /^([A-z0-9\-\.]+):([^,]+)$/) { $nfsmode = 0; $nfshost = $1; $nfspath = $2; } else { $nfsmode = 1; } if ($gconfig{'os_version'} >= 2.6) { # UnixWare 7 can list multiple NFS servers in mount print "<tr> <td><b>NFS Source</b></td>\n"; printf "<td><input type=radio name=nfs_serv value=0 %s>\n", $nfsmode == 0 ? "checked" : ""; print "<b>NFS Hostname</b></td>\n"; print "<td><input name=nfs_host size=20 value=\"$nfshost\">\n"; &nfs_server_chooser_button("nfs_host"); print " <b>NFS Directory</b>\n"; print "<input name=nfs_dir size=20 value=\"$nfspath\">\n"; &nfs_export_chooser_button("nfs_host", "nfs_dir"); print "</td> </tr>\n"; print "<tr> <td></td>\n"; printf "<td><input type=radio name=nfs_serv value=1 %s>\n", $nfsmode == 1 ? "checked" : ""; print "<b>Multiple NFS Servers</b></td>\n"; printf "<td><input name=nfs_list size=40 value=\"%s\">\n", $nfsmode == 1 ? $_[1] : ""; print "</td> </tr>\n"; if ($gconfig{'os_version'} >= 7) { print "<tr> <td></td> <td>\n"; printf "<input type=radio name=nfs_serv value=2 %s>\n", $nfsmode == 2 ? "checked" : ""; print "<b>WebNFS URL</b></td> <td>\n"; printf "<input name=nfs_url size=40 value=\"%s\">\n", $nfsmode == 2 ? $_[1] : "";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -