Here is a iSCSI init script for CentOS 5.x. I presume it will work on all flavors of Linux, but haven’t tested. This init script is the original init script but modified to support checking for MySQL databases that might be using the iSCSI mounted disk. So we do not want to umount the iSCSI disk while MySQL is running. I also added checks to see if iscsid is actually running, if it is then make sure you are not already logged in. If you are then don’t try to login again. Also once iSCSI is successfully started we want to mount the iSCSI disk so it is available.
*NOTE: Make sure to adjust your boot init setup. Normally mysql will start before iscsi and iscsid EXCEPT for init 5. Which by default init 5 is the only level which will start mysqld. If you have changed this or yours if not the same make sure you account for this.
chkconfig --list | egrep "iscsi|mysql"
iscsi 0:off 1:off 2:off 3:on 4:on 5:on 6:off
iscsid 0:off 1:off 2:off 3:on 4:on 5:on 6:off
mysqld 0:off 1:off 2:off 3:off 4:off 5:on 6:off
init level 3
ls -l /etc/rc.d/rc3.d/ | egrep -i "mysql|iscsi" | awk '{print $9, $10, $11, $12;}'
K36mysqld -> ../init.d/mysqld
S07iscsid -> ../init.d/iscsid
S13iscsi -> ../init.d/iscsi
init level 5
ls -l /etc/rc.d/rc5.d/ | egrep -i "mysql|iscsi" | awk '{print $9, $10, $11, $12;}' S07iscsid -> ../init.d/iscsid S13iscsi -> ../init.d/iscsi S64mysqld -> ../init.d/mysqld
/etc/init.d/iscsi
#!/bin/sh # # chkconfig: 345 13 89 # description: Logs into iSCSI targets needed at system startup # # Source function library. . /etc/init.d/functions PATH=/sbin:/bin:/usr/sbin:/usr/bin RETVAL=0 start() { status iscsid > /dev/null 2>&1 RETVAL=$? if [ $RETVAL -ne 0 ]; then /etc/init.d/iscsid start fi status iscsid > /dev/null 2>&1 RETVAL=$? # # Verify iscsi is logged in to the iscsi disk # and iscsid is actually running. # iscsiadm -m session -R > /dev/null 2>&1 iscsiadmcheck=$? if [ $RETVAL -eq 0 ]; then if [ $iscsiadmcheck -ne "0" ]; then echo -n $"Setting up iSCSI targets: " # this script is normally called from startup so log into # nodes marked node.startup=automatic iscsiadm -m node --loginall=automatic touch /var/lock/subsys/iscsi success echo sleep 5 mount -a -O _netdev else action $"iscsid: " /bin/true echo "iscsi: [ is running ]" fi else action $"Starting iscsid: " /bin/false RETVAL=1 fi } stop() { rm -f /var/lock/subsys/iscsi # If this is a final shutdown/halt, do nothing since # lvm/dm, md, power path, etc do not always handle this if [ "$RUNLEVEL" = "6" -o "$RUNLEVEL" = "0" -o "$RUNLEVEL" = "1" ]; then success return fi # Do NOT turn off iscsi if root is possibly on a iscsi disk rootopts=$(awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $4; }}' /etc/mtab) if [[ "$rootopts" =~ "_netdev" ]] ; then action $"Stopping iscsid: " /bin/false echo $"Can not shutdown iSCSI. Root is on a iSCSI disk." exit 1 fi # # MySQL checking. If MySQL loads a DB from an iscsi disk we need to make sure # MySQL is NOT running before we shutdown iscsi and umount the iscsi disk. # mysqlopts=$(awk '{ if ( $1 ~ /^datadir/ ) { gsub("datadir=", "", $1); print $1; } }' /etc/my.cnf) iscsiopts=$(awk -v mysqlopts=$mysqlopts '{ if ($1 !~ /^[ \t]*#/ && $2 != "/" && mysqlopts ~ $2 ) { print $4; }}' /etc/mtab) /etc/init.d/mysqld status > /dev/null 2>&1 mysqlcheck=$? if [[ "$iscsiopts" =~ "_netdev" ]] ; then if [ "$mysqlcheck" -eq 0 ]; then action $"Stopping iscsid: " /bin/false echo "Can not shutdown iSCSI. MySQL is mounted on a iSCSI disk." exit 1 fi fi iscsiadm -m node --logoutall=all /etc/init.d/iscsid stop success status iscsid > /dev/null 2>&1 RETVAL=$? iscsiadm -m session -R > /dev/null 2>&1 iscsiadmcheck=$? if [[ "$iscsiadmcheck" -ne 0 && $RETVAL -ne 0 ]]; then umount -a -O _netdev else action $"Stopping iscsid: " /bin/false echo "iSCSI did NOT logout successfully" RETVAL=1 fi } case "$1" in start) start ;; stop) stop ;; restart) stop start ;; status) status iscsid RETVAL=$? ;; condrestart) [ -f /var/lock/subsys/iscsi ] && restart ;; *) echo $"Usage: $0 {start|stop|restart|status|condrestart}" exit 1 esac exit $RETVAL
/etc/init.d/iscsid
#!/bin/sh # # chkconfig: 345 7 89 # description: Starts and stops the iSCSI daemon. # # processname: iscsid # pidfile: /var/run/iscsid.pid # config: /etc/iscsi/iscsid.conf # Source function library. . /etc/init.d/functions PATH=/sbin:/bin:/usr/sbin:/usr/bin RETVAL=0 start() { echo -n $"Turning off network shutdown. " # we do not want iscsi or network to run during system shutdown # incase there are RAID or multipath devices using # iscsi disks chkconfig --level 06 network off rm /etc/rc0.d/*network rm /etc/rc6.d/*network echo -n $"Starting iSCSI daemon: " modprobe -q iscsi_tcp modprobe -q ib_iser daemon iscsid RETVAL=$? echo [ $RETVAL -eq 0 ] || return touch /var/lock/subsys/iscsid success echo sleep 5 } stop() { rm -f /var/lock/subsys/iscsid # If this is a final shutdown/halt, do nothing since # we may need iscsid for as long as possible (halt script kills # us at the last second) if [ "$RUNLEVEL" = "6" -o "$RUNLEVEL" = "0" -o "$RUNLEVEL" = "1" ]; then success return fi # don't turn off iscsi if root is possibly on a iscsi disk rootopts=$(awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $4; }}' /etc/mtab) if [[ "$rootopts" =~ "_netdev" ]] ; then echo $"Can not shutdown iSCSI. Root is on a iSCSI disk." exit 1 fi mysqlopts=$(awk '{ if ( $1 ~ /^datadir/ ) { gsub("datadir=", "", $1); print $1; } }' /etc/my.cnf) iscsiopts=$(awk -v mysqlopts=$mysqlopts '{ if ($1 !~ /^[ \t]*#/ && $2 != "/" && mysqlopts ~ $2 ) { print $4; }}' /etc/mtab) /etc/init.d/mysqld status > /dev/null 2>&1 mysqlcheck=$? if [[ "$iscsiopts" = "rw,_netdev" ]] ; then if [ "$mysqlcheck" -eq 0 ]; then echo "Can not shutdown iSCSI. MySQL is on a iSCSI disk." exit 1 fi fi echo -n $"Stopping iSCSI daemon: " # iscsid does not have a nice shutdown process. # It really should never be stopped #pkill -KILL iscsid killproc iscsid echo modprobe -r ib_iser 2>/dev/null modprobe -r iscsi_tcp 2>/dev/null } restart() { stop start } case "$1" in start) start ;; stop) stop ;; restart) stop start ;; status) status iscsid RETVAL=$? ;; condrestart) [ -f /var/lock/subsys/iscsid ] && restart ;; *) echo $"Usage: $0 {start|stop|restart|status|condrestart}" exit 1 esac exit $RETVAL