Loading

RHEL9.4实现autofs自动挂载iso镜像

日期: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,到时候一起写。
posted @ 2024-11-24 00:35  李蔚  阅读(120)  评论(0)    收藏  举报