line |
stmt |
code |
1
|
|
# Copyright 2017-2020 SUSE LLC |
2
|
|
# SPDX-License-Identifier: GPL-2.0-or-later |
3
|
|
|
4
|
|
|
5
|
|
use Mojo::Base 'Exporter', -signatures; |
6
|
18
|
use Carp; |
|
18
|
|
|
18
|
|
7
|
18
|
use List::Util 'first'; |
|
18
|
|
|
18
|
|
8
|
18
|
use Mojo::File 'path'; |
|
18
|
|
|
18
|
|
9
|
18
|
use bmwqemu; |
|
18
|
|
|
18
|
|
10
|
18
|
use Mojo::IOLoop::ReadWriteProcess 'process'; |
|
18
|
|
|
18
|
|
11
|
18
|
|
|
18
|
|
|
18
|
|
12
|
|
our @EXPORT_OK = qw( |
13
|
|
dd_gen_params |
14
|
|
find_bin |
15
|
|
gen_params |
16
|
|
qv |
17
|
|
quote |
18
|
|
runcmd |
19
|
|
run |
20
|
|
run_diag |
21
|
|
attempt |
22
|
|
); |
23
|
|
|
24
|
|
# An helper to lookup into a folder and find an executable file between given candidates |
25
|
|
# First argument is the directory, the remainining are the candidates. |
26
|
|
|
27
|
87
|
# An helper to full a parameter list, typically used to build option arguments for executing external programs. |
|
39
|
|
|
39
|
|
|
39
|
|
|
39
|
|
|
39
|
|
|
107
|
|
28
|
|
# if the parameter is equal to "", the value is not pushed to the array. |
29
|
|
return unless ($parameter); |
30
|
|
$args{prefix} = "-" unless $args{prefix}; |
31
|
475
|
|
|
475
|
|
|
475
|
|
|
475
|
|
|
475
|
|
|
475
|
|
32
|
475
|
if (ref($parameter) eq "") { |
33
|
471
|
$parameter = quote($parameter) if $parameter =~ /\s+/ && !$args{no_quotes}; |
34
|
|
push(@$array, $args{prefix} . "${argument}", $parameter); |
35
|
471
|
} |
36
|
322
|
elsif (ref($parameter) eq "ARRAY") { |
37
|
322
|
push(@$array, $args{prefix} . "${argument}", join(',', @$parameter)); |
38
|
|
} |
39
|
|
|
40
|
149
|
} |
41
|
|
|
42
|
|
# doubledash shortcut version. Same can be achieved with gen_params. |
43
|
|
gen_params($array, $argument, $parameter, prefix => "--"); |
44
|
|
} |
45
|
|
|
46
|
5
|
# It merely splits a string into pieces interpolating variables inside it. |
|
5
|
|
|
5
|
|
|
5
|
|
|
5
|
|
47
|
5
|
# e.g. gen_params \@params, 'drive', "file=$basedir/l$i,cache=unsafe,if=none,id=hd$i,format=$vars->{HDDFORMAT}" can be rewritten as |
48
|
|
# gen_params \@params, 'drive', [qv "file=$basedir/l$i cache=unsafe if=none id=hd$i format=$vars->{HDDFORMAT}"] |
49
|
|
|
50
|
|
# Add single quote mark to string |
51
|
|
# Mainly use in the case of multiple kernel parameters to be passed to the -append option |
52
|
|
# and they need to be quoted using single or double quotes |
53
|
151
|
|
|
151
|
|
|
151
|
|
|
151
|
|
54
|
|
bmwqemu::diag "running `@args`"; |
55
|
|
my $p = process(execute => shift @args, args => [@args]); |
56
|
|
$p->quirkiness(1)->separate_err(0)->start()->wait_stop(); |
57
|
|
|
58
|
5
|
my $stdout = join('', $p->read_stream->getlines()); |
|
5
|
|
|
5
|
|
|
5
|
|
59
|
|
chomp $stdout; |
60
|
85
|
|
|
85
|
|
|
85
|
|
61
|
85
|
close($p->$_ ? $p->$_ : ()) for qw(read_stream write_stream error_stream); |
62
|
85
|
|
63
|
85
|
return $p->exit_status, $stdout; |
64
|
|
} |
65
|
66
|
|
66
|
66
|
# Do not check for anything - just execute and print |
67
|
|
my ($exit_status, $output); |
68
|
66
|
eval { |
69
|
|
local $SIG{__DIE__} = undef; |
70
|
66
|
($exit_status, $output) = run(@args); |
71
|
|
bmwqemu::diag("Command `@args` terminated with $exit_status" . (length($output) ? "\n$output" : '')); |
72
|
|
}; |
73
|
|
bmwqemu::diag("Fatal error in command `@args`: $@") if ($@); |
74
|
24
|
return $output; |
|
24
|
|
|
24
|
|
75
|
24
|
} |
76
|
24
|
|
77
|
24
|
# Open a process to run external program and check its return status |
78
|
24
|
my ($e, $out) = run(@cmd); |
79
|
5
|
bmwqemu::diag $out if $out && length($out) > 0; |
80
|
|
die "runcmd '" . join(' ', @cmd) . "' failed with exit code $e" . ($out ? ": '$out'" : '') unless $e == 0; |
81
|
24
|
return $e; |
82
|
24
|
} |
83
|
|
|
84
|
|
## use critic |
85
|
|
|
86
|
23
|
|
|
23
|
|
|
23
|
|
87
|
23
|
my $attempts = 0; |
88
|
23
|
my ($total_attempts, $condition, $cb, $or) = ref $_[0] eq 'HASH' ? (@{$_[0]}{qw(attempts condition cb or)}) : @_; |
89
|
23
|
until ($condition->() || $attempts >= $total_attempts) { |
90
|
18
|
bmwqemu::diag "Waiting for $attempts attempts"; |
91
|
|
$cb->(); |
92
|
|
wait_attempt; |
93
|
|
$attempts++; |
94
|
|
} |
95
|
14
|
$or->() if $or && !$condition->(); |
|
14
|
|
|
14
|
|
96
|
|
bmwqemu::diag "Finished after $attempts attempts"; |
97
|
|
} |
98
|
6
|
|
99
|
6
|
1; |
|
5
|
|