add interfaces parsing script

This commit is contained in:
Gavin Li 2015-01-05 20:07:14 -08:00
parent 199edd5716
commit 2473784c85
1 changed files with 96 additions and 7 deletions

View File

@ -74,6 +74,10 @@ log() {
echo "[$(date)]" "$@" >&2
}
extract_embedded_file() {
gawk -e '$0=="!!!!"{p=0};p;$0=="!!!!"n{p=1}' n="${1}" "${script_path}"
}
mask_to_prefix() {
local prefix=0 netmask=${1}
for octet in ${netmask//./ }; do
@ -134,12 +138,8 @@ install_compat_package() {
EOF
mkdir -p ${workdir}/usr/bin/
cat > ${workdir}/usr/bin/parse-debian-interfaces <<-EOF
#!/bin/bash
set -eu
set -o pipefail
echo ENIENIENIENI > /dev/kmsg
EOF
extract_embedded_file parse-debian-interfaces > \
${workdir}/usr/bin/parse-debian-interfaces
chmod 0755 ${workdir}/usr/bin/parse-debian-interfaces
( cd ${workdir} && bsdtar -cf compat.pkg.tar * )
@ -418,6 +418,10 @@ postbootstrap_configuration() {
rm /archroot/etc/shadow.new
)
# copy interfaces file
mkdir -p /archroot/etc/network/
cp /etc/network/interfaces /archroot/etc/network/interfaces
# set up internet network
local eni=/etc/network/interfaces
{
@ -673,7 +677,92 @@ elif [ "${script_path}" = "/installer/script.sh" ]; then
else
installer_main "$@"
fi
exit 0 # in case junk appended
exit 0
: <<EMBED
!!!!parse-debian-interfaces
#!/usr/bin/gawk -bf
function iface_setprop(prop, val, suffix) {
suffix = iface_family == "inet6" ? 6 : 4
interfaces[iface_name][prop suffix] = val
}
function iface_getprop(prop, suffix) {
return interfaces[iface_name][prop suffix]
}
function iface_dump(suffix, fn, addr, pfx, dnss) {
addr = iface_getprop("address", suffix)
pfx = iface_getprop("netmask", suffix)
if (addr && pfx)
print "Address=" addr "/" pfx > fn
else
return
addr = iface_getprop("gateway", suffix)
if (addr)
print "Gateway=" addr > fn
split(iface_getprop("dns", suffix), dnss, "|")
for (i = 1; i in dnss; i++)
print "DNS=" dnss[i] > fn
}
function netmask_to_prefix(mask, cmps, bit, pfx) {
if (mask ~ /\./) {
pfx = 0
split(mask, cmps, ".")
do {
bit = and(cmps[rshift(pfx, 3) + 1],
lshift(1, 7 - and(pfx, 7)))
} while (bit && ++pfx < 32)
} else {
pfx = mask
}
return pfx
}
BEGIN {
netdir = "/run/systemd/network"
if (system("mkdir -p " netdir) != 0)
exit 1
}
$1 == "iface" {
iface_name = $2
iface_family = $3
iface_type = $4
active = (iface_type == "static") &&
(iface_family == "inet" || iface_family == "inet6")
}
active && ($1 ~ /^(address|netmask|gateway)$/) {
($1 == "netmask") && ($2 = netmask_to_prefix($2))
iface_setprop($1, $2)
}
active && $1 == "dns-nameservers" {
joined = ""
for (i = 2; $i; i++)
joined = joined ? joined "|" $i : $i
iface_setprop("dns", joined)
}
END {
for (iface_name in interfaces) {
fn = netdir "/" iface_name ".network"
print "# Generated by parse-debian-interfaces" > fn
print "" > fn
print "[Match]" > fn
print "Name=" iface_name > fn
print "" > fn
print "[Network]" > fn
iface_dump(4, fn)
iface_dump(6, fn)
close(fn)
}
}
!!!!
EMBED
#################
# END OF SCRIPT #