為ROS建立的資源自動掛載資料盤

問樵發表於2016-09-01

為ROS建立的資源自動掛載資料盤

通過ROS可以方便建立,管理資源。例如自動掛載資料盤,如果是通過阿里雲的官方映象,使用ROS建立ECS資源,UserData可以很容易的幫你完成這一步,詳細內容可以參考這篇文章;但是如果你使用自己製作的映象,而且自己製作的映象不支援UserData功能怎麼辦呢,這篇文章幫你解決你的問題。

要完成自有映象自動掛載資料盤,只需要三步:

1.製作映象前,把下面的這個指令碼拷貝到你將要製作映象的ECS機器上,例如:/usr/local/bin/mount_disks下面。

 #!/bin/sh
set -e

logs=/root/mount_logs
mount_point_prefix="/data"

part_disk () {
    raw_disk=$1
    echo "Create partation table for $raw_disk" >> $logs
    fdisk -S 56 $raw_disk <<ESXU
n
p
1


w
ESXU
}

format_disk()
{
    local disk=$1
    mkfs.ext4 $disk
    if [ "$?" = "0" ];then
        echo "${disk} is formated!" >> $logs
    else
        echo "Fail to format ${disk}" >> $logs
    fi
}

mount_disk()
{
    local disk=$1
    local mount_point=$2

    if [ ! -d $mount_point ]; then
        mkdir $mount_point
    fi
    result=`awk -v disk=$disk `BEGIN {count=0;} {if ($1 == disk) {count=count+1;}} END{print count;}` /etc/fstab`
    if [ $result -eq 0 ]; then
        echo "$disk         $mount_point       ext4       defaults        0 0" >> /etc/fstab
    fi
    mount $disk $mount_point
    if [ $? -ne 0 ]; then
        echo "Fail to mount $disk on $mount_point" >> $logs
    fi
    chmod -R 777 $mount_point
    echo "$disk is mounted to ${mount_point}!" >> $logs
}

handle_raw_disk()
{
    local raw_disks="`fdisk -l 2>&1 | grep "doesn`t contain" | awk -F" " `{print $2}``"
    local disk=""
    local mount_point=""

    echo "handle_raw_disk raw_disks $raw_disks" >> $logs
    for raw_disk in $raw_disks
    do
        part_disk $raw_disk

        disk=${raw_disk}1
        format_disk $disk

        mount_point="${mount_point_prefix}_`echo $raw_disk | awk -F `/` `{print $3}``1"
        mount_disk $disk $mount_point
    done
}

handle_data_disk()
{
    local all_disks=""
    local all_mounted_disks=""
    local need_mounted_disks=""
    local disk=""
    local mount_point=""

    for disk_name in "xvd" "vd"
    do
        all_disks="`cat /proc/partitions | grep -E "${disk_name}[a-z].[0-9]*" | awk -F " " `{print $4}`` $all_disks"
        all_mounted_disks="`df -h | grep ${disk_name}* | awk -F " " `{print $1}` | awk -F `/` `{print $3}`` $all_mounted_disks"
    done

    echo "handle_data_disk all_disks $all_disks" >> $logs
    echo "handle_data_disk all_mounted_disks $all_mounted_disks" >> $logs
    for raw_disk in $all_disks
    do
        local found=`false`
        for disk in $all_mounted_disks
        do
             if [ "$disk" = "$raw_disk" ]; then
                 found=`true`
                 break
             fi
        done

        if [ "$found" = "false" ]; then
            full_name="`fdisk -l | grep $raw_disk | awk -F ` ` `{print $1}``"
            need_mounted_disks="$need_mounted_disks $full_name"
        fi
    done

    echo "handle_data_disk need_mounted_disks $need_mounted_disks" >> $logs
    for disk in $need_mounted_disks
    do
        mount_point="${mount_point_prefix}_`echo $disk | awk -F `/` `{print $3}``"
        mount_disk $disk $mount_point
    done
}

if [ ! -f $logs ]; then
    touch $logs
fi

export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"


do_start() {
    echo "================SATRT=================" >> $logs
    echo "`date` try to mount data disks..." >> $logs
    handle_raw_disk
    handle_data_disk
    echo "=================END==================" >> $logs
    return 0
}


case "$1" in
    start)
        do_start
        ;;
    restart|reload|force-reload)
        echo "Error: argument `$1` not supported" >&2
        exit 3
        ;;
    stop)
        ;;
    *)
        echo "Usage: $0 start|stop" >&2
        exit 3
        ;;
esac

2.修改/etc/rc.local,加入下面這句:

   /usr/lcoal/bin/mount_disks start

這一句意思是在映象啟動的時候執行mount_disks指令碼去掛載資料盤。

3.然後製作自己的映象

4.使用ROS模版建立ECS時,引用自己製作的映象,並給ECS建立資料盤。當ROS建立成功後,資料盤就可以被自動掛載。具體的模版可以參考這裡


相關文章