summaryrefslogtreecommitdiffstats
path: root/draklive-install
blob: be4355eb011c272c2af19464175cd0fe88fbbcaf (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
#!/usr/bin/perl

use lib qw(/usr/lib/libDrakX);
use standalone;
use interactive;
use fs::any;
use fs::type;
use fs::partitioning;
use fs::partitioning_wizard;
use MDK::Common;
use common;
use feature qw(state);

push @::textdomains, 'draklive-install';

{
    use diskdrake::interactive;
    package diskdrake::interactive;
    my $old = \&hd_possible_actions;
    undef *hd_possible_actions;
    *hd_possible_actions = sub {
	#- for the partition wizard to show the auto-allocate option
	local $::isInstall = 1;
	&$old;
    };
    undef *Done;
    #- skip the fstab/reboot checks
    *Done = \&diskdrake_interactive_Done;
    #- don't ask whether to Move/Hide old files
    undef *need_migration;
    *need_migration = sub { 'hide' };
}

install_live();

sub install_live() {
    my $in = 'interactive'->vnew('su');
    $in->{pop_wait_messages} = 0;

    $::isWizard = 1;
    $::Wizard_no_previous = 1;
    $::Wizard_pix_up = "MandrivaOne-install-icon";

    my $all_hds = {};
    my $fstab = [];
    $::prefix = '/mnt/install';
    my $copy_source = '/';

    display_start_message();
    init_hds($in, $all_hds, $fstab);
    ask_partitions_loop($in, $all_hds, $fstab, $copy_source);
    prepare_root($in, $all_hds);
    copy_root($in, $copy_source);
    complete_install($in, $all_hds);
    setup_bootloader($in, $all_hds, $fstab);
    finish_installation($fstab);
    display_end_message($in);
    $in->exit(0);
}

sub umount_all {
    my ($fstab) = @_;
    #- make sure nothing is mounted in the new root
    foreach (sort { $b cmp $a } grep { /^$::prefix/ } map { (split)[1] } cat_('/proc/mounts')) {
        system('umount', $_);
    }
    #- make sure selected devices aren't mounted, and swap isn't used
    foreach (grep { isSwap($_) } @$fstab) {
        eval { fs::mount::swapoff($_->{device}) };
    }
    foreach (map { $_->{mntpoint} && !isSwap($_) ? "/dev/$_->{device}" : () } @$fstab) {
        system('umount', $_);
    }
}

sub on_reboot_needed {
    my ($in) = @_;
    fs::partitioning_wizard::warn_reboot_needed($in);
    $in->exit(0);
}

sub display_start_message() {
    require any;
    my $has_running_wm = to_bool(any::running_window_manager());
    local $::isStandalone = $has_running_wm; # center me if run in xsetup.d script
    my $w = ugtk2->new(N("Mandriva Live"));
    ugtk2::gtkadd($w->{window},
                  ugtk2::gtkcreate_img("MandrivaOne-install"),
                  ugtk2::gtknew('Label', height => 5),
                  N("This wizard will help you to install the live distribution."),
                  ugtk2::create_okcancel($w));
    $w->{ok}->grab_focus;
    $w->main;
}

sub umount_first_pass() {
    local $::prefix = undef;
    my $all_hds = fsedit::get_hds();
    fs::get_raw_hds('', $all_hds);
    fs::get_info_from_fstab($all_hds);
    my $fstab = [ fs::get::fstab($all_hds) ];
    fs::merge_info_from_mtab($fstab);
    eval { fs::mount::umount_all($fstab) };
}

sub init_hds {
    my ($in, $all_hds, $fstab) = @_;
    my $_wait = $in->wait_message('', N("Please wait"));
    umount_first_pass();
    fs::any::get_hds($all_hds, $fstab, [], {}, 'skip_mtab', $in);
    umount_all($fstab);
}

sub ask_partitions_loop {
    my ($in, $all_hds, $fstab, $copy_source) = @_;

    while (1) {
        eval { ask_partitions($in, $all_hds, $fstab, $copy_source) };
        my $err = $@ or last;
        $in->exit(1) if $err =~ /wizcancel/ ||
          !$in->ask_warn(N("Error"), [ N("An error occurred"), formatError($err) ]);
    }
}

sub ask_partitions {
    my ($in, $all_hds, $fstab, $copy_source) = @_;
    fs::partitioning_wizard::main($in, $all_hds, $fstab, [], undef, {}, 'skip_mtab');

    mkdir_p($::prefix) or die "unable to create $::prefix";

    fs::any::write_hds($all_hds, $fstab, undef, sub { on_reboot_needed($in) }, {});
    fs::any::check_hds_boot_and_root($all_hds, $fstab);
    fs::partitioning::choose_partitions_to_format($in, $fstab);
    umount_all($fstab);

    my $total = get_total_size($in, $copy_source);
    my $available = fs::any::getAvailableSpace($fstab, 'skip_mounted');
    die N("Not enough space available (%s available while %s are needed)",
          formatXiB($available), formatXiB($total)) . "\n"
      if $total > $available;

    fs::partitioning::format_mount_partitions($in, $all_hds, $fstab);
}

sub prepare_root {
    my ($in, $all_hds) = @_;
    #- create required directories and devices (early to have a consistent root before calling other programs)
    my $_wait = $in->wait_message('', N("Please wait"));
    fs::any::prepare_minimal_root($all_hds);
    run_program::run('makedev', $::prefix . '/dev');
}

sub build_copy_command {
    my ($source, $dest) = @_;
    join(' ',
         'tar', 'c', '--one-file-system', '-C', $source, '.',
         '|',
         'tar', 'xvv', '-C', $dest,
     );
}

sub get_total_size {
    my ($in, $source) = @_;
    state %total;
    return $total{$source} if $total{$source};
    my $_wait = $in->wait_message('', N("Computing total size"));
    $total{$source} = first(split(/\s+/, `du -sbx $source 2>/dev/null`));
}

sub copy_root {
    my ($in, $copy_source) = @_;
    my $total = get_total_size($in, $copy_source);

    my ($wait, $update_progress) = copying_message_with_progress_bar($in, N("Copying in progress"));
    open(my $OUTPUT, '-|', build_copy_command($copy_source, $::prefix));
    {
        local $_;
        my $current = my $previous = 0;
        while (<$OUTPUT>) {
            (undef, undef, my $size) = split;
            $current += $size;
            if ($current <= $total && $current/$total > $previous/$total + 0.001) {
                $update_progress->('', $current, $total);
                $previous = $current;
            }
        }
    }
    if (!close($OUTPUT)) {
        undef $wait;
        undef $update_progress;
        $in->ask_warn(N("Error"), N("Unable to copy files to new root"));
        $in->exit(1);
    }
}

sub complete_install {
    my ($in, $all_hds) = @_;
    my $_wait = $in->wait_message('', N("Please wait"));

    my $real_rpm_dir = "/tmp/rpm/real";
    cp_f(glob($real_rpm_dir . "/*"), $::prefix . "/var/lib/rpm") if -d $real_rpm_dir;

    #- FIXME: maybe factorize with draklive, using draklive --clean-chroot ?
    #- remove unwanted files and packages
    my $live_user = chomp_(cat_('/etc/draklive-install.d/user'));
    my $live_user_desktop = $live_user && chomp_(run_program::rooted_get_stdout($::prefix, "su - $live_user -c 'xdg-user-dir DESKTOP'"));
    unlink(map { $::prefix . $_ } '/.autofsck',
           '/etc/modprobe.d/mandriva-live',
           if_($live_user_desktop,
               $live_user_desktop . '/draklive-copy-wizard.desktop',
               $live_user_desktop . '/draklive-install.desktop'),
           );
    system('chroot', $::prefix, 'rpm', '-e', 'draklive-install');

    #- copy sysconfig files for first boot
    cp_f(glob('/etc/draklive-install.d/sysconfig/*'), $::prefix . '/etc/sysconfig');

    #- allow mdkonline to be started again
    eval { rm_rf($::prefix . '/etc/skel/.MdkOnline', glob($::prefix . '/home/*/.MdkOnline')) };

    #- unselect live user in kdm
    my $kdm_cfg = '/etc/kde/kdm/kdmrc';
    update_gnomekderc($::prefix . $kdm_cfg,
                      'X-:0-Greeter' => (PreselectUser => 'None', DefaultUser => '')) if -f $kdm_cfg;
    any::set_autologin($in->do_pkgs, undef);

    #- allow to install doc in disk install
    substInFile { undef $_ if /^\%_excludedocs/ } $::prefix . '/etc/rpm/macros';

    fs::write_fstab($all_hds, $::prefix);

    #- remove harddisks from harddrake's config file, so that hardddisks
    #- are automatically rediscovered at first boot
    require Storable;
    my $harddrake_file = $::prefix . "/etc/sysconfig/harddrake2/previous_hw";
    my $harddrake_conf = eval { Storable::retrieve($harddrake_file) };
    if ($harddrake_conf) {
        delete $harddrake_conf->{HARDDISK};
        Storable::store($harddrake_conf, $harddrake_file);
    }

    # enable back some disabled services
    require services;
    services::start_service_on_boot($_) foreach qw(dkms crond);
}

sub setup_bootloader {
    my ($in, $all_hds, $fstab) = @_;
    use bootloader;
    my $bootloader = {};
    any::setupBootloaderBeforeStandalone($in->do_pkgs, $bootloader, $all_hds, $fstab);
    local $::Wizard_no_previous = 0;
    any::setupBootloaderUntilInstalled($in, $bootloader, $all_hds, $fstab, $ENV{SECURE_LEVEL});
}

sub finish_installation {
    my ($fstab) = @_;
    #- cleanly umount here, it will avoid fs journals to be corrupted after a hackish reboot
    umount_all($fstab);
}

sub display_end_message {
    my ($in) = @_;
    $::Wizard_finished = 1;
    $in->ask_okcancel(N("Congratulations"), N("Please halt your computer, remove your live system, and restart your computer."));
}

###
### duplicate code
###

#- from disdrake::interactive
{
    package diskdrake::interactive;
  sub diskdrake_interactive_Done {
    my ($in, $all_hds) = @_;
    eval { raid::verify($all_hds->{raids}) };
    if (my $err = $@) {
	$::expert or die;
	$in->ask_okcancel('', [ formatError($err), N("Continue anyway?") ]) or return;
    }
    foreach (@{$all_hds->{hds}}) {
	if (!write_partitions($in, $_, 'skip_check_rebootNeeded')) {
	    return if !$::isStandalone;
	    $in->ask_yesorno(N("Quit without saving"), N("Quit without writing the partition table?"), 1) or return;
	}
    }
    #- skip that fstab/reboot steps
    if (!$::isInstall && 0) { 
	my $new = fs::fstab_to_string($all_hds);
	if ($new ne $all_hds->{current_fstab} && $in->ask_yesorno('', N("Do you want to save /etc/fstab modifications"), 1)) {
	    $all_hds->{current_fstab} = $new;
	    fs::write_fstab($all_hds);
	}
	update_bootloader_for_renumbered_partitions($in, $all_hds);

	if (any { $_->{rebootNeeded} } @{$all_hds->{hds}}) {
	    $in->ask_warn('', N("You need to reboot for the partition table modifications to take place"));
	    tell_wm_and_reboot();
	}
    }
    if (my $part = find { $_->{mntpoint} && !maybeFormatted($_) } fs::get::fstab($all_hds)) {
	$in->ask_okcancel('', N("You should format partition %s.
Otherwise no entry for mount point %s will be written in fstab.
Quit anyway?", $part->{device}, $part->{mntpoint})) or return if $::isStandalone && 0; #- no, please
    }
    1;
  }
}

# forked from interactive::wait_message
sub copying_message {
    my ($o, $title, $message, $b_temp) = @_;

    my $w = $o->wait_messageW($title, N("Copying in progress"), ugtk2::gtknew('VBox', padding => 5, children_tight => [
        ugtk2::gtkcreate_img("MandrivaOne-advert"),
        $message,
    ]));
    push @tempory::objects, $w if $b_temp;
    my $b = before_leaving { $o->wait_message_endW($w) };

    #- enable access through set
    MDK::Common::Func::add_f4before_leaving(sub { $o->wait_message_nextW($_[1], $w) }, $b, 'set');
    $b;
}

# forked from interactive::gtk::wait_message_with_progress_bar
sub copying_message_with_progress_bar {
    my ($in, $o_title) = @_;

    my $progress = Gtk2::ProgressBar->new;
    my $w = copying_message($in, $o_title, $progress);
    my $displayed;
    $progress->signal_connect(expose_event => sub { $displayed = 1; 0 });
    $w, sub {
	my ($msg, $current, $total) = @_;
	if ($msg) {
	    $w->set($msg);
	}

	if ($total) {
	    $progress or internal_error('You must first give some text to display');
	    $progress->set_fraction($current / $total);
	    $progress->show;
	    $displayed = 0;
	    mygtk2::flush() while !$displayed;
	} else {
	    $progress->hide if !$total;
	}
    };
}