aredn/files/usr/local/bin/nvram-setup

126 lines
5.0 KiB
Plaintext
Raw Normal View History

#!/usr/bin/perl -w
=for commnet
Part of AREDN -- Used for creating Amateur Radio Emergency Data Networks
Copyright (C) 2015 Conrad Lara
See Contributors file for additional contributors
Copyright (c) 2013 David Rivenburg et al. BroadBand-HamNet
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional Terms:
Additional use restrictions exist on the AREDN(TM) trademark and logo.
See AREDNLicense.txt for more info.
Attributions to the AREDN Project must be retained in the source code.
If importing this code into a new or existing project attribution
to the AREDN project must be added to the source code.
You must not misrepresent the origin of the material conained within.
Modified versions must be modified to attribute to the original source
and be marked in reasonable ways as differentiate it from the original
version.
=cut
# do the initial setup of the essential nvram variables
# node - node name
# mac2 - last 3 bytes of wifi mac address in the form ddd.ddd.ddd
# wifi_mac - full mac address of wireless card in the form hh:hh:hh:hh:hh:hh
#
# intended to run every boot but it should only actually do anything
# on the first boot
sub fail
{
open(ERR, ">>/tmp/nvram_errors");
print ERR "@_\n";
close(ERR);
die "@_\n";
}
$commit = 0;
# Added new wifimac parm, the radio0 wont startup without it, use pci device location
# may need to find a better way to allow for wider hardware support
chomp ($wifi_mac = `uci -c /etc/local/uci/ -q get hsmmmesh.settings.wifimac`);
if($wifi_mac eq "")
{
open(FILE, "/sys/devices/pci0000:00/0000:00:00.0/ieee80211/phy0/macaddress") or fail("ERROR: wireless mac not available");
while(<FILE>)
{
next unless /(\w\w:\w\w:\w\w:\w\w:\w\w:\w\w)/;
$wifi_mac = $1;
last;
}
close(FILE);
fail("ERROR: wireless mac not found") if $wifi_mac eq "";
system "uci -c /etc/local/uci/ set hsmmmesh.settings.wifimac=$wifi_mac";
$commit = 1;
}
chomp ($mac2 = `uci -c /etc/local/uci/ -q get hsmmmesh.settings.mac2`);
if($mac2 eq "")
{
open(FILE, "/sys/devices/pci0000:00/0000:00:00.0/ieee80211/phy0/macaddress") or fail("ERROR: wireless mac not available");
while(<FILE>)
{
next unless /\w\w:\w\w:\w\w:(\w\w):(\w\w):(\w\w)/;
$mac2 = join ".", hex $1, hex $2, hex $3;
last;
}
close(FILE);
fail("ERROR: wireless mac not found") if $mac2 eq "";
system "uci -c /etc/local/uci/ set hsmmmesh.settings.mac2=$mac2";
$commit = 1;
}
chomp ($node = `uci -c /etc/local/uci/ -q get hsmmmesh.settings.node`);
if($node eq "")
{
$mac2 =~ s/\./-/g;
$node = "NOCALL-$mac2";
system "uci -c /etc/local/uci/ set hsmmmesh.settings.node=NOCALL-$mac2";
$commit = 1;
}
chomp ($dtdmac = `uci -c /etc/local/uci/ -q get hsmmmesh.settings.dtdmac`);
if($dtdmac eq "")
{
# We should always have an eth0 interface on all nodes.
# Not porting get_interface into this file at this time
# as on a firstboot where nvram-setup runs the interfaces
# should not be changed yet.
open(FILE, "/sbin/ifconfig eth0 |") or fail("ERROR: eth0 mac not available");
while(<FILE>)
{
next unless /\w\w:\w\w:\w\w:(\w\w):(\w\w):(\w\w)/;
$dtdmac = join ".", hex $1, hex $2, hex $3;
last;
}
close(FILE);
system "uci -c /etc/local/uci/ set hsmmmesh.settings.dtdmac=$dtdmac";
$commit = 1;
}
system "uci -c /etc/local/uci/ commit" if $commit;