From 7945259bdabf864f10aa64aaaf5df57b1d2b9cec Mon Sep 17 00:00:00 2001 From: David Bauer Date: Tue, 25 Aug 2020 01:00:18 +0200 Subject: [PATCH] gluon-core: add ath79 migration Add a universal migration script suited for migrating devices from the removed ar71xx target to ath79. While at it, try to abstract as much functionality possibly required in the future for other migrations and implement it in the newly created gluon.migration Lua package. Signed-off-by: David Bauer --- .../gluon/core/migration/create_led_config.sh | 15 +++ .../core/{sysconfig => migration/flags}/.keep | 0 .../gluon-core/files/lib/upgrade/keep.d/gluon | 1 + .../lib/gluon/upgrade/002-ath79-migration | 98 +++++++++++++++++++ .../luasrc/usr/lib/lua/gluon/migration.lua | 37 +++++++ 5 files changed, 151 insertions(+) create mode 100644 package/gluon-core/files/lib/gluon/core/migration/create_led_config.sh rename package/gluon-core/files/lib/gluon/core/{sysconfig => migration/flags}/.keep (100%) create mode 100644 package/gluon-core/luasrc/lib/gluon/upgrade/002-ath79-migration create mode 100644 package/gluon-core/luasrc/usr/lib/lua/gluon/migration.lua diff --git a/package/gluon-core/files/lib/gluon/core/migration/create_led_config.sh b/package/gluon-core/files/lib/gluon/core/migration/create_led_config.sh new file mode 100644 index 00000000..4e564dfb --- /dev/null +++ b/package/gluon-core/files/lib/gluon/core/migration/create_led_config.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +CFG=/etc/board.json + +. /lib/functions/config-generate.sh + +json_init +json_load "$(cat ${CFG})" + +umask 077 + +keys= + +json_get_keys keys led +for key in $keys; do generate_led $key; done diff --git a/package/gluon-core/files/lib/gluon/core/sysconfig/.keep b/package/gluon-core/files/lib/gluon/core/migration/flags/.keep similarity index 100% rename from package/gluon-core/files/lib/gluon/core/sysconfig/.keep rename to package/gluon-core/files/lib/gluon/core/migration/flags/.keep diff --git a/package/gluon-core/files/lib/upgrade/keep.d/gluon b/package/gluon-core/files/lib/upgrade/keep.d/gluon index bc82c775..97e916b9 100644 --- a/package/gluon-core/files/lib/upgrade/keep.d/gluon +++ b/package/gluon-core/files/lib/upgrade/keep.d/gluon @@ -1 +1,2 @@ /lib/gluon/core/sysconfig/ +/lib/gluon/core/migration/flags diff --git a/package/gluon-core/luasrc/lib/gluon/upgrade/002-ath79-migration b/package/gluon-core/luasrc/lib/gluon/upgrade/002-ath79-migration new file mode 100644 index 00000000..d49eb03d --- /dev/null +++ b/package/gluon-core/luasrc/lib/gluon/upgrade/002-ath79-migration @@ -0,0 +1,98 @@ +#!/usr/bin/lua + +local platform = require 'gluon.platform' +local sysconfig = require 'gluon.sysconfig' +local migration = require 'gluon.migration' + +local uci = require('simple-uci').cursor() + +local migration_name = 'ar71xx-ath79' + +local migration_target_config = { + -- {DEVICE, ETH_SWAP} +} + +-- Create a list for use with platform matching +local migration_targets = {} +for _, v in ipairs(migration_target_config) do + table.insert(migration_targets, v[0]) +end + +-- Check if device is a migration target +if not platform.match('ath79', 'generic', migration_targets) then + return +end + +-- Check if this is an initial installation +if not sysconfig.gluon_version then + os.execute('touch /lib/gluon/core/migrations/ar71xx-ath79') + return +end + +-- Check if migration has already been done +if migration.is_migration_done(migration_name) then + return +end + +-- Reinit board.json +migration.reinit_board_json() + +migration.reinit_led_config(uci) + +local function sysconfig_ifname_swap(input_ifnames) + local output_ifnames = {} + for v in input_ifnames:gmatch('%S+') do + if string.match(v, 'eth0') then + table.insert(output_ifnames, v:gsub('eth0', 'eth1')) + elseif string.match(v, 'eth1') then + table.insert(output_ifnames, v:gsub('eth1', 'eth0')) + end + end + + return table.concat(output_ifnames, ' ') +end + +-- For devices with embedded FE switch, eth0 and eth1 +-- have been swapped. +local function perform_eth_swap() + -- Modify running config + uci:foreach('network', 'interface', function(iface) + local interfaces = uci:get_list('network', iface['.name'], 'ifname') + if interfaces == nil then return end + + local new_interfaces = {} + + for _, v in ipairs(interfaces) do + if string.match(v, 'eth0') then + table.insert(new_interfaces, v:gsub('eth0', 'eth1')) + elseif string.match(v, 'eth1') then + table.insert(new_interfaces, v:gsub('eth1', 'eth0')) + end + end + + uci:set_list('network', iface['.name'], 'ifname', new_interfaces) + end) + uci:save('network') + + -- Modify Gluons port assignment. Important for setup-mode. + sysconfig.lan_ifname = sysconfig_ifname_swap(sysconfig.lan_ifname) + sysconfig.wan_ifname = sysconfig_ifname_swap(sysconfig.wan_ifname) +end + +local function board_requires_eth_swap() + local board_name = platform.get_board_name() + for _, v in ipairs(migration_target_config) do + if board_name == v[0] and v[1] then + return true + end + end + return false +end + +-- Perform eth swap +if board_requires_eth_swap() then + perform_eth_swap() +end + +-- Flag migration done +migration.flag_migration_done(migration_name) diff --git a/package/gluon-core/luasrc/usr/lib/lua/gluon/migration.lua b/package/gluon-core/luasrc/usr/lib/lua/gluon/migration.lua new file mode 100644 index 00000000..200a2764 --- /dev/null +++ b/package/gluon-core/luasrc/usr/lib/lua/gluon/migration.lua @@ -0,0 +1,37 @@ +local unistd = require 'posix.unistd' + + +local M = {} + +function M.get_migration_flag_path(migration_name) + return '/lib/gluon/core/migration/flags' .. migration_name +end + +function M.is_migration_done(migration_name) + return unistd.access(M.get_migration_flag_path(migration_name)) +end + +function M.flag_migration_done(migration_name) + os.execute(M.get_migration_flag_path(migration_name)) +end + +function M.reinit_led_config(uci) + -- Remove LED configuration + uci:foreach('system', 'rssid', function(config) + uci:delete('system', config['.name']) + end) + uci:foreach('system', 'led', function(config) + uci:delete('system', config['.name']) + end) + uci:save('led') + + -- Create LED configuration + os.execute('/lib/gluon/core/migration/create_led_config.sh') +end + +function M.reinit_board_json() + os.execute('rm /etc/board.json') + os.execute('board_detect') +end + +return M