Red Hat Training

A Red Hat training course is available for Red Hat Enterprise Linux

3.13. bind 掛​​​​​​​載​​​​​​​與​​​​​​​檔​​​​​​​案​​​​​​​系​​​​​​​統​​​​​​​的​​​​​​​掛​​​​​​​載​​​​​​​順​​​​​​​序​​​​​​​

當​​​​​​​您​​​​​​​使​​​​​​​用​​​​​​​ mount 指​​​​​​​令​​​​​​​的​​​​​​​ bind 選​​​​​​​項​​​​​​​時​​​​​​​,請​​​​​​​確​​​​​​​定​​​​​​​檔​​​​​​​案​​​​​​​系​​​​​​​統​​​​​​​是​​​​​​​以​​​​​​​正​​​​​​​確​​​​​​​的​​​​​​​順​​​​​​​序​​​​​​​掛​​​​​​​載​​​​​​​的​​​​​​​。​​​​​​​在​​​​​​​以​​​​​​​下​​​​​​​範​​​​​​​例​​​​​​​中​​​​​​​,/var/log/ 目​​​​​​​錄​​​​​​​必​​​​​​​須​​​​​​​在​​​​​​​執​​​​​​​行​​​​​​​ bind 掛​​​​​​​載​​​​​​​至​​​​​​​ /tmp/ 目​​​​​​​錄​​​​​​​之​​​​​​​前​​​​​​​,先​​​​​​​被​​​​​​​掛​​​​​​​載​​​​​​​。​​​​​​​
# mount --bind /var/log /tmp
檔​​​​​​​案​​​​​​​系​​​​​​​統​​​​​​​的​​​​​​​掛​​​​​​​載​​​​​​​順​​​​​​​序​​​​​​​是​​​​​​​由​​​​​​​以​​​​​​​下​​​​​​​準​​​​​​​則​​​​​​​來​​​​​​​決​​​​​​​定​​​​​​​的​​​​​​​:
  • 一​​​​​​​般​​​​​​​來​​​​​​​說​​​​​​​,檔​​​​​​​案​​​​​​​系​​​​​​​統​​​​​​​的​​​​​​​掛​​​​​​​載​​​​​​​順​​​​​​​序​​​​​​​是​​​​​​​由​​​​​​​ fstab 檔​​​​​​​案​​​​​​​裡​​​​​​​的​​​​​​​先​​​​​​​後​​​​​​​順​​​​​​​序​​​​​​​而​​​​​​​定​​​​​​​。​​​​​​​例​​​​​​​外​​​​​​​情​​​​​​​形​​​​​​​是​​​​​​​使​​​​​​​用​​​​​​​ _netdev 旗​​​​​​​標​​​​​​​掛​​​​​​​載​​​​​​​的​​​​​​​檔​​​​​​​案​​​​​​​系​​​​​​​統​​​​​​​,或​​​​​​​擁​​​​​​​有​​​​​​​自​​​​​​​己​​​​​​​的​​​​​​​ init script 之​​​​​​​檔​​​​​​​案​​​​​​​系​​​​​​​統​​​​​​​。​​​​​​​
  • 擁​​​​​​​有​​​​​​​自​​​​​​​己​​​​​​​的​​​​​​​ init script 之​​​​​​​檔​​​​​​​案​​​​​​​系​​​​​​​統​​​​​​​會​​​​​​​在​​​​​​​稍​​​​​​​後​​​​​​​的​​​​​​​初​​​​​​​始​​​​​​​化​​​​​​​過​​​​​​​程​​​​​​​中​​​​​​​掛​​​​​​​載​​​​​​​,這​​​​​​​會​​​​​​​在​​​​​​​ fstab 檔​​​​​​​案​​​​​​​中​​​​​​​的​​​​​​​系​​​​​​​統​​​​​​​
  • 使​​​​​​​用​​​​​​​ _netdev 旗​​​​​​​標​​​​​​​掛​​​​​​​載​​​​​​​的​​​​​​​檔​​​​​​​案​​​​​​​系​​​​​​​統​​​​​​​,會​​​​​​​在​​​​​​​系​​​​​​​統​​​​​​​上​​​​​​​的​​​​​​​網​​​​​​​路​​​​​​​功​​​​​​​能​​​​​​​啟​​​​​​​用​​​​​​​後​​​​​​​掛​​​​​​​載​​​​​​​。​​​​​​​
如​​​​​​​果​​​​​​​您​​​​​​​的​​​​​​​配​​​​​​​置​​​​​​​需​​​​​​​要​​​​​​​ bind 掛​​​​​​​載​​​​​​​至​​​​​​​ GFS2 檔​​​​​​​案​​​​​​​系​​​​​​​統​​​​​​​,您​​​​​​​可​​​​​​​以​​​​​​​將​​​​​​​ fstab 檔​​​​​​​案​​​​​​​排​​​​​​​列​​​​​​​如​​​​​​​下​​​​​​​:
  1. 掛​​​​​​​載​​​​​​​需​​​​​​​要​​​​​​​ bind 掛​​​​​​​載​​​​​​​的​​​​​​​本​​​​​​​地​​​​​​​檔​​​​​​​案​​​​​​​系​​​​​​​統​​​​​​​。​​​​​​​
  2. 以​​​​​​​ bind 方​​​​​​​式​​​​​​​將​​​​​​​ GFS2 檔​​​​​​​案​​​​​​​系​​​​​​​統​​​​​​​掛​​​​​​​載​​​​​​​至​​​​​​​目​​​​​​​錄​​​​​​​
  3. 掛​​​​​​​載​​​​​​​ GFS2 檔​​​​​​​案​​​​​​​系​​​​​​​統​​​​​​​
如​​​​​​​果​​​​​​​您​​​​​​​的​​​​​​​配​​​​​​​置​​​​​​​需​​​​​​​要​​​​​​​使​​​​​​​用​​​​​​​ bind 的​​​​​​​方​​​​​​​式​​​​​​​掛​​​​​​​載​​​​​​​本​​​​​​​地​​​​​​​目​​​​​​​錄​​​​​​​(或​​​​​​​檔​​​​​​​案​​​​​​​系​​​​​​​統​​​​​​​)至​​​​​​​一​​​​​​​個​​​​​​​ GFS2 檔​​​​​​​案​​​​​​​系​​​​​​​統​​​​​​​上​​​​​​​,在​​​​​​​ fstab 檔​​​​​​​案​​​​​​​裡​​​​​​​以​​​​​​​正​​​​​​​確​​​​​​​的​​​​​​​順​​​​​​​序​​​​​​​列​​​​​​​出​​​​​​​檔​​​​​​​案​​​​​​​系​​​​​​​統​​​​​​​,才​​​​​​​能​​​​​​​正​​​​​​​確​​​​​​​地​​​​​​​掛​​​​​​​載​​​​​​​,因​​​​​​​為​​​​​​​ GFS2 檔​​​​​​​案​​​​​​​系​​​​​​​統​​​​​​​會​​​​​​​直​​​​​​​到​​​​​​​ GFS2 的​​​​​​​ init script 執​​​​​​​行​​​​​​​之​​​​​​​後​​​​​​​才​​​​​​​能​​​​​​​掛​​​​​​​載​​​​​​​。​​​​​​​因​​​​​​​此​​​​​​​,您​​​​​​​需​​​​​​​要​​​​​​​撰​​​​​​​寫​​​​​​​ init script 來​​​​​​​執​​​​​​​行​​​​​​​ bind 掛​​​​​​​載​​​​​​​,這​​​​​​​樣​​​​​​​ bind 掛​​​​​​​載​​​​​​​才​​​​​​​會​​​​​​​在​​​​​​​ GFS2 檔​​​​​​​案​​​​​​​系​​​​​​​統​​​​​​​掛​​​​​​​載​​​​​​​之​​​​​​​後​​​​​​​行​​​​​​​之​​​​​​​。​​​​​​​
以​​​​​​​下​​​​​​​ script 是​​​​​​​自​​​​​​​訂​​​​​​​ init script 的​​​​​​​範​​​​​​​例​​​​​​​。​​​​​​​這​​​​​​​個​​​​​​​ script 會​​​​​​​ bind 掛​​​​​​​載​​​​​​​兩​​​​​​​個​​​​​​​目​​​​​​​錄​​​​​​​至​​​​​​​ GFS2 檔​​​​​​​案​​​​​​​系​​​​​​​統​​​​​​​上​​​​​​​的​​​​​​​兩​​​​​​​個​​​​​​​目​​​​​​​錄​​​​​​​。​​​​​​​在​​​​​​​此​​​​​​​範​​​​​​​例​​​​​​​中​​​​​​​有​​​​​​​一​​​​​​​個​​​​​​​現​​​​​​​存​​​​​​​的​​​​​​​ GFS2 掛​​​​​​​載​​​​​​​點​​​​​​​,位​​​​​​​置​​​​​​​是​​​​​​​ /mnt/gfs2a,會​​​​​​​在​​​​​​​ GFS2 init script 執​​​​​​​行​​​​​​​時​​​​​​​、​​​​​​​叢​​​​​​​集​​​​​​​啟​​​​​​​動​​​​​​​後​​​​​​​掛​​​​​​​載​​​​​​​。​​​​​​​
在​​​​​​​這​​​​​​​範​​​​​​​例​​​​​​​ script 中​​​​​​​,chkconfig 敘​​​​​​​述​​​​​​​的​​​​​​​值​​​​​​​表​​​​​​​示​​​​​​​:
  • 345 表​​​​​​​示​​​​​​​這​​​​​​​個​​​​​​​ script 會​​​​​​​在​​​​​​​ runlevel 3、​​​​​​​4 與​​​​​​​ 5 執​​​​​​​行​​​​​​​時​​​​​​​啟​​​​​​​動​​​​​​​
  • 29 表​​​​​​​示​​​​​​​啟​​​​​​​動​​​​​​​時​​​​​​​的​​​​​​​優​​​​​​​先​​​​​​​順​​​​​​​序​​​​​​​,在​​​​​​​這​​​​​​​例​​​​​​​子​​​​​​​中​​​​​​​,這​​​​​​​個​​​​​​​ script 會​​​​​​​在​​​​​​​開​​​​​​​機​​​​​​​時​​​​​​​,GFS2 的​​​​​​​ init script 執​​​​​​​行​​​​​​​後​​​​​​​啟​​​​​​​動​​​​​​​,啟​​​​​​​動​​​​​​​的​​​​​​​優​​​​​​​先​​​​​​​順​​​​​​​序​​​​​​​為​​​​​​​ 26
  • 73 是​​​​​​​停​​​​​​​止​​​​​​​時​​​​​​​的​​​​​​​優​​​​​​​先​​​​​​​順​​​​​​​序​​​​​​​,在​​​​​​​這​​​​​​​例​​​​​​​子​​​​​​​裡​​​​​​​ script 會​​​​​​​在​​​​​​​關​​​​​​​機​​​​​​​程​​​​​​​序​​​​​​​進​​​​​​​行​​​​​​​時​​​​​​​、​​​​​​​GFS2 script 之​​​​​​​前​​​​​​​停​​​​​​​止​​​​​​​,停​​​​​​​止​​​​​​​時​​​​​​​的​​​​​​​優​​​​​​​先​​​​​​​順​​​​​​​序​​​​​​​為​​​​​​​ 74
這​​​​​​​啟​​​​​​​動​​​​​​​與​​​​​​​停​​​​​​​止​​​​​​​的​​​​​​​值​​​​​​​表​​​​​​​示​​​​​​​您​​​​​​​可​​​​​​​以​​​​​​​手​​​​​​​動​​​​​​​執​​​​​​​行​​​​​​​這​​​​​​​些​​​​​​​工​​​​​​​作​​​​​​​,方​​​​​​​法​​​​​​​是​​​​​​​執​​​​​​​行​​​​​​​ service start 與​​​​​​​ service stop 指​​​​​​​令​​​​​​​。​​​​​​​舉​​​​​​​例​​​​​​​來​​​​​​​說​​​​​​​,如​​​​​​​果​​​​​​​這​​​​​​​ script 的​​​​​​​名​​​​​​​稱​​​​​​​是​​​​​​​ fredwilma,您​​​​​​​可​​​​​​​以​​​​​​​執​​​​​​​行​​​​​​​ service fredwilma start。​​​​​​​
這​​​​​​​個​​​​​​​ script 應​​​​​​​該​​​​​​​放​​​​​​​在​​​​​​​ /etc/init.d 目​​​​​​​錄​​​​​​​裡​​​​​​​,執​​​​​​​行​​​​​​​權​​​​​​​限​​​​​​​與​​​​​​​其​​​​​​​它​​​​​​​在​​​​​​​此​​​​​​​目​​​​​​​錄​​​​​​​的​​​​​​​檔​​​​​​​案​​​​​​​相​​​​​​​同​​​​​​​。​​​​​​​您​​​​​​​接​​​​​​​下​​​​​​​來​​​​​​​可​​​​​​​以​​​​​​​執​​​​​​​行​​​​​​​ chkconfig on 指​​​​​​​令​​​​​​​,連​​​​​​​結​​​​​​​這​​​​​​​個​​​​​​​ script 到​​​​​​​所​​​​​​​指​​​​​​​稱​​​​​​​的​​​​​​​ runlevel。​​​​​​​舉​​​​​​​例​​​​​​​來​​​​​​​說​​​​​​​,如​​​​​​​果​​​​​​​這​​​​​​​個​​​​​​​ script 名​​​​​​​為​​​​​​​ fredwilma,您​​​​​​​就​​​​​​​可​​​​​​​以​​​​​​​執​​​​​​​行​​​​​​​ chkconfig fredwilma on。​​​​​​​

#!/bin/bash
#
# chkconfig: 345 29 73
# description: mount/unmount my custom bind mounts onto a gfs2 subdirectory
#
#
### BEGIN INIT INFO
# Provides: 
### END INIT INFO

. /etc/init.d/functions
case "$1" in
  start)
	# In this example, fred and wilma want their home directories
	# bind-mounted over the gfs2 directory /mnt/gfs2a, which has
	# been mounted as /mnt/gfs2a
	mkdir -p /mnt/gfs2a/home/fred &> /dev/null
	mkdir -p /mnt/gfs2a/home/wilma &> /dev/null
	/bin/mount --bind /mnt/gfs2a/home/fred /home/fred
	/bin/mount --bind /mnt/gfs2a/home/wilma /home/wilma
        ;;

  stop)
	/bin/umount /mnt/gfs2a/home/fred
	/bin/umount /mnt/gfs2a/home/wilma
        ;;

  status)
        ;;

  restart)
        $0 stop
        $0 start
        ;;

  reload)
        $0 start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|reload|status}"
        exit 1
esac

exit 0