Shell rc d script using awk or sed

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
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"
 
Not 100% clear what the problem is, and it's hard to debug because you don't give us exact error messages, only "doesn't work on boot".
Here is an educated guess: You may have installed with /usr not being in the root file system. The executables for awk and sed are in /usr/bin. Maybe your script runs before the other file systems are mounted? In theory, if you say "Require: FILESYSTEMS" in your rc script, the normal file systems should be mounted first. But you spell that as "FILESYSTEM" (without the last "S"), and I don't think that will work.

Completely different question. Why do you use awk in your rc script? As far as I can see, you use it in one place, to take two variables $jail and $ljaill, and construct a .conf file from that. But for that construction you don't actually need awk, it could all be done within the shell, by processing the input file within a shell loop, and putting echo statements in there. Personally, I would simplify the rc script to not use any external programs (like head, grep, sed, awk, ...).
 
Hi ralphbsz,

Thanks for your reply.

Sorry if I was not clear. When I said it is not working, I mean I could run the script on my test with it on /usr/local/sbin and it add the lines on conf file. But when I move it to /usr/local/etc/rc.d/ it runs except the insert lines.

I thought about the awk not being loaded, was my first assumption. I tried usr on REQUIRE but no success before posting. Also searched on net before posting because I was thinking something simple that I was doing wrong.

I will try back to this and make this work since I terminated my test server.

About using awk, yes I am trying to insert jail name and ip. Also ljail to not overwrite when insert others jail lines.

After posted, I happened to arrive at this thread https://forums.freebsd.org/threads/rc-d-script-to-start-a-sh-script.63178/

It did not showed on my searches before, and I will try using it separate and also your sugestion. I did try once separating and only calling command but no success.

Edit: I will also check the missing S on FILESYSTEMS.

Edit 1: why using awk.

Thanks
 
Back
Top