📄 hpux-lib.pl
字号:
# hpux-lib.pl# Filesystem functions for HP-UX (works for me on 10.xx and 11.00)# 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[2] eq "ignore") { next; } if ($p[2] eq "swap") { $p[1] = "swap"; } $rv[$i++] = [ $p[1], $p[0], $p[2], $p[3], $p[5], "yes" ]; }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, $fsck, $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()); if ($_[4] eq "-") { $fsck = "0"; } else { $fsck = $_[4]; } open(FSTAB, ">> $config{fstab_file}"); print FSTAB "$_[1] $_[0] $_[2] $_[3] 0 $fsck\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]) { if ($_[5] eq "-") { $fsck = "0"; } else { $fsck = $_[5]; } # Found the line to replace print FSTAB "$_[2] $_[1] $_[3] $_[4] 0 $fsck\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/, `swapinfo -a`)) { if (/^dev\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/) { push(@rv, [ "swap", $8, "swap", "pri=$7" ]); } }foreach (split(/\n/, `cat /etc/mnttab`)) { s/#.*$//g; if (!/\S/) { next; } @p = split(/\s+/, $_); if ($p[2] eq "ignore") { next; } push(@rv, [ $p[1], $p[0], $p[2], $p[3] ]); }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 local(%options, $opts); &parse_options("swap", $_[3]); if (defined($options{"pri"})) { $opts = "-p $options{'pri'}"; } $out = `swapon $opts $_[1] 2>&1`; if ($? && !($out =~ /already enabled for paging/)) { return $out; } }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") { # Not possible! &error("Swap space cannot be removed"); }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 "swap") { return (); }`bdf $_[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{return ("hfs", "vxfs", "swap", "cdfs", "nfs", "lofs");}# fstype_name(type)# Given a short filesystem type, return a human-readable name for itsub fstype_name{local(%fsmap);%fsmap = ("hfs","HP Unix Filesystem", "vxfs","HP Journaled Unix Filesystem", "nfs","Network Filesystem", "cdfs","ISO9660 CD-ROM", "lofs","Loopback Filesystem", "swapfs","Filesystem Swap Space", "swap","Swap Space", "autofs","Automounter 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 "hfs" || $_[0] eq "vxfs") { return (1, 1, 1, 0); }elsif ($_[0] eq "swap") { return (1, 1, 0, 0); }else { return (1, 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 "nfs" || $_[0] eq "lofs");}# 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 $onenfs = !$_[1] || $_[1] =~ /^([A-z0-9\-\.]+):([^,]+)$/; print "<tr> <td><b>NFS Hostname</b></td>\n"; print "<td><input name=nfs_host size=20 value=\"$1\">\n"; &nfs_server_chooser_button("nfs_host"); print "</td>\n"; print "<td><b>NFS Directory</b></td>\n"; print "<td><input name=nfs_dir size=20 value=\"$2\">\n"; &nfs_export_chooser_button("nfs_host", "nfs_dir"); print "</td> </tr>\n"; }elsif ($_[0] eq "hfs") { # Mounted from a normal disk, LVM device or from # somewhere else print "<tr> <td valign=top><b>HFS Device</b></td>\n"; print "<td colspan=3>\n"; if ($_[1] =~ /^\/dev\/dsk\/c([0-9]+)t([0-9]+)d([0-9]+)s([0-9]+)$/) { $hfs_dev = 0; $scsi_c = $1; $scsi_t = $2; $scsi_d = $3; $scsi_s = $4; } elsif ($_[1] eq "") { $hfs_dev = 0; $scsi_c = $scsi_t = $scsi_s = $scsi_d = 0; } elsif ($_[1] =~ /^\/dev\/vg([0-9]+)\/(\S+)/) { $hfs_dev = 1; $scsi_vg = $1; $scsi_lv = $2; } else { $hfs_dev = 2; $scsi_path = $_[1]; } $scsi_path = $_[1]; printf "<input type=radio name=hfs_dev value=0 %s> SCSI Disk:\n", $hfs_dev == 0 ? "checked" : ""; print "Controller <input name=hfs_c size=3 value=\"$scsi_c\">\n"; print "Target <input name=hfs_t size=3 value=\"$scsi_t\">\n"; print "Unit <input name=hfs_d size=3 value=\"$scsi_d\">\n"; print "Partition <input name=hfs_s size=3 value=\"$scsi_s\"><br>\n"; printf "<input type=radio name=hfs_dev value=1 %s> LVM Device:\n", $hfs_dev == 1 ? "checked" : ""; print "Volume Group <input name=hfs_vg size=2 value=\"$scsi_vg\">\n"; print "Logical Volume <input name=hfs_lv size=20 value=\"$scsi_lv\"><br>\n"; printf "<input type=radio name=hfs_dev value=2 %s> Other Device:\n", $hfs_dev == 2 ? "checked" : ""; print "<input name=hfs_path size=20 value=\"$scsi_path\">"; print &file_chooser_button("hfs_path", 0); print "<br>\n"; print "</td> </tr>\n"; }elsif ($_[0] eq "vxfs") { # Mounted from a normal disk, LVM device or from # somewhere else print "<tr> <td valign=top><b>VXFS Device</b></td>\n"; print "<td colspan=3>\n"; if ($_[1] =~ /^\/dev\/dsk\/c([0-9]+)t([0-9]+)d([0-9]+)s([0-9]+)$/) { $jfs_dev = 0; $scsi_c = $1; $scsi_t = $2; $scsi_d = $3; $scsi_s = $4; } elsif ($_[1] eq "") { $jfs_dev = 0; $scsi_c = $scsi_t = $scsi_s = $scsi_d = 0; } elsif ($_[1] =~ /^\/dev\/vg([0-9]+)\/(\S+)/) { $jfs_dev = 1; $scsi_vg = $1; $scsi_lv = $2; } else { $jfs_dev = 2; $scsi_path = $_[1]; } $scsi_path = $_[1]; printf "<input type=radio name=jfs_dev value=0 %s> SCSI Disk:\n", $jfs_dev == 0 ? "checked" : ""; print "Controller <input name=jfs_c size=3 value=\"$scsi_c\">\n"; print "Target <input name=jfs_t size=3 value=\"$scsi_t\">\n"; print "Unit <input name=jfs_d size=3 value=\"$scsi_d\">\n"; print "Partition <input name=jfs_s size=3 value=\"$scsi_s\"><br>\n"; printf "<input type=radio name=jfs_dev value=1 %s> LVM Device:\n", $jfs_dev == 1 ? "checked" : ""; print "Volume Group <input name=jfs_vg size=2 value=\"$scsi_vg\">\n"; print "Logical Volume <input name=jfs_lv size=20 value=\"$scsi_lv\"><br>\n"; printf "<input type=radio name=jfs_dev value=2 %s> Other Device:\n", $jfs_dev == 2 ? "checked" : ""; print "<input name=jfs_path size=20 value=\"$scsi_path\">"; print &file_chooser_button("jfs_path", 0); print "<br>\n"; print "</td> </tr>\n"; }elsif ($_[0] eq "swap") { # Swapping to a disk partition or a file print "<tr> <td valign=top><b>Swap File</b></td>\n"; print "<td colspan=3>\n"; if ($_[1] =~ /^\/dev\/dsk\/c([0-9]+)t([0-9]+)d([0-9]+)s([0-9]+)$/) { $swap_dev = 0; $scsi_c = $1; $scsi_t = $2; $scsi_d = $3; $scsi_s = $4; } elsif ($_[1] =~ /^\/dev\/vg([0-9]+)\/(\S+)/) { $swap_dev = 1; $scsi_vg = $1; $scsi_lv = $2; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -