summaryrefslogtreecommitdiffstats
path: root/rescue/partimage_whole_disk
blob: ed0653a952200ea6f3924266f6d38908d9f50a7b (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
#!/usr/bin/perl

use lib qw(/usr/lib/libDrakX);
use standalone;
use fsedit;
use fs::format;
use fs::type;
use resize_fat::main;
use diskdrake::resize_ntfs;
use diskdrake::resize_ext2;
use common;
use partition_table::empty;
use Carp::Heavy;

my %options = (
    save_home_directory => 1,
    empty_space_at_end_of_disk => 0, # 300 * 1024 * 2, # 300MB
    ask_before_modifying_home => 1,
    bzip2 => 1,
);

my ($server);
if ($ARGV[0] eq '-s') {
    (undef, $server, @ARGV) = @ARGV;
} 
my $action = shift @ARGV;

sub usage() { die "partimage_whole_disk [-s <server>] (save_all <dir> | rest_all <dir>)\n" }

$ENV{PATH} = "/sbin:/usr/sbin:$ENV{PATH}";
$ENV{HOME} = '/';
log::openLog("/var/log/partimage_whole_disk.log");
my @partimage_cmd = ('partimage', if_($server, '-s', $server, '-n', '-L'));

@ARGV == 1 or usage();

if ($server && !is_network_configured()) {
    run_program::run('drvinst', 'NETWORK'); 
    run_program::run('dhcp-client');
}

run_program::run('drvinst', 'STORAGE_SCSI', 'STORAGE_IDE');

my $all_hds = fsedit::get_hds({});

if ($action eq 'save_all') {
    save_all($ARGV[0]);
} elsif ($action eq 'rest_all') {
    rest_all($ARGV[0]);
}

sub save_all {
    my ($dir) = @_;

    my $base_dir = $dir;
    for (my $i = 0; read_part_list($dir); $i++) {
	#- find a free dir
	$dir = sprintf("$base_dir.%03d", $i);
    }

    my $hd = $all_hds->{hds}[0] or die "no main hard drive\n";
    log::l("save_all on $hd->{device}");
    my $part_list = [ partition_table::get_normal_parts($hd) ];

    foreach (@$part_list) {
	$_->{saved} = !member($_->{fs_type}, 'ntfs', 'ntfs-3g', 'vfat', 'swap');
    }

    if (!$options{save_home_directory}) {
	#- shrink and don't save the last ext3 partition (which is the /home partition)
	if (my $part = find { isTrueLocalFS($_) } reverse @$part_list) {
	    $part->{size} = min($part->{size}, 1024 * 1024 * 2); # not greater than 1GB
	    $part->{saved} = 0;
        }
    }

    foreach (grep { $_->{saved} } @$part_list) {
	run_or_die(@partimage_cmd,
		   if_($options{bzip2}, '-z', 2),
		   '-V', 0, '--nombr', '--nodesc', '--nocheck', '-b', '-o',
		   'save', devices::make($_->{device}), "$dir/$_->{device}");
    }
    save_part_list($dir, $hd->{geom}, $part_list);
}
sub rest_all {
    my ($dir) = @_;

    my ($forced_geom, $part_list) = read_part_list($dir) or error("read_part_list $dir failed");

	(my $hd) = my @used_hds = uniq(map {
	    my $part = $_;
	    find { $part->{device} =~ /^\Q$_->{device}\E./ } fs::get::hds($all_hds) 
	      or error("can't find hard drive for partition $part->{device}");
	} @$part_list);

	@used_hds >= 1 or error("no matching hd");
	@used_hds <= 1 or error("multiple hds: " . join(' ', map { $_->{device} } @used_hds));

	fs::type::set_fs_type($_, $_->{fs_type}) foreach @$part_list;
	put_in_hash($_, partition_table::hd2minimal_part($hd)) foreach @$part_list;

	my ($from_partimage, $other) = partition { $_->{saved} } @$part_list;
	my ($from_resize, $created) = partition { member($_->{fs_type}, 'vfat', 'ntfs', 'ntfs-3g') } @$other;
	
	my $total = sum(map { $_->{size} } @$part_list);
	if ($total > $hd->{totalsectors}) {
	    error("$dir doesn't fit: $total > $hd->{totalsectors}");
	}


    foreach (@$from_resize) {
	#- resize first
	my $part = fs::get::device2part($_->{device}, [ fs::get::fstab($all_hds) ]);
	if (!$part) {
	    log::l("partition to resize is missing ($_->{device})");
	    $_->{missing} = 1;
	    next;
	}
	if ($part->{fs_type} ne $_->{fs_type}) {
	    log::l("partition $_->{device} doesn't have the right filesystem ($part->{fs_type} != $_->{fs_type})");
	    $_->{missing} = 1;
	    next;
	}

	if (@$from_resize == 1) {
	    my $half_size = int($hd->{totalsectors} / 2) - 2 * $hd->cylinder_size;
	    my $suggested_total = $total - $_->{size} + $half_size;
	    log::l("resizing bigger? (size $_->{size}, half_size $half_size, total $total, suggested_total $suggested_total)");
	    if ($half_size > $_->{size} && $suggested_total < $hd->{totalsectors}) {
		log::l("prefering to resize $_->{device} to size $half_size instead of $_->{size}");
		$_->{size} = $half_size;
	    }
	}

	$_->{start} = $part->{start};
	if ($_->{size} < $part->{size}) {
	    log::l("resizing $_->{device} to $_->{size} (it is $part->{size})");
	    my $resize_pkg = $_->{fs_type} eq 'vfat' ? 'resize_fat::main' : 'diskdrake::resize_ntfs';
	    my $resize = $resize_pkg->new($_->{device}, devices::make($_->{device}));
	    $resize->resize($_->{size});
	} else {
	    log::l("no need to resize, instead setting $_->{device}'s size to $part->{size} instead of $_->{size}");
	    $_->{size} = $part->{size};
	}
    }

    put_in_hash($hd->{geom}, $forced_geom);
    log::l("totalsectors $hd->{totalsectors} heads $hd->{geom}{heads} sectors $hd->{geom}{sectors}");
    partition_table::raw::compute_nb_cylinders($hd->{geom}, $hd->{totalsectors});

    #- grow the last ext3 partition
    if (my $part = find { isTrueLocalFS($_) } reverse @$part_list) {
	$part->{ratio} = 1;

	if ($options{ask_before_modifying_home}) {
	    print "\nkeep existing /home? (Y/n) ";
	    if (<STDIN> !~ /n/i) {
		my $l = @$from_partimage > 1 ? $from_partimage : $created;
		#- it was meant to be restored or formatted
		my $p = pop @$l;
		log::l("keeping existing /home: removing $p->{device}");
	    }
	}
    }

    #- write the partition table
    partition_table::raw::zero_MBR($hd);
    foreach my $part (grep { $_->{rootDevice} eq $hd->{device} } @$part_list) {
	next if $part->{missing};

	my $hole = find { isEmpty($_) && $_->{size} >= $part->{size} } partition_table::get_normal_parts_and_holes($hd) or die "not enough room for $part->{device}";
	$part->{start} = $hole->{start};
	    
	log::l("handling $part->{device}");
	my $extended = $part->{device} =~ /(\d+)$/ && $1 > 4 && $hd->hasExtended;

	my %wanted_part = %$part;
	if ($part->{ratio}) {
	    $part->{size} = $hole->{size} - ($options{empty_space_at_end_of_disk} || 0);
	} else {
	    $part->{size} += $hd->{geom}{sectors} if $extended;
	    $part->{size} += $hd->cylinder_size if $part->{start} == 1;
	}
	log::l("adding $part->{device} with size $part->{size}");
	partition_table::add($hd, $part, $extended ? 'Extended' : 'Primary');
	foreach ('device', if_(!$part->{ratio}, 'size')) {
	    $part->{$_} eq $wanted_part{$_} or log::l("bad $_ for $part->{device}: $part->{$_} != $wanted_part{$_}");
	}
    }
    partition_table::write($hd);

    #- restore from partimage
    foreach (@$from_partimage) {
	run_or_die(@partimage_cmd, 'restore', '-b', devices::make($_->{device}), "$dir/$_->{device}");

	if ($_->{ratio}) {
	    my $resize = diskdrake::resize_ext2->new($_->{device}, devices::make($_->{device}));
	    $resize->resize($_->{size});
	}
    }

    foreach (@$created) {
	fs::format::part_raw($_, undef);
    }

    run_program::run('guessmounts');

    if (my @missing = grep { $_->{missing} } @$part_list) {
	my $missing = join('|', map { quotemeta($_->{device}) } @missing);
	log::l("drop missing devices from fstab and lilo.conf: $missing");
	$::prefix = '/mnt';
	substInFile { $_ = '' if m!^/dev/($missing)\s! } "$::prefix/etc/fstab";

	my $match;
	substInFile { 
	    /^\S/ and $match = m!^other=/dev/($missing)$!; 
	    $_ = '' if $match;
	} "$::prefix/etc/lilo.conf";
    }
    
    run_or_die('install_bootloader', '--auto');

    print "\n", "Your system is ready, press enter to reboot (Y/n) ";
    if (<STDIN> !~ /n/i) {
	run_program::run('reboot');
    }
}

sub lst_fields() { qw(device size fs_type saved) }
sub save_part_list {
    my ($dir, $geom, $part_list) = @_;
    my @l = map { join(' ', @$_{lst_fields()}) } @$part_list;
    log::l("save_part_list $dir: $_") foreach @l;
    my $partimage = join(' ', @partimage_cmd);
    open(my $F, "| $partimage -z0 -Bfoo=bar -o save_file $dir/lst");
    print $F join("/", $geom->{heads}, $geom->{sectors}), "\n";
    print $F "$_\n" foreach @l;
}
sub read_part_list {
    my ($dir) = @_;
    my $partimage = join(' ', @partimage_cmd);
    open(my $F, "$partimage -z0 -Bfoo=bar rest_file $dir/lst |");
    my $geom_string = <$F> or return;
    my %geom; @geom{'heads', 'sectors'} = split('/', chomp_($geom_string));
    my @l = chomp_(cat__($F));
    log::l("read_part_list $dir: $_") foreach @l;
    \%geom, [ map { my %l; @l{lst_fields()} = split; \%l } @l ];
}

sub run_or_die {
    my (@l) = @_;
    run_program::raw({ timeout => 4 * 60 * 60 }, @l) or die join(' ', @l) . " failed\n";
}

sub error {
    my ($msg) = @_;
    log::l($msg);
    die "$msg\n";
}

sub is_network_configured() {
    my (undef, @l) = cat_('/proc/net/route');
    find { /^(\S+)/ && $1 ne 'lo' } @l;
}