Skip to content

Commit

Permalink
treewide: regenerate network and system configs on every reconfigure
Browse files Browse the repository at this point in the history
  • Loading branch information
neocturne committed Jan 9, 2022
1 parent 48cb99f commit 9924bb1
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,13 @@ local util = require 'gluon.util'
local uci = require('simple-uci').cursor()


local interfaces = uci:get('network', 'client', 'ifname') or {}

if type(interfaces) == 'string' then
local ifname = interfaces
interfaces = {}
for iface in ifname:gmatch('%S+') do
util.add_to_set(interfaces, iface)
end
end

local interfaces = { 'local-port' }
if sysconfig.lan_ifname and uci:get_bool('network', 'mesh_lan', 'disabled') then
for lanif in sysconfig.lan_ifname:gmatch('%S+') do
util.add_to_set(interfaces, lanif)
end
end

util.add_to_set(interfaces, 'local-port')


uci:delete('network', 'client')
uci:section('network', 'interface', 'client', {
type = 'bridge',
ifname = interfaces,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ local uci = require('simple-uci').cursor()
local next_node = site.next_node({})


uci:delete('network', 'local_node_dev')
uci:section('network', 'device', 'local_node_dev', {
type = 'veth',
name = 'local-node',
Expand All @@ -31,7 +30,6 @@ if next_node.ip6 then
ip6 = next_node.ip6 .. '/128'
end

uci:delete('network', 'local_node')
uci:section('network', 'interface', 'local_node', {
ifname = 'local-node',
proto = 'static',
Expand Down
20 changes: 20 additions & 0 deletions package/gluon-core/files/lib/gluon/upgrade/001-reset-uci
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh

NETWORK_CFG='/etc/config/network'
NETWORK_SAVED="${NETWORK_CFG}_gluon-old"

SYSTEM_CFG='/etc/config/system'
SYSTEM_SAVED="${SYSTEM_CFG}_gluon-old"

# Save old configs (unless there is already a saved config,
# which means that the previous upgrade was interrupted)
if [ -s "$NETWORK_CFG" ] && ! [ -s "$NETWORK_SAVED" ]; then
mv -f "$NETWORK_CFG" "$NETWORK_SAVED"
fi
if [ -s "$SYSTEM_CFG" ] && ! [ -s "$SYSTEM_SAVED" ]; then
mv -f "$SYSTEM_CFG" "$SYSTEM_SAVED"
fi

# Generate a new network config
rm -f "$NETWORK_CFG" "$SYSTEM_CFG"
config_generate
3 changes: 3 additions & 0 deletions package/gluon-core/files/lib/gluon/upgrade/998-commit
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ uci -q batch <<-EOF
delete gluon.core.reconfigure
commit
EOF

# New config is saved, we can delete the old one
rm -f /etc/config/*_gluon-old
31 changes: 31 additions & 0 deletions package/gluon-core/luasrc/lib/gluon/upgrade/002-migrate-system
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/lua

local uci = require('simple-uci').cursor()

-- Migrate system section
local system = uci:get_all('system_gluon-old', '@system[0]')
if system then
uci:tset('system', '@system[0]', system)
end

-- Migrate ntp section
local ntp = uci:get_all('system_gluon-old', 'ntp')
if ntp then
uci:tset('system', 'ntp', ntp)
end

-- Migrate gpio_switch sections
--
-- Only the value is copied from the old config, so updates to names and
-- pins are preserved
uci:foreach('system', 'gpio_switch', function(s)
local name = s['.name']
local value = uci:get('system_gluon-old', name, 'value')
if value then
uci:set('system', name, 'value', value)
end
end)

-- No other sections are migrated, so updated LED and RSSI configs can take effect

uci:save('system')
4 changes: 1 addition & 3 deletions package/gluon-core/luasrc/lib/gluon/upgrade/020-interfaces
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ else
end


uci:delete('network', 'lan')
uci:delete('network', 'wan')

uci:delete_all('network', 'device')
uci:delete_all('network', 'interface')

uci:save('network')
19 changes: 9 additions & 10 deletions package/gluon-core/luasrc/lib/gluon/upgrade/110-network
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
local uci = require('simple-uci').cursor()
local sysconfig = require 'gluon.sysconfig'

local wan = uci:get_all('network_gluon-old', 'wan') or {}
local wan6 = uci:get_all('network_gluon-old', 'wan6') or {}

uci:section('network', 'interface', 'wan', {
proto = wan.proto or 'dhcp',
ipaddr = wan.ipaddr,
netmask = wan.netmask,
gateway = wan.gateway,
ifname = sysconfig.wan_ifname,
type = 'bridge',
igmp_snooping = true,
Expand All @@ -13,24 +19,17 @@ uci:section('network', 'interface', 'wan', {
auto = true,
})

if not uci:get('network', 'wan', 'proto') then
uci:set('network', 'wan', 'proto', 'dhcp')
end


uci:section('network', 'interface', 'wan6', {
proto = wan6.proto or 'dhcpv6',
ip6addr = wan6.ip6addr,
ip6gw = wan6.ip6gw,
ifname = 'br-wan',
peerdns = false,
ip6table = 1,
sourcefilter = false,
reqprefix = 'no',
})

if not uci:get('network', 'wan6', 'proto') then
uci:set('network', 'wan6', 'proto', 'dhcpv6')
end


uci:section('network', 'rule6', 'wan6_lookup', {
mark = '0x01/0x01',
lookup = 1,
Expand Down
4 changes: 0 additions & 4 deletions package/gluon-core/luasrc/lib/gluon/upgrade/200-wireless
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ end
local function delete_ibss(radio_name)
local name = 'ibss_' .. radio_name

uci:delete('network', name)
uci:delete('network', name .. '_vlan')
uci:delete('wireless', name)
end

Expand All @@ -118,8 +116,6 @@ local function configure_mesh(config, radio, index, suffix, disabled)
local macfilter = uci:get('wireless', name, 'macfilter')
local maclist = uci:get('wireless', name, 'maclist')

uci:delete('network', name)
uci:delete('network', name .. '_vlan')
uci:delete('wireless', name)

if not config then
Expand Down
30 changes: 15 additions & 15 deletions package/gluon-core/luasrc/lib/gluon/upgrade/210-interface-wan
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
local site = require 'gluon.site'
local uci = require('simple-uci').cursor()

uci:section('network', 'interface', 'mesh_wan', {
ifname = 'br-wan',
proto = 'gluon_wired',
index = 0,
vxlan = site.mesh.vxlan(true),
})

local enable = site.mesh_on_wan(false)
local old_auto = uci:get('network', 'mesh_wan', 'auto')
local old_disabled = uci:get('network', 'mesh_wan', 'disabled')
if old_auto ~= nil or old_disabled ~= nil then
enable = old_auto ~= '0' and old_disabled ~= '1'
local disabled = uci:get('network_gluon-old', 'mesh_wan', 'disabled')
if disabled == nil then
disabled = not site.mesh_on_wan(false)
end
uci:set('network', 'mesh_wan', 'disabled', not enable)

if uci:get('network', 'mesh_wan', 'transitive') == nil then
uci:set('network', 'mesh_wan', 'transitive', true)
local transitive = uci:get('network_gluon-old', 'mesh_wan', 'transitive')
if transitive == nil then
transitive = true
end

uci:section('network', 'interface', 'mesh_wan', {
ifname = 'br-wan',
proto = 'gluon_wired',
index = 0,
vxlan = site.mesh.vxlan(true),
disabled = disabled,
transitive = transitive,
})

uci:save('network')
51 changes: 18 additions & 33 deletions package/gluon-core/luasrc/lib/gluon/upgrade/220-interface-lan
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/lua

local site = require 'gluon.site'
local util = require 'gluon.util'
local sysconfig = require 'gluon.sysconfig'

local uci = require('simple-uci').cursor()
Expand All @@ -10,44 +9,30 @@ if not sysconfig.lan_ifname then
os.exit(0)
end

uci:section('network', 'interface', 'mesh_lan', {
ifname = sysconfig.lan_ifname,
igmp_snooping = false,
proto = 'gluon_wired',
index = 4,
vxlan = site.mesh.vxlan(true),
})

local type
if sysconfig.lan_ifname:match(' ') then
uci:set('network', 'mesh_lan', 'type', 'bridge')
else
uci:delete('network', 'mesh_lan', 'type')
type = 'bridge'
end

local enable = site.mesh_on_lan(false)
local old_auto = uci:get('network', 'mesh_lan', 'auto')
local old_disabled = uci:get('network', 'mesh_lan', 'disabled')
if old_auto ~= nil or old_disabled ~= nil then
enable = old_auto ~= '0' and old_disabled ~= '1'
local disabled = uci:get('network_gluon-old', 'mesh_lan', 'disabled')
if disabled == nil then
disabled = not site.mesh_on_lan(false)
end

if enable then
local interfaces = uci:get_list('network', 'client', 'ifname')

if interfaces then
for lanif in sysconfig.lan_ifname:gmatch('%S+') do
if util.contains(interfaces, lanif) then
enable = false
break
end
end
end
local transitive = uci:get('network_gluon-old', 'mesh_lan', 'transitive')
if transitive == nil then
transitive = true
end

uci:set('network', 'mesh_lan', 'disabled', not enable)

if uci:get('network', 'mesh_lan', 'transitive') == nil then
uci:set('network', 'mesh_lan', 'transitive', true)
end
uci:section('network', 'interface', 'mesh_lan', {
ifname = sysconfig.lan_ifname,
type = type,
igmp_snooping = false,
proto = 'gluon_wired',
index = 4,
vxlan = site.mesh.vxlan(true),
disabled = disabled,
transitive = transitive,
})

uci:save('network')
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

local uci = require('simple-uci').cursor()

uci:delete('network', 'mmfd')
uci:section('network', 'interface', 'mmfd', {
proto = 'static',
ifname = 'mmfd0',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ local uci = require('simple-uci').cursor()
uci:delete('batman-adv', 'bat0')
uci:save('batman-adv')

local gw_mode = uci:get('network', 'gluon_bat0', 'gw_mode') or 'client'
uci:delete('network', 'gluon_bat0')
local gw_mode = uci:get('network_gluon-old', 'gluon_bat0', 'gw_mode') or 'client'
uci:section('network', 'interface', 'gluon_bat0', {
proto = 'gluon_bat0',
gw_mode = gw_mode,
})

uci:delete('network', 'bat0')
uci:section('network', 'interface', 'bat0', {
ifname = 'bat0',
proto = 'none',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ uci:section('network', 'interface', 'client', {
query_response_interval = 500,
})

uci:delete('network', 'local_node_route6')
uci:section('network', 'route6', 'local_node_route6', {
interface = 'client',
target = site.prefix6(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ local uci = require('simple-uci').cursor()
-- fix up potentially duplicate MAC addresses (for meshing)
if not site.mesh.vxlan(true) then
uci:set('network', 'wan', 'macaddr', util.generate_mac(0))
else
uci:delete('network', 'wan', 'macaddr')
end
uci:set('network', 'mesh_lan', 'macaddr', util.generate_mac(4))
uci:save('network')
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
local uci = require('simple-uci').cursor()
local site = require 'gluon.site'

local private_key = uci:get("network", 'wg_mesh', "private_key")
local private_key = uci:get("network_gluon-old", 'wg_mesh', "private_key")

if not private_key or not private_key:match("^" .. ("[%a%d+/]"):rep(42) .. "[AEIMQUYcgkosw480]=$") then
private_key = "generate"
Expand Down

0 comments on commit 9924bb1

Please sign in to comment.