RHEL9.4實現autofs自動掛載iso映象

李蔚發表於2024-11-24

日期:2024.11.24
目的:實現自動掛載ISO(International Organization for Standardization)映象檔案
參照:馬哥教育王老師課程

安裝autofs

[root@RHEL9 ~]# dnf info autofs
Updating Subscription Management repositories.
Last metadata expiration check: 3:16:28 ago on Sat 23 Nov 2024 08:43:01 PM CST.
Available Packages
Name         : autofs
Epoch        : 1
Version      : 5.1.7
Release      : 58.el9
Architecture : x86_64
Size         : 395 k
Source       : autofs-5.1.7-58.el9.src.rpm
Repository   : rhel-9-for-x86_64-baseos-rpms
Summary      : A tool for automatically mounting and unmounting filesystems
License      : GPLv2+
Description  : autofs is a daemon which automatically mounts filesystems when you use
             : them, and unmounts them later when you are not using them.  This can
             : include network filesystems, CD-ROMs, floppies, and so forth.

[root@RHEL9 ~]# dnf -y install autofs

反正都是掛載映象,區別只是光碟機裡的和硬碟裡的,我決定就接著用/etc/auto.misc,這個檔案原來是實現自動掛載光碟機的

[root@RHEL9 ~]# rpm -qc autofs
/etc/auto.master
/etc/auto.misc
/etc/auto.net
/etc/auto.smb
/etc/autofs.conf
/etc/autofs_ldap_auth.conf
/etc/sysconfig/autofs
/usr/lib/systemd/system/autofs.service
[root@RHEL9 ~]# cat /etc/auto.misc 
#
# This is an automounter map and it has the following format
# key [ -mount-options-separated-by-comma ] location
# Details may be found in the autofs(5) manpage

cd		-fstype=iso9660,ro,nosuid,nodev	:/dev/cdrom

# the following entries are samples to pique your imagination
#linux		-ro,soft		ftp.example.org:/pub/linux
#boot		-fstype=ext2		:/dev/hda1
#floppy		-fstype=auto		:/dev/fd0
#floppy		-fstype=ext2		:/dev/fd0
#e2floppy	-fstype=ext2		:/dev/fd0
#jaz		-fstype=ext2		:/dev/sdc1
#removable	-fstype=ext2		:/dev/hdd

想到一個偷懶的辦法,先把映象名字改短

[root@RHEL9 ISO]# ls
AlmaLinux-8-latest-x86_64-dvd.iso    EL-8-x86_64-latest-appstream.iso  ubuntu-16.04.7-server-amd64.iso
AlmaLinux-9-latest-x86_64-dvd.iso    EL-9-x86_64-latest-appstream.iso  ubuntu-18.04.6-live-server-amd64.iso
CentOS-6.10-x86_64-bin-DVD1.iso      rhel-8.10-x86_64-dvd.iso          ubuntu-20.04.6-live-server-amd64.iso
CentOS-6.10-x86_64-bin-DVD2.iso      rhel-9.4-x86_64-dvd.iso           ubuntu-22.04.5-live-server-amd64.iso
CentOS-7-x86_64-Everything-2009.iso  Rocky-8.10-x86_64-dvd1.iso        ubuntu-24.04.1-live-server-amd64.iso
CentOS-8.5.2111-x86_64-dvd1.iso      Rocky-9.4-x86_64-dvd.iso
[root@RHEL9 ISO]# ls
alma8.iso                        CentOS-6.10-x86_64-bin-DVD2.iso  euro8.iso  rhel9.iso   ubuntu16.iso  ubuntu22.iso
alma9.iso                        centos7.iso                      euro9.iso  rocky8.iso  ubuntu18.iso  ubuntu24.iso
CentOS-6.10-x86_64-bin-DVD1.iso  centos8.iso                      rhel8.iso  rocky9.iso  ubuntu20.iso

修改配置檔案,這兩個符號 * 和 & 是一對,表示前面的目錄和後面的映象檔名字同名,後面映象路徑為/data/ISO/foo.iso 直接訪問/misc/foo就能訪問這個映象

[root@RHEL9 ISO]# vim /etc/auto.misc 
  1 #
  2 # This is an automounter map and it has the following format
  3 # key [ -mount-options-separated-by-comma ] location
  4 # Details may be found in the autofs(5) manpage
  5 
  6 cd      -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom
  7 *       -fstype=iso9660,ro,nosuid,nodev :/data/ISO/&.iso
  8 
  9 # the following entries are samples to pique your imagination
 10 #linux      -ro,soft        ftp.example.org:/pub/linux
 11 #boot       -fstype=ext2        :/dev/hda1
 12 #floppy     -fstype=auto        :/dev/fd0
 13 #floppy     -fstype=ext2        :/dev/fd0
 14 #e2floppy   -fstype=ext2        :/dev/fd0
 15 #jaz        -fstype=ext2        :/dev/sdc1
 16 #removable  -fstype=ext2        :/dev/hdd

啟動autofs,會自動建立/misc目錄

[root@RHEL9 ~]# ls /
afs  bin  boot  data  dev  etc  home  kvm  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@RHEL9 ~]# systemctl enable --now autofs.service 
Created symlink /etc/systemd/system/multi-user.target.wants/autofs.service → /usr/lib/systemd/system/autofs.service.
[root@RHEL9 ~]# ls /
afs  bin  boot  data  dev  etc  home  kvm  lib  lib64  media  misc  mnt  net  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

測試

[root@RHEL9 ~]# cd /misc/centos7
[root@RHEL9 centos7]# ls
CentOS_BuildTag  EULA  images    LiveOS    repodata              RPM-GPG-KEY-CentOS-Testing-7
EFI              GPL   isolinux  Packages  RPM-GPG-KEY-CentOS-7  TRANS.TBL
[root@RHEL9 centos7]# ls /misc/ubuntu24
boot  boot.catalog  casper  dists  EFI  install  md5sum.txt  pool  ubuntu
  • 主要是修改ISO檔名字費了點事兒,配置檔案其實就寫了一行,不然的話每個ISO檔案都得寫一行配置。
  • 以後wget再下載映象直接儲存的時候把名字改短就行了,之前建立虛擬機器的指令碼ISO檔案的路徑是以變數形式儲存的,也方便修改。
  • 自動化指令碼先不寫了,後續還得玩nfs和autofs,到時候一起寫。

相關文章