sysrc prevents the same value from being added if already there.

Hello,
I am trying to use sysrc for adding bridge members
sysrc ifconfig_bridge0+="addm tap0" but unfortunately
sysrc prevents the same value from being added if already there.

I am making simple script for bhyve so all I need to do is to add values like addm tap0 to already exist line ifconfig_bridge0="addm em0" in /etc/rc.conf
is there is any way with sysrc or even echo, sed, etc...

Thanks,
Amr
 
Not an answer but I guess the idea of rc.conf is something which is the same on each reboot. So just edit it with your editor.
For all the rest a script file can be used to create bridges when you need them.
 
  • Thanks
Reactions: amr
echo 'ifconfig_bridge0+="addm tap0"' >>/etc/rc.conf
This will append the file. Notice the quote and double quotes. They are needed.
 
echo will not work, because it will just add a new line and sysrc don't allow to repeat a value for example if I try sysrc ifconfig_bridge0="addm em0 addm tap0"sysrc will automatically remove the second "addm"
 
It won't remove duplicate "addm"
1590864253419.png
 
Not an answer but I guess the idea of rc.conf is something which is the same on each reboot. So just edit it with your editor.
For all the rest a script file can be used to create bridges when you need them.
There are a few common misconceptions about rc.conf and friends (and Alain's post above is not wrong, it just doesn't help clarify the misconceptions):

  1. rc.conf is only executed during boot. No, it is also executed every time a service is started, stopped, or its status queried. So for example, just saying "service ntpd status" will execute rc.conf. Now, I don't happen to know anything in the FreeBSD base system that executes service commands frequently ... but that doesn't mean that there isn't something, in particular something that was added. This means that one has to be very careful when editing it, so it is always in good shape. That's a lot like the Consistency property of databases (the "C" in ACID): Executing rc.conf has to bring the system from one valid state to another valid state, and in particular, must not crash. So no leaving half-finished lines in it while editing; instead replace it completely with a new good copy.

    Fortunately, all common editors write a complete new copy when saving, so editing it in vi, emacs, ee, nano and such is perfectly fine, just make sure to only save the file when it is ready.
  2. rc.conf is executed to have direct tangible effects. No, it is only executed to set configuration variables (thence the name "conf"). While it is possible to put statements that do something in there, it is a very bad idea, because it will be run at unpredictable times.
Personally, I never use sysrc, and I just edit the file with emacs ... and yes, I've screwed it up and have had to go into single-user mode and fix things. Editing it with scripts seems like playing with fire, unless one is really good with scripts.
 
if we're going to be pedantic, rc.conf is never executed. The correct phrase is that startup scripts, the service script are reading rc.conf (and a few other files) to get configuration variables out of it when they are executed.
 
@VladiBG
sysrc ifconfig_bridge0+="addm tap1" you forgot to add + to add a value

@ralphbsz
Hi, I am also using text editors to edit /etc/rc.conf but I am writing simple shell script and I am using sysrc to add values automatically

I will try to do it by grep and awk if I succeeded I will post it immediately
 
if we're going to be pedantic, rc.conf is never executed. The correct phrase is that startup scripts, the service script are reading rc.conf (and a few other files) to get configuration variables out of it when they are executed.
Ha! Try entering service postgres start into rc.conf and watch your system fork bomb a few minutes after booting (don't ask how I know this).
rc.conf is sourced by the scripts, not parsed, therefore it is executed.
 
sysrc ifconfig_bridge0="`sysrc -n ifconfig_bridge0` addm em0 addm tap0"

The principle works. It's just, I have added too much addm.

sysrc ifconfig_bridge0="`sysrc -n ifconfig_bridge0` addm tap0"

addm is well added despite it's already present in ifconfig_bridge0. Wasn't that the question?
 
amr when you are using "+=" it will add additional value (appending) to existing variable without overwrite it but it will prevent duplicate values
when you are using it without "+=" it will overwrite the entire string if exist or create a new one at the end of the rc.conf file. It's not posible to use use "+" when you want to add same string as "addm" into the variable that's why you can't use it with "+="

For example:

In your rc.conf file if you have

cloned_interfaces="tun0"

And you want to add gif0 then you can use:

sysrc cloned_interfaces+=" gif0"

It will add the gif0 to the existing variable and inside your rc.conf it will look like this
cloned_interfaces="tun0 gif0"
IT will prevent you to add the same value multiple times if it exist so if you repeat sysrc cloned_interfaces+=" gif0" nothing will happen.

OR you can replace the entire string without using "+=" like this

sysrc cloned_interfaces="tun0 gif0"
This will replace the existing cloned_interfaces variable if there's any in your rc.conf

If you want to subtract an existing value you can use "-="

For more information how to use sysrc read the man page
 
Back
Top