📄 mount-lib.pl.old
字号:
# mount-lib.pl# Functions for handling the /etc/[v]fstab file. Some functions are defined in# here, and some in OS-specific files named <os_type>-lib.plrequire '../web-lib.pl';# 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 a - in the directory fieldsub list_mounts{local(@rv, @p, $_, $i); $i = 0;open(FSTAB, $config{fstab_file});while(<FSTAB>) { chop; s/#.*$//g; if (!/\S/) { next; } @p = split(/\s+/, $_); if ($gconfig{os_type} eq "solaris") { $rv[$i] = "$p[2] $p[0] $p[3] $p[6] $p[4] $p[5]"; } elsif ($gconfig{os_type} =~ /linux$/) { local(@o, $idx, $at_boot); $at_boot = "yes"; $rv[$i] = "$p[1] $p[0] $p[2] "; @o = split(/,/ , $p[3] eq "defaults" ? "" : $p[3]); if (($idx = &indexof("noauto", @o)) >= 0) { # filesytem is not mounted at boot splice(@o, $idx, 1); $at_boot = "no"; } if (@p >= 5) { $rv[$i] .= " $p[4]"; } else { $rv[$i] .= " 0"; } $rv[$i] .= " $at_boot"; } $i++; }close(FSTAB);return @rv;}# get_mount(directory|'swap', device)# Returns the index of this mount, or -1 if not knownsub get_mount{local(@mlist, @p, $d);@mlist = &list_mounts();for($i=0; $i<@mlist; $i++) { @p = split(/\s+/, $mlist[$i]); $d = &is_swap($_[3]) ? "swap" : $_[0]; if ($_[0] eq "*" && $p[1] eq $_[1]) { # found by match on device return $i; } elsif ($_[1] eq "*" && $p[0] eq $d) { # found by match on directory return $i; } elsif ($p[0] eq $d && $p[1] eq $_[1]) { # found by match on both return $i; } }return -1;}# 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);$len = @mlist = &list_mounts();open(FSTAB, ">> $config{fstab_file}");if ($gconfig{os_type} eq "solaris") { local($fsck); if ($_[3] eq "ufs" || $_[3] eq "s5fs" || $_[3] eq "cachefs") { ($fsck = $_[0]) =~ s/\/dsk\//\/rdsk\//g; } else { $fsck = "-"; } print FSTAB "$_[1] $fsck $_[0] $_[2] $_[4] $_[5] $_[3]\n"; }elsif ($gconfig{os_type} =~ /linux$/) { local($opts); print FSTAB "$_[1] $_[0] $_[1]"; $opts = $_[3]; if (!$_[5]) { $opts = join(',' , (split(/,/ , $opts) , "noauto")); } if ($opts eq "") { print FSTAB " defaults"; } else { print FSTAB " $opts"; } print FSTAB " 0 $_[4]\n"; }close(FSTAB);return $len;}# 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 a '-'sub list_mounted{local(@rv, @p);if ($gconfig{os_type} eq "solaris") { foreach (split(/\n/, `/usr/sbin/swap -l`)) { if (/^(\/\S+)\s+/) { push(@rv, "- $1 swap -"); } } foreach (split(/\n/, `/bin/cat /etc/mnttab`)) { s/#.*$//g; if (!/\S/) { next; } @p = split(/\s+/, $_); $p[3] = join(',' , (grep {!/^dev=/} split(/,/ , $p[3]))); push(@rv, "$p[1] $p[0] $p[2] $p[3]"); } }elsif ($gconfig{os_type} =~ /linux$/) { # Swap files are not handled yet for linux foreach (split(/\n/, `/bin/cat /etc/mtab`)) { s/#.*$//g; if (!/\S/) { next; } @p = split(/\s+/, $_); push(@rv, "$p[1] $p[0] $p[2] $p[3]"); } }return @rv;}# get_mounted(directory|'swap', device)# Returns the index of this current mount, or -1 if not knownsub get_mounted{local(@mlist, @p, $d, $i);@mlist = &list_mounted();for($i=0; $i<@mlist; $i++) { @p = split(/\s+/, $mlist[$i]); $d = &is_swap($_[3]) ? "swap" : $_[0]; if ($_[0] eq "*" && $p[1] eq $_[1]) { # found by match on device return $i; } elsif ($_[1] eq "*" && $p[0] eq $d) { # found by match on directory return $i; } elsif ($p[0] eq $d && $p[1] eq $_[1]) { # found by match on both return $i; } }return -1;}# 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);if ($gconfig{os_type} eq "solaris") { if (&is_swap($_[2])) { # Adding a swap device $out = `/usr/sbin/swap -a $_[1] 2>&1`; if ($?) { return $out; } } else { # Mounting a directory $out = `/usr/sbin/mount -F $_[2] -o "$_[3]" $_[1] $_[0] 2>&1`; if ($?) { return $out; } } }elsif ($gconfig{os_type} =~ /linux$/) { $out = `/bin/mount -F $_[2] -o "$_[3]" $_[1] $_[0] 2>&1`; if ($?) { return $out; } }return 0;}# umount_dir(directory, type)# Unmount a directory that is currently mounted. Returns 0 if ok,# or an error string if failedsub umount_dir{if ($gconfig{os_type} eq "solaris") { $out = `/usr/sbin/umount $_[0] 2>&1`; if ($?) { return $out; } }elsif ($gconfig{os_type} =~ /linux$/) { $out = `/bin/umount $_[0] 2>&1`; if ($?) { return $out; } }return 0;}# remount_dir(directory, device, type, options)# Apply some change in options to an already mounted directorysub remount_dir{}# is_swap(type)# Returns 1 if a filesystem type is for swap, 0 if notsub is_swap{return $_[0] eq "swap";}# 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 ($gconfig{os_type} eq "solaris") { if ($_[0] eq "fd" || $_[0] eq "proc" || $_[0] eq "swap") { return (); } `/usr/bin/df -k $_[1]` =~ /Mounted on\n\S+\s+(\S+)\s+\S+\s+(\S+)/; return ($1, $2); }elsif ($gconfig{os_type} =~ /linux$/) { if ($_[0] eq "proc" || $_[0] eq "swap") { return ""; } `/bin/df -k $_[1]` =~ /Mounted on\n\S+\s+(\S+)\s+\S+\s+(\S+)/; return ($1, $2); }return ();}# 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(%supp, $_);if ($gconfig{os_type} eq "solaris") { return ("ufs", "nfs", "hsfs", "pcfs", "swap", "tmpfs"); }elsif ($gconfig{os_type} =~ /linux$/) { open(PROCFS, "/proc/filesystems"); while(<PROCFS>) { chop; /(\S+)$/; $supp{$1}++; } close(PROCFS); return grep { $supp{$_} } ("ext2", "minix", "msdos", "nfs", "iso9660", "ext", "xiafs", "hpfs", "fat", "vfat", "umsdos", "sysv"); }}# fstype_name(type)# Given a short filesystem type, return a human-readable name for itsub fstype_name{local(%fsmap);if ($gconfig{os_type} eq "solaris") { %fsmap = ("ufs","Solaris Unix Filesystem", "nfs","Network Filesystem", "hsfs","ISO9660 CD-ROM", "pcfs","MS-DOS Filesystem", "swap","Virtual Memory", "tmpfs","Ram Disk", "proc","Process Image Filesystem", "fd","File Descriptor Filesystem"); }elsif ($gconfig{os_type} =~ /linux$/) { %fsmap = ("ext2","Linux Native Filesystem", "minix","Minix Filesystem", "msdos","MS-DOS Filesystem", "nfs","Network Filesystem", "iso9660","ISO9660 CD-ROM", "ext","Old EXT Linux Filesystem", "xiafs","Old XIAFS Linux Filesystem", "hpfs","HP Filesystem", "fat","MS-DOS Filesystem", "vfat","Windows 95 Filesystem", "umsdos","Linux on top of MS-DOS Filesystem", "sysv","System V Filesystem"); }return $fsmap{$_[0]};}# ask_fsck_boot(type)# Returns 1 if this filesystem type can be fcsk'd, 0 if not. This is used to # decide whether to ask for the fsck order for this filesystem.sub ask_fsck_boot{if ($gconfig{os_type} eq "solaris") { return 1 if ($_[0] eq "ufs"); }elsif ($gconfig{os_type} =~ /linux$/) { return 1 if ($_[0] eq "ext2" || $_[0] eq "minix" || $_[0] eq "xiafs"); }return 0;}# 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 $_[1] =~ /^([^:]+):(.*)$/; print "<tr> <td><b>NFS Hostname</b></td>\n"; print "<td><input name=nfs_host size=20 value=\"$1\"></td>\n"; print "<td><b>NFS Directory</b></td>\n"; print "<td><input name=nfs_dir size=20 value=\"$2\"></td> </tr>\n"; }elsif ($gconfig{os_type} eq "solaris" && $_[0] eq "tmpfs") { # Location is irrelevant for tmpfs filesystems }elsif ($gconfig{os_type} eq "solaris" && $_[0] eq "ufs") { # Mounted from a normal disk, raid (MD) device or from # somewhere else print "<tr> <td valign=top><b>UFS Disk</b></td>\n"; print "<td colspan=3>\n"; if ($_[1] =~ /^\/dev\/dsk\/c([0-9]+)t([0-9]+)d([0-9]+)s([0-9]+)$/) { $ufs_dev = 0; $scsi_c = $1; $scsi_t = $2; $scsi_d = $3; $scsi_s = $4; } elsif ($_[1] eq "") { $ufs_dev = 0; $scsi_c = $scsi_t = $scsi_s = $scsi_d = 0; } elsif ($_[1] =~ /^\/dev\/md\/dsk\/d([0-9]+)$/) { $ufs_dev = 1; $scsi_md = $1; } else { $ufs_dev = 2; $scsi_path = $_[0]; } printf "<input type=radio name=ufs_dev value=0 %s> SCSI Disk:\n", $ufs_dev == 0 ? "checked" : ""; print "Controller <input name=ufs_c size=3 value=\"$scsi_c\">\n"; print "Target <input name=ufs_t size=3 value=\"$scsi_t\">\n"; print "Unit <input name=ufs_d size=3 value=\"$scsi_d\">\n"; print "Partition <input name=ufs_s size=3 value=\"$scsi_s\"><br>\n";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -