aboutsummaryrefslogtreecommitdiff
path: root/lib/AlgaOS/Installer/GUI.pm
blob: e1ca51d3d00fd6eaa82577d1a9e4e6c62e741acc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
package AlgaOS::Installer::GUI;

use v5.40.0;
use strict;
use warnings;

use Moo;
use AlgaOS::Installer;
use AlgaOS::Installer::Util;
use List::Util;
use JSON;
use POSIX qw/WNOHANG/;
use PBKDF2::Tiny;
use Crypt::URandom qw/urandom/;
use File::ShareDir ':ALL';
my $dist_dir_files = dist_dir('AlgaOS-Installer');

my @perl_args_chroot = @ARGV;

BEGIN {
    *CORE::GLOBAL::exit = sub {
        POSIX::_exit( $_[0] // 0 );
    };
}

has app                            => ( is => 'lazy' );
has win                            => ( is => 'rw' );
has const                          => ( is => 'lazy' );
has pipe_name                      => ( is => 'ro', required => 1 );
has secret                         => ( is => 'ro', required => 1 );
has _grid_row                      => ( is => 'rw', default  => sub { 0 } );
has _proxy_started                 => ( is => 'rw' );
has _hostname_entry                => ( is => 'rw' );
has _username_entry                => ( is => 'rw' );
has _password_entry                => ( is => 'rw' );
has _start_installation_button     => ( is => 'rw' );
has _proxy_others                  => ( is => 'rw' );
has _dropdown_timezones            => ( is => 'rw' );
has _dropdown_locale               => ( is => 'rw' );
has _dropdown_block_devices        => ( is => 'rw' );
has _dropdown_recover_or_reinstall => ( is => 'rw' );
has _scroll                        => ( is => 'rw' );
has _recovery_uuid                 => ( is => 'rw' );
has _root_uuid                     => ( is => 'rw' );
has _efi_uuid                      => ( is => 'rw' );
has _current_recovery_partition    => ( is => 'lazy' );
has _install_is_recover            => ( is => 'rw' );

sub excfailexit {
    my @command        = @_;
    my $command_string = join ', ', map { "'$_'" } @command;
    say "system $command_string";
    my $return_code = system @command;
    if ($return_code) {
        system 'sudo umount -R /mnt/gentoo';
        system 'sudo umount -R /recovery';
        say "system $command_string: failed";
        exit 1;
    }
}
excfailexit qw{sudo mkdir -pv /mnt/gentoo/};

sub call_and_increment_grid_row( $self, $coderef ) {
    $coderef->();
    $self->_grid_row( $self->_grid_row + 1 );
}

sub _build_const {
    return AlgaOS::Installer::Constants->new;
}

sub _build_app {
    return Gtk::Application->new( "com.algaos.Installer", 0 );
}

sub _create_install_grid( $self, $desc ) {
    my $grid  = Gtk::Grid->new;
    my $const = $self->const;
    $grid->add_css_class('transparent_background');
    $self->_grid_row(0);
    $self->call_and_increment_grid_row(
        sub {
            my $label = Gtk::Label->new('Install AlgaOS');
            $label->add_css_class('title-1');
            $grid->attach( $label, 0, $self->_grid_row, 3, 1 );
        }
    );
    $self->call_and_increment_grid_row(
        sub {
            my $label = Gtk::Label->new($desc);
            $grid->attach( $label, 0, $self->_grid_row, 3, 1 );
        }
    );
    $grid->set_valign( $const->GTK_ALIGN_CENTER );
    $grid->set_halign( $const->GTK_ALIGN_CENTER );
    return $grid;
}

sub _overwrite_install_generic( $self, %args ) {
    my $disk_prepare = $args{disk_prepare};
    if ( !defined $disk_prepare ) {
        die 'disk_prepare callback not sent';
    }
    my $container_block = $args{container_block};
    my $const           = $self->const;
    my $grid            = Gtk::Grid->new;
    $grid->add_css_class('transparent_background');
    $self->call_and_increment_grid_row(
        sub {
            my $label = Gtk::Label->new('Install AlgaOS');
            $label->add_css_class('title-1');
            $grid->attach( $label, 0, $self->_grid_row, 3, 1 );
        }
    );
    $self->call_and_increment_grid_row(
        sub {
            $grid->attach( Gtk::Label->new('Select hostname'),
                0, $self->_grid_row, 1, 1 );
            $grid->attach( $self->_hostname_entry, 1, $self->_grid_row, 1, 1 );
        }
    );
    $self->call_and_increment_grid_row(
        sub {
            $grid->attach( Gtk::Label->new('New user name'),
                0, $self->_grid_row, 1, 1 );
            $grid->attach( $self->_username_entry, 1, $self->_grid_row, 1, 1 );
        }
    );
    $self->call_and_increment_grid_row(
        sub {
            $grid->attach( Gtk::Label->new('New user password'),
                0, $self->_grid_row, 1, 1 );
            $grid->attach( $self->_password_entry, 1, $self->_grid_row, 1, 1 );
        }
    );

    my $is_national = sub {
        my $nation = shift;
        return List::Util::any { $_ eq $nation }
        (qw{Europe/Madrid Africa/Ceuta Atlantic/Canary});
    };

    my $is_europe = sub {
        my $region = shift;
        return $region =~ /Europe/;
    };

    $self->call_and_increment_grid_row(
        sub {
            $grid->attach( Gtk::Label->new('Set your timezone'),
                0, $self->_grid_row, 1, 1 );
            my @timezones = split /\s+/s, `timedatectl list-timezones`;
            @timezones = grep { defined $_ } @timezones;
            @timezones = sort {
                return
                     ( $b eq 'Europe/Madrid' ) <=> ( $a eq 'Europe/Madrid' )
                  || $is_national->($b)        <=> $is_national->($a)
                  || $is_europe->($b)          <=> $is_europe->($a)
                  || $a cmp $b;
            } @timezones;
            my $dropdown = Gtk::Dropdown->new( [@timezones] );
            $self->_dropdown_timezones($dropdown);
            $grid->attach( $dropdown, 1, $self->_grid_row, 1, 1 );
        }
    );

    my $is_spain_spanish = sub {
        my $lang = shift;
        return $lang =~ /^es_ES/;
    };
    my $is_spanish = sub {
        my $lang = shift;
        return $lang =~ /^es/;
    };
    my $is_english = sub {
        my $lang = shift;
        return $lang =~ /^en/;
    };
    $self->call_and_increment_grid_row(
        sub {
            $grid->attach( Gtk::Label->new('Set your language and region'),
                0, $self->_grid_row, 1, 1 );
            my @locales = split /\s+/s, `locale -a`;
            @locales = grep { defined $_ && $_ =~ /utf8/ } @locales;
            @locales = sort {
                return
                     $is_spain_spanish->($b) <=> $is_spain_spanish->($a)
                  || $is_spanish->($b)       <=> $is_spanish->($a)
                  || $is_english->($b)       <=> $is_english->($a)
                  || $a cmp $b;
            } @locales;
            my $dropdown = Gtk::Dropdown->new( [@locales] );
            $self->_dropdown_locale($dropdown);
            $grid->attach( $dropdown, 1, $self->_grid_row, 1, 1 );
        }
    );
    my $confirm_button =
      Gtk::Button->new("Install in this disk and lose all data");
    $self->call_and_increment_grid_row(
        sub {
            $grid->attach( $confirm_button, 1, $self->_grid_row, 1, 1 );
        }
    );
    $confirm_button->connect(
        clicked => sub {
            my $dialog = Gtk::AlertDialog->new(
                "Your data in that storage media will be lost",
                "Ready to format?" );
            $dialog->make_yes_no();
            $dialog->choose(
                $self->win,
                sub($button) {
                    if ( $button != 1 ) {
                        return;
                    }
                    $self->_install(
                        container_block => $container_block,
                        hostname        => $self->_hostname_entry->get_text,
                        username        => $self->_username_entry->get_text,
                        password        => $self->_password_entry->get_text,
                        timezone => $self->_dropdown_timezones->selected_text,
                        locale   => $self->_dropdown_locale->selected_text,
                        complete_systemd => 1,
                        prepare          => sub {
                            $disk_prepare->();
                            system 'sudo mkdir /recovery';

                            system qw{sudo mount},
                              $self->_current_recovery_partition, '/recovery';
                            excfailexit
'sudo tar -C /mnt/gentoo -xvpf /stage3-algaos-latest.tar.xz --numeric-owner --xattrs-include="*.*"';
                            excfailexit qw{sudo cp -Lv}, '/etc/resolv.conf',
                              '/mnt/gentoo/etc/';
                            excfailexit
                              qw{sudo rsync -P -a -v /recovery/rootfs.squashfs /mnt/gentoo/recovery/};

                        }
                    );
                }
            );
        }
    );

    $grid->set_valign( $const->GTK_ALIGN_CENTER );
    $grid->set_halign( $const->GTK_ALIGN_CENTER );
    return $grid;
}

sub _create_real_install_grid($self) {
    my ($block_name) = split /\t+/,
      $self->_dropdown_block_devices->selected_text;
    my $block          = "/dev/$block_name";
    my $efi_block      = "${block}1";
    my $recovery_block = "${block}3";
    my $root_block     = "${block}4";
    $self->_overwrite_install_generic(
        container_block => $block,
        disk_prepare    => sub(%args) {
            system qw{sudo sgdisk -Z}, $block;
            excfailexit qw{sudo sgdisk -n 1::+1G},  $block;
            excfailexit qw{sudo sgdisk -t 1:ef00},  $block;
            excfailexit qw{sudo sgdisk -c},         "1:AlgaOSEFI", $block;
            excfailexit qw{sudo sgdisk -n 2::+1G},  $block;
            excfailexit qw{sudo sgdisk -t 2:ef02},  $block;
            excfailexit qw{sudo sgdisk -c},         "2:AlgaOSBIOSBoot", $block;
            excfailexit qw{sudo sgdisk -n 3::+20G}, $block;
            excfailexit qw{sudo sgdisk -c},         "3:AlgaOSRecovery", $block;
            excfailexit qw{sudo sgdisk -N 4},       $block;
            excfailexit qw{sudo sgdisk -c},         "4:AlgaOSRoot", $block;
            excfailexit qw{sudo dd if=/dev/zero bs=5M count=10},
              "of=$efi_block";
            excfailexit qw{sudo dd if=/dev/zero bs=5M count=10},
              "of=$recovery_block";
            excfailexit qw{sudo dd if=/dev/zero bs=5M count=10},
              "of=$root_block";
            excfailexit qw{sudo mkfs.vfat}, $efi_block;
            excfailexit qw{sudo mkfs.ext4}, $recovery_block;
            excfailexit qw{sudo mkfs.ext4}, $root_block;
            excfailexit qw{sudo mount},     $root_block, '/mnt/gentoo';
            excfailexit qw{sudo mkdir -pv}, '/mnt/gentoo/recovery';
            excfailexit qw{sudo mount}, $recovery_block, '/mnt/gentoo/recovery';
            excfailexit qw{sudo mkdir -pv}, '/mnt/gentoo/boot/efi';
            excfailexit qw{sudo mount},     $efi_block, '/mnt/gentoo/boot/efi';
        }
    );
}

sub _build__current_recovery_partition($self) {
    my $cmdline = `cat /proc/cmdline`;
    my ($partition) = $cmdline =~ /live:(\S+)/;
    return $partition;
}

sub _create_main_grid($self) {
    my $const = $self->const;
    my $grid  = Gtk::Grid->new;
    $grid->add_css_class('transparent_background');
    $self->call_and_increment_grid_row(
        sub {
            my $label = Gtk::Label->new('Install AlgaOS');
            $label->add_css_class('title-1');
            $grid->attach( $label, 0, $self->_grid_row, 3, 1 );
        }
    );
    $self->call_and_increment_grid_row(
        sub {
            $grid->attach( Gtk::Label->new('Set the installation destination'),
                0, $self->_grid_row, 1, 1 );
            my $block_devices =
              JSON::from_json(`lsblk --json -d -o NAME,SIZE,MODEL,TYPE,TRAN`);
            my @block_devices =
              map {
                join "\t", map { $_ // '' } @$_{qw/name model size type tran/};
              } $block_devices->{blockdevices}->@*;
            @block_devices = grep { defined $_ } @block_devices;
            my $dropdown = Gtk::Dropdown->new( [@block_devices] );
            $self->_dropdown_block_devices($dropdown);
            $grid->attach( $dropdown, 1, $self->_grid_row, 1, 1 );
        }
    );

    $self->call_and_increment_grid_row(
        sub {
            $grid->attach( $self->_start_installation_button,
                1, $self->_grid_row, 1, 1 );
        }
    );
    $self->_start_installation_button->connect(
        clicked => sub {
            $self->_on_click_select_media;
        }
    );

    $grid->set_valign( $const->GTK_ALIGN_CENTER );
    $grid->set_halign( $const->GTK_ALIGN_CENTER );
    return $grid;
}

sub _create_recover_installation_grid($self) {
    my $const = $self->const;
    my $grid  = Gtk::Grid->new;
    $grid->add_css_class('transparent_background');
    $self->call_and_increment_grid_row(
        sub {
            my $label = Gtk::Label->new('Install AlgaOS');
            $label->add_css_class('title-1');
            $grid->attach( $label, 0, $self->_grid_row, 3, 1 );
        }
    );
    $self->call_and_increment_grid_row(
        sub {
            $grid->attach( Gtk::Label->new('Recover AlgaOS or reinstall it?'),
                0, $self->_grid_row, 1, 1 );
            my $dropdown = Gtk::Dropdown->new( [qw/Recover Reinstall/] );
            $self->_dropdown_recover_or_reinstall($dropdown);
            $grid->attach( $dropdown, 1, $self->_grid_row, 1, 1 );
        }
    );
    $self->call_and_increment_grid_row(
        sub {
            my $button = Gtk::Button->new('Continue');
            $button->connect(
                clicked => sub {
                    if ( $self->_dropdown_recover_or_reinstall->selected_text eq
                        'Reinstall' )
                    {
                        $self->_scroll->set_child(
                            $self->_overwrite_installation_menu );
                        return;
                    }
                    system qw{sudo mount}, $self->_root_uuid, '/mnt/gentoo';
                    $self->_restore_system( '/stage3-algaos-latest.tar.xz',
                        '/mnt/gentoo', 1 );
                }
            );
            $grid->attach( $button, 1, $self->_grid_row, 1, 1 );
        }
    );

    $grid->set_valign( $const->GTK_ALIGN_CENTER );
    $grid->set_halign( $const->GTK_ALIGN_CENTER );
    return $grid;
}

sub _overwrite_installation_menu($self) {
    my ($block_name) = split /\t+/,
      $self->_dropdown_block_devices->selected_text;
    my $block          = "/dev/$block_name";
    my $efi_block      = 'PARTUUID=' . $self->_efi_uuid;
    my $recovery_block = 'PARTUUID=' . $self->_recovery_uuid;
    my $root_block     = 'PARTUUID=' . $self->_root_uuid;
    $self->_overwrite_install_generic(
        container_block => $block,
        disk_prepare => sub(%args) {
            excfailexit qw{sudo dd if=/dev/zero bs=1M count=10},
              'of=/dev/disk/by-partuuid/' . $self->_root_uuid;
            excfailexit qw{sudo mkfs.ext4},
              '/dev/disk/by-partuuid/' . $self->_root_uuid;
            excfailexit qw{sudo dd if=/dev/zero bs=1M count=10},
              'of=/dev/disk/by-partuuid/' . $self->_recovery_uuid;
            excfailexit qw{sudo mkfs.ext4},
              '/dev/disk/by-partuuid/' . $self->_recovery_uuid;
            excfailexit qw{sudo dd if=/dev/zero bs=1M count=10},
              'of=/dev/disk/by-partuuid/' . $self->_efi_uuid;
            excfailexit qw{sudo mkfs.vfat},
              '/dev/disk/by-partuuid/' . $self->_efi_uuid;
            excfailexit qw{sudo mkdir -pv /mnt/gentoo/};
            excfailexit qw{sudo mount},     $root_block, '/mnt/gentoo';
            excfailexit qw{sudo mkdir -pv}, '/mnt/gentoo/recovery';
            excfailexit qw{sudo mount}, $recovery_block, '/mnt/gentoo/recovery';
            excfailexit qw{sudo mkdir -pv}, '/mnt/gentoo/boot/efi';
            excfailexit qw{sudo mount},     $efi_block, '/mnt/gentoo/boot/efi';
        }
    );
}

# TODO: Slop function, in Chatyipity we trust
sub _restore_system ( $self, $archive, $root, $preserve_etc ) {
    $self->_install_is_recover(1);
    my ($block_name) = split /\t+/,
      $self->_dropdown_block_devices->selected_text;
    my $block = "/dev/$block_name";
    $self->_install(
        complete_systemd => 0,
        container_block  => $block,
        prepare          => sub {
            excfailexit qw{sudo mount}, 'PARTUUID=' . $self->_root_uuid,
              '/mnt/gentoo';
            excfailexit qw{sudo mkdir -pv}, '/mnt/gentoo/boot/efi';
            excfailexit qw{sudo mount}, 'PARTUUID=' . $self->_efi_uuid,
              '/mnt/gentoo/boot/efi';
            excfailexit qw{sudo mkdir -pv}, '/mnt/gentoo/recovery';
            excfailexit qw{sudo mount}, 'PARTUUID=' . $self->_recovery_uuid,
              '/mnt/gentoo/recovery';
            system 'sudo mkdir /recovery';

            system qw{sudo mount},
              $self->_current_recovery_partition, '/recovery';

            die "Invalid archive\n" unless -f $archive;
            die "Invalid root\n"    unless -d $root;
            die "Refusing /\n" if $root eq '/';
            die "Archive inside root\n"
              if index( $archive, "$root/" ) == 0;

            my @keep = qw(
              passwd shadow group gshadow subuid subgid
              machine-id hostname hosts locale.conf locale.gen timezone adjtime
              fstab crypttab ssh udev/rules.d
              NetworkManager/system-connections systemd
            );

            my $sudo = sub (@cmd) {
                system( 'sudo', @cmd ) == 0
                  or die "sudo @cmd failed\n";
            };

            $sudo->( 'tar', '-tf', $archive );

            my $old = "$root/etc.old";

            eval { $sudo->( 'cp', '-a', '--', "$root/etc", $old ); };

            $sudo->(
                'find',      $root,   '-mindepth', 1,
                '-maxdepth', 1,       '!',         '-name',
                'boot',      '!',     '-name',     'grub_hash',
                '!',         '-name', 'recovery',  '!',
                '-name',     'home',  '!',         '-name',
                'etc.old',   '-exec', 'rm',        '-rf',
                '--',        '{}',    '+'
            );

            $sudo->(
                'tar',                   '-xpf',
                $archive,                '-C',
                $root,                   '--numeric-owner',
                '--xattrs-include=*',    '--exclude=home',
                '--exclude=./home',      '--exclude=home/*',
                '--exclude=./home/*',    '--exclude=etc.old',
                '--exclude=./etc.old',   '--exclude=etc.old/*',
                '--exclude=./etc.old/*', '--exclude=grub_hash',
                '--exclude=./grub_hash',
            );

            if ($preserve_etc) {
                for my $file (@keep) {
                    my $src = "$old/$file";
                    my $dst = "$root/etc/$file";
                    next unless -e $src;

                    $sudo->(
                        'rsync', '-a', qw{--mkpath --numeric-ids},
                        '--',    $src, $dst
                    );
                }
            }
            excfailexit qw{sudo cp -Lv}, '/etc/resolv.conf', '/mnt/gentoo/etc/';
            excfailexit
              qw{sudo rsync -P -a -v /recovery/rootfs.squashfs /mnt/gentoo/recovery/};
        }
    );
}

sub _on_click_select_media($self) {
    my ($block_name) = split /\t+/,
      $self->_dropdown_block_devices->selected_text;
    my $block      = "/dev/$block_name";
    my $root_count = my ( $root_label, $root_uuid ) = split /\s+/,
      `lsblk -o PARTLABEL,PARTUUID $block | grep AlgaOSRoot`;
    my $efi_count = my ( $efi_label, $efi_uuid ) = split /\s+/,
      `lsblk -o PARTLABEL,PARTUUID $block | grep AlgaOSEFI`;
    my $recovery_count = my ( $recovery_label, $recovery_uuid ) = split /\s+/,
      `lsblk -o PARTLABEL,PARTUUID $block | grep AlgaOSRecovery`;
    if ( !scalar $root_count ) {
        $self->_scroll->set_child( $self->_create_real_install_grid );
        return;
    }
    $self->_recovery_uuid($recovery_uuid);
    $self->_root_uuid($root_uuid);
    $self->_efi_uuid($efi_uuid);
    $self->_scroll->set_child( $self->_create_recover_installation_grid );
}

sub _failed_install {
    my $self = shift;
    say 'Failure';
    $self->_scroll->set_child(
        $self->_create_install_grid(
"The @{[$self->_install_is_recover ? 'recovery' : 'installation']} failed"
        )
    );
}

sub _succesful_install {
    my $self = shift;
    say 'Success';
    $self->_scroll->set_child(
        $self->_create_install_grid(
"The @{[$self->_install_is_recover ? 'recovery' : 'installation']} went well reboot and unplug the booting media use it"
        )
    );
}

sub _install( $self, %args ) {
    my ( $prepare, $hostname, $username, $password, $timezone, $locale,
        $complete_systemd, $container_block )
      = @args{
        qw/prepare hostname username password timezone locale complete_systemd container_block/
      };
    if ( !defined $prepare || ref $prepare ne 'CODE' ) {
        die 'No prepare sub';
    }
    $self->_scroll->set_child(
        $self->_create_install_grid(
"The system is now being @{[$self->_install_is_recover ? 'recovered' : 'installed']}"
        )
    );
    my $pid = fork;
    if ( !$pid ) {
        local $SIG{INT} = sub {
            system 'sudo umount -R /mnt/gentoo';
            system 'sudo umount -R /recovery';
        };
        eval {
            $prepare->();
            excfailexit
              qw{sudo perl}, @perl_args_chroot, qw{-MAlgaOS::Installer::GUI -e AlgaOS::Installer::GUI::chroot_install_commands(@ARGV)},
              $hostname, $username, $password, $timezone, $locale,
              $container_block, $complete_systemd;

            system 'sudo umount -R /mnt/gentoo';
            system 'sudo umount -R /recovery';
        };
        if ($@) {
            warn $@;
            exit 1;
        }
        say "Finish $$";
        exit 0;
    }
    $self->app->timeout_add(
        1000,
        sub {
            my $waitpid_result = waitpid( $pid, WNOHANG );
            if ( $waitpid_result == -1 ) {
                $self->_failed_install;
                return 0;
            }
            if ( $waitpid_result == 0 ) {
                return 1;
            }
            if ( $waitpid_result == $pid ) {
                if ( $? != 0 ) {
                    $self->_failed_install;
                    return 0;
                }
                $self->_succesful_install;
                return 0;
            }
            return 0;
        }
    );
}

sub chroot_install_commands {
    my ( $hostname, $username, $password, $timezone, $locale, $block_devices,
        $complete_systemd )
      = @ARGV;
    system qw{mount -t proc proc /mnt/gentoo/proc};
    system qw{mount -t sysfs sysfs /mnt/gentoo/sys};
    system qw{mount --rbind /dev/ /mnt/gentoo/dev};
    system qw{mount --make-rslave /mnt/gentoo/proc};
    system qw{mount --make-rslave /mnt/gentoo/sys};
    system qw{mount --make-rslave /mnt/gentoo/dev};
    my $devices = `lsblk -o PARTLABEL,PARTUUID $block_devices`;
    chroot '/mnt/gentoo';
    chdir '/';
    open my $fh, '>', '/etc/hostname';
    say $fh "$hostname";
    close $fh;
    my @devices = split /\n/, $devices;
    shift @devices;
    @devices = grep { !/^\s*$/ } @devices;
    my %devices = map { ( split /\s+/, $_ ) } @devices;
    open $fh, '>', '/etc/fstab';
    say $fh <<"EOF";
PARTUUID=$devices{AlgaOSEFI}		/boot/efi		vfat		defaults        1 2
PARTUUID=$devices{AlgaOSRoot}		/		        ext4		defaults		0 1
PARTUUID=$devices{AlgaOSRecovery}   	/recovery 		ext4		defaults		0 1
EOF
    close $fh;

    if ($complete_systemd) {
        excfailexit qw{systemd-machine-id-setup};
        excfailexit qw{systemctl preset-all};
    }
    else {
        excfailexit qw{systemctl preset-all --preset-mode=enable-only};
    }
    excfailexit qw{systemctl enable gdm};
    excfailexit qw{systemctl enable NetworkManager};
    excfailexit qw{systemctl enable cronie};
    excfailexit qw{systemctl enable bluetooth};
    excfailexit qw{systemctl enable chronyd};
    if ($timezone) {
        excfailexit qw{ln -svf}, "../usr/share/zoneinfo/$timezone",
          '/etc/localtime';
    }
    if ($locale) {
        excfailexit qw{eselect locale set}, $locale;
    }
    if ($username) {
        system qw{useradd -m}, $username, qw{-s /bin/bash};
        system qw{gpasswd -a}, $username, qw{plugdev};
    }
    if ( $username && $password ) {
        open $fh, '|-', qw{passwd --stdin}, $username;
        say $fh $password;
        close $fh;
    }

    if ( $? != 0 ) {
        excfailexit "forcing-a-fail-because-passwd-failed-and-iam-lazy";
    }
    my $sudoers_dir = '/etc/sudoers.d';
    excfailexit qw{mkdir -pv}, $sudoers_dir;
    open $fh, '>', "$sudoers_dir/algaos";
    say $fh "user ALL=(ALL) NOPASSWD: ALL";
    close $fh;
    excfailexit
      "rsync -a --mkpath /boot/kernel* /boot/initramfs* /boot/recovery/";
    my $grub_dir = "/boot/grub";
    system qw{mkdir -pv}, $grub_dir;
    open $fh, '<', '/grub_hash';
    local $/ = undef;
    my $hash_complete = <$fh>;
    close $fh;

    if ( !$hash_complete ) {
        my $salt       = urandom(64);
        my $salt_hex   = unpack( 'H*', $salt );
        my $iterations = 1000;

        die 'No pass' if !$password;
        my $hash =
          PBKDF2::Tiny::derive_hex( 'SHA-512', $password, $salt, $iterations,
            64 );
        $hash_complete = "$iterations.$salt_hex.$hash";
        open my $fh, '>', '/grub_hash';
        print $fh $hash_complete;
        close $fh;
    }

    open $fh, '>', "$grub_dir/grub.cfg";
    say $fh <<"EOF";
set timeout=5
set default=0
set superusers="admin"
password_pbkdf2 admin grub.pbkdf2.sha512.$hash_complete
EOF

    for my $kver ( glob("/boot/kernel-*") ) {
        die "No kernel found in /boot\n" unless $kver;

        $kver =~ s{.*/kernel-}{};
        say $fh <<"EOF";
menuentry "AlgaOS" --unrestricted {
    linux /boot/kernel-$kver root=PARTUUID=$devices{AlgaOSRoot} splash quiet
    initrd /boot/initramfs-$kver.img
};
EOF
    }
    excfailexit qw{emerge --sync};
    excfailexit qw{plymouth-set-default-theme -R colorful_loop};
    for my $kver ( glob("/boot/recovery/kernel-*") ) {
        die "No kernel found in /boot/recovery\n" unless $kver;

        $kver =~ s{.*/kernel-}{};
        excfailexit qw{dracut --force --kver}, $kver,
          qw{--no-hostonly --stdlog 6 --force --add}, "plymouth dmsquash-live",
          "/boot/recovery/initramfs-${kver}.img";

        say $fh <<"EOF";
menuentry "AlgaOS Recovery" --users admin {
    linux /boot/recovery/kernel-$kver root=live:PARTUUID=$devices{AlgaOSRecovery} rd.live.dir=/ rd.live.squashimg=rootfs.squashfs rd.live.overlay.overlayfs=1 rd.live.debug=1 rd.systemd.show_status=1 rd.systemd.log_level=debug splash quiet
    initrd /boot/recovery/initramfs-$kver.img
};
EOF
    }
    excfailexit qw{rm -frv /var/db/repos/algaos/};
    $ENV{HOME}          = '/home/test';
    $ENV{USER}          = 'test';
    $ENV{LOGNAME}       = 'test';
    $ENV{XDG_DATA_HOME} = '/home/test/.local/share';
    $ENV{XDG_DATA_DIRS} =
'/home/test/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share';
    if ($username) {
        excfailexit qw{sudo -u}, $username, qw{dbus-run-session -- bash -c},
"flatpak --user remote-add --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo";
        excfailexit qw{sudo -u}, $username, qw{dbus-run-session -- bash -c},
          "flatpak --user install --noninteractive com.valvesoftware.Steam";
        excfailexit qw{sudo -u}, $username, qw{dbus-run-session -- bash -c},
          "flatpak --user install --noninteractive com.usebottles.bottles";
    }
    excfailexit qw{grub-install --target=i386-pc --recheck}, $block_devices;
    excfailexit qw{grub-install
      --target=x86_64-efi
      --efi-directory=/boot/efi
      --removable};
    exit 0;
}

sub activate($self) {
    my $const                     = $self->const;
    my $win                       = Gtk::ApplicationWindow->new( $self->app );
    my $start_installation_button = Gtk::Button->new("Install in this disk");
    $self->_start_installation_button($start_installation_button);
    $win->set_title("Install AlgaOS");
    my $display  = $win->get_display;
    my $provider = Gtk::CssProvider->new;
    $provider->load_from_path($dist_dir_files.'/style.css');
    $display->add_css_provider( $provider,
        $const->GTK_STYLE_PROVIDER_PRIORITY_APPLICATION );
    my $width  = 800;
    my $height = ( 1080 * 800 ) / 1920;
    $win->set_default_size( $width, $height );
    $win->set_resizable(0);
    $self->win($win);
    my $overlay = Gtk::Overlay->new;
    my $file    = Gio::File->new($dist_dir_files.'/beach0.jpg');
    my $texture = Gdk::Texture->new($file);
    my $picture = Gtk::Picture->new($texture);
    $overlay->set_child($picture);
    my $hostname_entry = Gtk::Entry->new;
    $self->_hostname_entry($hostname_entry);
    my $username_entry = Gtk::Entry->new;
    $self->_username_entry($username_entry);
    my $password_entry = Gtk::Entry->new;
    $self->_password_entry($password_entry);
    my $scroll = Gtk::ScrolledWindow->new;
    $scroll->set_child( $self->_create_main_grid );
    $self->_scroll($scroll);
    $overlay->add_overlay($scroll);
    $win->set_child($overlay);
    $win->present;
}

sub run($self) {
    system qw{sudo umount -R /mnt/gentoo};
    system 'sudo umount -R /recovery';
    $self->app->connect(
        'activate' => sub {
            $self->activate;
        }
    );

    $self->app->run(@ARGV);
}
1;