Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Make wrapper to execute make file in a sandbox VM
use strict;
use warnings;
use English;
use Sys::Virt;
use Net::OpenSSH;
use File::Spec;
use File::Basename;
use Cwd 'realpath';
my $LIBVIRT_DEFAULT_URI = "qemu:///session";
my $vm_name = "stretch-builder";
my ($makefile, $make_target) = @ARGV;
die "usage: $0 Makefile target\n" if ! $makefile || ! $make_target;
my $make_dir = dirname(File::Spec->rel2abs($makefile));
# add builder user
# set up qemu port fortwarding like
# set up p9 passthrough like
#"virsh attach-device --config stretch-builder /dev/stdin << EOF
#<filesystem type='mount' accessmode='mapped'>
# <source dir='/home/manu/Projects/git/fai-cloud-images'/>
# <target dir='vmake-export'/>
my $vm_manager = Sys::Virt->new(uri => $LIBVIRT_DEFAULT_URI);
my $vm = $vm_manager->get_domain_by_name($vm_name);
if (! $vm->is_active()) {
$vm->create(); # start the VM
print STDERR "starting builder VM $vm_name\n";
my $ssh = Net::OpenSSH->new('root@localhost:2222',
master_opts => [-o => 'UserKnownHostsFile=/dev/null'], # ssh host key will be different on
master_opts => [-o => 'StrictHostKeyChecking=no'] # each build VM
if ($ssh->error()) {
die "Couldn't establish SSH connection: ". $ssh->error;
} else {
print STDERR "ssh connection established\n";
$ssh->system("mkdir --parents /fai-cloud-images && \
mount vmake-export /fai-cloud-images -t 9p -o trans=virtio")
or die "remote command failed: " . $ssh->error;
my $output = $ssh->pipe_out("pmake -C /fai-cloud-images $make_target")
or die "pipe_in failed: " . $ssh->error;
while (my $line = readline $output) {
print $line;
close $output;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment