-
Notifications
You must be signed in to change notification settings - Fork 25
/
proxmox-low-level-installer
executable file
·168 lines (136 loc) · 4.83 KB
/
proxmox-low-level-installer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/perl
use strict;
use warnings;
use lib '.'; # FIXME
use File::Path qw(make_path);
use Getopt::Long;
use JSON;
use Time::HiRes qw(usleep);
{
my $test_image;
GetOptions(
'test-image|t=s' => \$test_image
) or die "usage error\n";
Proxmox::Install::ISOEnv::set_test_image($test_image) if $test_image;
}
use Proxmox::Install::ISOEnv;
use Proxmox::Install::RunEnv;
use Proxmox::Sys::Udev;
use Proxmox::Sys::File qw(file_write_all);
use Proxmox::Log;
use Proxmox::Install;
use Proxmox::Install::Config;
use Proxmox::UI;
my $commands = {
'dump-env' => 'Dump the current ISO and Hardware environment to base the installer UI on.',
'dump-udev' => 'Dump disk and network device info. Used for the auto installation.',
'start-session' => 'Start an installation session, with command and result transmitted via stdin/out',
'start-session-test' => 'Start an installation TEST session, with command and result transmitted via stdin/out',
'help' => 'Output this usage help.',
};
sub usage {
my ($cmd) = @_;
if (!$cmd) {
printf("ERROR: missing command\n\n");
} elsif (!exists($commands->{$cmd})) {
printf("ERROR: unknown command '$cmd'\n\n");
}
print "USAGE: $0 <cmd>\n";
for my $cmd (sort keys $commands->%*) {
printf(" %-20s - %s\n", $cmd, $commands->{$cmd});
}
exit($cmd ne 'help' ? 1 : 0);
}
sub read_and_merge_config {
my $config_raw;
while (my $line = <>) {
if ($line =~ /^\s*\{/) {
$config_raw = $line;
last;
}
}
my $config = eval { from_json($config_raw, { utf8 => 1 }) };
die "failed to parse config from stdin - $@\n" if $@;
Proxmox::Install::Config::merge($config);
log_info("got installation config: ". to_json(Proxmox::Install::Config::get(), { utf8 => 1, canonical => 1 }) ."\n");
file_write_all("/tmp/low-level-config.json", to_json(Proxmox::Install::Config::get()));
}
sub send_reboot_ui_message {
if (Proxmox::Install::Config::get_autoreboot()) {
my $secs = 5;
while ($secs > 0) {
Proxmox::UI::finished(1, "Installation finished - auto-rebooting in $secs seconds ..");
sleep 1;
$secs -= 1;
}
} else {
Proxmox::UI::finished(1, "Installation finished - reboot now?");
}
}
my $cmd = shift;
if (!$cmd || $cmd eq 'help' || !exists($commands->{$cmd})) {
usage($cmd // '');
}
Proxmox::Log::init("/tmp/install-low-level-${cmd}.log");
my $env = Proxmox::Install::ISOEnv::get();
if ($cmd eq 'dump-env') {
Proxmox::UI::init_stdio({}, $env);
my $out_dir = $env->{locations}->{run};
make_path($out_dir);
die "failed to create output directory '$out_dir'\n" if !-d $out_dir;
my $locales_serialized = to_json($env->{locales}, {canonical => 1, utf8 => 1}) ."\n";
file_write_all("$out_dir/locales.json", $locales_serialized);
my $iso_info = {
'iso-info' => $env->{iso},
'product' => $env->{product},
'product-cfg' => $env->{cfg},
'run-env-cache-file' => $env->{'run-env-cache-file'},
'locations' => $env->{locations},
};
my $iso_serialized = to_json($iso_info, {canonical => 1, utf8 => 1}) ."\n";
file_write_all("$out_dir/iso-info.json", $iso_serialized);
my $run_env_file = Proxmox::Install::ISOEnv::get('run-env-cache-file');
my $run_env = Proxmox::Install::RunEnv::query_installation_environment();
my $run_env_serialized = to_json($run_env, {canonical => 1, utf8 => 1}) ."\n";
file_write_all($run_env_file, $run_env_serialized);
} elsif ($cmd eq 'dump-udev') {
my $out_dir = $env->{locations}->{run};
make_path($out_dir);
die "failed to create output directory '$out_dir'\n" if !-d $out_dir;
my $output = {
disks => Proxmox::Sys::Block::udevadm_disk_details(),
nics => Proxmox::Sys::Net::udevadm_netdev_details(),
};
my $output_serialized = to_json($output, {canonical => 1, utf8 => 1}) ."\n";
file_write_all("$out_dir/run-env-udev.json", $output_serialized);
} elsif ($cmd eq 'start-session') {
Proxmox::UI::init_stdio({}, $env);
read_and_merge_config();
eval { Proxmox::Install::extract_data() };
if (my $err = $@) {
# suppress "empty" error as we got some case where the user choose to abort on a prompt,
# there it doesn't make sense to show them an error again, they "caused" it after all.
if ($err ne "\n") {
warn "installation failed - $err\n";
log_error("installation failed: $err");
Proxmox::UI::finished(0, $err);
}
} else {
send_reboot_ui_message();
}
} elsif ($cmd eq 'start-session-test') {
Proxmox::UI::init_stdio({}, $env);
read_and_merge_config();
my $res = Proxmox::UI::prompt("Reply anything?") ? 'ok' : 'not ok';
Proxmox::UI::message("Test Message - got $res");
for (my $i = 1; $i <= 1000; $i += 3) {
Proxmox::UI::progress($i/100000, 0, 100, "foo $i");
if ($i > 500 && $i < 600) {
usleep(50 * 1000);
} else {
usleep(10 * 1000);
}
}
send_reboot_ui_message();
}
exit(0);