/* * Guillaume Cottenceau (gc@mandrakesoft.com) * * Copyright 2000 MandrakeSoft * * This software may be freely redistributed under the terms of the GNU * public license. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */ /* * Portions from Erik Troan (ewt@redhat.com) * * Copyright 1996 Red Hat Software * */ #include // dietlibc can do hostname lookup, whereas glibc can't when linked statically :-( #if defined(__dietlibc__) #include #include #include #include #include #include "network.h" #include "log.h" #include "dns.h" int mygethostbyname(char * name, struct in_addr * addr) { struct hostent * h; /* prevent from timeouts */ if (dns_server.s_addr == 0) return -1; h = gethostbyname(name); if (!h) { if (domain) { // gethostbyname from dietlibc doesn't support domain handling char fully_qualified[500]; sprintf(fully_qualified, "%s.%s", name, domain); h = gethostbyname(fully_qualified); if (!h) { log_message("unknown host %s", name); return -1; } } else return -1; } if (h->h_addr_list && (h->h_addr_list)[0]) { memcpy(addr, (h->h_addr_list)[0], sizeof(*addr)); log_message("is-at: %s", inet_ntoa(*addr)); return 0; } return -1; } char * mygethostbyaddr(char * ipnum) { struct in_addr in; struct hostent * host; /* prevent from timeouts */ if (dns_server.s_addr == 0) return NULL; if (!inet_aton(ipnum, &in)) return NULL; host = gethostbyaddr(&(in.s_addr), sizeof(in.s_addr) /* INADDRSZ */, AF_INET); if (host && host->h_name) return host->h_name; return NULL; } #elif defined(__GLIBC__) #include #include #include #include #include #include #include #include #include "log.h" #include "dns.h" /* This is dumb, but glibc doesn't like to do hostname lookups w/o libc.so */ union dns_response { HEADER hdr; u_char buf[PACKETSZ]; } ; static int do_query(char * query, int queryType, char ** domainName, struct in_addr * ipNum) { int len, ancount, type; u_char * data, * end; char name[MAXDNAME]; union dns_response response; #ifdef __sparc__ /* from jj: */ /* We have to wait till ethernet negotiation is done */ _res.retry = 3; #else _res.retry = 2; #endif len = res_search(query, C_IN, queryType, (void *) &response, sizeof(response)); if (len <= 0) return -1; if (ntohs(response.hdr.rcode) != NOERROR) return -1; ancount = ntohs(response.hdr.ancount); if (ancount < 1) return -1; data = response.buf + sizeof(HEADER); end = response.buf + len; /* skip the question */ data += dn_skipname(data, end) + QFIXEDSZ; /* parse the answer(s) */ while (--ancount >= 0 && data < end) { /* skip the domain name portion of the RR record */ data += dn_skipname(data, end); /* get RR information */ GETSHORT(type, data); data += INT16SZ; /* skipp class */ data += INT32SZ; /* skipp TTL */ GETSHORT(len, data); if (type == T_PTR) { /* we got a pointer */ len = dn_expand(response.buf, end, data, name, sizeof(name)); if (len <= 0) return -1; if (queryType == T_PTR && domainName) { /* we wanted a pointer */ *domainName = malloc(strlen(name) + 1); strcpy(*domainName, name); return 0; } } else if (type == T_A) { /* we got an address */ if (queryType == T_A && ipNum) { /* we wanted an address */ memcpy(ipNum, data, sizeof(*ipNum)); return 0; } } /* move ahead to next RR */ data += len; } return -1; } char * mygethostbyaddr(char * ipnum) { int rc; char * result; char * strbuf; char * chptr; char * splits[4]; int i; _res.retry = 1; strbuf = alloca(strlen(ipnum) + 1); strcpy(strbuf, ipnum); ipnum = alloca(strlen(strbuf) + 20); for (i = 0; i < 4; i++) { chptr = strbuf; while (*chptr && *chptr != '.') chptr++; *chptr = '\0'; if (chptr - strbuf > 3) return NULL; splits[i] = strbuf; strbuf = chptr + 1; } sprintf(ipnum, "%s.%s.%s.%s.in-addr.arpa", splits[3], splits[2], splits[1], splits[0]); rc = do_query(ipnum, T_PTR, &result, NULL); if (rc) return NULL; else return result; } int mygethostbyname(char * name, struct in_addr * addr) { int rc = do_query(name, T_A, NULL, addr); if (!rc) log_message("is-at %s", inet_ntoa(*addr)); return rc; } #else #error "Unsupported C library" #endif format.pm?h=15.71'>logtreecommitdiffstats
blob: 90f6cc89a9ecc6cba8438ac17095b7cbcc1a1532 (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
package fs::format; # $Id$

use diagnostics;
use strict;
use String::ShellQuote;

use run_program;
use common;
use fs::type;
use fs::loopback;
use log;

my %cmds = (
    ext2     => [ 'e2fsprogs', 'mkfs.ext2', '-F' ],
    ext3     => [ 'e2fsprogs', 'mkfs.ext3', '-F' ],
    ext4dev     => [ 'e2fsprogs', 'mkfs.ext3', '-F', '-I', '256' ], # FIXME: enable more options once we've better mkfs support
    reiserfs => [ 'reiserfsprogs', 'mkfs.reiserfs', '-ff' ],
    reiser4  => [ 'reiser4progs', 'mkfs.reiser4', '-f', '-y' ],
    xfs      => [ 'xfsprogs', 'mkfs.xfs', '-f', '-q' ],
    jfs      => [ 'jfsutils', 'mkfs.jfs', '-f' ],
    hfs      => [ 'hfsutils', 'hformat' ],
    dos      => [ 'dosfstools', 'mkdosfs' ],
    vfat     => [ 'dosfstools', 'mkdosfs', '-F', '32' ],
    swap     => [ 'util-linux-ng', 'mkswap' ],
    ntfs     => [ 'ntfsprogs', 'mkntfs', '--fast' ],
   'ntfs-3g' => [ 'ntfsprogs', 'mkntfs', '--fast' ],
);

my %LABELs = ( #- option, length, handled_by_mount
    ext2     => [ '-L', 16, 1 ],
    ext3     => [ '-L', 16, 1 ],
    ext4dev     => [ '-L', 16, 1 ],
    reiserfs => [ '-l', 16, 1 ],
    xfs      => [ '-L', 12, 1 ],
    jfs      => [ '-L', 16, 1 ],
    hfs      => [ '-l', 27, 0 ],
    dos      => [ '-n', 11, 0 ],
    vfat     => [ '-n', 11, 0 ],
    swap     => [ '-L', 15, 1 ],
);

sub package_needed_for_partition_type {
    my ($part) = @_;
    my $l = $cmds{$part->{fs_type}} or return;
    $l->[0];
}

sub known_type {
    my ($part) = @_;
    to_bool($cmds{$part->{fs_type}});
}

sub check_package_is_installed {
    my ($do_pkgs, $fs_type) = @_;

    my ($pkg, $binary) = @{$cmds{$fs_type} || return};
    whereis_binary($binary) || $do_pkgs->ensure_binary_is_installed($pkg, $binary); #- ensure_binary_is_installed checks binary chrooted, whereas we run the binary non-chrooted (pb for Mandriva One)
}

sub part {
    my ($all_hds, $part, $wait_message) = @_;
    if (isRAID($part)) {
	$wait_message->(N("Formatting partition %s", $part->{device})) if $wait_message;
	require raid;
	raid::format_part($all_hds->{raids}, $part);
    } elsif (isLoopback($part)) {
	$wait_message->(N("Creating and formatting file %s", $part->{loopback_file})) if $wait_message;
	fs::loopback::format_part($part);
    } else {
	$wait_message->(N("Formatting partition %s", $part->{device})) if $wait_message;
	part_raw($part, $wait_message);
    }
}

sub part_raw {
    my ($part, $wait_message) = @_;

    $part->{isFormatted} and return;

    if ($part->{encrypt_key}) {
	fs::mount::set_loop($part);
    }

    my $dev = $part->{real_device} || $part->{device};

    my @options = if_($part->{toFormatCheck}, "-c");
    log::l("formatting device $dev (type $part->{fs_type})");

    my $fs_type = $part->{fs_type};

    if (member($fs_type, qw(ext2 ext3 ext4dev))) {
	push @options, "-m", "0" if $part->{mntpoint} =~ m|^/home|;
    } elsif (isDos($part)) {
	$fs_type = 'dos';
    } elsif ($fs_type eq 'hfs') {
        push @options, '-l', "Untitled";
    } elsif (isAppleBootstrap($part)) {
	push @options, '-l', 'bootstrap';
    } elsif ($fs_type eq 'swap') {
	push @options, '-U', $part->{device_UUID} if $part->{device_UUID};
    }

    if ($part->{device_LABEL}) {
	if ($LABELs{$fs_type}) {
	    my ($option, $length, $handled_by_mount) = @{$LABELs{$fs_type}};
	    if (length $part->{device_LABEL} > $length) {
		my $short = substr($part->{device_LABEL}, 0, $length);
		log::l("shortening LABEL $part->{device_LABEL} to $short");
		$part->{device_LABEL} = $short;
	    }
	    delete $part->{prefer_device_LABEL}
	      if !$handled_by_mount || $part->{mntpoint} eq '/' && !member($fs_type, qw(ext2 ext3 ext4dev));

	    push @options, $option, $part->{device_LABEL};
	} else {
	    log::l("dropping LABEL=$part->{device_LABEL} since we don't know how to set labels for fs_type $part->{fs_type}");
	    delete $part->{device_LABEL};
	    delete $part->{prefer_device_LABEL};
	}
    }

    my ($_pkg, $cmd, @first_options) = @{$cmds{$fs_type} || die N("I do not know how to format %s in type %s", $part->{device}, $part->{fs_type})};

    my @args = ($cmd, @first_options, @options, devices::make($dev));

    if ($cmd eq 'mkfs.ext3' && $wait_message) {
	mkfs_ext3($wait_message, @args) or die N("%s formatting of %s failed", $fs_type, $dev);
    } else {
	run_program::raw({ timeout => 'never' }, @args) or die N("%s formatting of %s failed", $fs_type, $dev);
    }

    if (member($fs_type, qw(ext3 ext4dev))) {
	disable_forced_fsck($dev);
    }

    my $p = fs::type::type_subpart_from_magic($part);
    $part->{device_UUID} = $p && $p->{device_UUID};

    set_isFormatted($part, 1);
}

sub mkfs_ext3 {
    my ($wait_message, @args) = @_;

    my $cmd = shell_quote_best_effort(@args);
    log::l("running: $cmd");
    open(my $F, "$cmd |");

    local $/ = "\b";
    local $_;
    while (<$F>) {
	#- even if it still takes some time when format is over, we don't want the progress bar to stay at 85%
	$wait_message->('', $1, $2) if m!^\s*(\d+)/(\d+)\b!;
    }
    return close($F);
}

sub disable_forced_fsck {
    my ($dev) = @_;
    run_program::run("tune2fs", "-c0", "-i0", devices::make($dev));
}

sub formatMount_part {
    my ($part, $all_hds, $fstab, $wait_message) = @_;

    if (isLoopback($part)) {
	formatMount_part($part->{loopback_device}, $all_hds, $fstab, $wait_message);
    }
    if (my $p = fs::get::up_mount_point($part->{mntpoint}, $fstab)) {
	formatMount_part($p, $all_hds, $fstab, $wait_message) if !fs::type::carry_root_loopback($part);
    }
    if ($part->{toFormat}) {
	fs::format::part($all_hds, $part, $wait_message);
    }

    #- setting user_xattr on /home (or "/" if no /home)
    if (!$part->{isMounted} && member($part->{fs_type}, qw(ext3 ext4dev))
	  && ($part->{mntpoint} eq '/home' ||
		!fs::get::has_mntpoint('/home', $all_hds) && $part->{mntpoint} eq '/')) {
	run_program::run('tune2fs', '-o', 'user_xattr', devices::make($part->{real_device} || $part->{device}));
    }

    fs::mount::part($part, 0, $wait_message);
}

sub formatMount_all {
    my ($all_hds, $fstab, $wait_message) = @_;
    formatMount_part($_, $all_hds, $fstab, $wait_message) 
      foreach sort { isLoopback($a) ? 1 : isSwap($a) ? -1 : 0 } grep { $_->{mntpoint} } @$fstab;

    #- ensure the link is there
    fs::loopback::carryRootCreateSymlink($_) foreach @$fstab;

    #- for fun :)
    #- that way, when install exits via ctrl-c, it gives hand to partition
    eval {
	my ($_type, $major, $minor) = devices::entry(fs::get::root($fstab)->{device});
	output "/proc/sys/kernel/real-root-dev", makedev($major, $minor);
    };
}


1;