use eatmydata if available
[os-autoinst:os-autoinst.git] / inst / startqemu.pm
1 #!/usr/bin/perl -w
2 use strict;
3 use File::Path qw/mkpath/;
4
5 my $basedir="raid";
6 my $qemuimg="/usr/bin/kvm-img";
7 if(!-e $qemuimg) {$qemuimg="/usr/bin/qemu-img"}
8 my $qemubin="/usr/bin/kvm";
9 if(!-x $qemubin) {$qemubin=~s/kvm/qemu-kvm/}
10 if(!-x $qemubin) {$qemubin=~s/-kvm//}
11 if(!-x $qemubin) {die "no Qemu/KVM found"}
12
13 my $iso=$ENV{ISO};
14 my $sizegb=8;
15 if($ENV{BTRFS}) {$sizegb=10}
16 $ENV{HDDMODEL}||="virtio";
17 $ENV{NICMODEL}||="virtio";
18 $ENV{QEMUVGA}||="cirrus";
19 $ENV{QEMUCPUS}||=1;
20 $ENV{NUMDISKS}||=2;
21 if(defined($ENV{RAIDLEVEL})) {$ENV{NUMDISKS}=4}
22 my @cdrom=("-cdrom", $iso);
23
24 $ENV{QEMU_AUDIO_DRV}="wav";
25 $ENV{QEMU_WAV_PATH}="/dev/null";
26
27 my $ison=$iso; $ison=~s{.*/}{}; # drop path
28 if($ison=~m/Live/i) {$ENV{LIVECD}=1}
29 if($ison=~m/Promo/) {$ENV{PROMO}=1}
30 if($ison=~m/-i[3-6]86-/) {$ENV{QEMUCPU}||="qemu32"}
31 if($ison=~m/openSUSE-Smeegol/) {$ENV{DESKTOP}||="gnome"}
32 if($ison=~m/openSUSE-(DVD|NET|KDE|GNOME|LXDE|XFCE)/) {
33         $ENV{$1}=1; $ENV{NETBOOT}=$ENV{NET};
34         if($ENV{LIVECD}) {
35                 $ENV{DESKTOP}=lc($1);
36         }
37 }
38
39 if ($ENV{UEFI} && !-e $ENV{UEFI_BIOS_DIR}.'/bios.bin') {
40         die "'$ENV{UEFI_BIOS_DIR}/bios.bin' missing, check UEFI_BIOS_DIR\n";
41 }
42
43 mkpath($basedir);
44
45 if($ENV{UPGRADE} && !$ENV{LIVECD}) {
46         my $file=$ENV{UPGRADE};
47         if(!-e $file) {die "'$ENV{UPGRADE}' should be old img.gz"}
48         $ENV{KEEPHDDS}=1;
49         # use qemu snapshot/cow feature to work on old image without writing it
50         unlink "$basedir/l1";
51         unlink "$basedir/1";
52         #system($qemuimg, "create", "-b", $file, "-f", "qcow2", "$basedir/l1");
53         system(qw"cp -a", $file, "$basedir/l1"); # reduce disk IO later
54         for my $i (2..$ENV{NUMDISKS}) {
55                 system($qemuimg, "create" ,"$basedir/$i", $sizegb."G");
56         }
57 }
58
59 if(!$ENV{KEEPHDDS}) {
60         # fresh HDDs
61         for my $i (1..4) {
62                 unlink("$basedir/l$i");
63                 if(-e "$basedir/$i.lvm") {
64                         symlink("$i.lvm","$basedir/l$i");
65                         system("/bin/dd", "if=/dev/zero", "count=1", "of=$basedir/l1"); # for LVM
66                 } else {
67                         system($qemuimg, "create" ,"$basedir/$i", $sizegb."G");
68                         symlink($i,"$basedir/l$i");
69                 }
70         }
71         if($ENV{USBBOOT}) {
72                 $ENV{NUMDISKS}=2;
73                 system("dd", "if=$iso", "of=$basedir/l1", "bs=1M", "conv=notrunc");
74                 @cdrom=();
75         }
76 }
77 sleep 5;
78
79
80 for my $i (1..4) { # create missing symlinks
81         next if -e "$basedir/l$i";
82         next unless -e "$basedir/$i";
83         symlink($i,"$basedir/l$i");
84 }
85 $self->{'pid'}=fork();
86 die "fork failed" if(!defined($self->{'pid'}));
87 if($self->{'pid'}==0) {
88         my @params=($qemubin, qw(-m 1024 -net user -monitor), "tcp:127.0.0.1:$ENV{QEMUPORT},server,nowait", "-net", "nic,model=$ENV{NICMODEL},macaddr=52:54:00:12:34:56", "-serial", "file:serial0", "-soundhw", "ac97", "-vga", $ENV{QEMUVGA}, "-S");
89         if ($ENV{LAPTOP}) {
90             for my $f (<$ENV{LAPTOP}/*.bin>) {
91                 push @params, '-smbios', "file=$f";
92             }
93         }
94
95         for my $i (1..$ENV{NUMDISKS}) {
96                 my $boot="";#$i==1?",boot=on":""; # workaround bnc#696890
97                 push(@params, "-drive", "file=$basedir/l$i,cache=unsafe,if=$ENV{HDDMODEL}$boot");
98         }
99         push(@params, "-boot", "dc", @cdrom) if($iso);
100         if($ENV{VNC}) {
101                 if($ENV{VNC}!~/:/) {$ENV{VNC}=":$ENV{VNC}"}
102                 push(@params, "-vnc", $ENV{VNC});
103                 push(@params, "-k", $ENV{VNCKB}) if($ENV{VNCKB});
104         }
105         if($ENV{QEMUCPU}) { push(@params, "-cpu", $ENV{QEMUCPU}); }
106         if($ENV{UEFI}) { push(@params, "-L", $ENV{UEFI_BIOS_DIR}); }
107         if($ENV{MULTINET}) {push(@params, qw"-net nic,vlan=1,model=virtio,macaddr=52:54:00:12:34:57 -net none,vlan=1")}
108         push(@params, "-usb", "-usbdevice", "tablet");
109         push(@params, "-smp", $ENV{QEMUCPUS});
110         if(-e "/usr/bin/eatmydata") { unshift(@params, "/usr/bin/eatmydata") }
111         bmwqemu::diag("starting: ".join(" ", @params));
112         exec(@params);
113         die "exec $qemubin failed";
114 }
115 open(my $pidf, ">", $self->{'pidfilename'}) or die "can not write ".$self->{'pidfilename'};
116 print $pidf $self->{'pid'},"\n";
117 close $pidf;
118 sleep 6; # time to let qemu start
119
120 1;