2022-02-10 19:58:56 +01:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
|
|
|
|
# File: guest-server
|
|
|
|
#
|
|
|
|
# Purpose: PBot VM Guest server. Runs inside PBot VM Guest and processes
|
|
|
|
# incoming VM commands from vm-exec.
|
|
|
|
|
pbot-vm: massive refactor
* replace heartbeat with health-check
* instead of steady stream of newlines every 5s, now awaits input
and responds with `vmstat` output
* more reliably use host/config/vm-exec.json to get libvirt domain name
for snapshot-revert, server address, serial ports, vagrant setting, etc
* use iptables/nftables to disable networking
* added guest/bin/disable-network-[iptables,nftables]
* added guest/bin/enable-network-[iptables,nftables]
* replace ugly ___OUTPUT___ texts in sh, bash, ksh, zsh languages
* documentation updates and tweaks
2024-04-10 10:21:18 +02:00
|
|
|
# SPDX-FileCopyrightText: 2022-2024 Pragmatic Software <pragma78@gmail.com>
|
2022-02-10 19:58:56 +01:00
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
use 5.020;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
use feature qw/signatures/;
|
|
|
|
no warnings qw(experimental::signatures);
|
|
|
|
|
|
|
|
use constant {
|
2022-02-13 01:06:04 +01:00
|
|
|
USERNAME => 'vm',
|
|
|
|
MOD_DIR => '/usr/local/share/pbot-vm/',
|
2023-03-13 21:33:10 +01:00
|
|
|
SERIAL => '/dev/ttyS2',
|
pbot-vm: massive refactor
* replace heartbeat with health-check
* instead of steady stream of newlines every 5s, now awaits input
and responds with `vmstat` output
* more reliably use host/config/vm-exec.json to get libvirt domain name
for snapshot-revert, server address, serial ports, vagrant setting, etc
* use iptables/nftables to disable networking
* added guest/bin/disable-network-[iptables,nftables]
* added guest/bin/enable-network-[iptables,nftables]
* replace ugly ___OUTPUT___ texts in sh, bash, ksh, zsh languages
* documentation updates and tweaks
2024-04-10 10:21:18 +02:00
|
|
|
HEALTH => '/dev/ttyS3',
|
2022-02-13 01:06:04 +01:00
|
|
|
VPORT => $ENV{PBOTVM_VPORT} // 5555,
|
2022-02-10 19:58:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
use lib MOD_DIR;
|
2022-02-13 01:06:04 +01:00
|
|
|
use lib MOD_DIR . "Languages";
|
|
|
|
|
|
|
|
use Guest;
|
|
|
|
|
|
|
|
use File::Basename;
|
2022-02-10 19:58:56 +01:00
|
|
|
|
|
|
|
my %languages;
|
|
|
|
|
|
|
|
sub load_modules() {
|
2022-02-13 01:06:04 +01:00
|
|
|
my @files = glob MOD_DIR . "Languages/*.pm";
|
|
|
|
|
2022-02-10 19:58:56 +01:00
|
|
|
foreach my $mod (@files){
|
|
|
|
print "Loading module $mod\n";
|
2022-02-13 01:06:04 +01:00
|
|
|
|
2022-02-10 19:58:56 +01:00
|
|
|
my $filename = basename($mod);
|
2022-02-13 01:06:04 +01:00
|
|
|
|
2022-02-10 19:58:56 +01:00
|
|
|
require $filename;
|
2022-02-13 01:06:04 +01:00
|
|
|
|
2022-02-10 19:58:56 +01:00
|
|
|
$filename =~ s/\.pm$//;
|
2022-02-13 01:06:04 +01:00
|
|
|
|
2022-02-10 19:58:56 +01:00
|
|
|
$languages{$filename} = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
sub vsock_server() {
|
|
|
|
print "Starting VSOCK server on PID $$\n";
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
system("socat VSOCK-LISTEN:".VPORT.",reuseaddr,fork EXEC:accept-vsock-client");
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
print "VSOCK server shutdown.\n";
|
|
|
|
exit; # exit child process
|
|
|
|
}
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
sub serial_server() {
|
|
|
|
print "Starting serial server on PID $$\n";
|
|
|
|
|
|
|
|
# set serial to 115200 baud instead of 9600
|
|
|
|
system('stty -F '.SERIAL.' 115200');
|
|
|
|
|
|
|
|
open(my $input, '<', SERIAL) or die $!;
|
|
|
|
open(my $output, '>', SERIAL) or die $!;
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
my $buffer = '';
|
2022-02-10 19:58:56 +01:00
|
|
|
|
pbot-vm: massive refactor
* replace heartbeat with health-check
* instead of steady stream of newlines every 5s, now awaits input
and responds with `vmstat` output
* more reliably use host/config/vm-exec.json to get libvirt domain name
for snapshot-revert, server address, serial ports, vagrant setting, etc
* use iptables/nftables to disable networking
* added guest/bin/disable-network-[iptables,nftables]
* added guest/bin/enable-network-[iptables,nftables]
* replace ugly ___OUTPUT___ texts in sh, bash, ksh, zsh languages
* documentation updates and tweaks
2024-04-10 10:21:18 +02:00
|
|
|
while (1) {
|
2022-02-13 01:06:04 +01:00
|
|
|
my $command = Guest::read_input($input, \$buffer, 'Serial');
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
if (not defined $command) {
|
|
|
|
# recoverable error while reading, try again
|
|
|
|
next;
|
|
|
|
}
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
if ($command == 0) {
|
|
|
|
# serial closed, exit child process
|
|
|
|
exit;
|
|
|
|
}
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
if (not exists $languages{$command->{lang}}) {
|
|
|
|
$command->{lang} = '_default';
|
|
|
|
}
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
my $mod = $command->{lang}->new(%$command);
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
my $result = Guest::process_command($command, $mod, USERNAME, 'Serial');
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
if (not defined $result) {
|
|
|
|
$result = "[Fatal error]";
|
|
|
|
}
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
if ($result) {
|
|
|
|
Guest::send_output($output, $result, 'Serial');
|
|
|
|
exit; # exit child process
|
|
|
|
}
|
2022-02-10 19:58:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
sub do_server() {
|
2022-02-10 19:58:56 +01:00
|
|
|
my $pid = fork;
|
|
|
|
|
|
|
|
if (not defined $pid) {
|
2022-02-13 01:06:04 +01:00
|
|
|
print STDERR "Could not fork server: $!\n";
|
|
|
|
die;
|
2022-02-10 19:58:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($pid == 0) {
|
2022-02-13 01:06:04 +01:00
|
|
|
vsock_server();
|
|
|
|
} else {
|
|
|
|
serial_server();
|
|
|
|
}
|
|
|
|
}
|
2022-02-10 19:58:56 +01:00
|
|
|
|
pbot-vm: massive refactor
* replace heartbeat with health-check
* instead of steady stream of newlines every 5s, now awaits input
and responds with `vmstat` output
* more reliably use host/config/vm-exec.json to get libvirt domain name
for snapshot-revert, server address, serial ports, vagrant setting, etc
* use iptables/nftables to disable networking
* added guest/bin/disable-network-[iptables,nftables]
* added guest/bin/enable-network-[iptables,nftables]
* replace ugly ___OUTPUT___ texts in sh, bash, ksh, zsh languages
* documentation updates and tweaks
2024-04-10 10:21:18 +02:00
|
|
|
sub do_healthcheck() {
|
|
|
|
open(my $health_in, '<', HEALTH) or die $!;
|
|
|
|
open(my $health_out, '>', HEALTH) or die $!;
|
2022-02-10 19:58:56 +01:00
|
|
|
|
pbot-vm: massive refactor
* replace heartbeat with health-check
* instead of steady stream of newlines every 5s, now awaits input
and responds with `vmstat` output
* more reliably use host/config/vm-exec.json to get libvirt domain name
for snapshot-revert, server address, serial ports, vagrant setting, etc
* use iptables/nftables to disable networking
* added guest/bin/disable-network-[iptables,nftables]
* added guest/bin/enable-network-[iptables,nftables]
* replace ugly ___OUTPUT___ texts in sh, bash, ksh, zsh languages
* documentation updates and tweaks
2024-04-10 10:21:18 +02:00
|
|
|
print "Healthcheck listening on PID $$...\n";
|
2022-02-10 19:58:56 +01:00
|
|
|
|
pbot-vm: massive refactor
* replace heartbeat with health-check
* instead of steady stream of newlines every 5s, now awaits input
and responds with `vmstat` output
* more reliably use host/config/vm-exec.json to get libvirt domain name
for snapshot-revert, server address, serial ports, vagrant setting, etc
* use iptables/nftables to disable networking
* added guest/bin/disable-network-[iptables,nftables]
* added guest/bin/enable-network-[iptables,nftables]
* replace ugly ___OUTPUT___ texts in sh, bash, ksh, zsh languages
* documentation updates and tweaks
2024-04-10 10:21:18 +02:00
|
|
|
while (1) {
|
|
|
|
my $input = <$health_in>;
|
|
|
|
my $vmstat = `vmstat`;
|
|
|
|
print $health_out "$vmstat\n";
|
|
|
|
print $health_out ":END\n";
|
2022-02-10 19:58:56 +01:00
|
|
|
}
|
|
|
|
|
pbot-vm: massive refactor
* replace heartbeat with health-check
* instead of steady stream of newlines every 5s, now awaits input
and responds with `vmstat` output
* more reliably use host/config/vm-exec.json to get libvirt domain name
for snapshot-revert, server address, serial ports, vagrant setting, etc
* use iptables/nftables to disable networking
* added guest/bin/disable-network-[iptables,nftables]
* added guest/bin/enable-network-[iptables,nftables]
* replace ugly ___OUTPUT___ texts in sh, bash, ksh, zsh languages
* documentation updates and tweaks
2024-04-10 10:21:18 +02:00
|
|
|
print "Healthcheck stopped.\n";
|
2022-02-13 01:06:04 +01:00
|
|
|
exit; # exit child process
|
|
|
|
}
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
sub install_signal_handlers() {
|
|
|
|
use POSIX qw(:signal_h :errno_h :sys_wait_h);
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
$SIG{CHLD} = \&REAPER;
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
sub REAPER {
|
|
|
|
my $pid = waitpid(-1, &WNOHANG);
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
if ($pid == -1) {
|
|
|
|
# no child waiting. Ignore it.
|
|
|
|
} elsif (WIFEXITED($?)) {
|
|
|
|
print "Process $pid exited.\n";
|
2022-02-10 19:58:56 +01:00
|
|
|
}
|
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
$SIG{CHLD} = \&REAPER; # in case of unreliable signals
|
2022-02-10 19:58:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub main() {
|
2022-02-13 01:06:04 +01:00
|
|
|
print "Starting PBot VM Guest server on PID $$\n";
|
|
|
|
|
2022-02-10 19:58:56 +01:00
|
|
|
load_modules();
|
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
install_signal_handlers();
|
2022-02-10 19:58:56 +01:00
|
|
|
|
|
|
|
my $pid = fork // die "Fork failed: $!";
|
|
|
|
|
|
|
|
if ($pid == 0) {
|
pbot-vm: massive refactor
* replace heartbeat with health-check
* instead of steady stream of newlines every 5s, now awaits input
and responds with `vmstat` output
* more reliably use host/config/vm-exec.json to get libvirt domain name
for snapshot-revert, server address, serial ports, vagrant setting, etc
* use iptables/nftables to disable networking
* added guest/bin/disable-network-[iptables,nftables]
* added guest/bin/enable-network-[iptables,nftables]
* replace ugly ___OUTPUT___ texts in sh, bash, ksh, zsh languages
* documentation updates and tweaks
2024-04-10 10:21:18 +02:00
|
|
|
do_healthcheck();
|
2022-02-10 19:58:56 +01:00
|
|
|
} else {
|
2022-02-13 01:06:04 +01:00
|
|
|
do_server();
|
2022-02-10 19:58:56 +01:00
|
|
|
}
|
2022-02-13 01:06:04 +01:00
|
|
|
|
|
|
|
print "PBot VM Guest server shutdown.\n";
|
2022-02-10 19:58:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
main();
|