Hi all,
I am trying to use awk in a rc d script to insert multiple lines on a file. It is working running the script on shell but do not running on boot.
- the rc.d script is on /usr/local/etc/rc.d/
- the other part of script works fine on boot, only issue at moment is these part inserting lines on conf file
- the script loads a vnet/netgraph
- it is a #!/bin/sh
- conf file already exist, /etc/jail.conf
When I failed with awk, I tried with sed and echo. Same thing, works on script on shell but not when booting.
I am posting few simple test examples I did to try find my error with no success.
awk test
sed test
echo test
All the above works on command line but not on booting.
full script
I am trying to use awk in a rc d script to insert multiple lines on a file. It is working running the script on shell but do not running on boot.
- the rc.d script is on /usr/local/etc/rc.d/
- the other part of script works fine on boot, only issue at moment is these part inserting lines on conf file
- the script loads a vnet/netgraph
- it is a #!/bin/sh
- conf file already exist, /etc/jail.conf
When I failed with awk, I tried with sed and echo. Same thing, works on script on shell but not when booting.
I am posting few simple test examples I did to try find my error with no success.
awk test
Code:
awk 'NR==13{print "new line text at row awk 13"}1' /etc/jail.conf > tmp && mv tmp /etc/jail.conf
sed test
Code:
sed -i -e '12i\\
test sed' /etc/jail.conf
echo test
Code:
head -n11 /etc/jail.conf > tmp
echo "I'm line 12 now" >> tmp
tail -n +12 /etc/jail.conf >> tmp
mv tmp /etc/jail.conf
All the above works on command line but not on booting.
full script
Code:
#!/bin/sh
# This script loads netgraph for a jail list that is used to name the interface (eiface)
# ether refer to an external interface
# Each jail is assigned to an ipv4 and ipv6
# ljail is the line number in jail.conf where start to insert the jail block
# PROVIDE: ng_ta
# REQUIRE: LOGIN FILESYSTEM
# BEFORE: securelevel jail
. /etc/rc.subr
name="ng_ta"
rcvar=ng_ta_enable
# command="/bin/sh /etc/rc"
# required_files="/bin/sh /etc/rc"
start_cmd="${name}_start"
stop_cmd="${name}_stop"
jail_list="haproxy git"
lnumber=0
ip=1
ngnumber=0
ether="xn0"
ljail=12
ng_ta_start()
{
echo "Start: `date`"
echo "Starting Netgraph Vnet"
echo "Load ng_ether, ${ether}"
# kld_list="ng_ether" to load on boot (/etc/rc.conf)
kldload ng_ether
ngctl msg ${ether}: setpromisc 1
ngctl msg ${ether}: setautosrc 0
echo "Creating a bridge and link to ${ether}"
ngctl mkpeer ${ether}: bridge lower link${lnumber}
# Incremental lnumber
lnumber=$((lnumer+1))
echo "lnumber ${lnumber}"
echo "Change name to ${ether}bridge"
ngctl name ${ether}:lower ${ether}bridge
echo "Connect upper ${ether} to ${ether}bridge"
ngctl connect ${ether}: ${ether}bridge: upper link${lnumber}
# Incremental lnumber
lnumber=$((lnumber+1))
# echo "lnumber ${lnumber}"
for jail in ${jail_list}; do
echo "Creating interface ng0_${jail}"
echo "Create an eiface and connect to ${ether}bridge"
ngctl mkpeer ${ether}bridge: eiface link${lnumber} ether
# Incremental lnumber
lnumber=$((lnumber+1))
# echo "lnumber ${lnumber}"
echo "Change name to ng0_${jail}"
ngctl name ngeth${ngnumber}: ng0_${jail}
ifconfig ngeth${ngnumber} name ng0_${jail}
# Incremental lnumber
ngnumber=$((ngnumber+1))
# echo "ngnumber ${ngnumber}"
echo "Assign ipv4 ipv6 to ng0_${jail}"
ifconfig ng0_${jail} 172.18.0.${ip}/24
ifconfig ng0_${jail} inet6 fd12:f18:c26:4426::${ip} prefixlen 128 alias
# Incremental ip
ip=$((ip+1))
# echo "${ip}"
echo "Inserting ipv4 172.18.0.${ip} ipv6 fd12:f18:c26:4426::${ip} to ${jail}"
# only working on shell, during boot not working
awk -v ljail=$ljail -v jail=$jail -v ip=$ip 'NR==ljail{
print
print jail" {"
print " ip6.addr = \"fd12:f18:c26:4426::"ip"\";"
print " ip4.addr = \"172.18.0."ip"\";"
print "}"
print;
next
}1' /etc/jail.conf > tmp && mv tmp /etc/jail.conf
# space between block jails
ljail=$((ljail+5))
# echo "ljail ${ljail}"
done
echo "Finish: `date`"
}
load_rc_config $name
run_rc_command "$1"