Skip to content

Instantly share code, notes, and snippets.

@tlyakhov
Last active October 13, 2025 06:19
Show Gist options
  • Select an option

  • Save tlyakhov/15172db645d01dc67a0b585096f28ab3 to your computer and use it in GitHub Desktop.

Select an option

Save tlyakhov/15172db645d01dc67a0b585096f28ab3 to your computer and use it in GitHub Desktop.
single-wan OpnSense HA setup. Put this file into /usr/local/etc/rc.syshook.d/carp on both primary/backup firewalls
#!/usr/local/bin/php
<?php
require_once("config.inc");
require_once("system.inc");
require_once("interfaces.inc");
require_once("util.inc");
$subsystem = !empty($argv[1]) ? $argv[1] : '';
$type = !empty($argv[2]) ? $argv[2] : '';
if ($type != 'MASTER' && $type != 'BACKUP') {
log_error("Carp '$type' event unknown from source '{$subsystem}'");
exit(1);
}
if (!strstr($subsystem, '@')) {
log_error("Carp '$type' event triggered from wrong source '{$subsystem}'");
exit(1);
}
// Add more interfaces that need to be disabled/enabled after a CARP event.
$wan_interfaces = array('wan');
// interfaces object
$conf_ifs = $config['interfaces'];
foreach ($wan_interfaces as $if_name) {
if (!array_key_exists($if_name, $conf_ifs))
continue;
$os_if_name = $conf_ifs[$if_name]['if'];
print "Configuring $if_name ($os_if_name)\n";
if ($type === "MASTER") {
log_error("enable interface '$if_name' due CARP event '$type'");
$config['interfaces'][$if_name]['enable'] = '1';
interfaces_bring_up($if_name);
interface_configure(false, $if_name, true, true);
write_config("enable interface '$if_name' due CARP event '$type'", false);
usleep(200 * 1000);
} else {
log_error("disable interface '$if_name' due CARP event '$type'");
interface_reset($if_name, true);
unset($config['interfaces'][$if_name]['enable']);
interface_configure(false, $if_name, false, false);
exec("/sbin/ifconfig {$os_if_name} down 2>&1", $ifc, $ret);
write_config("disable interface '$if_name' due CARP event '$type'", false);
}
}
?>
@tlyakhov
Copy link
Copy Markdown
Author

tlyakhov commented Jun 8, 2024

Updated again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment