generate_netplan

This commit is contained in:
devdatt 2025-12-23 15:15:57 +05:30
parent 684b07043f
commit e47a390769
1 changed files with 21 additions and 8 deletions

View File

@ -148,14 +148,11 @@ function build_interface(array $cfg, string $type): array
function generate_netplan(array $data, string $iface): array function generate_netplan(array $data, string $iface): array
{ {
$netplan = [ $netplan = [
'network' => [ 'network' => [
'version' => 2, 'version' => 2,
'renderer' => 'networkd', 'renderer' => 'networkd',
'ethernets' => [ 'ethernets' => [],
$iface => new stdClass() // base NIC only
],
'vlans' => [] 'vlans' => []
] ]
]; ];
@ -168,31 +165,47 @@ function generate_netplan(array $data, string $iface): array
$vlan = trim($data['primary']['network_primary_vlan'] ?? ''); $vlan = trim($data['primary']['network_primary_vlan'] ?? '');
if ($vlan !== '') { if ($vlan !== '') {
/* VLAN configuration */
$netplan['network']['ethernets'][$iface] = new stdClass();
$netplan['network']['vlans']["{$iface}.{$vlan}"] = $netplan['network']['vlans']["{$iface}.{$vlan}"] =
array_merge( array_merge(
['id' => (int)$vlan, 'link' => $iface], ['id' => (int)$vlan, 'link' => $iface],
build_interface($data['primary'], 'primary') build_interface($data['primary'], 'primary')
); );
} else {
/* NO VLAN → configure base NIC */
$netplan['network']['ethernets'][$iface] =
build_interface($data['primary'], 'primary');
} }
} }
/* ---------- SECONDARY ---------- */ /* ---------- SECONDARY (only if primary not configured on base) ---------- */
if ( if (
!isset($netplan['network']['ethernets'][$iface]) &&
(
$data['secondary']['mode'] !== 'disabled' || $data['secondary']['mode'] !== 'disabled' ||
$data['secondary']['modev6'] !== 'disabled' $data['secondary']['modev6'] !== 'disabled'
)
) { ) {
$vlan = trim($data['secondary']['network_secondary_vlan'] ?? ''); $vlan = trim($data['secondary']['network_secondary_vlan'] ?? '');
if ($vlan !== '') { if ($vlan !== '') {
$netplan['network']['ethernets'][$iface] = new stdClass();
$netplan['network']['vlans']["{$iface}.{$vlan}"] = $netplan['network']['vlans']["{$iface}.{$vlan}"] =
array_merge( array_merge(
['id' => (int)$vlan, 'link' => $iface], ['id' => (int)$vlan, 'link' => $iface],
build_interface($data['secondary'], 'secondary') build_interface($data['secondary'], 'secondary')
); );
} else {
/* NO VLAN → configure base NIC */
$netplan['network']['ethernets'][$iface] =
build_interface($data['secondary'], 'secondary');
} }
} }
/* Ensure vlans is a mapping */ /* Normalize vlans */
if (empty($netplan['network']['vlans'])) { if (empty($netplan['network']['vlans'])) {
$netplan['network']['vlans'] = new stdClass(); $netplan['network']['vlans'] = new stdClass();
} }