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

108 lines
4.3 KiB
Perl
Executable File

#!/usr/bin/perl -w
=for commnet
Part of BBHN Mesh -- Used for creating Amateur Radio friendly mesh 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/>.
=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 "")
{
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;