设置开机ceph自动map rbd块设备

1. 先下载init-rbdmap到/etc/init.d目录:

wget https://raw.github.com/ceph/ceph/a4ddf704868832e119d7949e96fe35ab1920f06a/src/init-rbdmap 
-O /etc/init.d/rbdmap
下载的原始文件有些错误,打开此文件:
vim /etc/init.d/rbdmap

#!/bin/bash
#chkconfig: 2345 80 60
#description: start/stop rbdmap
### BEGIN INIT INFO
# Provides:          rbdmap
# Required-Start:    $network
# Required-Stop:     $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Ceph RBD Mapping
# Description:       Ceph RBD Mapping
### END INIT INFO

DESC="RBD Mapping"
RBDMAPFILE="/etc/ceph/rbdmap"

. /lib/lsb/init-functions
#. /etc/redhat-lsb/lsb_log_message,加入此行后不正长
do_map() {
	if [ ! -f "$RBDMAPFILE" ]; then
		log_warning_msg "$DESC : No $RBDMAPFILE found."
		exit 0
	fi

	log_daemon_msg "Starting $DESC"
	# Read /etc/rbdtab to create non-existant mapping
	newrbd=
	RET=0
	while read DEV PARAMS; do
		case "$DEV" in
		  ""|\#*)
			continue
			;;
		  */*)
			;;
		  *)
			DEV=rbd/$DEV
			;;
		esac
		OIFS=$IFS
		IFS=','
		for PARAM in ${PARAMS[@]}; do
			CMDPARAMS="$CMDPARAMS --$(echo $PARAM | tr '=' ' ')"
		done
		IFS=$OIFS
		if [ ! -b /dev/rbd/$DEV ]; then
			log_progress_msg $DEV
			rbd map $DEV $CMDPARAMS
			[ $? -ne "0" ] && RET=1
			newrbd="yes"
		fi
	done < $RBDMAPFILE
	log_end_msg $RET

	# Mount new rbd
	if [ "$newrbd" ]; then
                log_action_begin_msg "Mounting all filesystems"
		mount -a
		log_action_end_msg $?
	fi
}

do_unmap() {
	log_daemon_msg "Stopping $DESC"
	RET=0
	# Unmap all rbd device
	for DEV in /dev/rbd[0-9]*; do
		log_progress_msg $DEV
		# Umount before unmap
		MNTDEP=$(findmnt --mtab --source $DEV --output TARGET | sed 1,1d | sort -r)
		for MNT in $MNTDEP; do
			umount $MNT || sleep 1 && umount -l $DEV
		done
		rbd unmap $DEV
		[ $? -ne "0" ] && RET=1
	done
	log_end_msg $RET
}


case "$1" in
  start)
	do_map
	;;

  stop)
	do_unmap
	;;

  reload)
	do_map
	;;

  status)
	rbd showmapped
	;;

  *)
	log_success_msg "Usage: rbdmap {start|stop|reload|status}"
	exit 1
	;;
esac

exit 0
运行会提示如下错误:
./rbdmap: line 34: log_progress_msg: command not found
./rbdmap: line 44: log_action_begin_msg: command not found
./rbdmap: line 46: log_action_end_msg: command not found
解决方法是将log_*换成echo,或者直接注释掉。

然后chkconfig --add rbdmap; chkconfig rbdmap on就可以了。

有时候rbdmap服务不会自动启动,可以将rbdmap脚本中的函数拆分出来加入到/etc/init.d/ceph中:将其中的do_map加入到start)中,将do_unmap加入到stop中,将rbd showmapped加入到status)中。这样rbdmap就随ceph一起启动和停止了。

有兴趣的可以研究一下/etc/init.d/ceph脚本:

#!/bin/sh
# Start/stop ceph daemons
# chkconfig: 2345 60 80

### BEGIN INIT INFO
# Provides:          ceph
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Required-Start:    $remote_fs $named $network $time
# Required-Stop:     $remote_fs $named $network $time
# Short-Description: Start Ceph distributed file system daemons at boot time
# Description:       Enable Ceph distributed file system services.
### END INIT INFO
DESC="RBD Mapping"
RBDMAPFILE="/etc/ceph/rbdmap"

. /lib/lsb/init-functions

do_map() {
	if [ ! -f "$RBDMAPFILE" ]; then
		echo "$DESC : No $RBDMAPFILE found."
		exit 0
	fi

	echo "Starting $DESC"
	# Read /etc/rbdtab to create non-existant mapping
	newrbd=
	RET=0
	while read DEV PARAMS; do
		case "$DEV" in
		  ""|\#*)
			continue
			;;
		  */*)
			;;
		  *)
			DEV=rbd/$DEV
			;;
		esac
		OIFS=$IFS
		IFS=','
		for PARAM in ${PARAMS[@]}; do
			CMDPARAMS="$CMDPARAMS --$(echo $PARAM | tr '=' ' ')"
		done
		IFS=$OIFS
		if [ ! -b /dev/rbd/$DEV ]; then
			#log_progress_msg $DEV
			rbd map $DEV $CMDPARAMS
			[ $? -ne "0" ] && RET=1
			newrbd="yes"
		fi
	done < $RBDMAPFILE
	#echo $RET

	# Mount new rbd
	if [ "$newrbd" ]; then
                echo "Mounting all filesystems"
		mount -a
		#log_action_end_msg $?
	fi
}

do_unmap() {
	echo "Stopping $DESC"
	RET=0
	# Unmap all rbd device
	for DEV in /dev/rbd[0-9]*; do
		#log_progress_msg $DEV
		# Umount before unmap
		MNTDEP=$(findmnt --mtab --source $DEV --output TARGET | sed 1,1d | sort -r)
		for MNT in $MNTDEP; do
			umount $MNT || sleep 1 && umount -l $DEV
		done
		rbd unmap $DEV
		[ $? -ne "0" ] && RET=1
	done
	#log_end_msg $RET
}


# if we start up as ./mkcephfs, assume everything else is in the
# current directory too.
if [ `dirname $0` = "." ] && [ $PWD != "/etc/init.d" ]; then
    BINDIR=.
    SBINDIR=.
    LIBDIR=.
    ETCDIR=.
else
    BINDIR=/usr/bin
    SBINDIR=/usr/sbin
    LIBDIR=/usr/lib64/ceph
    ETCDIR=/etc/ceph
fi

usage_exit() {
    echo "usage: $0 [options] {start|stop|restart|condrestart} [mon|osd|mds]..."
    printf "\t-c ceph.conf\n"
    printf "\t--valgrind\trun via valgrind\n"
    printf "\t--hostname [hostname]\toverride hostname lookup\n"
    exit
}

# behave if we are not completely installed (e.g., Debian "removed,
# config remains" state)
test -f $LIBDIR/ceph_common.sh || exit 0

. $LIBDIR/ceph_common.sh

EXIT_STATUS=0

# detect systemd
SYSTEMD=0
grep -qs systemd /proc/1/comm && SYSTEMD=1

signal_daemon() {
    name=$1
    daemon=$2
    pidfile=$3
    signal=$4
    action=$5
    [ -z "$action" ] && action="Stopping"
    echo -n "$action Ceph $name on $host..."
    do_cmd "if [ -e $pidfile ]; then
        pid=`cat $pidfile`
        if [ -e /proc/\$pid ] && grep -q $daemon /proc/\$pid/cmdline ; then
	    cmd=\"kill $signal \$pid\"
	    echo -n \$cmd...
	    \$cmd
        fi
    fi"
    echo done
}

daemon_is_running() {
    name=$1
    daemon=$2
    daemon_id=$3
    pidfile=$4
    do_cmd "[ -e $pidfile ] || exit 1   # no pid, presumably not running
	pid=\`cat $pidfile\`
	[ -e /proc/\$pid ] && grep -q $daemon /proc/\$pid/cmdline && grep -qwe -i.$daemon_id /proc/\$pid/cmdline && exit 0 # running
        exit 1  # pid is something else" "" "okfail"
}

stop_daemon() {
    name=$1
    daemon=$2
    pidfile=$3
    signal=$4
    action=$5
    [ -z "$action" ] && action="Stopping"
    echo -n "$action Ceph $name on $host..."
    do_cmd "while [ 1 ]; do
	[ -e $pidfile ] || break
	pid=\`cat $pidfile\`
	while [ -e /proc/\$pid ] && grep -q $daemon /proc/\$pid/cmdline ; do
	    cmd=\"kill $signal \$pid\"
	    echo -n \$cmd...
	    \$cmd
	    sleep 1
	    continue
	done
	break
    done"
    echo done
}

## command line options
options=

version=0
dovalgrind=
docrun=
allhosts=0
debug=0
monaddr=
dofsmount=1
dofsumount=0
verbose=0

while echo $1 | grep -q '^-'; do     # FIXME: why not '^-'?
case $1 in
    -v | --verbose)
	    verbose=1
	    ;;
    --valgrind)
	    dovalgrind=1
	    ;;
    --novalgrind)
	    dovalgrind=0
	    ;;
    --allhosts | -a)
	    allhosts=1;
	    ;;
    --restart)
	    docrun=1
	    ;;
    --norestart)
	    docrun=0
	    ;;
    -m )
	    [ -z "$2" ] && usage_exit
	    options="$options $1"
	    shift
	    MON_ADDR=$1
	    ;;
    --btrfs | --fsmount)
	    dofsmount=1
	    ;;
    --nobtrfs | --nofsmount)
	    dofsmount=0
	    ;;
    --btrfsumount | --fsumount)
	    dofsumount=1
	    ;;
    --conf | -c)
	    [ -z "$2" ] && usage_exit
	    options="$options $1"
	    shift
	    conf=$1
	    ;;
    --hostname )
	    [ -z "$2" ] && usage_exit
	    options="$options $1"
	    shift
	    hostname=$1
            ;;
    *)
	    echo unrecognized option \'$1\'
	    usage_exit
	    ;;
esac
options="$options $1"
shift
done

verify_conf

command=$1
[ -n "$*" ] && shift

get_local_name_list
get_name_list "$@"

# Reverse the order if we are stopping
if [ "$command" = "stop" ]; then
    for f in $what; do
       new_order="$f $new_order"
    done
    what="$new_order"
fi

for name in $what; do
    type=`echo $name | cut -c 1-3`   # e.g. 'mon', if $item is 'mon1'
    id=`echo $name | cut -c 4- | sed 's/^\\.//'`
    cluster=`echo $conf | awk -F'/' '{print $(NF)}' | cut -d'.' -f 1`
    num=$id
    name="$type.$id"

    check_host || continue

    binary="$BINDIR/ceph-$type"
    cmd="$binary -i $id"

    get_conf run_dir "/var/run/ceph" "run dir"

    get_conf pid_file "$run_dir/$type.$id.pid" "pid file"

    if [ "$command" = "start" ]; then
	if [ -n "$pid_file" ]; then
	    do_cmd "mkdir -p "`dirname $pid_file`
	    cmd="$cmd --pid-file $pid_file"
	fi

	get_conf log_dir "" "log dir"
	[ -n "$log_dir" ] && do_cmd "mkdir -p $log_dir"

        get_conf auto_start "" "auto start"
        if [ "$auto_start" = "no" ] || [ "$auto_start" = "false" ] || [ "$auto_start" = "0" ]; then
            if [ -z "$@" ]; then
                echo "Skipping Ceph $name on $host... auto start is disabled"
                continue
            fi
        fi

	if daemon_is_running $name ceph-$type $id $pid_file; then
	    echo "Starting Ceph $name on $host...already running"
	    do_map
	    continue
	fi

	get_conf copy_executable_to "" "copy executable to"
	if [ -n "$copy_executable_to" ]; then
	    scp $binary "$host:$copy_executable_to"
	    binary="$copy_executable_to"
	fi
    fi

    # conf file
    cmd="$cmd -c $conf"

    if echo $name | grep -q ^osd; then
	get_conf osd_data "/var/lib/ceph/osd/ceph-$id" "osd data"
	get_conf fs_path "$osd_data" "fs path"  # mount point defaults so osd data
        get_conf fs_devs "" "devs"
	if [ -z "$fs_devs" ]; then
	    # try to fallback to old keys
	    get_conf tmp_btrfs_devs "" "btrfs devs"
	    if [ -n "$tmp_btrfs_devs" ]; then
		fs_devs="$tmp_btrfs_devs"
	    fi
	fi
        first_dev=`echo $fs_devs | cut '-d ' -f 1`
    fi

    # do lockfile, if RH
    get_conf lockfile "/var/lock/subsys/ceph" "lock file"
    lockdir=`dirname $lockfile`
    if [ ! -d "$lockdir" ]; then
	lockfile=""
    fi

    get_conf asok "$run_dir/ceph-$type.$id.asok" "admin socket"

    case "$command" in
	start)
            # Increase max_open_files, if the configuration calls for it.
            get_conf max_open_files "32768" "max open files"

            # build final command
	    wrap=""
	    runmode=""
	    runarg=""

	    [ -z "$docrun" ] && get_conf_bool docrun "0" "restart on core dump"
	    [ "$docrun" -eq 1 ] && wrap="$BINDIR/ceph-run"

	    [ -z "$dovalgrind" ] && get_conf_bool valgrind "" "valgrind"
	    [ -n "$valgrind" ] && wrap="$wrap valgrind $valgrind"

	    [ -n "$wrap" ] && runmode="-f &" && runarg="-f"
	    [ -n "$max_open_files" ] && files="ulimit -n $max_open_files;"

	    if [ $SYSTEMD -eq 1 ]; then
		cmd="systemd-run -r bash -c '$files $cmd --cluster $cluster -f'"
	    else
		cmd="$files $wrap $cmd --cluster $cluster $runmode"
	    fi

	    if [ $dofsmount -eq 1 ] && [ -n "$fs_devs" ]; then
		get_conf pre_mount "true" "pre mount command"
		get_conf fs_type "" "osd mkfs type"

		if [ -z "$fs_type" ]; then
		    # try to fallback to to old keys
		    get_conf tmp_devs "" "btrfs devs"
		    if [ -n "$tmp_devs" ]; then
			fs_type="btrfs"
		    else
		        echo No filesystem type defined!
		        exit 0
                    fi
		fi

		get_conf fs_opt "" "osd mount options $fs_type"
		if [ -z "$fs_opt" ]; then
		    if [ "$fs_type" = "btrfs" ]; then
		        #try to fallback to old keys
			get_conf fs_opt "" "btrfs options"
		    fi

		    if [ -z "$fs_opt" ]; then
			if [ "$fs_type" = "xfs" ]; then
			    fs_opt="rw,noatime,inode64"
			else
		            #fallback to use at least noatime
		            fs_opt="rw,noatime"
			fi
		    fi
		fi

		[ -n "$fs_opt" ] && fs_opt="-o $fs_opt"
		[ -n "$pre_mount" ] && do_cmd "$pre_mount"

		if [ "$fs_type" = "btrfs" ]; then
		    echo Mounting Btrfs on $host:$fs_path
		    do_root_cmd_okfail "modprobe btrfs ; btrfs device scan || btrfsctl -a ; egrep -q '^[^ ]+ $fs_path' /proc/mounts || mount -t btrfs $fs_opt $first_dev $fs_path"
		else
		    echo Mounting $fs_type on $host:$fs_path
		    do_root_cmd_okfail "modprobe $fs_type ; egrep -q '^[^ ]+ $fs_path' /proc/mounts || mount -t $fs_type $fs_opt $first_dev $fs_path"
		fi
		if [ "$ERR" != "0" ]; then
		    EXIT_STATUS=$ERR
		    continue
		fi
	    fi

	    if [ "$type" = "osd" ]; then
		get_conf update_crush "" "osd crush update on start"
		if [ "${update_crush:-1}" = "1" -o "${update_crush:-1}" = "true" ]; then
		    # update location in crush
		    get_conf osd_location_hook "$BINDIR/ceph-crush-location" "osd crush location hook"
		    osd_location=`$osd_location_hook --cluster ceph --id $id --type osd`
		    get_conf osd_weight "" "osd crush initial weight"
		    defaultweight="$(df -P -k $osd_data/. | tail -1 | awk '{ print sprintf("%.2f",$2/1073741824) }')"
		    get_conf osd_keyring "$osd_data/keyring" "keyring"
		    do_cmd "timeout 30 $BINDIR/ceph -c $conf --name=osd.$id --keyring=$osd_keyring osd crush create-or-move -- $id ${osd_weight:-${defaultweight:-1}} $osd_location"
		fi
	    fi

	    echo Starting Ceph $name on $host...
	    mkdir -p $run_dir
	    get_conf pre_start_eval "" "pre start eval"
	    [ -n "$pre_start_eval" ] && $pre_start_eval
	    get_conf pre_start "" "pre start command"
	    get_conf post_start "" "post start command"
	    [ -n "$pre_start" ] && do_cmd "$pre_start"
	    do_cmd_okfail "$cmd" $runarg
	    if [ "$ERR" != "0" ]; then
		EXIT_STATUS=$ERR
	    fi

	    if [ "$type" = "mon" ]; then
		# this will only work if we are using default paths
		# for the mon data and admin socket.  if so, run
		# ceph-create-keys.  this is the case for (normal)
		# chef and ceph-deploy clusters, which is who needs
		# these keys.  it's also true for default installs
		# via mkcephfs, which is fine too; there is no harm
		# in creating these keys.
		get_conf mon_data "/var/lib/ceph/mon/ceph-$id" "mon data"
		if [ "$mon_data" = "/var/lib/ceph/mon/ceph-$id" -a "$asok" = "/var/run/ceph/ceph-mon.$id.asok" ]; then
		    echo Starting ceph-create-keys on $host...
		    cmd2="$SBINDIR/ceph-create-keys -i $id 2> /dev/null &"
		    do_cmd "$cmd2"
		fi
	    fi

	    [ -n "$post_start" ] && do_cmd "$post_start"
	    [ -n "$lockfile" ] && [ "$?" -eq 0 ] && touch $lockfile
	    do_map
	    ;;

	stop)
	    get_conf pre_stop "" "pre stop command"
	    get_conf post_stop "" "post stop command"
	    [ -n "$pre_stop" ] && do_cmd "$pre_stop"
	    stop_daemon $name ceph-$type $pid_file
	    [ -n "$post_stop" ] && do_cmd "$post_stop"
	    [ -n "$lockfile" ] && [ "$?" -eq 0 ] && rm -f $lockfile
	    if [ $dofsumount -eq 1 ] && [ -n "$fs_devs" ]; then
		echo Unmounting OSD volume on $host:$fs_path
		do_root_cmd "umount $fs_path || true"
	    fi
	    do_unmap
	    ;;

	status)
	    if daemon_is_running $name ceph-$type $id $pid_file; then
		echo -n "$name: running "
		do_cmd "$BINDIR/ceph --admin-daemon $asok version 2>/dev/null" || echo unknown
            elif [ -e "$pid_file" ]; then
                # daemon is dead, but pid file still exists
                echo "$name: dead."
                EXIT_STATUS=1
            else
                # daemon is dead, and pid file is gone
                echo "$name: not running."
                EXIT_STATUS=3
            fi
	    rbd showmapped
	    ;;

	ssh)
	    $ssh
	    ;;

	forcestop)
	    get_conf pre_forcestop "" "pre forcestop command"
	    get_conf post_forcestop "" "post forcestop command"
	    [ -n "$pre_forcestop" ] && do_cmd "$pre_forcestop"
	    stop_daemon $name ceph-$type $pid_file -9
	    [ -n "$post_forcestop" ] && do_cmd "$post_forcestop"
	    [ -n "$lockfile" ] && [ "$?" -eq 0 ] && rm -f $lockfile
	    ;;

	killall)
	    echo "killall ceph-$type on $host"
	    do_cmd "pkill ^ceph-$type || true"
	    [ -n "$lockfile" ] && [ "$?" -eq 0 ] && rm -f $lockfile
	    ;;

	force-reload | reload)
	    signal_daemon $name ceph-$type $pid_file -1 "Reloading"
	    ;;

	restart)
	    $0 $options stop $name
	    $0 $options start $name
	    ;;

        condrestart)
            if daemon_is_running $name ceph-$type $id $pid_file; then
                $0 $options stop $name
                $0 $options start $name
            else
                echo "$name: not running."
            fi
            ;;

	cleanlogs)
	    echo removing logs
	    [ -n "$log_dir" ] && do_cmd "rm -f $log_dir/$type.$id.*"
	    ;;

	cleanalllogs)
	    echo removing all logs
	    [ -n "$log_dir" ] && do_cmd "rm -f $log_dir/* || true"
	    ;;

	*)
	    usage_exit
	    ;;
    esac
done

# activate latent osds?
if [ "$command" = "start" -a "$BINDIR" != "." ]; then
    if [ "$*" = "" ] || echo $* | grep -q ^osd\$ ; then
	ceph-disk activate-all
    fi
fi

exit $EXIT_STATUS

你可能感兴趣的:(设置开机ceph自动map rbd块设备)