#!/bin/vbash ### BEGIN INIT INFO # Provides: os-conf # Short-Description: The initial os-conf job, config the system. ### END INIT INFO first_partition_dir='/live/image' ## The image must be prepared first with this custom directory in the root of the first partition os_conf_dir=${first_partition_dir}/aliyun_custom_image os_conf_file=${os_conf_dir}/os.conf load_os_conf() { if [[ -f $os_conf_file ]]; then . $os_conf_file return 0 else return 1 fi } cleanup() { ## ensure $os_conf_file is deleted, to avoid repeating the configuration with each boot rm $os_conf_file >& /dev/null } config_password() { ## Alibaba says it's setting the root password in their system creation dialog, but we're ## really setting the password into the vyos user. root should not have a password ## Also, you must use the password option in the setup, not the keyfile option if [[ -n $password ]]; then password=$(echo $password | base64 -d) if [[ $? == 0 && -n $password ]]; then /usr/bin/sg vyattacfg "/opt/vyatta/etc/config/scripts/vyos-command.sh \"system login user vyos authentication plaintext-password $password\"" fi fi } config_hostname() { if [[ -n $hostname ]]; then /usr/bin/sg vyattacfg "/opt/vyatta/etc/config/scripts/vyos-command.sh \"system host-name $hostname\"" fi } config_dns() { if [[ -n $dns_nameserver ]]; then for i in $dns_nameserver; do /usr/bin/sg vyattacfg "/opt/vyatta/etc/config/scripts/vyos-command.sh \"system name-server $i\"" done fi } is_classic_network() { ## I have not tested this on a "Classic" setup, because new users can't setup Classic servers # vpc: eth0 # classic: eth0 eth1 grep -q 'eth1' $os_conf_file } config_network() { config_interface eth0 ${eth0_ip_addr} ${eth0_netmask} ${eth0_mac_addr} ## Don't know how to do this route on VyOS, but the VPC setup only needs the default gateway anyway # config_route eth0 ${eth0_route} config_default_gateway ${eth0_gateway} if is_classic_network ; then config_interface eth1 ${eth1_ip_addr} ${eth1_netmask} ${eth1_mac_addr} # config_route eth1 ${eth1_route} fi } config_interface() { local interface=$1 local ip=$2 local netmask=$3 local cidr=$( mask2cidr $netmask ) local mac=$4 /usr/bin/sg vyattacfg "/opt/vyatta/etc/config/scripts/vyos-command.sh \"interfaces ethernet $interface address $ip/$cidr\"" /usr/bin/sg vyattacfg "/opt/vyatta/etc/config/scripts/vyos-command.sh \"interfaces ethernet $interface mac $mac\"" } config_default_gateway() { local gateway=$1 /usr/bin/sg vyattacfg "/opt/vyatta/etc/config/scripts/vyos-command.sh \"system gateway-address $gateway\"" } ## Don't know how to do the routes like this. Commenting out this subroutine and leaving it with their example ## Also not needed for the VPC setup. Only the default-gateway is needed #config_route() { # local interface=$1 # local route=$2 # route_conf=/etc/sysconfig/network-scripts/route-${interface} # > $route_conf # echo $route | sed 's/;/\n/' | \ # while read line; do # dst=$(echo $line | awk '{print $1}') # gw=$(echo $line | awk '{print $2}') # if ! grep -q "$dst" $route_conf 2> /dev/null; then # echo "$dst via $gw dev $interface" >> $route_conf # fi # if [[ "$dst" == "0.0.0.0/0" ]]; then # config_default_gateway $gw # fi # done #} ## VyOS needs the cidr for the interface setup, but Alibaba only provides the subnet mask. ## I found this subroutine for converting from netmask mask2cidr () { # Assumes there's no "255." after a non-255 byte in the mask local x=${1##*255.} set -- 0^^^128^192^224^240^248^252^254^ $(( (${#1} - ${#x})*2 )) ${x%%.*} x=${1%%$3*} echo $(( $2 + (${#x}/4) )) } ## Do the stuff if load_os_conf ; then config_password config_network config_hostname config_dns cleanup exit 0 else echo "No os.conf file to load. This is okay." exit 0 fi