のぴぴのメモ

自分用のLinuxとかの技術メモ

Fedora/RHEL cloop自動マウント用rcスクリプト

 OS起動停止時に、cloopモジュールロード・アンロードとcloopファイルシステムのマウント・アンマウントを行うrcシェルを作る。
 cloopモジュールの実装は → こちらを参照

1.cloop用シェル

 サンプルシェル(cloop)を以下に示す。

#!/bin/sh
# script to start and stop cloop devices
#        and mount and umount cloop filesystems
#
# chkconfig: 35 60 20
# description: Starts, stops cloop modules
#

# set the maximum number of cloop device
# refer to the MAX_CLOOP in compressed_loop.c
MAX_CLOOP=8

CLOOP_DEV[0]=/dev/cloop0
CLOOP_FIL[0]=/tmp/a.cloop
CLOOP_DIR[0]=/tmp/a

#CLOOP_DEV[1]=/dev/cloop1
#CLOOP_FIL[1]=/dummy1
#CLOOP_DIR[1]=/dummy1

#CLOOP_DEV[2]=/dev/cloop2
#CLOOP_FIL[2]=/dummyr2
#CLOOP_DIR[2]=/dummy2

#CLOOP_DEV[3]=/dev/cloop3
#CLOOP_FIL[3]=/dummy3
#CLOOP_DIR[3]=/dummy3


# Source function library.
. /etc/init.d/functions


function abend(){
  echo ERROR: $@
  failure
  exit 1
}

function load_cloop_module(){

  lsmod |grep cloop > /dev/null
  [ $? -eq 0 ] && (echo "cloop has already been loaded.";return )
  modprobe cloop
  [ $? -ne 0 ] && abend can not load the cloop module. 
 
  i=0; FLAG=FALSE
  while [ $i -lt 10 ]
  do
    sleep 1
    if [ -b "/dev/cloop`expr $MAX_CLOOP - 1`" ]
    then
       FLAG=TRUE; break
    fi
    i=`expr $i + 1`
  done
  [ "$FLAG" != TRUE ] && abend not found the maximum cloop device.

}

function bind_and_mount(){

  # check a cloop device
  [ -b $1 ]     || abend $1 is a invalid cloop device.

  # check a cloop file
  [ -f $2 ]     || abend not found $2 file.
  head -3 $2 | grep cloop 2>&1 > /dev/null
  [ $? -eq 0 ]  || abend $2 is a invalid cloop file.

  # check mount directory
  [ -d $3 ]     || abend $3 is not directory.
 
  losetup $1 $2 || abend can not bind $2 file to $1 device.

  mount -r -t iso9660 $1 $3 || abend can not mount $1 on $3.

}

function start(){
 
  load_cloop_module

  i=0
  while [ -n "${CLOOP_DEV[$i]}" -a \
          -n "${CLOOP_FIL[$i]}" -a \
          -n "${CLOOP_DIR[$i]}" ]
  do
  bind_and_mount ${CLOOP_DEV[$i]} ${CLOOP_FIL[$i]} ${CLOOP_DIR[$i]}
  
  i=`expr $i + 1`
  done
  
  success
  touch /var/lock/subsys/cloop

}

function stop(){

  i=0
  while [ -n "${CLOOP_DEV[$i]}" -a \
          -n "${CLOOP_FIL[$i]}" -a \
          -n "${CLOOP_DIR[$i]}" ]
  do
  umount ${CLOOP_DIR[$i]}
  losetup -d ${CLOOP_DEV[$i]}

  i=`expr $i + 1`
  done
 
  modprobe -r cloop
 
  success
  rm -f /var/lock/subsys/cloop

} 
  

# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart|reload)
        stop
        start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 1
esac

exit 0

※備考

環境に応じて、下記パラメータを設定する。

(a)MAX_CLOOP=8

→cloopデバイスの最大数。compressed_loop.cの"#define CLOOP_MAX"の値を設定する。

(b)CLOOP_DEV[n],CLOOP_FIL[n],CLOOP_DIR[n]

→マウントするcloopの情報。3つ一体で設定。nは0番から順番に設定する。それぞれの変数は以下の通り。
CLOOP_DEV[n] : cloopファイルシステムを割り当てるcloopデバイス。
CLOOP_FIL[n] : マウントするcloopファイルシステム。フルパス(絶対パス)で指定する。
CLOOP_DIR[n] : マウントポイントのディレクトリをフルパスで指定する。

2.シェルの組み込み

(1)上記のサンプルシェルを/etc/init.dに配置する。

# vi /etc/init.d/cloop
# chown root:root /etc/init.d/cloop
# chmod 755 /etc/init.d/cloop

(2)chkconfigコマンドでOS起動停止処理に組み込む

# chkconfig --add cloop
# chkconfig --list|grep cloop

     ※備考
     chkconfigの設定はスクリプトヘッダの下記部分が該当する。

# chkconfig: 35 60 20
# description: Starts, stops cloop modules