Added the ability to configure the hostname

This commit is contained in:
Gunnar Wolf 2019-07-19 15:34:46 -03:00
parent ad7a56130e
commit 79d62f1f55
2 changed files with 45 additions and 22 deletions

View File

@ -5,13 +5,43 @@ use IO::File;
use IO::Pipe; use IO::Pipe;
use feature 'switch'; use feature 'switch';
my ($filename, $fh, %conf); my ($filename, $conf);
$filename = '/boot/firmware/sysconf.txt'; $filename = '/boot/firmware/sysconf.txt';
exit 0 unless -f $filename; exit 0 unless -f $filename;
logger('info', "Applying the system configuration settings in $filename"); logger('info', "Reading the system configuration settings from $filename");
$conf = read_conf($filename);
if (my $pass = delete($conf->{root_pw})) {
my $pipe;
logger('debug', 'Resetting root password');
open($pipe, '|-', '/usr/sbin/chpasswd') or die $!;
$pipe->print("root:$pass");
close($pipe);
}
if (my $name = delete($conf->{hostname})) {
my $fh;
logger('debug', "Setting hostname to '$name'");
$fh = IO::File->new('/etc/hostname', 'w') or die $!;
$fh->print($name);
$fh->close;
system('hostname', '--file', '/etc/hostname');
}
if (scalar keys %$conf) {
logger('warn', 'Unprocessed keys left in $filename: ' .
join(', ', sort keys %$conf));
}
exit 0;
sub read_conf {
my ($file, $conf, $fh);
$file = shift;
$conf = {};
$fh = IO::File->new($filename, 'r'); $fh = IO::File->new($filename, 'r');
while (my $line = $fh->getline) { while (my $line = $fh->getline) {
my ($key, $value); my ($key, $value);
@ -19,29 +49,19 @@ while (my $line = $fh->getline) {
$line =~ s/#.+//; $line =~ s/#.+//;
if ( ($key, $value) = ($line =~ m/^\s*([^=]+)\s*=\s*(.*)\s*$/)) { if ( ($key, $value) = ($line =~ m/^\s*([^=]+)\s*=\s*(.*)\s*$/)) {
$key = lc($key); $key = lc($key);
if (exists($conf{$key})) { if (exists($conf->{$key})) {
logger('warn', logger('warn',
"Repeated configuration key: $key. " . "Repeated configuration key: $key. " .
"Overwriting with new value ($value)"); "Overwriting with new value ($value)");
} }
$conf{$key} = $value; $conf->{$key} = $value;
} }
} }
$fh->close;
if (my $pass = delete($conf{root_pw})) { return $conf;
my $pipe;
open($pipe, '|-', '/usr/sbin/chpasswd') or die $!;
print $pipe "root:$pass";
close($pipe);
} }
if (scalar keys %conf) {
logger('warn', 'Unprocessed keys left in $filename: ' .
join(', ', sort keys %conf));
}
exit 0;
sub logger { sub logger {
my ($prio, $msg) = @_; my ($prio, $msg) = @_;
system('/bin/logger', '-p', "daemon.$prio", system('/bin/logger', '-p', "daemon.$prio",

View File

@ -17,3 +17,6 @@
# root_pw - Set a password for the root user (by default, it allows # root_pw - Set a password for the root user (by default, it allows
# for a passwordless login) # for a passwordless login)
#root_pw=FooBar #root_pw=FooBar
# hostname - Set the system hostname.
#hostname=rpi