firstboot script 类似功能实现

#!/bin/bash

echo "fdisk success"

-sed -i '/fdisk.sh/d'  /etc/rc.d/rc.local
+sed -i 's/fdisk.sh/reboot.sh/'  /etc/rc.d/rc.local
+ reboot

主要是格式化磁盘,写在 /etc/rc.local 删除不彻底(/etc/rc.local 是links,用sed 修改文件时 把/etc/rc.local 变成regular file)  这个回头找个时间再看看sed实现原理,运行脚本是执行完后 从rc.d/rc.local 中去掉 /opt/fdisk.sh 

#!/bin/bash

-if [ ! -f /opt/fdisk ]
-then
-  echo "fdisk not exist"
-  touch /opt/fdisk
-  echo "reboot"
-else
  -echo "fdisk is exist" 
  touch /opt/testNB
  sed -i '/reboot.sh/d'  /etc/rc.d/rc.local
-fi

这一步 先test -f /opt/fdisk 文件存在,则执行mkfs.ext4 等操作, 这个文件本身是不存在的,所以先利用这一点 创建fdisk 文件,再reboot, 启动后,会在执行这个脚本,由于文件fdisk 存在了,就开始执行下面的操作。

我是写了个/opt/testNB的文件,最后都搞定了,就需要把reboot.sh 这个脚本干掉。

[root@BJ-W-2101 opt]# cat /etc/rc.d/rc.local 
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
/opt/fdisk.sh
-/opt/reboot.sh


下面是在开机把salt-minion id的 信息添加到/etc/salt/minion中

#!/bin/bash

if [ -z `grep "^id:" /etc/salt/minion ` ]
then
 echo "id is not found"
 ip=`cat /etc/sysconfig/network-scripts/ifcfg-eth0|awk -F"=" '/IPADDR/{print $2}'|tr -d '"'`
 echo "id: $ip" >> /etc/salt/minion
 /etc/init.d/salt-minion restart
else
 echo "id is exist in file"
fi
sed -i '/salt.sh/d'  /etc/rc.d/rc.local

格式化磁盘脚本 开机自动格式化磁盘,生成/dev/vda5 挂载到/export上

#!/bin/bash
# script name fdisk.sh
# create user: davis.ying@gmail.com
fdisk -cu /dev/vda <<ESXU
n 
e


n


w
ESXU
sed -i 's/fdisk.sh/new.sh/'  /etc/rc.d/rc.local
reboot

#!/bin/bash
# script name: new.sh
mkfs.ext4 /dev/vda5

#mount -t ext4 /dev/vda5 /export

uuid=`blkid /dev/vda5|awk -F'"' '{print $2}'`

echo "UUID=$uuid /export                 ext4    defaults      1 2" >>/etc/fstab

mount -a

sed -i '/new/d' /etc/rc.d/rc.local

[root@ export]# cat /etc/rc.local 
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
/opt/fdisk.sh


转载于:https://my.oschina.net/davisqi/blog/295623

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值