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