为ROS创建的资源自动挂载数据盘

为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创建成功后,数据盘就可以被自动挂载。具体的模版可以参考这里

上一篇:ROS轻松帮你在ECS之间传递SSH Key


下一篇:资源编排(ROS)博文索引