]>
Commit | Line | Data |
---|---|---|
89a12446 DM |
1 | #!/usr/bin/perl -w |
2 | ||
7becc472 DM |
3 | $ENV{DEBIAN_FRONTEND} = 'noninteractive'; |
4 | $ENV{LC_ALL} = 'C'; | |
5 | ||
89a12446 DM |
6 | use strict; |
7 | use Getopt::Long; | |
62c05878 | 8 | use IPC::Open2; |
89a12446 DM |
9 | use IPC::Open3; |
10 | use IO::File; | |
89a12446 DM |
11 | use IO::Select; |
12 | use Cwd 'abs_path'; | |
7becc472 DM |
13 | use Gtk3 '-init'; |
14 | use Gtk3::WebKit; | |
89a12446 | 15 | use Encode; |
84761f93 | 16 | use String::ShellQuote; |
7becc472 | 17 | use Data::Dumper; |
a5af22f5 | 18 | use File::Basename; |
89a12446 | 19 | |
3ac71c8f | 20 | my $release = '4.0'; |
89a12446 DM |
21 | |
22 | my $kapi = `uname -r`; | |
23 | chomp $kapi; | |
24 | ||
25 | my $opt_testmode; | |
26 | ||
7becc472 DM |
27 | if (!$ENV{G_SLICE} || $ENV{G_SLICE} ne "always-malloc") { |
28 | die "do not use slice allocator (run with 'G_SLICE=always-malloc ./proxinstall ...')\n"; | |
29 | } | |
30 | ||
89a12446 DM |
31 | if (!GetOptions ('testmode=s' => \$opt_testmode)) { |
32 | die "usage error\n"; | |
33 | exit (-1); | |
34 | } | |
35 | ||
d2120e51 DM |
36 | sub file_read_firstline { |
37 | my ($filename) = @_; | |
38 | ||
39 | my $fh = IO::File->new ($filename, "r"); | |
40 | return undef if !$fh; | |
41 | my $res = <$fh>; | |
42 | chomp $res if $res; | |
43 | $fh->close; | |
44 | return $res; | |
45 | } | |
46 | ||
89a12446 DM |
47 | my $logfd = IO::File->new (">/tmp/install.log"); |
48 | ||
7becc472 | 49 | my $proxmox_dir = $opt_testmode ? Cwd::cwd() : "/var/lib/pve-installer"; |
89a12446 | 50 | |
84761f93 DM |
51 | my $grub_plattform = "pc"; # pc, efi-amd64 or efi-ia32 |
52 | ||
53 | $grub_plattform = "efi-amd64" if -d "/sys/firmware/efi"; | |
89a12446 | 54 | |
32300628 | 55 | my $IPV4OCTET = "(?:25[0-5]|(?:2[0-4]|1[0-9]|[1-9])?[0-9])"; |
d2120e51 DM |
56 | my $IPV4RE = "(?:(?:$IPV4OCTET\\.){3}$IPV4OCTET)"; |
57 | my $ipv4_mask_hash = { | |
58 | '128.0.0.0' => 1, | |
59 | '192.0.0.0' => 2, | |
60 | '224.0.0.0' => 3, | |
61 | '240.0.0.0' => 4, | |
62 | '248.0.0.0' => 5, | |
63 | '252.0.0.0' => 6, | |
64 | '254.0.0.0' => 7, | |
65 | '255.0.0.0' => 8, | |
66 | '255.128.0.0' => 9, | |
67 | '255.192.0.0' => 10, | |
68 | '255.224.0.0' => 11, | |
69 | '255.240.0.0' => 12, | |
70 | '255.248.0.0' => 13, | |
71 | '255.252.0.0' => 14, | |
72 | '255.254.0.0' => 15, | |
73 | '255.255.0.0' => 16, | |
74 | '255.255.128.0' => 17, | |
75 | '255.255.192.0' => 18, | |
76 | '255.255.224.0' => 19, | |
77 | '255.255.240.0' => 20, | |
78 | '255.255.248.0' => 21, | |
79 | '255.255.252.0' => 22, | |
80 | '255.255.254.0' => 23, | |
81 | '255.255.255.0' => 24, | |
82 | '255.255.255.128' => 25, | |
83 | '255.255.255.192' => 26, | |
84 | '255.255.255.224' => 27, | |
85 | '255.255.255.240' => 28, | |
86 | '255.255.255.248' => 29, | |
87 | '255.255.255.252' => 30 | |
88 | }; | |
89 | ||
7becc472 | 90 | my ($window, $cmdbox, $inbox, $htmlview); |
c6ed3b24 | 91 | my ($next, $next_fctn, $target_hd); |
89a12446 | 92 | my ($progress, $progress_status); |
d2120e51 DM |
93 | my ($ipaddress, $ipconf_entry_addr); |
94 | my ($netmask, $ipconf_entry_mask); | |
95 | my ($gateway, $ipconf_entry_gw); | |
96 | my ($dnsserver, $ipconf_entry_dns); | |
89a12446 DM |
97 | my $hostname = 'proxmox'; |
98 | my $domain = 'domain.tld'; | |
d2120e51 | 99 | my $cmdline = file_read_firstline("/proc/cmdline"); |
89a12446 DM |
100 | my $ipconf; |
101 | my $country; | |
102 | my $timezone = 'Europe/Vienna'; | |
103 | my $password; | |
104 | my $mailto; | |
105 | my $keymap = 'en-us'; | |
106 | my $cmap; | |
107 | ||
aed81ff0 DM |
108 | # parse command line args |
109 | ||
110 | my $config_options = {}; | |
111 | ||
80090926 | 112 | if ($cmdline =~ m/\s(ext3|ext4|xfs|zfs)(\s.*)?$/) { |
5c06ced5 DM |
113 | $config_options->{filesys} = $1; |
114 | } else { | |
aeb3d07f | 115 | $config_options->{filesys} = 'ext4'; |
5c06ced5 | 116 | } |
aed81ff0 DM |
117 | |
118 | if ($cmdline =~ m/hdsize=(\d+(\.\d+)?)[\s\n]/i) { | |
119 | $config_options->{hdsize} = $1; | |
120 | } | |
121 | ||
122 | if ($cmdline =~ m/swapsize=(\d+(\.\d+)?)[\s\n]/i) { | |
123 | $config_options->{swapsize} = $1; | |
124 | } | |
125 | ||
126 | if ($cmdline =~ m/maxroot=(\d+(\.\d+)?)[\s\n]/i) { | |
127 | $config_options->{maxroot} = $1; | |
128 | } | |
129 | ||
130 | if ($cmdline =~ m/minfree=(\d+(\.\d+)?)[\s\n]/i) { | |
131 | $config_options->{minfree} = $1; | |
132 | } | |
133 | ||
134 | if ($cmdline =~ m/maxvz=(\d+(\.\d+)?)[\s\n]/i) { | |
135 | $config_options->{maxvz} = $1; | |
136 | } | |
89a12446 DM |
137 | |
138 | my $postfix_main_cf = <<_EOD; | |
139 | # See /usr/share/postfix/main.cf.dist for a commented, more complete version | |
140 | ||
141 | myhostname=__FQDN__ | |
142 | ||
143 | smtpd_banner = \$myhostname ESMTP \$mail_name (Debian/GNU) | |
144 | biff = no | |
145 | ||
146 | # appending .domain is the MUA's job. | |
147 | append_dot_mydomain = no | |
148 | ||
149 | # Uncomment the next line to generate "delayed mail" warnings | |
150 | #delay_warning_time = 4h | |
151 | ||
152 | alias_maps = hash:/etc/aliases | |
153 | alias_database = hash:/etc/aliases | |
154 | mydestination = \$myhostname, localhost.\$mydomain, localhost | |
968fa90b | 155 | relayhost = |
89a12446 DM |
156 | mynetworks = 127.0.0.0/8 |
157 | inet_interfaces = loopback-only | |
158 | recipient_delimiter = + | |
159 | ||
160 | _EOD | |
161 | ||
84761f93 DM |
162 | sub shellquote { |
163 | my $str = shift; | |
164 | ||
165 | return String::ShellQuote::shell_quote($str); | |
166 | } | |
167 | ||
168 | sub cmd2string { | |
169 | my ($cmd) = @_; | |
170 | ||
171 | die "no arguments" if !$cmd; | |
172 | ||
173 | return $cmd if !ref($cmd); | |
174 | ||
175 | my @qa = (); | |
176 | foreach my $arg (@$cmd) { push @qa, shellquote($arg); } | |
177 | ||
178 | return join (' ', @qa); | |
179 | } | |
180 | ||
968fa90b | 181 | sub syscmd { |
89a12446 DM |
182 | my ($cmd) = @_; |
183 | ||
184 | return run_command ($cmd, undef, undef, 1); | |
185 | } | |
968fa90b | 186 | |
89a12446 DM |
187 | sub run_command { |
188 | my ($cmd, $func, $input, $noout) = @_; | |
189 | ||
84761f93 DM |
190 | my $cmdstr; |
191 | if (!ref($cmd)) { | |
192 | $cmdstr = $cmd; | |
193 | if ($cmd =~ m/|/) { | |
194 | # see 'man bash' for option pipefail | |
195 | $cmd = [ '/bin/bash', '-c', "set -o pipefail && $cmd" ]; | |
196 | } else { | |
197 | $cmd = [ $cmd ]; | |
198 | } | |
199 | } else { | |
200 | $cmdstr = cmd2string($cmd); | |
201 | } | |
202 | ||
89a12446 | 203 | my $cmdtxt; |
84761f93 DM |
204 | if ($input && ($cmdstr !~ m/chpasswd/)) { |
205 | $cmdtxt = "# $cmdstr <<EOD\n$input"; | |
89a12446 DM |
206 | chomp $cmdtxt; |
207 | $cmdtxt .= "\nEOD\n"; | |
208 | } else { | |
84761f93 | 209 | $cmdtxt = "# $cmdstr\n"; |
89a12446 | 210 | } |
4a5dbe69 DM |
211 | |
212 | if ($opt_testmode) { | |
213 | print $cmdtxt; | |
214 | STDOUT->flush(); | |
215 | } | |
216 | ||
89a12446 DM |
217 | print $logfd $cmdtxt; |
218 | ||
219 | my $reader = IO::File->new(); | |
220 | my $writer = IO::File->new(); | |
221 | my $error = IO::File->new(); | |
222 | ||
223 | my $orig_pid = $$; | |
224 | ||
225 | my $pid; | |
226 | eval { | |
84761f93 | 227 | $pid = open3 ($writer, $reader, $error, @$cmd) || die $!; |
89a12446 DM |
228 | }; |
229 | ||
230 | my $err = $@; | |
231 | ||
232 | # catch exec errors | |
233 | if ($orig_pid != $$) { | |
968fa90b DM |
234 | POSIX::_exit (1); |
235 | kill ('KILL', $$); | |
89a12446 DM |
236 | } |
237 | ||
238 | die $err if $err; | |
239 | ||
240 | print $writer $input if defined $input; | |
241 | close $writer; | |
242 | ||
243 | my $select = new IO::Select; | |
244 | $select->add ($reader); | |
245 | $select->add ($error); | |
246 | ||
247 | my ($ostream, $logout) = ('', '', ''); | |
248 | ||
249 | while ($select->count) { | |
250 | my @handles = $select->can_read (0.2); | |
251 | ||
d2120e51 | 252 | Gtk3::main_iteration() while Gtk3::events_pending(); |
89a12446 DM |
253 | |
254 | next if !scalar (@handles); # timeout | |
255 | ||
256 | foreach my $h (@handles) { | |
257 | my $buf = ''; | |
258 | my $count = sysread ($h, $buf, 4096); | |
259 | if (!defined ($count)) { | |
260 | my $err = $!; | |
261 | kill (9, $pid); | |
262 | waitpid ($pid, 0); | |
263 | die "command '$cmd' failed: $err"; | |
264 | } | |
265 | $select->remove ($h) if !$count; | |
266 | if ($h eq $reader) { | |
267 | $ostream .= $buf if !($noout || $func); | |
268 | $logout .= $buf; | |
269 | while ($logout =~ s/^([^\010\r\n]*)(\r|\n|(\010)+|\r\n)//s) { | |
270 | my $line = $1; | |
271 | &$func($line) if $func; | |
272 | } | |
273 | ||
274 | } elsif ($h eq $error) { | |
275 | $ostream .= $buf if !($noout || $func); | |
276 | } | |
277 | print $buf; | |
278 | STDOUT->flush(); | |
279 | print $logfd $buf; | |
280 | } | |
281 | } | |
282 | ||
283 | &$func($logout) if $func; | |
284 | ||
285 | my $rv = waitpid ($pid, 0); | |
286 | ||
287 | return $? if $noout; # behave like standard system(); | |
288 | ||
289 | my $ec = ($? >> 8); | |
290 | ||
968fa90b | 291 | if ($ec) { |
4a5dbe69 | 292 | die "command '$cmdstr' failed with exit code $ec"; |
89a12446 DM |
293 | } |
294 | ||
295 | return $ostream; | |
296 | } | |
297 | ||
298 | sub detect_country { | |
299 | ||
300 | print "trying to detect country...\n"; | |
62c05878 DM |
301 | my $cpid = open2(\*TMP, undef, "traceroute -N 1 -q 1 -n 8.8.8.8"); |
302 | return undef if !$cpid; | |
968fa90b | 303 | |
89a12446 DM |
304 | my $country; |
305 | ||
306 | my $previous_alarm = alarm (10); | |
307 | eval { | |
308 | local $SIG{ALRM} = sub { die "timed out!\n" }; | |
309 | my $line; | |
310 | while (defined ($line = <TMP>)) { | |
311 | print $logfd "DC TRACEROUTE: $line"; | |
312 | if ($line =~ m/\s*\d+\s+(\d+\.\d+\.\d+\.\d+)\s/) { | |
313 | my $geoip = `geoiplookup $1`; | |
314 | print $logfd "DC GEOIP: $geoip"; | |
315 | if ($geoip =~ m/GeoIP Country Edition:\s*([A-Z]+),/) { | |
316 | $country = lc ($1); | |
62c05878 | 317 | print $logfd "DC FOUND: $country\n"; |
89a12446 DM |
318 | last; |
319 | } | |
320 | } | |
321 | } | |
322 | }; | |
323 | ||
324 | my $err = $@; | |
325 | ||
326 | alarm ($previous_alarm); | |
327 | ||
328 | close (TMP); | |
329 | ||
330 | if ($err) { | |
331 | print "unable to detect country - $err\n"; | |
332 | } elsif ($country) { | |
333 | print "detected country: " . uc($country) . "\n"; | |
334 | } else { | |
335 | print "unable to detect country\n"; | |
336 | } | |
337 | ||
338 | return $country; | |
339 | } | |
340 | ||
341 | sub get_memtotal { | |
342 | ||
343 | open (MEMINFO, "/proc/meminfo"); | |
344 | ||
345 | my $res = 512; # default to 512 if something goes wrong | |
346 | while (my $line = <MEMINFO>) { | |
347 | if ($line =~ m/^MemTotal:\s+(\d+)\s*kB/i) { | |
348 | $res = int ($1 / 1024); | |
968fa90b | 349 | } |
89a12446 DM |
350 | } |
351 | ||
352 | close (MEMINFO); | |
353 | ||
354 | return $res; | |
355 | } | |
356 | ||
357 | my $total_memory = get_memtotal(); | |
358 | ||
359 | sub link_points_to { | |
360 | my ($src, $dest) = @_; | |
361 | ||
362 | my ($dev1,$ino1) = stat ($src); | |
363 | my ($dev2,$ino2) = stat ($dest); | |
364 | ||
365 | return 0 if !($dev1 && $dev2 && $ino1 && $ino2); | |
366 | ||
367 | return $ino1 == $ino2 && $dev1 == $dev2; | |
368 | } | |
369 | ||
370 | sub find_stable_path { | |
371 | my ($stabledir, $bdev) = @_; | |
372 | ||
a5af22f5 DM |
373 | foreach my $path (<$stabledir/*>) { |
374 | if (link_points_to ($path, $bdev)) { | |
375 | return wantarray ? ($path, basename($path)) : $path; | |
89a12446 | 376 | } |
89a12446 | 377 | } |
89a12446 DM |
378 | } |
379 | ||
380 | sub find_dev_by_uuid { | |
381 | my $bdev = shift; | |
382 | ||
383 | my ($full_path, $name) = find_stable_path ("/dev/disk/by-uuid", $bdev); | |
384 | ||
385 | return $name; | |
386 | } | |
387 | ||
388 | sub hd_list { | |
389 | ||
390 | my $res = (); | |
391 | ||
392 | if ($opt_testmode) { | |
393 | push @$res, [-1, $opt_testmode, int((-s $opt_testmode)/512), "TESTDISK"]; | |
394 | } | |
395 | ||
396 | my $count = 0; | |
397 | ||
398 | foreach my $bd (</sys/block/*>) { | |
399 | next if $bd =~ m|^/sys/block/ram\d+$|; | |
400 | next if $bd =~ m|^/sys/block/loop\d+$|; | |
401 | next if $bd =~ m|^/sys/block/md\d+$|; | |
402 | next if $bd =~ m|^/sys/block/dm-.*$|; | |
403 | next if $bd =~ m|^/sys/block/fd\d+$|; | |
404 | next if $bd =~ m|^/sys/block/sr\d+$|; | |
405 | ||
d2120e51 | 406 | my $dev = file_read_firstline("$bd/dev"); |
89a12446 | 407 | chomp $dev; |
968fa90b | 408 | |
89a12446 DM |
409 | next if !$dev; |
410 | ||
411 | my $info = `udevadm info --path $bd --query all`; | |
412 | next if !$info; | |
413 | ||
414 | next if $info !~ m/^E: DEVTYPE=disk$/m; | |
415 | ||
416 | next if $info =~ m/^E: ID_CDROM/m; | |
417 | ||
418 | my ($name) = $info =~ m/^N: (\S+)$/m; | |
419 | ||
968fa90b | 420 | if ($name) { |
89a12446 DM |
421 | my $real_name = "/dev/$name"; |
422 | ||
d2120e51 | 423 | my $size = file_read_firstline("$bd/size"); |
89a12446 DM |
424 | chomp $size; |
425 | $size = undef if !($size && $size =~ m/^\d+$/); | |
426 | ||
d2120e51 | 427 | my $model = file_read_firstline("$bd/device/model") || ''; |
89a12446 DM |
428 | $model =~ s/^\s+//; |
429 | $model =~ s/\s+$//; | |
430 | if (length ($model) > 30) { | |
431 | $model = substr ($model, 0, 30); | |
432 | } | |
433 | push @$res, [$count++, $real_name, $size, $model] if $size; | |
434 | } else { | |
435 | print STDERR "ERROR: unable to map device $dev ($bd)\n"; | |
436 | } | |
437 | } | |
438 | ||
439 | return $res; | |
440 | } | |
441 | ||
442 | sub read_cmap { | |
968fa90b | 443 | my $countryfn = $opt_testmode ? "/usr/share/pve-manager/country.dat" : |
89a12446 DM |
444 | "/proxmox/country.dat"; |
445 | open (TMP, "<$countryfn") || die "unable to open '$countryfn' - $!\n"; | |
446 | my $line; | |
447 | my $country = {}; | |
448 | my $countryhash = {}; | |
449 | my $kmap = {}; | |
450 | my $kmaphash = {}; | |
451 | while (defined ($line = <TMP>)) { | |
452 | if ($line =~ m|^map:([^\s:]+):([^:]+):([^:]+):([^:]+):([^:]+):([^:]*):$|) { | |
453 | $kmap->{$1} = { | |
454 | name => $2, | |
455 | kvm => $3, | |
456 | console => $4, | |
457 | x11 => $5, | |
458 | x11var => $6, | |
459 | }; | |
460 | $kmaphash->{$2} = $1; | |
461 | } elsif ($line =~ m|^([a-z]{2}):([^:]+):([^:]*):([^:]*):$|) { | |
462 | $country->{$1} = { | |
463 | name => $2, | |
464 | kmap => $3, | |
465 | mirror => $4, | |
466 | }; | |
467 | $countryhash->{lc($2)} = $1; | |
468 | } else { | |
469 | warn "unable to parse 'country.dat' line: $line"; | |
470 | } | |
471 | } | |
472 | close (TMP); | |
473 | ||
474 | my $zones = {}; | |
475 | my $cczones = {}; | |
476 | my $zonefn = "/usr/share/zoneinfo/zone.tab"; | |
477 | open (TMP, "<$zonefn") || die "unable to open '$zonefn' - $!\n"; | |
478 | while (defined ($line = <TMP>)) { | |
479 | next if $line =~ m/^\#/; | |
480 | next if $line =~ m/^\s*$/; | |
481 | if ($line =~ m|^([A-Z][A-Z])\s+\S+\s+(([^/]+)/\S+)\s|) { | |
482 | my $cc = lc($1); | |
483 | $cczones->{$cc}->{$2} = 1; | |
484 | $country->{$cc}->{zone} = $2 if !defined ($country->{$cc}->{zone}); | |
485 | $zones->{$2} = 1; | |
486 | ||
487 | } | |
488 | } | |
489 | close (TMP); | |
490 | ||
491 | return { | |
492 | zones => $zones, | |
493 | cczones => $cczones, | |
494 | country => $country, | |
495 | countryhash => $countryhash, | |
496 | kmap => $kmap, | |
497 | kmaphash => $kmaphash, | |
498 | } | |
499 | } | |
500 | ||
501 | # search for Harddisks | |
502 | my $hds = hd_list (); | |
503 | ||
504 | sub hd_size { | |
505 | my ($dev) = @_; | |
506 | ||
507 | foreach my $hd (@$hds) { | |
508 | my ($disk, $devname, $size, $model) = @$hd; | |
509 | return int($size/2) if $devname eq $dev; | |
510 | } | |
511 | ||
512 | die "no such device '$dev'"; | |
513 | } | |
514 | ||
89a12446 | 515 | sub get_partition_dev { |
c6ed3b24 DM |
516 | my ($dev, $partnum) = @_; |
517 | ||
518 | if ($dev =~ m|^/dev/[hxsev]d[a-z]$|) { | |
519 | return "${dev}$partnum"; | |
520 | } elsif ($dev =~ m|^/dev/[^/]+/c\d+d\d+$|) { | |
521 | return "${dev}p$partnum"; | |
522 | } elsif ($dev =~ m|^/dev/[^/]+/d\d+$|) { | |
523 | return "${dev}p$partnum"; | |
524 | } elsif ($dev =~ m|^/dev/[^/]+/hd[a-z]$|) { | |
525 | return "${dev}$partnum"; | |
89a12446 | 526 | } else { |
c6ed3b24 | 527 | die "unable to get device for partition $partnum on device $dev\n"; |
89a12446 DM |
528 | } |
529 | ||
530 | } | |
531 | ||
8a50920c DM |
532 | sub file_get_contents { |
533 | my ($filename, $max) = @_; | |
534 | ||
535 | my $fh = IO::File->new($filename, "r") || | |
536 | die "can't open '$filename' - $!\n"; | |
537 | ||
538 | local $/; # slurp mode | |
539 | ||
540 | my $content = <$fh>; | |
541 | ||
542 | close $fh; | |
543 | ||
544 | return $content; | |
545 | } | |
546 | ||
89a12446 DM |
547 | sub write_config { |
548 | my ($text, $filename) = @_; | |
549 | ||
550 | my $fd = IO::File->new (">$filename") || | |
551 | die "unable to open file '$filename' - $!"; | |
552 | print $fd $text; | |
553 | $fd->close(); | |
554 | } | |
555 | ||
556 | sub update_progress { | |
557 | my ($frac, $start, $end, $text) = @_; | |
558 | ||
559 | my $part = $end - $start; | |
560 | my $res = $start + $frac*$part; | |
561 | ||
562 | $progress->set_fraction ($res); | |
563 | $progress->set_text (sprintf ("%d%%", int ($res*100))); | |
564 | $progress_status->set_text ($text) if defined ($text); | |
565 | ||
d2120e51 | 566 | Gtk3::main_iteration() while Gtk3::events_pending(); |
89a12446 DM |
567 | } |
568 | ||
80090926 DM |
569 | my $fssetup = { |
570 | ext3 => { | |
571 | mkfs => 'mkfs.ext3 -F', | |
1f8d0104 DM |
572 | mkfs_root_opt => '', |
573 | mkfs_data_opt => '-m 0', | |
80090926 DM |
574 | root_mountopt => 'errors=remount-ro', |
575 | }, | |
576 | ext4 => { | |
577 | mkfs => 'mkfs.ext4 -F', | |
1f8d0104 DM |
578 | mkfs_root_opt => '', |
579 | mkfs_data_opt => '-m 0', | |
80090926 DM |
580 | root_mountopt => 'errors=remount-ro', |
581 | }, | |
582 | xfs => { | |
583 | mkfs => 'mkfs.xfs -f', | |
1f8d0104 DM |
584 | mkfs_root_opt => '', |
585 | mkfs_data_opt => '', | |
80090926 DM |
586 | root_mountopt => '', |
587 | }, | |
588 | }; | |
589 | ||
89a12446 | 590 | sub create_filesystem { |
1f8d0104 | 591 | my ($dev, $name, $type, $start, $end, $fs, $fe) = @_; |
89a12446 DM |
592 | |
593 | my $range = $end - $start; | |
594 | my $rs = $start + $range*$fs; | |
595 | my $re = $start + $range*$fe; | |
596 | my $max = 0; | |
597 | ||
80090926 | 598 | my $fsdata = $fssetup->{$type} || die "internal error - unknown file system '$type'"; |
1f8d0104 | 599 | my $opts = $name eq 'root' ? $fsdata->{mkfs_root_opt} : $fsdata->{mkfs_data_opt}; |
80090926 | 600 | |
89a12446 DM |
601 | update_progress (0, $rs, $re, "creating $name filesystem"); |
602 | ||
80090926 | 603 | run_command ("$fsdata->{mkfs} $opts $dev", sub { |
89a12446 DM |
604 | my $line = shift; |
605 | ||
606 | if ($line =~ m/Writing inode tables:\s+(\d+)\/(\d+)/) { | |
607 | $max = $2; | |
608 | } elsif ($max && $line =~ m/(\d+)\/$max/) { | |
609 | update_progress (($1/$max)*0.9, $rs, $re); | |
610 | } elsif ($line =~ m/Creating journal.*done/) { | |
611 | update_progress (0.95, $rs, $re); | |
612 | } elsif ($line =~ m/Writing superblocks and filesystem.*done/) { | |
613 | update_progress (1, $rs, $re); | |
968fa90b | 614 | } |
89a12446 DM |
615 | }); |
616 | } | |
617 | ||
618 | sub debconfig_set { | |
619 | my ($targetdir, $dcdata) = @_; | |
620 | ||
621 | my $cfgfile = "/tmp/debconf.txt"; | |
622 | write_config ($dcdata, "$targetdir/$cfgfile"); | |
968fa90b DM |
623 | syscmd ("chroot $targetdir debconf-set-selections $cfgfile"); |
624 | unlink "$targetdir/$cfgfile"; | |
89a12446 DM |
625 | } |
626 | ||
627 | sub diversion_add { | |
628 | my ($targetdir, $cmd, $new_cmd) = @_; | |
629 | ||
630 | syscmd ("chroot $targetdir dpkg-divert --package proxmox " . | |
631 | "--add --rename $cmd") == 0 || | |
632 | die "unable to exec dpkg-divert\n"; | |
633 | ||
634 | syscmd ("ln -sf ${new_cmd} $targetdir/$cmd") == 0 || | |
968fa90b | 635 | die "unable to link diversion to ${new_cmd}\n"; |
89a12446 DM |
636 | } |
637 | ||
638 | sub diversion_remove { | |
639 | my ($targetdir, $cmd) = @_; | |
640 | ||
641 | syscmd ("mv $targetdir/${cmd}.distrib $targetdir/${cmd};") == 0 || | |
642 | die "unable to remove $cmd diversion\n"; | |
968fa90b | 643 | |
89a12446 DM |
644 | syscmd ("chroot $targetdir dpkg-divert --remove $cmd") == 0 || |
645 | die "unable to remove $cmd diversion\n"; | |
646 | } | |
647 | ||
5c06ced5 | 648 | sub zfs_create_rpool { |
c6ed3b24 | 649 | my ($zfspoolname, $vdev) = @_; |
486c490d | 650 | |
52682627 | 651 | syscmd ("zpool create -f -o ashift=12 -o cachefile=none $zfspoolname $vdev") == 0 || |
5c06ced5 DM |
652 | die "unable to create zfs root pool\n"; |
653 | ||
654 | syscmd ("zfs create $zfspoolname/ROOT") == 0 || | |
7bc4f6bd | 655 | die "unable to create zfs $zfspoolname/ROOT volume\n"; |
5c06ced5 DM |
656 | |
657 | syscmd ("zfs create $zfspoolname/ROOT/pve-1") == 0 || | |
7bc4f6bd | 658 | die "unable to create zfs $zfspoolname/ROOT/pve-1 volume\n"; |
5c06ced5 DM |
659 | |
660 | # disable atime during insatll | |
661 | syscmd ("zfs set atime=off $zfspoolname") == 0 || | |
662 | die "unable to set zfs properties\n"; | |
25b62a3d | 663 | syscmd ("zfs set compression=lz4 $zfspoolname") == 0 || |
5c06ced5 DM |
664 | die "unable to set zfs properties\n"; |
665 | } | |
666 | ||
7bc4f6bd DM |
667 | sub zfs_create_swap { |
668 | my ($zfspoolname, $swapsize) = @_; | |
669 | ||
670 | syscmd ("zfs create -V ${swapsize}K -b 4K $zfspoolname/swap") == 0 || | |
671 | die "unable to create zfs swap device\n"; | |
672 | ||
673 | syscmd ("zfs set com.sun:auto-snapshot=false $zfspoolname/swap") == 0 || | |
674 | die "unable to set zfs properties\n"; | |
675 | ||
676 | # reduces memory pressure | |
677 | syscmd ("zfs set sync=always $zfspoolname/swap") == 0 || | |
678 | die "unable to set zfs properties\n"; | |
679 | ||
680 | return "/dev/zvol/$zfspoolname/swap"; | |
681 | } | |
682 | ||
c6ed3b24 DM |
683 | sub partition_bootable_disk { |
684 | my ($target_dev, $maxhdsize, $use_zfs) = @_; | |
89a12446 | 685 | |
c6ed3b24 | 686 | die "too dangerous" if $opt_testmode; |
89a12446 | 687 | |
6ab785ef | 688 | syscmd("sgdisk -Z ${target_dev}"); |
c6ed3b24 DM |
689 | my $hdsize = hd_size($target_dev); # size in blocks (1024 bytes) |
690 | ||
691 | if ($maxhdsize && ($maxhdsize < $hdsize)) { | |
692 | $hdsize = $maxhdsize; | |
693 | } | |
694 | ||
695 | my $hdgb = int($hdsize/(1024*1024)); | |
696 | die "hardisk '$target_dev' too small (${hdsize}GB)" if $hdgb < 8; | |
697 | ||
698 | # 1 - GRUB boot partition: 1M | |
a2876e48 DM |
699 | # 2 - EFI ESP: 128M |
700 | # 3 - OS/Data partition | |
c6ed3b24 DM |
701 | |
702 | my $grubbootdev = get_partition_dev($target_dev, 1); | |
703 | my $efibootdev = get_partition_dev($target_dev, 2); | |
a2876e48 | 704 | my $osdev = get_partition_dev ($target_dev, 3); |
aed81ff0 | 705 | |
c6ed3b24 | 706 | my $hdsize_mb = $hdsize/1024; |
5c06ced5 | 707 | |
f810f5d0 | 708 | my $pcmd = ['sgdisk', '-a1']; |
89a12446 | 709 | |
f810f5d0 DM |
710 | my $pnum = 1; |
711 | push @$pcmd, "-n$pnum:34:2047", "-t$pnum:EF02"; | |
b282cfe8 | 712 | |
f810f5d0 DM |
713 | $pnum = 3; |
714 | # Note: from 128M to end | |
715 | push @$pcmd, "-n$pnum:128M:0", "-t$pnum:8E00"; | |
b282cfe8 | 716 | |
f810f5d0 DM |
717 | |
718 | $pnum = 2; | |
719 | # Note: from sector 2048 to 128M (0 mean till next partition) | |
720 | push @$pcmd, "-n$pnum:2048:0", "-t$pnum:EF00"; | |
721 | ||
722 | push @$pcmd, $target_dev; | |
b282cfe8 | 723 | |
f810f5d0 | 724 | my $os_size = $hdsize - 128*1024; |
89a12446 | 725 | |
f810f5d0 DM |
726 | syscmd($pcmd) == 0 || |
727 | die "unable to partition harddisk '${target_dev}'\n"; | |
89a12446 | 728 | |
f810f5d0 DM |
729 | return ($os_size, $osdev, $efibootdev); |
730 | } | |
5c06ced5 | 731 | |
f810f5d0 DM |
732 | # ZFS has this use_whole_disk concept, so we try to partition the same |
733 | # way as zfs does by default. There is room at start of disk to insert | |
734 | # a grub boot partition. But adding a EFI ESP is not possible. | |
735 | # | |
736 | # Note: zfs people think this is just a waste of space an not | |
737 | # required. Instead, you should put the ESP on another disk (log, | |
738 | # ..). | |
89a12446 | 739 | |
f810f5d0 DM |
740 | sub partition_bootable_zfs_disk { |
741 | my ($target_dev) = @_; | |
742 | ||
743 | die "too dangerous" if $opt_testmode; | |
744 | ||
649d65b6 | 745 | syscmd("sgdisk -Z ${target_dev}"); |
f810f5d0 DM |
746 | my $hdsize = hd_size($target_dev); # size in blocks (1024 bytes) |
747 | ||
748 | my $hdgb = int($hdsize/(1024*1024)); | |
749 | die "hardisk '$target_dev' too small (${hdsize}GB)" if $hdgb < 8; | |
750 | ||
751 | # 1 - GRUB boot partition: 1M | |
752 | # 2 - OS/Data partition | |
753 | # 9 - ZFS reserved partition | |
754 | ||
755 | my $grubbootdev = get_partition_dev($target_dev, 1); | |
756 | my $osdev = get_partition_dev ($target_dev, 2); | |
757 | ||
758 | my $pcmd = ['sgdisk', '-a1']; | |
759 | ||
760 | my $pnum = 1; | |
761 | push @$pcmd, "-n$pnum:34:2047", "-t$pnum:EF02"; | |
762 | ||
763 | $pnum = 9; | |
764 | push @$pcmd, "-n$pnum:-8M:0", "-t$pnum:BF07"; | |
765 | ||
766 | $pnum = 2; | |
767 | push @$pcmd, "-n$pnum:2048:0", "-t$pnum:BF01", '-c', "$pnum:zfs"; | |
768 | ||
769 | push @$pcmd, $target_dev; | |
c6ed3b24 | 770 | |
f810f5d0 | 771 | my $os_size = $hdsize - 1024 - 1024*8; |
968fa90b | 772 | |
c6ed3b24 DM |
773 | syscmd($pcmd) == 0 || |
774 | die "unable to partition harddisk '${target_dev}'\n"; | |
968fa90b | 775 | |
f810f5d0 | 776 | return ($os_size, $osdev); |
c6ed3b24 | 777 | } |
84761f93 | 778 | |
c6ed3b24 DM |
779 | sub create_lvm_volumes { |
780 | my ($lvmdev, $os_size, $swap_size) = @_; | |
7bc4f6bd | 781 | |
c6ed3b24 DM |
782 | my $rootdev = '/dev/pve/root'; |
783 | my $datadev = '/dev/pve/data'; | |
784 | my $swapfile = '/dev/pve/swap'; | |
84761f93 | 785 | |
c6ed3b24 DM |
786 | # we use --metadatasize 250k, which reseults in "pe_start = 512" |
787 | # so pe_start is aligned on a 128k boundary (advantage for SSDs) | |
788 | syscmd ("/sbin/pvcreate --metadatasize 250k -y -ff $lvmdev") == 0 || | |
789 | die "unable to initialize physical volume $lvmdev"; | |
790 | syscmd ("/sbin/vgcreate pve $lvmdev") == 0 || | |
791 | die "unable to create volume group"; | |
89a12446 | 792 | |
c6ed3b24 DM |
793 | my $hdgb = int($os_size/(1024*1024)); |
794 | my $space = (($hdgb > 128) ? 16 : ($hdgb/8))*1024*1024; | |
89a12446 | 795 | |
c6ed3b24 DM |
796 | my $maxroot; |
797 | if ($config_options->{maxroot}) { | |
798 | $maxroot = $config_options->{maxroot}; | |
799 | } else { | |
800 | $maxroot = 96; | |
801 | } | |
89a12446 | 802 | |
c6ed3b24 | 803 | my $rootsize = (($hdgb > ($maxroot*4)) ? $maxroot : $hdgb/4)*1024*1024; |
89a12446 | 804 | |
c6ed3b24 | 805 | my $rest = $os_size - $swap_size - $rootsize; # in KB |
7bc4f6bd | 806 | |
c6ed3b24 DM |
807 | my $minfree; |
808 | if ($config_options->{minfree}) { | |
809 | $minfree = (($config_options->{minfree}*1024*1024) >= $rest ) ? $space : | |
810 | $config_options->{minfree}*1024*1024 ; | |
811 | } else { | |
812 | $minfree = $space; | |
813 | } | |
7bc4f6bd | 814 | |
c6ed3b24 | 815 | $rest = $rest - $minfree; |
7bc4f6bd | 816 | |
c6ed3b24 DM |
817 | if ($config_options->{maxvz}) { |
818 | $rest = (($config_options->{maxvz}*1024*1024) <= $rest) ? | |
819 | $config_options->{maxvz}*1024*1024 : $rest; | |
820 | } | |
7bc4f6bd | 821 | |
c6ed3b24 DM |
822 | syscmd ("/sbin/lvcreate -L${swap_size}K -nswap pve") == 0 || |
823 | die "unable to create swap volume"; | |
89a12446 | 824 | |
c6ed3b24 DM |
825 | syscmd ("/sbin/lvcreate -L${rootsize}K -nroot pve") == 0 || |
826 | die "unable to create root volume"; | |
89a12446 | 827 | |
c6ed3b24 DM |
828 | syscmd ("/sbin/lvcreate -L${rest}K -ndata pve") == 0 || |
829 | die "unable to create data volume"; | |
89a12446 | 830 | |
c6ed3b24 DM |
831 | syscmd ("/sbin/vgchange -a y pve") == 0 || |
832 | die "unable to activate volume group"; | |
7bc4f6bd | 833 | |
c6ed3b24 DM |
834 | return ($rootdev, $datadev, $swapfile); |
835 | } | |
7bc4f6bd | 836 | |
c6ed3b24 DM |
837 | sub compute_swapsize { |
838 | my ($hdsize) = @_; | |
89a12446 | 839 | |
c6ed3b24 | 840 | my $hdgb = int($hdsize/(1024*1024)); |
5c06ced5 | 841 | |
c6ed3b24 DM |
842 | my $swapsize; |
843 | if ($config_options->{swapsize}) { | |
844 | $swapsize = $config_options->{swapsize}*1024*1024; | |
845 | } else { | |
846 | my $ss = int ($total_memory / 1024); | |
847 | $ss = 4 if $ss < 4; | |
848 | $ss = ($hdgb/8) if $ss > ($hdgb/8); | |
849 | $swapsize = $ss*1024*1024; | |
850 | } | |
851 | } | |
5c06ced5 | 852 | |
c6ed3b24 DM |
853 | sub extract_data { |
854 | my ($tgzfile, $targetdir) = @_; | |
89a12446 | 855 | |
c6ed3b24 | 856 | die "target '$targetdir' does not exist\n" if ! -d $targetdir; |
89a12446 | 857 | |
c6ed3b24 | 858 | my $bootdevinfo = []; |
84761f93 | 859 | |
c6ed3b24 DM |
860 | my $datadev; |
861 | my $swapfile; | |
862 | my $rootdev; | |
84761f93 | 863 | |
c6ed3b24 DM |
864 | my $use_zfs; |
865 | my $zfspoolname = $opt_testmode ? "test_rpool" : 'rpool'; | |
89092156 | 866 | |
c6ed3b24 | 867 | my $filesys = $config_options->{filesys}; |
89092156 | 868 | |
c6ed3b24 DM |
869 | if ($filesys =~ m/zfs/) { |
870 | $target_hd = undef; # do not use this config | |
871 | $use_zfs = 1; | |
872 | $targetdir = "/$zfspoolname/ROOT/pve-1"; | |
873 | } else { | |
874 | $use_zfs = 0; | |
875 | } | |
876 | ||
877 | if ($use_zfs) { | |
878 | my $i; | |
879 | for ($i = 5; $i > 0; $i--) { | |
880 | syscmd("modprobe zfs"); | |
881 | last if -c "/dev/zfs"; | |
882 | sleep(1); | |
883 | } | |
89092156 | 884 | |
c6ed3b24 DM |
885 | die "unable to load zfs kernel module\n" if !$i; |
886 | } | |
89092156 | 887 | |
c6ed3b24 | 888 | eval { |
89a12446 | 889 | |
89a12446 | 890 | |
c6ed3b24 | 891 | my $maxper = 0.25; |
89a12446 | 892 | |
c6ed3b24 DM |
893 | update_progress (0, 0, $maxper, "create partitions"); |
894 | ||
895 | if ($opt_testmode) { | |
89a12446 | 896 | |
5c06ced5 | 897 | $rootdev = abs_path($target_hd); |
89a12446 | 898 | syscmd ("umount $rootdev"); |
5c06ced5 DM |
899 | |
900 | if ($use_zfs) { | |
901 | ||
c6ed3b24 DM |
902 | die "unsupported zfs mode (for testing environment)\n" |
903 | if $filesys ne 'zfs (RAID0)'; | |
904 | ||
5c06ced5 DM |
905 | syscmd ("zpool destroy $zfspoolname") if $opt_testmode; |
906 | ||
907 | zfs_create_rpool($zfspoolname, $rootdev); | |
908 | } | |
c6ed3b24 DM |
909 | |
910 | } elsif ($use_zfs) { | |
911 | ||
c6ed3b24 DM |
912 | my ($devlist, $bootdevlist, $vdev) = get_zfs_raid_setup(); |
913 | ||
914 | my $disksize; | |
915 | foreach my $hd (@$bootdevlist) { | |
916 | my $devname = @$hd[1]; | |
f810f5d0 DM |
917 | my ($size, $osdev) = |
918 | partition_bootable_zfs_disk($devname); | |
c6ed3b24 DM |
919 | die "unable to mirror disks with different sizes!\n" |
920 | if $disksize && ($size != $disksize); | |
f810f5d0 | 921 | push @$bootdevinfo, { devname => $devname, osdev => $osdev}; |
c6ed3b24 | 922 | $disksize = $size; |
c6ed3b24 DM |
923 | } |
924 | ||
925 | sleep(1); # give kernel time to reread part table | |
926 | ||
927 | # trigger udev to create /dev/disk/by-uuid | |
928 | syscmd ("udevadm trigger --subsystem-match block"); | |
929 | syscmd ("udevadm settle --timeout 10"); | |
930 | ||
35c6f89c DM |
931 | foreach my $di (@$bootdevinfo) { |
932 | my $devname = $di->{devname}; | |
933 | $di->{by_id} = find_stable_path ("/dev/disk/by-id", $devname); | |
934 | ||
935 | # Note: using /dev/disk/by-id/ does not work for unknown reason, we get | |
936 | # cannot create 'rpool': no such pool or dataset | |
937 | #my $osdev = find_stable_path ("/dev/disk/by-id", $di->{osdev}) || $di->{osdev}; | |
c6ed3b24 | 938 | |
35c6f89c DM |
939 | my $osdev = $di->{osdev}; |
940 | $vdev =~ s/ $devname/ $osdev/; | |
941 | } | |
942 | ||
943 | zfs_create_rpool($zfspoolname, $vdev); | |
944 | ||
c6ed3b24 DM |
945 | my $swap_size = compute_swapsize($disksize); |
946 | $swapfile = zfs_create_swap($zfspoolname, $swap_size); | |
947 | ||
948 | } else { | |
949 | ||
950 | die "target '$target_hd' is not a valid block device\n" if ! -b $target_hd; | |
951 | ||
952 | my $maxhdsize; | |
953 | if ($config_options->{hdsize}) { | |
954 | # max hdsize passed on cmdline (GB) | |
955 | $maxhdsize = $config_options->{hdsize}*1024*1024; | |
956 | } | |
957 | ||
958 | my ($os_size, $osdev, $efidev); | |
a2876e48 | 959 | ($os_size, $osdev, $efidev) = |
f810f5d0 | 960 | partition_bootable_disk($target_hd, $maxhdsize); |
c6ed3b24 | 961 | |
c6ed3b24 DM |
962 | sleep(1); # give kernel time to reread part table |
963 | ||
964 | # trigger udev to create /dev/disk/by-uuid | |
965 | syscmd ("udevadm trigger --subsystem-match block"); | |
966 | syscmd ("udevadm settle --timeout 10"); | |
967 | ||
35c6f89c DM |
968 | my $by_id = find_stable_path ("/dev/disk/by-id", $target_hd); |
969 | push @$bootdevinfo, { esp => $efidev, devname => $target_hd, | |
970 | osdev => $osdev, by_id => $by_id }; | |
c6ed3b24 | 971 | |
35c6f89c DM |
972 | my $swap_size = compute_swapsize($os_size); |
973 | ($rootdev, $datadev, $swapfile) = | |
974 | create_lvm_volumes($osdev, $os_size, $swap_size); | |
c6ed3b24 | 975 | |
35c6f89c DM |
976 | # trigger udev to create /dev/disk/by-uuid |
977 | syscmd ("udevadm trigger --subsystem-match block"); | |
978 | syscmd ("udevadm settle --timeout 10"); | |
89a12446 DM |
979 | } |
980 | ||
481671c3 DM |
981 | if ($use_zfs) { |
982 | # to be fast during installation | |
983 | syscmd ("zfs set sync=disabled $zfspoolname") == 0 || | |
984 | die "unable to set zfs properties\n"; | |
985 | } | |
986 | ||
89a12446 DM |
987 | update_progress (0.03, 0, $maxper, "create swap space"); |
988 | if ($swapfile) { | |
7bc4f6bd | 989 | syscmd ("mkswap -f $swapfile") == 0 || |
89a12446 DM |
990 | die "unable to create swap space\n"; |
991 | } | |
992 | ||
993 | update_progress (0.05, 0, $maxper, "creating filesystems"); | |
994 | ||
c6ed3b24 | 995 | foreach my $di (@$bootdevinfo) { |
f810f5d0 | 996 | next if !$di->{esp}; |
c6ed3b24 DM |
997 | syscmd ("mkfs.vfat -F32 $di->{esp}") == 0 || |
998 | die "unable to initialize EFI ESP on device $di->{esp}\n"; | |
999 | } | |
1000 | ||
c6ed3b24 DM |
1001 | if (!$use_zfs) { |
1002 | ||
1003 | if ( -b $target_hd) { | |
1004 | ||
a2876e48 | 1005 | create_filesystem ($rootdev, 'root', $filesys, 0.05, $maxper, 0, 0.5); |
1f8d0104 | 1006 | create_filesystem ($datadev, 'data', $filesys, 0.05, $maxper, 0.5, 1); |
c6ed3b24 DM |
1007 | |
1008 | } else { | |
1009 | ||
1f8d0104 | 1010 | create_filesystem ($rootdev, 'root', $filesys, 0.05, $maxper, 0, 1); |
5c06ced5 | 1011 | } |
89a12446 DM |
1012 | } |
1013 | ||
1014 | update_progress (1, 0.05, $maxper, "mounting target $rootdev"); | |
1015 | ||
35c6f89c DM |
1016 | if (!$use_zfs) { |
1017 | syscmd ("mount -n $rootdev -o noatime,barrier=0 $targetdir") == 0 || | |
1018 | die "unable to mount $rootdev\n"; | |
1019 | } | |
89a12446 | 1020 | |
35c6f89c DM |
1021 | mkdir "$targetdir/boot"; |
1022 | mkdir "$targetdir/boot/efi"; | |
89a12446 | 1023 | |
35c6f89c | 1024 | if ($datadev) { |
84761f93 | 1025 | |
35c6f89c DM |
1026 | mkdir "$targetdir/var"; |
1027 | mkdir "$targetdir/var/lib"; | |
1028 | mkdir "$targetdir/var/lib/vz"; | |
1029 | syscmd ("mount -n $datadev $targetdir/var/lib/vz") == 0 || | |
1030 | die "unable to mount $datadev\n"; | |
89a12446 DM |
1031 | } |
1032 | ||
1033 | display_html ("extract2-rulesystem.htm"); | |
1034 | update_progress (1, 0.05, $maxper, "extracting base system"); | |
1035 | ||
1036 | my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size) = stat ($tgzfile); | |
1037 | $ino || die "unable to open file '$tgzfile' - $!\n"; | |
968fa90b | 1038 | |
89a12446 DM |
1039 | my $files; |
1040 | if ($opt_testmode) { | |
d2120e51 | 1041 | $files = file_read_firstline("/pve/$release/install/pve-base.cnt"); |
89a12446 | 1042 | } else { |
d2120e51 | 1043 | $files = file_read_firstline("/proxmox/pve-base.cnt"); |
89a12446 DM |
1044 | } |
1045 | ||
1046 | my $per = 0; | |
1047 | my $count = 0; | |
1048 | ||
1049 | run_command ("tar xvf $tgzfile -C $targetdir", sub { | |
1050 | my $line = shift; | |
1051 | $count++; | |
1052 | my $nper = int (($count *100)/$files); | |
1053 | if ($nper != $per) { | |
1054 | $per = $nper; | |
1055 | my $frac = $per > 100 ? 100 : $per/100; | |
1056 | update_progress ($frac, $maxper, 0.5); | |
1057 | } | |
1058 | }); | |
1059 | ||
1060 | syscmd ("mount -n -t tmpfs tmpfs $targetdir/tmp") == 0 || | |
1061 | die "unable to mount tmpfs on $targetdir/tmp\n"; | |
1062 | syscmd ("mount -n -t proc proc $targetdir/proc") == 0 || | |
1063 | die "unable to mount proc on $targetdir/proc\n"; | |
1064 | syscmd ("mount -n -t sysfs sysfs $targetdir/sys") == 0 || | |
1065 | die "unable to mount sysfs on $targetdir/sys\n"; | |
1066 | ||
1067 | display_html ("extract3-spam.htm"); | |
1068 | update_progress (1, $maxper, 0.5, "configuring base system"); | |
1069 | ||
1070 | # configure hosts | |
1071 | ||
968fa90b | 1072 | my $hosts = |
89a12446 | 1073 | "127.0.0.1 localhost.localdomain localhost\n" . |
968fa90b | 1074 | "$ipaddress $hostname.$domain $hostname pvelocalhost\n\n" . |
89a12446 DM |
1075 | "# The following lines are desirable for IPv6 capable hosts\n\n" . |
1076 | "::1 ip6-localhost ip6-loopback\n" . | |
1077 | "fe00::0 ip6-localnet\n" . | |
1078 | "ff00::0 ip6-mcastprefix\n" . | |
1079 | "ff02::1 ip6-allnodes\n" . | |
1080 | "ff02::2 ip6-allrouters\n" . | |
1081 | "ff02::3 ip6-allhosts\n"; | |
1082 | ||
968fa90b | 1083 | write_config ($hosts, "$targetdir/etc/hosts"); |
89a12446 | 1084 | |
968fa90b | 1085 | write_config ("$hostname\n", "$targetdir/etc/hostname"); |
89a12446 DM |
1086 | |
1087 | syscmd ("/bin/hostname $hostname") if !$opt_testmode; | |
1088 | ||
1089 | # configure interfaces | |
1090 | ||
968fa90b | 1091 | my $ifaces = |
89a12446 DM |
1092 | "auto lo\niface lo inet loopback\n\n" . |
1093 | "auto vmbr0\niface vmbr0 inet static\n" . | |
1094 | "\taddress $ipaddress\n" . | |
1095 | "\tnetmask $netmask\n" . | |
1096 | "\tgateway $gateway\n" . | |
1097 | "\tbridge_ports eth0\n" . | |
1098 | "\tbridge_stp off\n" . | |
1099 | "\tbridge_fd 0\n"; | |
1100 | ||
1101 | write_config ($ifaces, "$targetdir/etc/network/interfaces"); | |
1102 | ||
1103 | # configure dns | |
1104 | ||
1105 | my $resolfconf = "search $domain\nnameserver $dnsserver\n"; | |
1106 | write_config ($resolfconf, "$targetdir/etc/resolv.conf"); | |
1107 | ||
5c06ced5 DM |
1108 | # configure fstab |
1109 | ||
1110 | my $fstab = "# <file system> <mount point> <type> <options> <dump> <pass>\n"; | |
1111 | ||
1112 | if (!$use_zfs) { | |
80090926 DM |
1113 | my $root_mountopt = $fssetup->{$filesys}->{root_mountopt} || 'defaults'; |
1114 | $fstab .= "$rootdev / $filesys ${root_mountopt} 0 1\n"; | |
5c06ced5 | 1115 | $fstab .= "$datadev /var/lib/vz $filesys defaults 0 1\n" if $datadev; |
7bc4f6bd | 1116 | } |
a84ea010 DM |
1117 | |
1118 | # mount /boot/efi | |
1119 | # Note: this is required by current grub, but really dangerous, because | |
1120 | # vfat does not have journaling, so it triggers manual fsck after each crash | |
1121 | # so we only mount /boot/efi if really required (efi systems). | |
1122 | if ($grub_plattform =~ m/^efi-/) { | |
1123 | if (scalar(@$bootdevinfo)) { | |
f810f5d0 DM |
1124 | my $di = @$bootdevinfo[0]; # simply use first disk |
1125 | if ($di->{esp}) { | |
1126 | my $efi_boot_uuid = $di->{esp}; | |
1127 | if (my $uuid = find_dev_by_uuid ($di->{esp})) { | |
1128 | $efi_boot_uuid = "UUID=$uuid"; | |
1129 | } | |
84761f93 | 1130 | |
f810f5d0 DM |
1131 | $fstab .= "${efi_boot_uuid} /boot/efi vfat defaults 0 1\n"; |
1132 | } | |
a84ea010 | 1133 | } |
84761f93 DM |
1134 | } |
1135 | ||
a84ea010 | 1136 | |
89a12446 DM |
1137 | $fstab .= "$swapfile none swap sw 0 0\n" if $swapfile; |
1138 | ||
1139 | $fstab .= "proc /proc proc defaults 0 0\n"; | |
1140 | ||
1141 | write_config ($fstab, "$targetdir/etc/fstab"); | |
1142 | write_config ("", "$targetdir/etc/mtab"); | |
968fa90b | 1143 | |
89a12446 | 1144 | syscmd ("cp ${proxmox_dir}/policy-disable-rc.d " . |
968fa90b | 1145 | "$targetdir/usr/sbin/policy-rc.d") == 0 || |
89a12446 DM |
1146 | die "unable to copy policy-rc.d\n"; |
1147 | syscmd ("cp ${proxmox_dir}/fake-start-stop-daemon " . | |
968fa90b | 1148 | "$targetdir/sbin/") == 0 || |
89a12446 DM |
1149 | die "unable to copy start-stop-daemon\n"; |
1150 | ||
1151 | diversion_add ($targetdir, "/sbin/start-stop-daemon", "/sbin/fake-start-stop-daemon"); | |
1152 | diversion_add ($targetdir, "/usr/sbin/update-grub", "/bin/true"); | |
1153 | diversion_add ($targetdir, "/usr/sbin/update-initramfs", "/bin/true"); | |
1154 | ||
1155 | syscmd ("touch $targetdir/proxmox_install_mode"); | |
1156 | ||
e35d5efb | 1157 | my $grub_install_devices_txt = ''; |
3573c046 | 1158 | foreach my $di (@$bootdevinfo) { |
e35d5efb | 1159 | $grub_install_devices_txt .= ', ' if $grub_install_devices_txt; |
ff863262 | 1160 | $grub_install_devices_txt .= $di->{by_id} || $di->{devname}; |
3573c046 DM |
1161 | } |
1162 | ||
814f5c39 DM |
1163 | # Note: debian-installer/locale is used by console-setup |
1164 | ||
89a12446 DM |
1165 | debconfig_set ($targetdir, <<_EOD); |
1166 | locales locales/default_environment_locale select en_US.UTF-8 | |
1167 | locales locales/locales_to_be_generated select en_US.UTF-8 UTF-8 | |
1168 | samba-common samba-common/dhcp boolean false | |
1169 | samba-common samba-common/workgroup string WORKGROUP | |
1170 | postfix postfix/main_mailer_type select Local only | |
814f5c39 DM |
1171 | keyboard-configuration keyboard-configuration/xkb-keymap select $keymap |
1172 | d-i debian-installer/locale select en_US.UTF-8 | |
3573c046 | 1173 | grub-pc grub-pc/install_devices select $grub_install_devices_txt |
89a12446 DM |
1174 | _EOD |
1175 | ||
89a12446 DM |
1176 | my $pkgdir = $opt_testmode ? "packages" : "/proxmox/packages"; |
1177 | my $pkg_count = 0; | |
1178 | while (<$pkgdir/*.deb>) { $pkg_count++ }; | |
1179 | ||
1180 | $count = 0; | |
1181 | while (<$pkgdir/*.deb>) { | |
1182 | chomp; | |
1183 | my $path = $_; | |
1184 | my ($deb) = $path =~ m/$pkgdir\/(.*\.deb)/; | |
1e61f3d8 DM |
1185 | # if ($deb =~ m/^grub-efi-/ && $deb !~ m/^grub-${grub_plattform}/) { |
1186 | # $count++; | |
1187 | # next; | |
1188 | # } | |
89a12446 DM |
1189 | update_progress ($count/$pkg_count, 0.5, 0.75, "extracting $deb"); |
1190 | print "extracting: $deb\n"; | |
1191 | syscmd ("cp $path $targetdir/tmp/$deb") == 0 || | |
1192 | die "installation of package $deb failed\n"; | |
1193 | syscmd ("chroot $targetdir dpkg --force-depends --no-triggers --unpack /tmp/$deb") == 0 || | |
968fa90b | 1194 | die "installation of package $deb failed\n"; |
89a12446 DM |
1195 | update_progress ((++$count)/$pkg_count, 0.5, 0.75); |
1196 | } | |
1197 | ||
1198 | display_html ("extract4-virus.htm"); | |
1199 | ||
1200 | my $cmd = "chroot $targetdir dpkg --force-confold --configure -a"; | |
1201 | $count = 0; | |
1202 | run_command ($cmd, sub { | |
1203 | my $line = shift; | |
1204 | if ($line =~ m/Setting up\s+(\S+)/) { | |
1205 | update_progress ((++$count)/$pkg_count, 0.75, 0.95, | |
1206 | "configuring $1"); | |
1207 | } | |
1208 | }); | |
968fa90b DM |
1209 | |
1210 | debconfig_set ($targetdir, <<_EOD); | |
89a12446 DM |
1211 | postfix postfix/main_mailer_type select No configuration |
1212 | _EOD | |
1213 | ||
1214 | unlink "$targetdir/etc/mailname"; | |
1215 | $postfix_main_cf =~ s/__FQDN__/${hostname}.${domain}/; | |
1216 | write_config ($postfix_main_cf, "$targetdir/etc/postfix/main.cf"); | |
1217 | ||
1218 | # make sure we have all postfix directories | |
1219 | syscmd ("chroot $targetdir /usr/sbin/postfix check"); | |
1220 | # cleanup mail queue | |
1221 | syscmd ("chroot $targetdir /usr/sbin/postsuper -d ALL"); | |
1222 | ||
1223 | unlink "$targetdir/proxmox_install_mode"; | |
1224 | ||
968fa90b | 1225 | # set timezone |
89a12446 DM |
1226 | unlink ("$targetdir/etc/localtime"); |
1227 | symlink ("/usr/share/zoneinfo/$timezone", "$targetdir/etc/localtime"); | |
1228 | write_config ("$timezone\n", "$targetdir/etc/timezone"); | |
1229 | ||
89a12446 DM |
1230 | # set apt mirror |
1231 | if (my $mirror = $cmap->{country}->{$country}->{mirror}) { | |
1232 | my $fn = "$targetdir/etc/apt/sources.list"; | |
968fa90b | 1233 | syscmd ("sed -i 's/ftp\\.debian\\.org/$mirror/' '$fn'"); |
89a12446 DM |
1234 | } |
1235 | ||
19edf8b7 DM |
1236 | # create extended_states for apt (avoid cron job warning if that |
1237 | # file does not exist) | |
1238 | write_config ('', "$targetdir/var/lib/apt/extended_states"); | |
1239 | ||
c2657b8b DM |
1240 | # allow ssh root login |
1241 | syscmd ("sed -i 's/^PermitRootLogin.*/PermitRootLogin yes/' '$targetdir/etc/ssh/sshd_config'"); | |
1242 | ||
89a12446 DM |
1243 | # save installer settings |
1244 | my $ucc = uc ($country); | |
1245 | debconfig_set ($targetdir, <<_EOD); | |
1246 | pve-manager pve-manager/country string $ucc | |
1247 | _EOD | |
1248 | ||
1249 | update_progress (0.8, 0.95, 1, "make system bootable"); | |
1250 | ||
5c06ced5 | 1251 | if ($use_zfs) { |
52f45831 | 1252 | syscmd ("sed -i -e 's/^GRUB_CMDLINE_LINUX=.*/GRUB_CMDLINE_LINUX=\"root=ZFS=$zfspoolname\\/ROOT\\/pve-1 boot=zfs\"/' $targetdir/etc/default/grub") == 0 || |
5c06ced5 DM |
1253 | die "unable to update /etc/default/grub\n"; |
1254 | ||
1255 | } | |
23c337f5 | 1256 | |
89a12446 DM |
1257 | diversion_remove ($targetdir, "/usr/sbin/update-grub"); |
1258 | diversion_remove ($targetdir, "/usr/sbin/update-initramfs"); | |
1259 | ||
c6ed3b24 | 1260 | if (!$opt_testmode) { |
89a12446 DM |
1261 | |
1262 | unlink ("$targetdir/etc/mtab"); | |
1263 | symlink ("/proc/mounts", "$targetdir/etc/mtab"); | |
1264 | syscmd ("mount -n --bind /dev $targetdir/dev"); | |
1265 | ||
1266 | syscmd ("chroot $targetdir /usr/sbin/update-initramfs -c -k $kapi") == 0 || | |
1267 | die "unable to install initramfs\n"; | |
1268 | ||
c6ed3b24 DM |
1269 | foreach my $di (@$bootdevinfo) { |
1270 | my $dev = $di->{devname}; | |
f810f5d0 DM |
1271 | syscmd ("chroot $targetdir /usr/sbin/grub-install --target i386-pc --no-floppy --bootloader-id='proxmox' $dev") == 0 || |
1272 | die "unable to install the i386-pc boot loader on '$dev'\n"; | |
1273 | ||
1274 | if ($di->{esp}) { | |
1275 | syscmd ("mount -n $di->{esp} $targetdir/boot/efi") == 0 || | |
1276 | die "unable to mount $di->{esp}\n"; | |
1277 | syscmd ("chroot $targetdir /usr/sbin/grub-install --target x86_64-efi --no-floppy --bootloader-id='proxmox' $dev") == 0 || | |
1278 | die "unable to install the EFI boot loader on '$dev'\n"; | |
1279 | syscmd ("umount $targetdir/boot/efi") == 0 || | |
1280 | die "unable to umount $targetdir/boot/efi\n"; | |
1e61f3d8 | 1281 | } |
c6ed3b24 | 1282 | } |
89a12446 DM |
1283 | |
1284 | syscmd ("chroot $targetdir /usr/sbin/update-grub") == 0 || | |
c6ed3b24 | 1285 | die "unable to update boot loader config\n"; |
89a12446 DM |
1286 | |
1287 | syscmd ("umount $targetdir/dev"); | |
1288 | } | |
1289 | ||
968fa90b | 1290 | # cleanup |
89a12446 | 1291 | |
968fa90b | 1292 | # hack: remove dead.letter from sshd installation |
89a12446 DM |
1293 | syscmd ("rm -rf $targetdir/dead.letter"); |
1294 | ||
89a12446 DM |
1295 | unlink "$targetdir/usr/sbin/policy-rc.d"; |
1296 | ||
1297 | diversion_remove ($targetdir, "/sbin/start-stop-daemon"); | |
1298 | ||
1299 | # set root password | |
968fa90b | 1300 | my $octets = encode("utf-8", $password); |
89a12446 DM |
1301 | run_command ("chroot $targetdir /usr/sbin/chpasswd", undef, |
1302 | "root:$octets\n"); | |
7053f98b DM |
1303 | |
1304 | # create pmxcfs DB | |
1305 | ||
1306 | my $tmpdir = "$targetdir/tmp/pve"; | |
1307 | mkdir $tmpdir; | |
1308 | ||
1309 | # write vnc keymap to datacenter.cfg | |
1310 | my $vnckmap = $cmap->{kmap}->{$keymap}->{kvm} || 'en-us'; | |
968fa90b | 1311 | write_config ("keyboard: $vnckmap\n", |
7053f98b DM |
1312 | "$tmpdir/datacenter.cfg"); |
1313 | ||
1314 | # save admin email | |
968fa90b | 1315 | write_config ("user:root\@pam:1:0:::${mailto}::\n", |
7053f98b | 1316 | "$tmpdir/user.cfg"); |
968fa90b | 1317 | |
7053f98b DM |
1318 | run_command("chroot $targetdir /usr/bin/create_pmxcfs_db /tmp/pve /var/lib/pve-cluster/config.db"); |
1319 | ||
1320 | syscmd ("rm -rf $tmpdir"); | |
89a12446 DM |
1321 | }; |
1322 | ||
1323 | my $err = $@; | |
1324 | ||
1325 | update_progress (1, 0, 1, ""); | |
1326 | ||
1327 | print $err if $err; | |
1328 | ||
1329 | if ($opt_testmode) { | |
1330 | syscmd ("chroot $targetdir /usr/bin/dpkg-query -W --showformat='\${package}\n'> pve-final.pkglist"); | |
1331 | } | |
1332 | ||
89a12446 DM |
1333 | syscmd ("umount $targetdir/var/lib/vz"); |
1334 | syscmd ("umount $targetdir/tmp"); | |
1335 | syscmd ("umount $targetdir/proc"); | |
1336 | syscmd ("umount $targetdir/sys"); | |
6fbd1fb1 DM |
1337 | |
1338 | if ($use_zfs) { | |
1339 | syscmd ("zfs umount -a") == 0 || | |
1340 | die "unable to unmount zfs\n"; | |
1341 | } else { | |
1342 | syscmd ("umount -d $targetdir"); | |
1343 | } | |
89a12446 | 1344 | |
5c06ced5 | 1345 | if (!$err && $use_zfs) { |
481671c3 DM |
1346 | syscmd ("zfs set sync=standard $zfspoolname") == 0 || |
1347 | die "unable to set zfs properties\n"; | |
1348 | ||
5c06ced5 DM |
1349 | syscmd ("zfs set mountpoint=/ $zfspoolname/ROOT/pve-1") == 0 || |
1350 | die "zfs set mountpoint failed\n"; | |
1351 | ||
1352 | syscmd ("zpool set bootfs=$zfspoolname/ROOT/pve-1 $zfspoolname") == 0 || | |
1353 | die "zfs set bootfs failed\n"; | |
1354 | } | |
1355 | ||
89a12446 DM |
1356 | die $err if $err; |
1357 | } | |
1358 | ||
1359 | sub display_html { | |
1360 | my ($filename) = @_; | |
1361 | ||
8a50920c DM |
1362 | my $path = "${proxmox_dir}/html/$filename"; |
1363 | my $url = "file://$path"; | |
1364 | ||
1365 | my $data = file_get_contents($path); | |
1366 | ||
1367 | if ($filename eq 'license.htm') { | |
1368 | my $licensefn = -f "/EULA" ? "/EULA" : | |
1369 | $opt_testmode ? "./copyright" : | |
1370 | "/usr/share/doc/pve-installer/copyright"; | |
f91c161b DM |
1371 | my $license = decode('utf8', file_get_contents($licensefn)); |
1372 | $license =~ m/^\s+(.+\n)/; | |
1373 | my $title = $licensefn eq '/EULA' ? "END USER LICENSE AGREEMENT (EULA)": $1; | |
1374 | $data =~ s/__LICENSE__/$license/; | |
8a50920c DM |
1375 | $data =~ s/__LICENSE_TITLE__/$title/; |
1376 | } | |
1377 | ||
1378 | $htmlview->load_html_string($data, $url); | |
7becc472 DM |
1379 | } |
1380 | ||
89a12446 DM |
1381 | sub set_next { |
1382 | my ($text, $fctn) = @_; | |
1383 | ||
1384 | $next_fctn = $fctn; | |
1385 | $text = "_Next" if !$text; | |
1386 | $next->set_label ($text); | |
968fa90b | 1387 | |
89a12446 DM |
1388 | $next->grab_focus (); |
1389 | } | |
1390 | ||
1391 | sub url_requested { | |
1392 | my ($doc, $url, $stream) = @_; | |
968fa90b | 1393 | |
89a12446 DM |
1394 | $stream->set_cancel_func (sub {}); # hack: avoid warning |
1395 | ||
1396 | my $path = "${proxmox_dir}/html/$url"; | |
1397 | ||
1398 | if (-f $path) { | |
1399 | open (HTMLTMP, $path) || | |
1400 | die "unable to open file '$path' - $! "; | |
1401 | my $buf; | |
1402 | while (my $i = read (HTMLTMP, $buf, 4096)) { | |
968fa90b | 1403 | $stream->write ($buf); |
d2120e51 | 1404 | Gtk3::main_iteration() while Gtk3::events_pending(); |
89a12446 DM |
1405 | } |
1406 | close (HTMLTMP); | |
1407 | } | |
1408 | ||
1409 | #$stream->close(); # hack: dont close - avoid crash | |
1410 | } | |
1411 | ||
1412 | sub create_main_window { | |
1413 | ||
7becc472 | 1414 | $window = Gtk3::Window->new (); |
89a12446 | 1415 | $window->set_default_size (1024, 768); |
84761f93 | 1416 | $window->set_has_resize_grip(0); |
89a12446 DM |
1417 | $window->set_decorated (0) if !$opt_testmode; |
1418 | ||
7becc472 | 1419 | my $vbox = Gtk3::VBox->new (0, 0); |
89a12446 | 1420 | |
7becc472 | 1421 | my $image = Gtk3::Image->new_from_file ("${proxmox_dir}/proxlogo.xpm"); |
89a12446 DM |
1422 | $vbox->pack_start ($image, 0, 0, 0); |
1423 | ||
7becc472 | 1424 | my $hbox = Gtk3::HBox->new (0, 0); |
89a12446 DM |
1425 | $vbox->pack_start ($hbox, 1, 1, 0); |
1426 | ||
7becc472 DM |
1427 | # my $f1 = Gtk3::Frame->new ('test'); |
1428 | # $f1->set_shadow_type ('none'); | |
1429 | # $hbox->pack_start ($f1, 1, 1, 0); | |
89a12446 | 1430 | |
7becc472 | 1431 | my $sep1 = Gtk3::HSeparator->new; |
89a12446 DM |
1432 | $vbox->pack_start ($sep1, 0, 0, 0); |
1433 | ||
7becc472 | 1434 | $cmdbox = Gtk3::HBox->new (); |
89a12446 DM |
1435 | $vbox->pack_start ($cmdbox, 0, 0, 10); |
1436 | ||
7becc472 | 1437 | $next = Gtk3::Button->new ('_Next'); |
89a12446 DM |
1438 | $next->signal_connect (clicked => sub { &$next_fctn (); }); |
1439 | $cmdbox->pack_end ($next, 0, 0, 10); | |
7becc472 DM |
1440 | my $abort = Gtk3::Button->new ('_Abort'); |
1441 | $abort->set_can_focus (0); | |
89a12446 DM |
1442 | $cmdbox->pack_start ($abort, 0, 0, 10); |
1443 | $abort->signal_connect (clicked => sub { exit (-1); }); | |
1444 | ||
7becc472 DM |
1445 | my $vbox2 = Gtk3::VBox->new (0, 0); |
1446 | $hbox->add ($vbox2); | |
89a12446 | 1447 | |
7becc472 DM |
1448 | $htmlview = Gtk3::WebKit::WebView->new(); |
1449 | my $scrolls = Gtk3::ScrolledWindow->new(); | |
1450 | $scrolls->add($htmlview); | |
1451 | ||
1452 | my $hbox2 = Gtk3::HBox->new (0, 0); | |
1453 | $hbox2->pack_start ($scrolls, 1, 1, 0); | |
89a12446 DM |
1454 | |
1455 | $vbox2->pack_start ($hbox2, 1, 1, 0); | |
1456 | ||
7becc472 | 1457 | my $vbox3 = Gtk3::VBox->new (0, 0); |
89a12446 DM |
1458 | $vbox2->pack_start ($vbox3, 0, 0, 0); |
1459 | ||
7becc472 | 1460 | my $sep2 = Gtk3::HSeparator->new; |
89a12446 DM |
1461 | $vbox3->pack_start ($sep2, 0, 0, 0); |
1462 | ||
7becc472 | 1463 | $inbox = Gtk3::HBox->new (0, 0); |
89a12446 DM |
1464 | $vbox3->pack_start ($inbox, 0, 0, 0); |
1465 | ||
1466 | $window->add ($vbox); | |
1467 | ||
1468 | $window->show_all; | |
1469 | $window->realize (); | |
1470 | } | |
1471 | ||
d2120e51 DM |
1472 | sub cleanup_view { |
1473 | $inbox->foreach(sub { | |
1474 | my $child = shift; | |
1475 | $inbox->remove ($child); | |
1476 | }); | |
89a12446 DM |
1477 | } |
1478 | ||
aed81ff0 DM |
1479 | # fixme: newer GTK3 has special properties to handle numbers with Entry |
1480 | # only allow floating point numbers with Gtk3::Entry | |
1481 | sub check_float { | |
1482 | my ($entry, $event) = @_; | |
1483 | ||
1484 | ||
1485 | my $val = $event->get_keyval; | |
1486 | ||
1487 | if ($val == ord '.' || | |
1488 | $val == Gtk3::Gdk::KEY_ISO_Left_Tab || | |
1489 | $val == Gtk3::Gdk::KEY_Shift_L || | |
1490 | $val == Gtk3::Gdk::KEY_Tab || | |
1491 | $val == Gtk3::Gdk::KEY_Left || | |
1492 | $val == Gtk3::Gdk::KEY_Right || | |
1493 | $val == Gtk3::Gdk::KEY_BackSpace || | |
1494 | $val == Gtk3::Gdk::KEY_Delete || | |
1495 | ($val >= ord '0' && $val <= ord '9') || | |
1496 | ($val >= Gtk3::Gdk::KEY_KP_0 && | |
1497 | $val <= Gtk3::Gdk::KEY_KP_9)) { | |
1498 | return undef; | |
1499 | } | |
1500 | ||
1501 | return 1; | |
1502 | } | |
1503 | ||
d2120e51 | 1504 | sub create_text_input { |
89a12446 DM |
1505 | my ($default, $text) = @_; |
1506 | ||
7becc472 | 1507 | my $hbox = Gtk3::HBox->new (0, 0); |
89a12446 | 1508 | |
7becc472 | 1509 | my $label = Gtk3::Label->new ($text); |
89a12446 DM |
1510 | $label->set_size_request (150, -1); |
1511 | $label->set_alignment (1, 0.5); | |
1512 | $hbox->pack_start ($label, 0, 0, 10); | |
7becc472 | 1513 | my $e1 = Gtk3::Entry->new (); |
89a12446 DM |
1514 | $e1->set_width_chars (30); |
1515 | $hbox->pack_start ($e1, 0, 0, 0); | |
1516 | $e1->set_text ($default); | |
1517 | ||
1518 | return ($hbox, $e1); | |
1519 | } | |
1520 | ||
89a12446 DM |
1521 | sub get_ip_config { |
1522 | ||
1523 | my $ifconfig = `ifconfig eth0`; | |
1524 | ||
1525 | my ($addr) = $ifconfig =~ m/inet addr:(\S*)/m; | |
1526 | my ($mask) = $ifconfig =~ m/Mask:(\S*)/m; | |
1527 | ||
1528 | my $route = `route -n`; | |
1529 | my ($gateway) = $route =~ m/^0\.0\.0\.0\s+(\d+\.\d+\.\d+\.\d+)\s+/m; | |
1530 | ||
1531 | my $resolvconf = `cat /etc/resolv.conf`; | |
1532 | my ($dnsserver) = $resolvconf =~ m/^nameserver\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/m; | |
713790a4 | 1533 | my ($domain) = $resolvconf =~ m/^domain\s+(\S+)$/m; |
89a12446 DM |
1534 | |
1535 | return { | |
1536 | addr => $addr, | |
1537 | mask => $mask, | |
1538 | gateway => $gateway, | |
1539 | dnsserver => $dnsserver, | |
713790a4 | 1540 | domain => $domain, |
89a12446 DM |
1541 | } |
1542 | } | |
1543 | ||
1544 | sub display_message { | |
1545 | my ($msg) = @_; | |
1546 | ||
7becc472 | 1547 | my $dialog = Gtk3::MessageDialog->new ($window, 'modal', |
89a12446 DM |
1548 | 'info', 'ok', $msg); |
1549 | $dialog->run(); | |
1550 | $dialog->destroy(); | |
1551 | } | |
1552 | ||
1553 | sub display_error { | |
1554 | my ($msg) = @_; | |
1555 | ||
7becc472 | 1556 | my $dialog = Gtk3::MessageDialog->new ($window, 'modal', |
89a12446 DM |
1557 | 'error', 'ok', $msg); |
1558 | $dialog->run(); | |
1559 | $dialog->destroy(); | |
1560 | } | |
1561 | ||
1562 | sub create_ipconf_view { | |
1563 | ||
1564 | cleanup_view (); | |
1565 | display_html ("ipconf.htm"); | |
1566 | ||
7becc472 | 1567 | my $vbox = Gtk3::VBox->new (0, 0); |
89a12446 | 1568 | $inbox->pack_start ($vbox, 1, 0, 0); |
7becc472 | 1569 | my $hbox = Gtk3::HBox->new (0, 0); |
53986d77 | 1570 | $vbox->pack_start ($hbox, 0, 0, 10); |
7becc472 | 1571 | my $vbox2 = Gtk3::VBox->new (0, 0); |
89a12446 DM |
1572 | $hbox->add ($vbox2); |
1573 | ||
1574 | my $addr = $ipconf->{addr} || '192.168.100.2'; | |
1575 | my $mask = $ipconf->{mask} || '255.255.255.0'; | |
968fa90b | 1576 | |
713790a4 DM |
1577 | my $hn = $ipconf->{domain} ? "pve.$ipconf->{domain}" : 'pve.domain.tld'; |
1578 | ||
968fa90b | 1579 | my ($hostbox, $hostentry) = |
d2120e51 | 1580 | create_text_input ($hn, 'Hostname (FQDN):'); |
89a12446 DM |
1581 | $vbox2->pack_start ($hostbox, 0, 0, 2); |
1582 | ||
1583 | my $ipbox; | |
d2120e51 DM |
1584 | ($ipbox, $ipconf_entry_addr) = |
1585 | create_text_input ($addr, 'IP Address:'); | |
89a12446 DM |
1586 | $vbox2->pack_start ($ipbox, 0, 0, 2); |
1587 | ||
1588 | my $maskbox; | |
d2120e51 DM |
1589 | ($maskbox, $ipconf_entry_mask) = |
1590 | create_text_input ($mask, 'Netmask:'); | |
89a12446 DM |
1591 | $vbox2->pack_start ($maskbox, 0, 0, 2); |
1592 | ||
1593 | $gateway = $ipconf->{gateway} || '192.168.100.1'; | |
1594 | ||
1595 | my $gwbox; | |
d2120e51 DM |
1596 | ($gwbox, $ipconf_entry_gw) = |
1597 | create_text_input ($gateway, 'Gateway:'); | |
89a12446 | 1598 | |
53986d77 | 1599 | $vbox2->pack_start ($gwbox, 0, 0, 2); |
89a12446 DM |
1600 | |
1601 | $dnsserver = $ipconf->{dnsserver} || $gateway; | |
1602 | ||
1603 | my $dnsbox; | |
d2120e51 DM |
1604 | ($dnsbox, $ipconf_entry_dns) = |
1605 | create_text_input ($dnsserver, 'DNS Server:'); | |
89a12446 DM |
1606 | |
1607 | $vbox2->pack_start ($dnsbox, 0, 0, 0); | |
1608 | ||
1609 | $inbox->show_all; | |
968fa90b | 1610 | set_next (undef, sub { |
d2120e51 DM |
1611 | |
1612 | # verify hostname | |
1613 | ||
89a12446 | 1614 | my $text = $hostentry->get_text(); |
968fa90b | 1615 | |
89a12446 DM |
1616 | $text =~ s/^\s+//; |
1617 | $text =~ s/\s+$//; | |
1618 | ||
ac3757a9 | 1619 | my $namere = "([a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?)"; |
968fa90b | 1620 | |
ac3757a9 | 1621 | if ($text && $text =~ m/^(${namere}\.)*${namere}$/ && $text !~ m/.domain.tld$/ && |
89a12446 DM |
1622 | $text =~ m/^([^\.]+)\.(\S+)$/) { |
1623 | $hostname = $1; | |
1624 | $domain = $2; | |
d2120e51 DM |
1625 | } else { |
1626 | display_message ("Hostname does not look like a fully qualified domain name."); | |
1627 | $hostentry->grab_focus(); | |
89a12446 DM |
1628 | return; |
1629 | } | |
d2120e51 DM |
1630 | |
1631 | # verify ip address | |
1632 | ||
1633 | $text = $ipconf_entry_addr->get_text(); | |
1634 | $text =~ s/^\s+//; | |
1635 | $text =~ s/\s+$//; | |
1636 | if ($text =~ m!^($IPV4RE)$!) { | |
1637 | $ipaddress = $text; | |
1638 | } else { | |
1639 | display_message ("IP address is not valid."); | |
1640 | $ipconf_entry_addr->grab_focus(); | |
1641 | return; | |
1642 | } | |
1643 | ||
1644 | $text = $ipconf_entry_mask->get_text(); | |
1645 | $text =~ s/^\s+//; | |
1646 | $text =~ s/\s+$//; | |
1647 | if (defined($ipv4_mask_hash->{$text})) { | |
1648 | $netmask = $text; | |
1649 | } else { | |
1650 | display_message ("Netmask is not valid."); | |
1651 | $ipconf_entry_mask->grab_focus(); | |
1652 | return; | |
1653 | } | |
1654 | ||
1655 | $text = $ipconf_entry_gw->get_text(); | |
1656 | $text =~ s/^\s+//; | |
1657 | $text =~ s/\s+$//; | |
1658 | if ($text =~ m!^($IPV4RE)$!) { | |
1659 | $gateway = $text; | |
1660 | } else { | |
1661 | display_message ("Gateway is not valid."); | |
1662 | $ipconf_entry_gw->grab_focus(); | |
1663 | return; | |
1664 | } | |
1665 | ||
1666 | $text = $ipconf_entry_dns->get_text(); | |
1667 | $text =~ s/^\s+//; | |
1668 | $text =~ s/\s+$//; | |
1669 | if ($text =~ m!^($IPV4RE)$!) { | |
1670 | $dnsserver = $text; | |
1671 | } else { | |
1672 | display_message ("DNS server is not valid."); | |
1673 | $ipconf_entry_dns->grab_focus(); | |
1674 | return; | |
1675 | } | |
1676 | ||
1677 | #print "TEST $ipaddress $netmask $gateway $dnsserver\n"; | |
1678 | ||
1679 | create_extract_view (); | |
89a12446 DM |
1680 | }); |
1681 | ||
1682 | $hostentry->grab_focus(); | |
1683 | } | |
1684 | ||
1685 | sub get_device_desc { | |
1686 | my ($devname, $size, $model) = @_; | |
1687 | ||
d2120e51 | 1688 | if ($size && ($size > 0)) { |
89a12446 DM |
1689 | $size = int($size/2048); # size in MB |
1690 | ||
d2120e51 | 1691 | my $text = "$devname ("; |
89a12446 DM |
1692 | if ($size >= 1024) { |
1693 | $size = int($size/1024); # size in GB | |
d2120e51 | 1694 | $text .= "${size}GB"; |
89a12446 | 1695 | } else { |
d2120e51 | 1696 | $text .= "${size}MB"; |
89a12446 DM |
1697 | } |
1698 | ||
d2120e51 DM |
1699 | $text .= ", $model" if $model; |
1700 | $text .= ")"; | |
1701 | ||
89a12446 DM |
1702 | } else { |
1703 | return $devname; | |
1704 | } | |
1705 | } | |
1706 | ||
1707 | sub update_layout { | |
1708 | my ($cb, $kmap) = @_; | |
1709 | ||
1710 | my $ind; | |
1711 | my $def; | |
1712 | my $i = 0; | |
1713 | my $kmaphash = $cmap->{kmaphash}; | |
1714 | foreach my $layout (sort keys %$kmaphash) { | |
1715 | $def = $i if $kmaphash->{$layout} eq 'en-us'; | |
1716 | $ind = $i if $kmap && $kmaphash->{$layout} eq $kmap; | |
1717 | $i++; | |
1718 | } | |
1719 | ||
1720 | $cb->set_active ($ind || $def || 0); | |
1721 | } | |
1722 | ||
1723 | my $lastzonecb; | |
1724 | sub update_zonelist { | |
1725 | my ($box, $cc) = @_; | |
1726 | ||
1727 | my $cczones = $cmap->{cczones}; | |
1728 | my $zones = $cmap->{zones}; | |
1729 | ||
1730 | my $sel; | |
1731 | if ($lastzonecb) { | |
1732 | $sel = $lastzonecb->get_active_text(); | |
1733 | $box->remove ($lastzonecb); | |
1734 | } else { | |
1735 | $sel = $timezone; # used once to select default | |
1736 | } | |
1737 | ||
bcbfab6b | 1738 | my $cb = $lastzonecb = Gtk3::ComboBoxText->new(); |
89a12446 DM |
1739 | $cb->set_size_request (200, -1); |
1740 | ||
1741 | $cb->signal_connect ('changed' => sub { | |
1742 | $timezone = $cb->get_active_text(); | |
1743 | }); | |
1744 | ||
1745 | my @za; | |
1746 | if ($cc && defined ($cczones->{$cc})) { | |
1747 | @za = keys %{$cczones->{$cc}}; | |
1748 | } else { | |
1749 | @za = keys %$zones; | |
1750 | } | |
1751 | my $ind; | |
1752 | my $i = 0; | |
1753 | foreach my $zone (sort @za) { | |
1754 | $ind = $i if $sel && $zone eq $sel; | |
1755 | $cb->append_text ($zone); | |
1756 | $i++; | |
1757 | } | |
1758 | ||
1759 | $cb->set_active ($ind || 0); | |
1760 | ||
1761 | $cb->show; | |
1762 | $box->pack_start ($cb, 0, 0, 0); | |
1763 | } | |
1764 | ||
1765 | sub create_password_view { | |
1766 | ||
1767 | cleanup_view (); | |
1768 | ||
7becc472 | 1769 | my $vbox2 = Gtk3::VBox->new (0, 0); |
89a12446 | 1770 | $inbox->pack_start ($vbox2, 1, 0, 0); |
7becc472 | 1771 | my $vbox = Gtk3::VBox->new (0, 0); |
53986d77 | 1772 | $vbox2->pack_start ($vbox, 0, 0, 10); |
89a12446 | 1773 | |
7becc472 DM |
1774 | my $hbox1 = Gtk3::HBox->new (0, 0); |
1775 | my $label = Gtk3::Label->new ("Password"); | |
89a12446 DM |
1776 | $label->set_size_request (150, -1); |
1777 | $label->set_alignment (1, 0.5); | |
1778 | $hbox1->pack_start ($label, 0, 0, 10); | |
7becc472 | 1779 | my $pwe1 = Gtk3::Entry->new (); |
89a12446 DM |
1780 | $pwe1->set_visibility (0); |
1781 | $pwe1->set_size_request (200, -1); | |
1782 | $hbox1->pack_start ($pwe1, 0, 0, 0); | |
1783 | ||
7becc472 DM |
1784 | my $hbox2 = Gtk3::HBox->new (0, 0); |
1785 | $label = Gtk3::Label->new ("Confirm"); | |
89a12446 DM |
1786 | $label->set_size_request (150, -1); |
1787 | $label->set_alignment (1, 0.5); | |
1788 | $hbox2->pack_start ($label, 0, 0, 10); | |
7becc472 | 1789 | my $pwe2 = Gtk3::Entry->new (); |
89a12446 DM |
1790 | $pwe2->set_visibility (0); |
1791 | $pwe2->set_size_request (200, -1); | |
1792 | $hbox2->pack_start ($pwe2, 0, 0, 0); | |
1793 | ||
7becc472 DM |
1794 | my $hbox3 = Gtk3::HBox->new (0, 0); |
1795 | $label = Gtk3::Label->new ("E-Mail"); | |
89a12446 DM |
1796 | $label->set_size_request (150, -1); |
1797 | $label->set_alignment (1, 0.5); | |
1798 | $hbox3->pack_start ($label, 0, 0, 10); | |
7becc472 | 1799 | my $eme = Gtk3::Entry->new (); |
89a12446 DM |
1800 | $eme->set_size_request (200, -1); |
1801 | $hbox3->pack_start ($eme, 0, 0, 0); | |
1802 | ||
1803 | ||
1804 | $vbox->pack_start ($hbox1, 0, 0, 5); | |
1805 | $vbox->pack_start ($hbox2, 0, 0, 5); | |
1806 | $vbox->pack_start ($hbox3, 0, 0, 15); | |
1807 | ||
1808 | $inbox->show_all; | |
1809 | ||
1810 | display_html ("passwd.htm"); | |
1811 | ||
1812 | set_next (undef, sub { | |
1813 | ||
1814 | my $t1 = $pwe1->get_text; | |
1815 | my $t2 = $pwe2->get_text; | |
1816 | ||
1817 | if (length ($t1) < 5) { | |
1818 | display_message ("Password is too short."); | |
1819 | $pwe1->grab_focus(); | |
1820 | return; | |
1821 | } | |
1822 | ||
1823 | if ($t1 ne $t2) { | |
1824 | display_message ("Password does not match."); | |
1825 | $pwe1->grab_focus(); | |
1826 | return; | |
1827 | } | |
1828 | ||
1829 | my $t3 = $eme->get_text; | |
1830 | if ($t3 !~ m/^\S+\@\S+\.\S+$/) { | |
1831 | display_message ("E-Mail does not look like a vaild address" . | |
1832 | " (user\@domain.tld)"); | |
1833 | $eme->grab_focus(); | |
1834 | return; | |
1835 | ||
1836 | } | |
1837 | ||
1838 | $password = $t1; | |
1839 | $mailto = $t3; | |
1840 | ||
1841 | create_ipconf_view(); | |
1842 | }); | |
1843 | ||
1844 | $pwe1->grab_focus(); | |
1845 | ||
1846 | } | |
1847 | ||
1848 | sub create_country_view { | |
1849 | ||
1850 | cleanup_view (); | |
1851 | ||
1852 | my $countryhash = $cmap->{countryhash}; | |
1853 | my $ctr = $cmap->{country}; | |
1854 | ||
7becc472 | 1855 | my $vbox2 = Gtk3::VBox->new (0, 0); |
89a12446 | 1856 | $inbox->pack_start ($vbox2, 1, 0, 0); |
7becc472 | 1857 | my $vbox = Gtk3::VBox->new (0, 0); |
53986d77 | 1858 | $vbox2->pack_start ($vbox, 0, 0, 10); |
89a12446 | 1859 | |
7becc472 | 1860 | my $w = Gtk3::Entry->new (); |
89a12446 DM |
1861 | $w->set_size_request (200, -1); |
1862 | ||
7becc472 | 1863 | my $c = Gtk3::EntryCompletion->new (); |
89a12446 DM |
1864 | $c->set_text_column (0); |
1865 | $c->set_minimum_key_length(0); | |
1866 | $c->set_popup_set_width (1); | |
4443aa27 | 1867 | $c->set_inline_completion (1); |
89a12446 | 1868 | |
7becc472 DM |
1869 | my $hbox2 = Gtk3::HBox->new (0, 0); |
1870 | my $label = Gtk3::Label->new ("Time zone"); | |
89a12446 DM |
1871 | $label->set_size_request (150, -1); |
1872 | $label->set_alignment (1, 0.5); | |
1873 | $hbox2->pack_start ($label, 0, 0, 10); | |
1874 | update_zonelist ($hbox2); | |
1875 | ||
7becc472 DM |
1876 | my $hbox3 = Gtk3::HBox->new (0, 0); |
1877 | $label = Gtk3::Label->new ("Keyboard Layout"); | |
89a12446 DM |
1878 | $label->set_size_request (150, -1); |
1879 | $label->set_alignment (1, 0.5); | |
1880 | $hbox3->pack_start ($label, 0, 0, 10); | |
1881 | ||
bcbfab6b | 1882 | my $kmapcb = Gtk3::ComboBoxText->new(); |
89a12446 DM |
1883 | $kmapcb->set_size_request (200, -1); |
1884 | foreach my $layout (sort keys %{$cmap->{kmaphash}}) { | |
1885 | $kmapcb->append_text ($layout); | |
1886 | } | |
1887 | ||
1888 | update_layout ($kmapcb); | |
1889 | $hbox3->pack_start ($kmapcb, 0, 0, 0); | |
1890 | ||
1891 | $kmapcb->signal_connect ('changed' => sub { | |
1892 | my $sel = $kmapcb->get_active_text(); | |
1893 | if (my $kmap = $cmap->{kmaphash}->{$sel}) { | |
1894 | my $xkmap = $cmap->{kmap}->{$kmap}->{x11}; | |
1895 | my $xvar = $cmap->{kmap}->{$kmap}->{x11var}; | |
1896 | syscmd ("setxkbmap $xkmap $xvar") if !$opt_testmode; | |
1897 | $keymap = $kmap; | |
1898 | } | |
1899 | }); | |
1900 | ||
1901 | $w->signal_connect ('changed' => sub { | |
1902 | my ($entry, $event) = @_; | |
1903 | my $text = $entry->get_text; | |
1904 | ||
1905 | if (my $cc = $countryhash->{lc($text)}) { | |
1906 | update_zonelist ($hbox2, $cc); | |
1907 | my $kmap = $ctr->{$cc}->{kmap} || 'en-us'; | |
1908 | update_layout ($kmapcb, $kmap); | |
1909 | } | |
1910 | }); | |
1911 | ||
1912 | $w->signal_connect (key_press_event => sub { | |
1913 | my ($entry, $event) = @_; | |
1914 | my $text = $entry->get_text; | |
1915 | ||
7becc472 DM |
1916 | my $val = $event->get_keyval; |
1917 | ||
1918 | if ($val == Gtk3::Gdk::KEY_Tab) { | |
89a12446 | 1919 | my $cc = $countryhash->{lc($text)}; |
4443aa27 | 1920 | |
89a12446 DM |
1921 | my $found = 0; |
1922 | my $compl; | |
7becc472 | 1923 | |
4443aa27 DM |
1924 | if ($cc) { |
1925 | $found = 1; | |
1926 | $compl = $ctr->{$cc}->{name}; | |
1927 | } else { | |
1928 | foreach my $cc (keys %$ctr) { | |
1929 | my $ct = $ctr->{$cc}->{name}; | |
1930 | if ($ct =~ m/^\Q$text\E.*$/i) { | |
1931 | $found++; | |
1932 | $compl = $ct; | |
1933 | } | |
1934 | last if $found > 1; | |
89a12446 | 1935 | } |
89a12446 | 1936 | } |
4443aa27 | 1937 | |
89a12446 | 1938 | if ($found == 1) { |
7becc472 | 1939 | $entry->set_text($compl); |
3df718ea | 1940 | $c->complete(); |
89a12446 DM |
1941 | return undef; |
1942 | } else { | |
7becc472 DM |
1943 | #Gtk3::Gdk::beep(); |
1944 | print chr(7); # beep ? | |
89a12446 DM |
1945 | } |
1946 | ||
3df718ea DM |
1947 | $c->complete(); |
1948 | ||
7becc472 DM |
1949 | my $buf = $w->get_buffer(); |
1950 | $buf->insert_text(-1, '', -1); # popup selection | |
1951 | ||
89a12446 DM |
1952 | return 1; |
1953 | } | |
1954 | ||
1955 | return undef; | |
1956 | }); | |
7becc472 DM |
1957 | |
1958 | my $ls = Gtk3::ListStore->new('Glib::String'); | |
89a12446 DM |
1959 | foreach my $cc (sort {$ctr->{$a}->{name} cmp $ctr->{$b}->{name} } keys %$ctr) { |
1960 | my $iter = $ls->append(); | |
1961 | $ls->set ($iter, 0, $ctr->{$cc}->{name}); | |
1962 | } | |
1963 | $c->set_model ($ls); | |
1964 | ||
968fa90b | 1965 | $w->set_completion ($c); |
89a12446 | 1966 | |
7becc472 | 1967 | my $hbox = Gtk3::HBox->new (0, 0); |
89a12446 | 1968 | |
7becc472 | 1969 | $label = Gtk3::Label->new ("Country"); |
89a12446 DM |
1970 | $label->set_alignment (1, 0.5); |
1971 | $label->set_size_request (150, -1); | |
1972 | $hbox->pack_start ($label, 0, 0, 10); | |
1973 | $hbox->pack_start ($w, 0, 0, 0); | |
1974 | ||
1975 | $vbox->pack_start ($hbox, 0, 0, 5); | |
1976 | $vbox->pack_start ($hbox2, 0, 0, 5); | |
1977 | $vbox->pack_start ($hbox3, 0, 0, 5); | |
1978 | ||
1979 | if ($country) { | |
1980 | $w->set_text ($ctr->{$country}->{name}); | |
1981 | } | |
1982 | ||
1983 | $inbox->show_all; | |
1984 | ||
1985 | display_html ("country.htm"); | |
1986 | set_next (undef, sub { | |
1987 | ||
1988 | my $text = $w->get_text; | |
1989 | ||
1990 | if (my $cc = $countryhash->{lc($text)}) { | |
1991 | $country = $cc; | |
1992 | create_password_view(); | |
1993 | return; | |
1994 | } else { | |
1995 | display_message ("Please select a country first."); | |
1996 | $w->grab_focus(); | |
1997 | } | |
1998 | }); | |
1999 | ||
2000 | $w->grab_focus(); | |
2001 | } | |
2002 | ||
c6ed3b24 DM |
2003 | my $target_hd_combo; |
2004 | my $target_hd_label; | |
2005 | ||
2006 | my $hdopion_first_setup = 1; | |
2007 | ||
aed81ff0 DM |
2008 | sub create_hdoption_view { |
2009 | ||
2010 | my $dialog = Gtk3::Dialog->new(); | |
2011 | ||
2012 | $dialog->set_title("Harddisk options"); | |
2013 | ||
2014 | $dialog->add_button("_OK", 1); | |
2015 | ||
2016 | my $contarea = $dialog->get_content_area(); | |
2017 | ||
2018 | my $hbox2 = Gtk3::Box->new('horizontal', 0); | |
2019 | $contarea->pack_start($hbox2, 1, 1, 10); | |
2020 | ||
2021 | my $grid = Gtk3::Grid->new(); | |
2022 | $grid->set_column_spacing(10); | |
2023 | $grid->set_row_spacing(10); | |
c6ed3b24 | 2024 | |
aed81ff0 | 2025 | $hbox2->pack_start($grid, 1, 0, 10); |
c6ed3b24 DM |
2026 | |
2027 | my $row = 0; | |
2028 | ||
aed81ff0 DM |
2029 | # Filesystem type |
2030 | ||
2031 | my $label0 = Gtk3::Label->new ("Filesystem"); | |
2032 | $label0->set_alignment (1, 0.5); | |
c6ed3b24 DM |
2033 | $grid->attach($label0, 0, $row, 1, 1); |
2034 | ||
bcbfab6b | 2035 | my $fstypecb = Gtk3::ComboBoxText->new(); |
aed81ff0 | 2036 | |
80090926 | 2037 | my $fstype = ['ext3', 'ext4', 'xfs', 'zfs (RAID0)', 'zfs (RAID1)', |
c6ed3b24 DM |
2038 | 'zfs (RAID10)', 'zfs (RAIDZ-1)', |
2039 | 'zfs (RAIDZ-2)', 'zfs (RAIDZ-3)']; | |
aed81ff0 | 2040 | |
c6ed3b24 DM |
2041 | my $tcount = 0; |
2042 | foreach my $tmp (@$fstype) { | |
2043 | $fstypecb->append_text($tmp); | |
2044 | $fstypecb->set_active ($tcount) | |
2045 | if $config_options->{filesys} eq $tmp; | |
2046 | $tcount++; | |
2047 | } | |
2048 | ||
2049 | $grid->attach($fstypecb, 1, $row, 1, 1); | |
2050 | ||
2051 | $hbox2->show_all(); | |
2052 | ||
2053 | $row++; | |
2054 | ||
2055 | my @disk_label; | |
2056 | my @disk_selector; | |
2057 | for (my $i = 0; $i < 6; $i++) { | |
2058 | $disk_label[$i] = Gtk3::Label->new ("Hardisk $i"); | |
2059 | $disk_label[$i]->set_alignment (1, 0.5); | |
2060 | $grid->attach($disk_label[$i], 0, $row, 1, 1); | |
bcbfab6b | 2061 | $disk_selector[$i] = Gtk3::ComboBoxText->new(); |
e23a3e4d DM |
2062 | $disk_selector[$i]->append_text("-- do not use --"); |
2063 | $disk_selector[$i]->set_active(0); | |
bcbfab6b | 2064 | foreach my $hd (@$hds) { |
c6ed3b24 | 2065 | my ($disk, $devname, $size, $model) = @$hd; |
e23a3e4d | 2066 | $disk_selector[$i]->append_text(get_device_desc ($devname, $size, $model)); |
c6ed3b24 DM |
2067 | $disk_selector[$i]->{pve_disk_id} = $i; |
2068 | $disk_selector[$i]->signal_connect (changed => sub { | |
2069 | my $w = shift; | |
2070 | my $diskid = $w->{pve_disk_id}; | |
e23a3e4d | 2071 | my $a = $w->get_active - 1; |
c6ed3b24 DM |
2072 | $config_options->{"disksel${diskid}"} = ($a >= 0) ? $hds->[$a] : undef; |
2073 | }); | |
2074 | } | |
2075 | ||
2076 | if ($hdopion_first_setup) { | |
e23a3e4d | 2077 | $disk_selector[$i]->set_active ($i+1) if $hds->[$i]; |
c6ed3b24 DM |
2078 | } else { |
2079 | my $hdind = 0; | |
2080 | if (my $cur_hd = $config_options->{"disksel$i"}) { | |
2081 | foreach my $hd (@$hds) { | |
2082 | if (@$hd[1] eq @$cur_hd[1]) { | |
e23a3e4d | 2083 | $disk_selector[$i]->set_active($hdind+1); |
c6ed3b24 DM |
2084 | last; |
2085 | } | |
2086 | $hdind++; | |
2087 | } | |
2088 | ||
2089 | } | |
2090 | } | |
2091 | ||
2092 | $grid->attach($disk_selector[$i], 1, $row, 1, 1); | |
2093 | ||
2094 | $row++; | |
aed81ff0 DM |
2095 | } |
2096 | ||
c6ed3b24 | 2097 | $hdopion_first_setup = 0; |
aed81ff0 DM |
2098 | |
2099 | my $label_hdsize = Gtk3::Label->new ("hdsize"); | |
2100 | $label_hdsize->set_alignment (1, 0.5); | |
c6ed3b24 | 2101 | $grid->attach($label_hdsize, 0, $row, 1, 1); |
aed81ff0 DM |
2102 | |
2103 | # size compute | |
c6ed3b24 | 2104 | my $hdsize = 0; |
aed81ff0 DM |
2105 | if ( -b $target_hd) { |
2106 | $hdsize = int(hd_size ($target_hd) / (1024*1024.0)); # size in GB | |
c6ed3b24 | 2107 | } elsif ($target_hd) { |
aed81ff0 DM |
2108 | $hdsize = int((-s $target_hd) / (1024*1024*1024.0)); |
2109 | } | |
2110 | ||
2111 | my $hdsize_size_adj = Gtk3::Adjustment->new($config_options->{hdsize} || $hdsize, 0, $hdsize+1, 1, 1, 1); | |
2112 | my $spinbutton_hdsize = Gtk3::SpinButton->new($hdsize_size_adj, 1, 1); | |
2113 | $spinbutton_hdsize->set_tooltip_text("only use specified size (GB) of the harddisk (rest left unpartitioned)"); | |
c6ed3b24 DM |
2114 | $grid->attach($spinbutton_hdsize, 1, $row, 1, 1); |
2115 | $row++; | |
aed81ff0 DM |
2116 | |
2117 | my $label_swapsize = Gtk3::Label->new ("swapsize"); | |
2118 | $label_swapsize->set_alignment (1, 0.5); | |
c6ed3b24 | 2119 | $grid->attach($label_swapsize, 0, $row, 1, 1); |
aed81ff0 DM |
2120 | |
2121 | my $entry_swapsize = Gtk3::Entry->new(); | |
2122 | $entry_swapsize->set_tooltip_text("maximum SWAP size (GB)"); | |
2123 | $entry_swapsize->signal_connect (key_press_event => \&check_float); | |
2124 | $entry_swapsize->set_text($config_options->{swapsize}) if $config_options->{swapsize}; | |
c6ed3b24 DM |
2125 | $grid->attach($entry_swapsize, 1, $row, 1, 1); |
2126 | $row++; | |
aed81ff0 DM |
2127 | |
2128 | my $label_maxroot = Gtk3::Label->new ("maxroot"); | |
2129 | $label_maxroot->set_alignment (1, 0.5); | |
c6ed3b24 | 2130 | $grid->attach($label_maxroot, 0, $row, 1, 1); |
aed81ff0 DM |
2131 | |
2132 | my $entry_maxroot = Gtk3::Entry->new(); | |
2133 | $entry_maxroot->set_tooltip_text("maximum size (GB) for LVM root volume"); | |
2134 | $entry_maxroot->signal_connect (key_press_event => \&check_float); | |
2135 | $entry_maxroot->set_text($config_options->{maxroot}) if $config_options->{maxroot}; | |
c6ed3b24 DM |
2136 | $grid->attach($entry_maxroot, 1, $row, 1, 1); |
2137 | $row++; | |
aed81ff0 DM |
2138 | |
2139 | my $label_minfree = Gtk3::Label->new ("minfree"); | |
2140 | $label_minfree->set_alignment (1, 0.5); | |
c6ed3b24 | 2141 | $grid->attach($label_minfree, 0, $row, 1, 1); |
aed81ff0 DM |
2142 | |
2143 | my $entry_minfree = Gtk3::Entry->new(); | |
2144 | $entry_minfree->set_tooltip_text("minumum free LVM space (GB, required for LVM snapshots)"); | |
2145 | $entry_minfree->signal_connect (key_press_event => \&check_float); | |
2146 | $entry_minfree->set_text($config_options->{minfree}) if $config_options->{minfree}; | |
c6ed3b24 DM |
2147 | $grid->attach($entry_minfree, 1, $row, 1, 1); |
2148 | $row++; | |
aed81ff0 DM |
2149 | |
2150 | my $label_maxvz = Gtk3::Label->new ("maxvz"); | |
2151 | $label_maxvz->set_alignment (1, 0.5); | |
c6ed3b24 | 2152 | $grid->attach($label_maxvz, 0, $row, 1, 1); |
aed81ff0 DM |
2153 | |
2154 | my $entry_maxvz = Gtk3::Entry->new(); | |
2155 | $entry_maxvz->set_tooltip_text("maximum size (GB) for LVM data volume"); | |
2156 | $entry_maxvz->signal_connect (key_press_event => \&check_float); | |
2157 | $entry_maxvz->set_text($config_options->{maxvz}) if $config_options->{maxvz}; | |
c6ed3b24 DM |
2158 | $grid->attach($entry_maxvz, 1, $row, 1, 1); |
2159 | $row++; | |
aed81ff0 | 2160 | |
f7b853d1 | 2161 | my $set_sensitive_flag = sub { |
c6ed3b24 DM |
2162 | my $enable_size_hints = $config_options->{filesys} !~ m/zfs/; |
2163 | ||
2164 | if ($config_options->{filesys} =~ m/zfs/) { | |
2165 | $target_hd_combo->set_visible(0); | |
2166 | $target_hd_label->set_text("Target: $config_options->{filesys} "); | |
2167 | foreach my $w (@disk_selector) { $w->set_visible(1); } | |
2168 | foreach my $w (@disk_label) { $w->set_visible(1); } | |
2169 | } else { | |
2170 | $target_hd_combo->set_visible(1); | |
2171 | foreach my $w (@disk_selector) { $w->set_visible(0); } | |
2172 | foreach my $w (@disk_label) { $w->set_visible(0); } | |
2173 | $target_hd_label->set_text("Target Harddisk: "); | |
2174 | } | |
2175 | ||
2176 | $spinbutton_hdsize->set_visible($enable_size_hints); | |
2177 | $label_hdsize->set_visible($enable_size_hints); | |
2178 | $entry_swapsize->set_visible($enable_size_hints); | |
2179 | $label_swapsize->set_visible($enable_size_hints); | |
2180 | $entry_maxroot->set_visible($enable_size_hints); | |
2181 | $label_maxroot->set_visible($enable_size_hints); | |
2182 | $entry_minfree->set_visible($enable_size_hints); | |
2183 | $label_minfree->set_visible($enable_size_hints); | |
2184 | $entry_maxvz->set_visible($enable_size_hints); | |
2185 | $label_maxvz->set_visible($enable_size_hints); | |
f7b853d1 DM |
2186 | }; |
2187 | ||
2188 | &$set_sensitive_flag(); | |
2189 | ||
2190 | $fstypecb->signal_connect (changed => sub { | |
2191 | $config_options->{filesys} = $fstypecb->get_active_text(); | |
2192 | &$set_sensitive_flag(); | |
2193 | }); | |
2194 | ||
c6ed3b24 | 2195 | $dialog->show(); |
aed81ff0 DM |
2196 | |
2197 | $dialog->run(); | |
2198 | ||
2199 | my $get_float = sub { | |
2200 | my ($entry) = @_; | |
2201 | ||
2202 | my $text = $entry->get_text(); | |
2203 | return undef if !defined($text); | |
2204 | ||
2205 | $text =~ s/^\s+//; | |
2206 | $text =~ s/\s+$//; | |
2207 | ||
2208 | return undef if $text !~ m/^\d+(\.\d+)?$/; | |
2209 | ||
2210 | return $text; | |
2211 | }; | |
2212 | ||
2213 | my $tmp; | |
2214 | ||
2215 | if (($tmp = &$get_float($spinbutton_hdsize)) && ($tmp != $hdsize)) { | |
2216 | $config_options->{hdsize} = $tmp; | |
2217 | } else { | |
2218 | delete $config_options->{hdsize}; | |
2219 | } | |
2220 | ||
2221 | if (defined($tmp = &$get_float($entry_swapsize))) { | |
2222 | $config_options->{swapsize} = $tmp; | |
2223 | } else { | |
2224 | delete $config_options->{swapsize}; | |
2225 | } | |
2226 | ||
2227 | if (defined($tmp = &$get_float($entry_maxroot))) { | |
2228 | $config_options->{maxroot} = $tmp; | |
2229 | } else { | |
2230 | delete $config_options->{maxroot}; | |
2231 | } | |
2232 | ||
2233 | if (defined($tmp = &$get_float($entry_minfree))) { | |
2234 | $config_options->{minfree} = $tmp; | |
2235 | } else { | |
2236 | delete $config_options->{minfree}; | |
2237 | } | |
2238 | ||
2239 | if (defined($tmp = &$get_float($entry_maxvz))) { | |
2240 | $config_options->{maxvz} = $tmp; | |
2241 | } else { | |
2242 | delete $config_options->{maxvz}; | |
2243 | } | |
2244 | ||
2245 | $dialog->destroy(); | |
2246 | } | |
2247 | ||
c6ed3b24 DM |
2248 | sub get_zfs_raid_setup { |
2249 | ||
2250 | my $filesys = $config_options->{filesys}; | |
2251 | ||
2252 | my $dev_name_hash = {}; | |
2253 | ||
2254 | my $devlist = []; | |
2255 | my $bootdevlist = []; | |
2256 | for (my $i = 0; $i < 6; $i++) { | |
2257 | if (my $hd = $config_options->{"disksel$i"}) { | |
2258 | my ($disk, $devname, $size, $model) = @$hd; | |
2259 | die "device '$devname' is used more than once\n" | |
2260 | if $dev_name_hash->{$devname}; | |
2261 | $dev_name_hash->{$devname} = $hd; | |
2262 | push @$devlist, $hd; | |
2263 | } | |
2264 | } | |
2265 | ||
224bb7b0 DM |
2266 | my $diskcount = scalar(@$devlist); |
2267 | die "$filesys need at least one device\n" if $diskcount < 1; | |
c6ed3b24 DM |
2268 | |
2269 | my $cmd= ''; | |
2270 | if ($filesys eq 'zfs (RAID0)') { | |
2271 | push @$bootdevlist, @$devlist[0]; | |
2272 | foreach my $hd (@$devlist) { | |
2273 | $cmd .= " @$hd[1]"; | |
2274 | } | |
2275 | } elsif ($filesys eq 'zfs (RAID1)') { | |
224bb7b0 | 2276 | die "zfs (RAID1) need at least 2 device\n" if $diskcount < 2; |
c6ed3b24 | 2277 | $cmd .= ' mirror '; |
269c66a6 DM |
2278 | my $hd = @$devlist[0]; |
2279 | my $expected_size = @$hd[2]; # all disks needs same size | |
2280 | foreach $hd (@$devlist) { | |
2281 | die "mirrored disks must have same size\n" if @$hd[2] != $expected_size; | |
c6ed3b24 DM |
2282 | $cmd .= " @$hd[1]"; |
2283 | push @$bootdevlist, $hd; | |
2284 | } | |
2285 | } elsif ($filesys eq 'zfs (RAID10)') { | |
224bb7b0 DM |
2286 | my $half = int($diskcount/2); |
2287 | die "zfs (RAID10) need at least 4 device\n" if $diskcount < 4; | |
2288 | die "zfs (RAID10) need at an even numver of devices\n" if $half*2 != $diskcount; | |
c6ed3b24 DM |
2289 | |
2290 | push @$bootdevlist, @$devlist[0], @$devlist[1]; | |
2291 | ||
224bb7b0 | 2292 | for (my $i = 0; $i < $diskcount; $i+=2) { |
c6ed3b24 DM |
2293 | my $hd1 = @$devlist[$i]; |
2294 | my $hd2 = @$devlist[$i+1]; | |
269c66a6 | 2295 | die "mirrored disks must have same size\n" if @$hd1[2] != @$hd2[2]; |
c6ed3b24 DM |
2296 | $cmd .= ' mirror ' . @$hd1[1] . ' ' . @$hd2[1]; |
2297 | } | |
2298 | ||
2299 | } elsif ($filesys =~ m/^zfs \(RAIDZ-([123])\)$/) { | |
2300 | my $level = $1; | |
2301 | my $mindisks = 2 + $level; | |
2302 | die "zfs (RAIDZ-$level) need at least $mindisks devices\n" if scalar(@$devlist) < $mindisks; | |
269c66a6 DM |
2303 | my $hd = @$devlist[0]; |
2304 | my $expected_size = @$hd[2]; # all disks needs same size | |
097ecf8f | 2305 | $cmd .= " raidz$level"; |
269c66a6 DM |
2306 | foreach $hd (@$devlist) { |
2307 | die "mirrored disks must have same size\n" if @$hd[2] != $expected_size; | |
c6ed3b24 DM |
2308 | $cmd .= " @$hd[1]"; |
2309 | push @$bootdevlist, $hd; | |
2310 | } | |
2311 | } else { | |
2312 | die "unknown zfs mode '$filesys'\n"; | |
2313 | } | |
2314 | ||
2315 | return ($devlist, $bootdevlist, $cmd); | |
2316 | } | |
2317 | ||
89a12446 DM |
2318 | sub create_hdsel_view { |
2319 | ||
2320 | cleanup_view (); | |
2321 | ||
7becc472 | 2322 | my $vbox = Gtk3::VBox->new (0, 0); |
89a12446 | 2323 | $inbox->pack_start ($vbox, 1, 0, 0); |
7becc472 | 2324 | my $hbox = Gtk3::HBox->new (0, 0); |
53986d77 | 2325 | $vbox->pack_start ($hbox, 0, 0, 10); |
968fa90b | 2326 | |
89a12446 DM |
2327 | my ($disk, $devname, $size, $model) = @{@$hds[0]}; |
2328 | $target_hd = $devname; | |
89a12446 | 2329 | |
c6ed3b24 DM |
2330 | $target_hd_label = Gtk3::Label->new ("Target Harddisk: "); |
2331 | $hbox->pack_start ($target_hd_label, 0, 0, 0); | |
89a12446 | 2332 | |
bcbfab6b | 2333 | $target_hd_combo = Gtk3::ComboBoxText->new(); |
89a12446 | 2334 | |
1aa5bd02 DM |
2335 | foreach my $hd (@$hds) { |
2336 | ($disk, $devname, $size, $model) = @$hd; | |
c6ed3b24 | 2337 | $target_hd_combo->append_text (get_device_desc ($devname, $size, $model)); |
1aa5bd02 | 2338 | } |
89a12446 | 2339 | |
c6ed3b24 DM |
2340 | $target_hd_combo->set_active (0); |
2341 | $target_hd_combo->signal_connect (changed => sub { | |
1aa5bd02 DM |
2342 | $a = shift->get_active; |
2343 | my ($disk, $devname) = @{@$hds[$a]}; | |
2344 | $target_hd = $devname; | |
1aa5bd02 | 2345 | }); |
c6ed3b24 DM |
2346 | |
2347 | $hbox->pack_start ($target_hd_combo, 0, 0, 10); | |
aed81ff0 DM |
2348 | |
2349 | my $options = Gtk3::Button->new ('_Options'); | |
2350 | $options->signal_connect (clicked => \&create_hdoption_view); | |
2351 | $hbox->pack_start ($options, 0, 0, 0); | |
2352 | ||
89a12446 DM |
2353 | |
2354 | $inbox->show_all; | |
2355 | ||
2356 | display_html ("page1.htm"); | |
c6ed3b24 DM |
2357 | |
2358 | set_next (undef, sub { | |
2359 | ||
2360 | if ($config_options->{filesys} =~ m/zfs/) { | |
2361 | eval { get_zfs_raid_setup(); }; | |
2362 | if (my $err = $@) { | |
2363 | display_message ("Warning: $err\n" . | |
269c66a6 | 2364 | "Please fix ZFS setup first."); |
c6ed3b24 DM |
2365 | } else { |
2366 | create_country_view(); | |
2367 | } | |
2368 | } else { | |
2369 | create_country_view(); | |
2370 | } | |
2371 | }); | |
89a12446 DM |
2372 | } |
2373 | ||
2374 | sub create_extract_view { | |
2375 | ||
89a12446 DM |
2376 | cleanup_view (); |
2377 | ||
2378 | display_html ("extract1-license.htm"); | |
2379 | $next->set_sensitive (0); | |
2380 | ||
7becc472 | 2381 | my $vbox = Gtk3::VBox->new (0, 0); |
89a12446 | 2382 | $inbox->pack_start ($vbox, 1, 0, 0); |
7becc472 | 2383 | my $hbox = Gtk3::HBox->new (0, 0); |
53986d77 | 2384 | $vbox->pack_start ($hbox, 0, 0, 10); |
89a12446 | 2385 | |
7becc472 | 2386 | my $vbox2 = Gtk3::VBox->new (0, 0); |
89a12446 DM |
2387 | $hbox->pack_start ($vbox2, 0, 0, 0); |
2388 | ||
7becc472 | 2389 | $progress_status = Gtk3::Label->new (''); |
89a12446 | 2390 | $vbox2->pack_start ($progress_status, 1, 1, 0); |
968fa90b | 2391 | |
7becc472 | 2392 | $progress = Gtk3::ProgressBar->new; |
45feca6f | 2393 | $progress->set_show_text(1); |
7becc472 | 2394 | $progress->set_size_request (600, -1); |
89a12446 DM |
2395 | |
2396 | $vbox2->pack_start ($progress, 0, 0, 0); | |
2397 | ||
2398 | $inbox->show_all; | |
2399 | ||
2400 | my $tdir = $opt_testmode ? "target" : "/target"; | |
2401 | mkdir $tdir; | |
2402 | my $base = $opt_testmode ? "/pve/$release/install/pve-base.tar" : "/proxmox/pve-base.tar"; | |
2403 | ||
2404 | eval { extract_data ($base, $tdir); }; | |
2405 | my $err = $@; | |
2406 | ||
2407 | $next->set_sensitive (1); | |
2408 | ||
2409 | set_next ("_Reboot", sub { exit (0); } ); | |
2410 | ||
296cf41f DM |
2411 | if ($err) { |
2412 | display_html ("fail.htm"); | |
2413 | display_error ($err); | |
2414 | } else { | |
2415 | cleanup_view (); | |
2416 | display_html ("success.htm"); | |
2417 | } | |
89a12446 DM |
2418 | } |
2419 | ||
89a12446 DM |
2420 | sub create_intro_view { |
2421 | ||
2422 | cleanup_view (); | |
2423 | ||
2424 | display_html ("license.htm"); | |
2425 | ||
2426 | set_next ("I a_gree", \&create_hdsel_view); | |
2427 | } | |
2428 | ||
2429 | $ipconf = get_ip_config (); | |
2430 | ||
62c05878 | 2431 | $country = detect_country() if $ipconf->{addr} || $opt_testmode;; |
89a12446 DM |
2432 | |
2433 | # read country, kmap and timezone infos | |
2434 | $cmap = read_cmap (); | |
2435 | ||
2436 | create_main_window (); | |
2437 | ||
2438 | if (!defined ($hds) || (scalar (@$hds) <= 0)) { | |
2439 | print "no hardisks found\n"; | |
2440 | display_html ("nohds.htm"); | |
2441 | set_next ("Reboot", sub { exit (0); } ); | |
2442 | } else { | |
2443 | ||
2444 | foreach my $hd (@$hds) { | |
2445 | my ($disk, $devname) = @$hd; | |
2446 | next if $devname =~ m|^/dev/md\d+$|; | |
2447 | print "found Disk$disk N:$devname\n"; | |
2448 | } | |
2449 | ||
2450 | create_intro_view (); | |
2451 | } | |
2452 | ||
7becc472 | 2453 | Gtk3->main; |
89a12446 DM |
2454 | |
2455 | exit 0; |