Config.xml
-
Can someone explain the proper/clean way to update the config.xml file in pfsense? I see some code meant to handle the XML, and i see write_config(), but I'm having a hard time putting it all together. A brief tour of the componants and a quick example would help me out immensely.
Thanks in advance!
-
Use the webGUI - every time you press Save the code there updates config.xml with the settings. Then config.xml is used to generate all the various conf files and pf ruleset… and the "Save" button code will signal or restart whatever is needed to implement the changes on-the-fly.
-
If you need to do it from the cli try:
/etc/rc.reload_all
Steve
-
@efk:
Can someone explain the proper/clean way to update the config.xml file in pfsense? I see some code meant to handle the XML, and i see write_config(), but I'm having a hard time putting it all together. A brief tour of the componants and a quick example would help me out immensely.
Thanks in advance!
From what I understand:
- conf_mount_rw(); For making nanobsd read/write
- global $config; For manipulating the config
- $config['installedpackages']['packagename]['a']['b']['something'] = "something"; Array elements
- write_config(); To write the config
- conf_mount_ro(); For moving nanobsd back to read only
Example Code
function create_vnstati_image() { conf_mount_rw(); global $config; $iface = $_POST['vnstat_interface']; $ifaces_final = convert_friendly_interface_to_real_interface_name($iface); $config['installedpackages']['vnstat2']['config'][0]['vnstat_interface'] = $ifaces_final; exec("/usr/local/bin/vnstati -i ". $ifaces_final ." -vs -o /tmp/newpicture1.png"); exec("/usr/local/bin/vnstati -i ". $ifaces_final ." -m -o /tmp/newpicture2.png"); exec("/usr/local/bin/vnstati -i ". $ifaces_final ." -d -o /tmp/newpicture3.png"); exec("/usr/local/bin/vnstati -i ". $ifaces_final ." -t -o /tmp/newpicture4.png"); write_config(); conf_mount_ro(); }
Matching Example XML
<installedpackages><vnstat2><config><monthrotate>1</monthrotate> <vnstat_interface>em0</vnstat_interface> <vnstat_phpfrontend>on</vnstat_phpfrontend> <vnstat_interface2>em1</vnstat_interface2></config></vnstat2></installedpackages>
I think we multiple tags that are in the same xml hierarchical level you would then use an index element in the array
$config['installedpackages']['packagename]['testpackage']['config'][0]['something'] = "something in config 0";
$config['installedpackages']['packagename]['testpackage']['config'][1]['something'] = "something in config 1";
$config['installedpackages']['packagename]['testpackage']['config'][2]['something'] = "something in config 2";
<installedpackages><testpackage><config><something>something in config 0</something></config> <config><something>something in config 1</something></config> <config><something>something in config 2</something></config></testpackage></installedpackages>