disks.c revision 1.4.2.2 1 1.4.2.2 tls /* $NetBSD: disks.c,v 1.4.2.2 2014/08/10 07:00:24 tls Exp $ */
2 1.4.2.2 tls
3 1.4.2.2 tls /*
4 1.4.2.2 tls * Copyright 1997 Piermont Information Systems Inc.
5 1.4.2.2 tls * All rights reserved.
6 1.4.2.2 tls *
7 1.4.2.2 tls * Written by Philip A. Nelson for Piermont Information Systems Inc.
8 1.4.2.2 tls *
9 1.4.2.2 tls * Redistribution and use in source and binary forms, with or without
10 1.4.2.2 tls * modification, are permitted provided that the following conditions
11 1.4.2.2 tls * are met:
12 1.4.2.2 tls * 1. Redistributions of source code must retain the above copyright
13 1.4.2.2 tls * notice, this list of conditions and the following disclaimer.
14 1.4.2.2 tls * 2. Redistributions in binary form must reproduce the above copyright
15 1.4.2.2 tls * notice, this list of conditions and the following disclaimer in the
16 1.4.2.2 tls * documentation and/or other materials provided with the distribution.
17 1.4.2.2 tls * 3. The name of Piermont Information Systems Inc. may not be used to endorse
18 1.4.2.2 tls * or promote products derived from this software without specific prior
19 1.4.2.2 tls * written permission.
20 1.4.2.2 tls *
21 1.4.2.2 tls * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
22 1.4.2.2 tls * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.4.2.2 tls * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.4.2.2 tls * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
25 1.4.2.2 tls * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 1.4.2.2 tls * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 1.4.2.2 tls * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.4.2.2 tls * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.4.2.2 tls * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.4.2.2 tls * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 1.4.2.2 tls * THE POSSIBILITY OF SUCH DAMAGE.
32 1.4.2.2 tls *
33 1.4.2.2 tls */
34 1.4.2.2 tls
35 1.4.2.2 tls /* disks.c -- routines to deal with finding disks and labeling disks. */
36 1.4.2.2 tls
37 1.4.2.2 tls
38 1.4.2.2 tls #include <errno.h>
39 1.4.2.2 tls #include <stdio.h>
40 1.4.2.2 tls #include <stdlib.h>
41 1.4.2.2 tls #include <unistd.h>
42 1.4.2.2 tls #include <fcntl.h>
43 1.4.2.2 tls #include <util.h>
44 1.4.2.2 tls #include <uuid.h>
45 1.4.2.2 tls
46 1.4.2.2 tls #include <sys/param.h>
47 1.4.2.2 tls #include <sys/sysctl.h>
48 1.4.2.2 tls #include <sys/swap.h>
49 1.4.2.2 tls #include <ufs/ufs/dinode.h>
50 1.4.2.2 tls #include <ufs/ffs/fs.h>
51 1.4.2.2 tls #define FSTYPENAMES
52 1.4.2.2 tls #include <sys/disklabel.h>
53 1.4.2.2 tls #include <sys/disklabel_gpt.h>
54 1.4.2.2 tls
55 1.4.2.2 tls #include <dev/scsipi/scsipi_all.h>
56 1.4.2.2 tls #include <sys/scsiio.h>
57 1.4.2.2 tls
58 1.4.2.2 tls #include <dev/ata/atareg.h>
59 1.4.2.2 tls #include <sys/ataio.h>
60 1.4.2.2 tls
61 1.4.2.2 tls #include "defs.h"
62 1.4.2.2 tls #include "md.h"
63 1.4.2.2 tls #include "msg_defs.h"
64 1.4.2.2 tls #include "menu_defs.h"
65 1.4.2.2 tls #include "txtwalk.h"
66 1.4.2.2 tls
67 1.4.2.2 tls /* Disk descriptions */
68 1.4.2.2 tls struct disk_desc {
69 1.4.2.2 tls char dd_name[SSTRSIZE];
70 1.4.2.2 tls char dd_descr[70];
71 1.4.2.2 tls uint dd_no_mbr;
72 1.4.2.2 tls uint dd_cyl;
73 1.4.2.2 tls uint dd_head;
74 1.4.2.2 tls uint dd_sec;
75 1.4.2.2 tls uint dd_secsize;
76 1.4.2.2 tls uint dd_totsec;
77 1.4.2.2 tls };
78 1.4.2.2 tls
79 1.4.2.2 tls /* gpt(8) use different filesystem names.
80 1.4.2.2 tls So, we cant use ./common/lib/libutil/getfstypename.c */
81 1.4.2.2 tls struct gptfs_t {
82 1.4.2.2 tls const char *name;
83 1.4.2.2 tls int id;
84 1.4.2.2 tls uuid_t uuid;
85 1.4.2.2 tls };
86 1.4.2.2 tls static const struct gptfs_t gpt_filesystems[] = {
87 1.4.2.2 tls { "swap", FS_SWAP, GPT_ENT_TYPE_NETBSD_SWAP, },
88 1.4.2.2 tls { "ffs", FS_BSDFFS, GPT_ENT_TYPE_NETBSD_FFS, },
89 1.4.2.2 tls { "lfs", FS_BSDLFS, GPT_ENT_TYPE_NETBSD_LFS, },
90 1.4.2.2 tls { "linux", FS_EX2FS, GPT_ENT_TYPE_LINUX_DATA, },
91 1.4.2.2 tls { "windows,", FS_MSDOS, GPT_ENT_TYPE_MS_BASIC_DATA, },
92 1.4.2.2 tls { "hfs", FS_HFS, GPT_ENT_TYPE_APPLE_HFS, },
93 1.4.2.2 tls { "ufs", FS_OTHER, GPT_ENT_TYPE_APPLE_UFS, },
94 1.4.2.2 tls { "ccd", FS_CCD, GPT_ENT_TYPE_NETBSD_CCD, },
95 1.4.2.2 tls { "raid", FS_RAID, GPT_ENT_TYPE_NETBSD_RAIDFRAME, },
96 1.4.2.2 tls { "cgd", FS_CGD, GPT_ENT_TYPE_NETBSD_CGD, },
97 1.4.2.2 tls { "efi", FS_OTHER, GPT_ENT_TYPE_EFI, },
98 1.4.2.2 tls { "bios", FS_OTHER, GPT_ENT_TYPE_BIOS, },
99 1.4.2.2 tls { NULL, -1, GPT_ENT_TYPE_UNUSED, },
100 1.4.2.2 tls };
101 1.4.2.2 tls
102 1.4.2.2 tls /* Local prototypes */
103 1.4.2.2 tls static int foundffs(struct data *, size_t);
104 1.4.2.2 tls #ifdef USE_SYSVBFS
105 1.4.2.2 tls static int foundsysvbfs(struct data *, size_t);
106 1.4.2.2 tls #endif
107 1.4.2.2 tls static int fsck_preen(const char *, int, const char *);
108 1.4.2.2 tls static void fixsb(const char *, const char *, char);
109 1.4.2.2 tls static int is_gpt(const char *);
110 1.4.2.2 tls static int incoregpt(pm_devs_t *, partinfo *);
111 1.4.2.2 tls
112 1.4.2.2 tls #ifndef DISK_NAMES
113 1.4.2.2 tls #define DISK_NAMES "wd", "sd", "ld", "raid"
114 1.4.2.2 tls #endif
115 1.4.2.2 tls
116 1.4.2.2 tls static const char *disk_names[] = { DISK_NAMES, "vnd", "cgd", NULL };
117 1.4.2.2 tls
118 1.4.2.2 tls static bool tmpfs_on_var_shm(void);
119 1.4.2.2 tls
120 1.4.2.2 tls const char *
121 1.4.2.2 tls getfslabelname(uint8_t f)
122 1.4.2.2 tls {
123 1.4.2.2 tls if (f >= __arraycount(fstypenames) || fstypenames[f] == NULL)
124 1.4.2.2 tls return "invalid";
125 1.4.2.2 tls return fstypenames[f];
126 1.4.2.2 tls }
127 1.4.2.2 tls
128 1.4.2.2 tls /*
129 1.4.2.2 tls * Decide wether we want to mount a tmpfs on /var/shm: we do this always
130 1.4.2.2 tls * when the machine has more than 16 MB of user memory. On smaller machines,
131 1.4.2.2 tls * shm_open() and friends will not perform well anyway.
132 1.4.2.2 tls */
133 1.4.2.2 tls static bool
134 1.4.2.2 tls tmpfs_on_var_shm()
135 1.4.2.2 tls {
136 1.4.2.2 tls uint64_t ram;
137 1.4.2.2 tls size_t len;
138 1.4.2.2 tls
139 1.4.2.2 tls len = sizeof(ram);
140 1.4.2.2 tls if (sysctlbyname("hw.usermem64", &ram, &len, NULL, 0))
141 1.4.2.2 tls return false;
142 1.4.2.2 tls
143 1.4.2.2 tls return ram > 16UL*1024UL*1024UL;
144 1.4.2.2 tls }
145 1.4.2.2 tls
146 1.4.2.2 tls /* from src/sbin/atactl/atactl.c
147 1.4.2.2 tls * extract_string: copy a block of bytes out of ataparams and make
148 1.4.2.2 tls * a proper string out of it, truncating trailing spaces and preserving
149 1.4.2.2 tls * strict typing. And also, not doing unaligned accesses.
150 1.4.2.2 tls */
151 1.4.2.2 tls static void
152 1.4.2.2 tls ata_extract_string(char *buf, size_t bufmax,
153 1.4.2.2 tls uint8_t *bytes, unsigned numbytes,
154 1.4.2.2 tls int needswap)
155 1.4.2.2 tls {
156 1.4.2.2 tls unsigned i;
157 1.4.2.2 tls size_t j;
158 1.4.2.2 tls unsigned char ch1, ch2;
159 1.4.2.2 tls
160 1.4.2.2 tls for (i = 0, j = 0; i < numbytes; i += 2) {
161 1.4.2.2 tls ch1 = bytes[i];
162 1.4.2.2 tls ch2 = bytes[i+1];
163 1.4.2.2 tls if (needswap && j < bufmax-1) {
164 1.4.2.2 tls buf[j++] = ch2;
165 1.4.2.2 tls }
166 1.4.2.2 tls if (j < bufmax-1) {
167 1.4.2.2 tls buf[j++] = ch1;
168 1.4.2.2 tls }
169 1.4.2.2 tls if (!needswap && j < bufmax-1) {
170 1.4.2.2 tls buf[j++] = ch2;
171 1.4.2.2 tls }
172 1.4.2.2 tls }
173 1.4.2.2 tls while (j > 0 && buf[j-1] == ' ') {
174 1.4.2.2 tls j--;
175 1.4.2.2 tls }
176 1.4.2.2 tls buf[j] = '\0';
177 1.4.2.2 tls }
178 1.4.2.2 tls
179 1.4.2.2 tls /*
180 1.4.2.2 tls * from src/sbin/scsictl/scsi_subr.c
181 1.4.2.2 tls */
182 1.4.2.2 tls #define STRVIS_ISWHITE(x) ((x) == ' ' || (x) == '\0' || (x) == (u_char)'\377')
183 1.4.2.2 tls
184 1.4.2.2 tls static void
185 1.4.2.2 tls scsi_strvis(char *sdst, size_t dlen, const char *ssrc, size_t slen)
186 1.4.2.2 tls {
187 1.4.2.2 tls u_char *dst = (u_char *)sdst;
188 1.4.2.2 tls const u_char *src = (const u_char *)ssrc;
189 1.4.2.2 tls
190 1.4.2.2 tls /* Trim leading and trailing blanks and NULs. */
191 1.4.2.2 tls while (slen > 0 && STRVIS_ISWHITE(src[0]))
192 1.4.2.2 tls ++src, --slen;
193 1.4.2.2 tls while (slen > 0 && STRVIS_ISWHITE(src[slen - 1]))
194 1.4.2.2 tls --slen;
195 1.4.2.2 tls
196 1.4.2.2 tls while (slen > 0) {
197 1.4.2.2 tls if (*src < 0x20 || *src >= 0x80) {
198 1.4.2.2 tls /* non-printable characters */
199 1.4.2.2 tls dlen -= 4;
200 1.4.2.2 tls if (dlen < 1)
201 1.4.2.2 tls break;
202 1.4.2.2 tls *dst++ = '\\';
203 1.4.2.2 tls *dst++ = ((*src & 0300) >> 6) + '0';
204 1.4.2.2 tls *dst++ = ((*src & 0070) >> 3) + '0';
205 1.4.2.2 tls *dst++ = ((*src & 0007) >> 0) + '0';
206 1.4.2.2 tls } else if (*src == '\\') {
207 1.4.2.2 tls /* quote characters */
208 1.4.2.2 tls dlen -= 2;
209 1.4.2.2 tls if (dlen < 1)
210 1.4.2.2 tls break;
211 1.4.2.2 tls *dst++ = '\\';
212 1.4.2.2 tls *dst++ = '\\';
213 1.4.2.2 tls } else {
214 1.4.2.2 tls /* normal characters */
215 1.4.2.2 tls if (--dlen < 1)
216 1.4.2.2 tls break;
217 1.4.2.2 tls *dst++ = *src;
218 1.4.2.2 tls }
219 1.4.2.2 tls ++src, --slen;
220 1.4.2.2 tls }
221 1.4.2.2 tls
222 1.4.2.2 tls *dst++ = 0;
223 1.4.2.2 tls }
224 1.4.2.2 tls
225 1.4.2.2 tls
226 1.4.2.2 tls static int
227 1.4.2.2 tls get_descr_scsi(struct disk_desc *dd, int fd)
228 1.4.2.2 tls {
229 1.4.2.2 tls struct scsipi_inquiry_data inqbuf;
230 1.4.2.2 tls struct scsipi_inquiry cmd;
231 1.4.2.2 tls scsireq_t req;
232 1.4.2.2 tls /* x4 in case every character is escaped, +1 for NUL. */
233 1.4.2.2 tls char vendor[(sizeof(inqbuf.vendor) * 4) + 1],
234 1.4.2.2 tls product[(sizeof(inqbuf.product) * 4) + 1],
235 1.4.2.2 tls revision[(sizeof(inqbuf.revision) * 4) + 1];
236 1.4.2.2 tls char size[5];
237 1.4.2.2 tls int error;
238 1.4.2.2 tls
239 1.4.2.2 tls memset(&inqbuf, 0, sizeof(inqbuf));
240 1.4.2.2 tls memset(&cmd, 0, sizeof(cmd));
241 1.4.2.2 tls memset(&req, 0, sizeof(req));
242 1.4.2.2 tls
243 1.4.2.2 tls cmd.opcode = INQUIRY;
244 1.4.2.2 tls cmd.length = sizeof(inqbuf);
245 1.4.2.2 tls memcpy(req.cmd, &cmd, sizeof(cmd));
246 1.4.2.2 tls req.cmdlen = sizeof(cmd);
247 1.4.2.2 tls req.databuf = &inqbuf;
248 1.4.2.2 tls req.datalen = sizeof(inqbuf);
249 1.4.2.2 tls req.timeout = 10000;
250 1.4.2.2 tls req.flags = SCCMD_READ;
251 1.4.2.2 tls req.senselen = SENSEBUFLEN;
252 1.4.2.2 tls
253 1.4.2.2 tls error = ioctl(fd, SCIOCCOMMAND, &req);
254 1.4.2.2 tls if (error == -1 || req.retsts != SCCMD_OK)
255 1.4.2.2 tls return 0;
256 1.4.2.2 tls
257 1.4.2.2 tls scsi_strvis(vendor, sizeof(vendor), inqbuf.vendor,
258 1.4.2.2 tls sizeof(inqbuf.vendor));
259 1.4.2.2 tls scsi_strvis(product, sizeof(product), inqbuf.product,
260 1.4.2.2 tls sizeof(inqbuf.product));
261 1.4.2.2 tls scsi_strvis(revision, sizeof(revision), inqbuf.revision,
262 1.4.2.2 tls sizeof(inqbuf.revision));
263 1.4.2.2 tls
264 1.4.2.2 tls humanize_number(size, sizeof(size),
265 1.4.2.2 tls (uint64_t)dd->dd_secsize * (uint64_t)dd->dd_totsec,
266 1.4.2.2 tls "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
267 1.4.2.2 tls
268 1.4.2.2 tls snprintf(dd->dd_descr, sizeof(dd->dd_descr),
269 1.4.2.2 tls "%s (%s, %s %s)",
270 1.4.2.2 tls dd->dd_name, size, vendor, product);
271 1.4.2.2 tls
272 1.4.2.2 tls return 1;
273 1.4.2.2 tls }
274 1.4.2.2 tls
275 1.4.2.2 tls static int
276 1.4.2.2 tls get_descr_ata(struct disk_desc *dd, int fd)
277 1.4.2.2 tls {
278 1.4.2.2 tls struct atareq req;
279 1.4.2.2 tls static union {
280 1.4.2.2 tls unsigned char inbuf[DEV_BSIZE];
281 1.4.2.2 tls struct ataparams inqbuf;
282 1.4.2.2 tls } inbuf;
283 1.4.2.2 tls struct ataparams *inqbuf = &inbuf.inqbuf;
284 1.4.2.2 tls char model[sizeof(inqbuf->atap_model)+1];
285 1.4.2.2 tls char size[5];
286 1.4.2.2 tls int error, needswap = 0;
287 1.4.2.2 tls
288 1.4.2.2 tls memset(&inbuf, 0, sizeof(inbuf));
289 1.4.2.2 tls memset(&req, 0, sizeof(req));
290 1.4.2.2 tls
291 1.4.2.2 tls req.flags = ATACMD_READ;
292 1.4.2.2 tls req.command = WDCC_IDENTIFY;
293 1.4.2.2 tls req.databuf = (void *)&inbuf;
294 1.4.2.2 tls req.datalen = sizeof(inbuf);
295 1.4.2.2 tls req.timeout = 1000;
296 1.4.2.2 tls
297 1.4.2.2 tls error = ioctl(fd, ATAIOCCOMMAND, &req);
298 1.4.2.2 tls if (error == -1 || req.retsts != ATACMD_OK)
299 1.4.2.2 tls return 0;
300 1.4.2.2 tls
301 1.4.2.2 tls #if BYTE_ORDER == LITTLE_ENDIAN
302 1.4.2.2 tls /*
303 1.4.2.2 tls * On little endian machines, we need to shuffle the string
304 1.4.2.2 tls * byte order. However, we don't have to do this for NEC or
305 1.4.2.2 tls * Mitsumi ATAPI devices
306 1.4.2.2 tls */
307 1.4.2.2 tls
308 1.4.2.2 tls if (!(inqbuf->atap_config != WDC_CFG_CFA_MAGIC &&
309 1.4.2.2 tls (inqbuf->atap_config & WDC_CFG_ATAPI) &&
310 1.4.2.2 tls ((inqbuf->atap_model[0] == 'N' &&
311 1.4.2.2 tls inqbuf->atap_model[1] == 'E') ||
312 1.4.2.2 tls (inqbuf->atap_model[0] == 'F' &&
313 1.4.2.2 tls inqbuf->atap_model[1] == 'X')))) {
314 1.4.2.2 tls needswap = 1;
315 1.4.2.2 tls }
316 1.4.2.2 tls #endif
317 1.4.2.2 tls
318 1.4.2.2 tls ata_extract_string(model, sizeof(model),
319 1.4.2.2 tls inqbuf->atap_model, sizeof(inqbuf->atap_model), needswap);
320 1.4.2.2 tls humanize_number(size, sizeof(size),
321 1.4.2.2 tls (uint64_t)dd->dd_secsize * (uint64_t)dd->dd_totsec,
322 1.4.2.2 tls "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
323 1.4.2.2 tls
324 1.4.2.2 tls snprintf(dd->dd_descr, sizeof(dd->dd_descr), "%s (%s, %s)",
325 1.4.2.2 tls dd->dd_name, size, model);
326 1.4.2.2 tls
327 1.4.2.2 tls return 1;
328 1.4.2.2 tls }
329 1.4.2.2 tls
330 1.4.2.2 tls static void
331 1.4.2.2 tls get_descr(struct disk_desc *dd)
332 1.4.2.2 tls {
333 1.4.2.2 tls char diskpath[MAXPATHLEN];
334 1.4.2.2 tls int fd = -1;
335 1.4.2.2 tls
336 1.4.2.2 tls fd = opendisk(dd->dd_name, O_RDONLY, diskpath, sizeof(diskpath), 0);
337 1.4.2.2 tls if (fd < 0)
338 1.4.2.2 tls goto done;
339 1.4.2.2 tls
340 1.4.2.2 tls dd->dd_descr[0] = '\0';
341 1.4.2.2 tls
342 1.4.2.2 tls /* try ATA */
343 1.4.2.2 tls if (get_descr_ata(dd, fd))
344 1.4.2.2 tls goto done;
345 1.4.2.2 tls /* try SCSI */
346 1.4.2.2 tls if (get_descr_scsi(dd, fd))
347 1.4.2.2 tls goto done;
348 1.4.2.2 tls /* XXX: get description from raid, cgd, vnd... */
349 1.4.2.2 tls
350 1.4.2.2 tls done:
351 1.4.2.2 tls if (fd >= 0)
352 1.4.2.2 tls close(fd);
353 1.4.2.2 tls if (strlen(dd->dd_descr) == 0)
354 1.4.2.2 tls strcpy(dd->dd_descr, dd->dd_name);
355 1.4.2.2 tls }
356 1.4.2.2 tls
357 1.4.2.2 tls /* disknames - contains device names without partition letters
358 1.4.2.2 tls * cdrom_devices - contains devices including partition letters
359 1.4.2.2 tls * returns the first entry in hw.disknames matching a cdrom_device, or
360 1.4.2.2 tls * first entry on error or no match
361 1.4.2.2 tls */
362 1.4.2.2 tls const char *
363 1.4.2.2 tls get_default_cdrom(void)
364 1.4.2.2 tls {
365 1.4.2.2 tls static const char *cdrom_devices[] = { CD_NAMES, 0};
366 1.4.2.2 tls static const char mib_name[] = "hw.disknames";
367 1.4.2.2 tls size_t len;
368 1.4.2.2 tls char *disknames;
369 1.4.2.2 tls char *last;
370 1.4.2.2 tls char *name;
371 1.4.2.2 tls const char **arg;
372 1.4.2.2 tls const char *cd_dev;
373 1.4.2.2 tls
374 1.4.2.2 tls /* On error just use first entry in cdrom_devices */
375 1.4.2.2 tls if (sysctlbyname(mib_name, NULL, &len, NULL, 0) == -1)
376 1.4.2.2 tls return cdrom_devices[0];
377 1.4.2.2 tls if ((disknames = malloc(len + 2)) == 0) /* skip on malloc fail */
378 1.4.2.2 tls return cdrom_devices[0];
379 1.4.2.2 tls
380 1.4.2.2 tls (void)sysctlbyname(mib_name, disknames, &len, NULL, 0);
381 1.4.2.2 tls for ((name = strtok_r(disknames, " ", &last)); name;
382 1.4.2.2 tls (name = strtok_r(NULL, " ", &last))) {
383 1.4.2.2 tls for (arg = cdrom_devices; *arg; ++arg) {
384 1.4.2.2 tls cd_dev = *arg;
385 1.4.2.2 tls /* skip unit and partition */
386 1.4.2.2 tls if (strncmp(cd_dev, name, strlen(cd_dev) - 2) != 0)
387 1.4.2.2 tls continue;
388 1.4.2.2 tls if (name != disknames)
389 1.4.2.2 tls strcpy(disknames, name);
390 1.4.2.2 tls strcat(disknames, "a");
391 1.4.2.2 tls /* XXX: leaks, but so what? */
392 1.4.2.2 tls return disknames;
393 1.4.2.2 tls }
394 1.4.2.2 tls }
395 1.4.2.2 tls free(disknames);
396 1.4.2.2 tls return cdrom_devices[0];
397 1.4.2.2 tls }
398 1.4.2.2 tls
399 1.4.2.2 tls static int
400 1.4.2.2 tls get_disks(struct disk_desc *dd)
401 1.4.2.2 tls {
402 1.4.2.2 tls const char **xd;
403 1.4.2.2 tls char *cp;
404 1.4.2.2 tls struct disklabel l;
405 1.4.2.2 tls int i;
406 1.4.2.2 tls int numdisks;
407 1.4.2.2 tls
408 1.4.2.2 tls /* initialize */
409 1.4.2.2 tls numdisks = 0;
410 1.4.2.2 tls
411 1.4.2.2 tls for (xd = disk_names; *xd != NULL; xd++) {
412 1.4.2.2 tls for (i = 0; i < MAX_DISKS; i++) {
413 1.4.2.2 tls strlcpy(dd->dd_name, *xd, sizeof dd->dd_name - 2);
414 1.4.2.2 tls cp = strchr(dd->dd_name, ':');
415 1.4.2.2 tls if (cp != NULL)
416 1.4.2.2 tls dd->dd_no_mbr = !strcmp(cp, ":no_mbr");
417 1.4.2.2 tls else {
418 1.4.2.2 tls dd->dd_no_mbr = 0;
419 1.4.2.2 tls cp = strchr(dd->dd_name, 0);
420 1.4.2.2 tls }
421 1.4.2.2 tls
422 1.4.2.2 tls snprintf(cp, 2 + 1, "%d", i);
423 1.4.2.2 tls if (!get_geom(dd->dd_name, &l)) {
424 1.4.2.2 tls if (errno == ENOENT)
425 1.4.2.2 tls break;
426 1.4.2.2 tls continue;
427 1.4.2.2 tls }
428 1.4.2.2 tls
429 1.4.2.2 tls /*
430 1.4.2.2 tls * Exclude a disk mounted as root partition,
431 1.4.2.2 tls * in case of install-image on a USB memstick.
432 1.4.2.2 tls */
433 1.4.2.2 tls if (is_active_rootpart(dd->dd_name, 0))
434 1.4.2.2 tls continue;
435 1.4.2.2 tls
436 1.4.2.2 tls dd->dd_cyl = l.d_ncylinders;
437 1.4.2.2 tls dd->dd_head = l.d_ntracks;
438 1.4.2.2 tls dd->dd_sec = l.d_nsectors;
439 1.4.2.2 tls dd->dd_secsize = l.d_secsize;
440 1.4.2.2 tls dd->dd_totsec = l.d_secperunit;
441 1.4.2.2 tls get_descr(dd);
442 1.4.2.2 tls dd++;
443 1.4.2.2 tls numdisks++;
444 1.4.2.2 tls if (numdisks >= MAX_DISKS)
445 1.4.2.2 tls return numdisks;
446 1.4.2.2 tls }
447 1.4.2.2 tls }
448 1.4.2.2 tls return numdisks;
449 1.4.2.2 tls }
450 1.4.2.2 tls
451 1.4.2.2 tls int
452 1.4.2.2 tls find_disks(const char *doingwhat)
453 1.4.2.2 tls {
454 1.4.2.2 tls struct disk_desc disks[MAX_DISKS];
455 1.4.2.2 tls menu_ent dsk_menu[nelem(disks) + 1]; // + 1 for extended partitioning entry
456 1.4.2.2 tls struct disk_desc *disk;
457 1.4.2.2 tls int i, already_found;
458 1.4.2.2 tls int numdisks, selected_disk = -1;
459 1.4.2.2 tls int menu_no;
460 1.4.2.2 tls pm_devs_t *pm_i, *pm_last = NULL;
461 1.4.2.2 tls
462 1.4.2.2 tls /* Find disks. */
463 1.4.2.2 tls numdisks = get_disks(disks);
464 1.4.2.2 tls
465 1.4.2.2 tls /* need a redraw here, kernel messages hose everything */
466 1.4.2.2 tls touchwin(stdscr);
467 1.4.2.2 tls refresh();
468 1.4.2.2 tls /* Kill typeahead, it won't be what the user had in mind */
469 1.4.2.2 tls fpurge(stdin);
470 1.4.2.2 tls
471 1.4.2.2 tls /* partman_go: <0 - we want to see menu with extended partitioning
472 1.4.2.2 tls ==0 - we want to see simple select disk menu
473 1.4.2.2 tls >0 - we do not want to see any menus, just detect all disks */
474 1.4.2.2 tls if (partman_go <= 0) {
475 1.4.2.2 tls if (numdisks == 0) {
476 1.4.2.2 tls /* No disks found! */
477 1.4.2.2 tls msg_display(MSG_nodisk);
478 1.4.2.2 tls process_menu(MENU_ok, NULL);
479 1.4.2.2 tls /*endwin();*/
480 1.4.2.2 tls return -1;
481 1.4.2.2 tls } else {
482 1.4.2.2 tls /* One or more disks found! */
483 1.4.2.2 tls for (i = 0; i < numdisks; i++) {
484 1.4.2.2 tls dsk_menu[i].opt_name = disks[i].dd_descr;
485 1.4.2.2 tls dsk_menu[i].opt_menu = OPT_NOMENU;
486 1.4.2.2 tls dsk_menu[i].opt_flags = OPT_EXIT;
487 1.4.2.2 tls dsk_menu[i].opt_action = set_menu_select;
488 1.4.2.2 tls }
489 1.4.2.2 tls if (partman_go < 0) {
490 1.4.2.2 tls dsk_menu[i].opt_name = MSG_partman;
491 1.4.2.2 tls dsk_menu[i].opt_menu = OPT_NOMENU;
492 1.4.2.2 tls dsk_menu[i].opt_flags = OPT_EXIT;
493 1.4.2.2 tls dsk_menu[i].opt_action = set_menu_select;
494 1.4.2.2 tls }
495 1.4.2.2 tls menu_no = new_menu(MSG_Available_disks,
496 1.4.2.2 tls dsk_menu, numdisks + ((partman_go<0)?1:0), -1, 4, 0, 0, MC_SCROLL,
497 1.4.2.2 tls NULL, NULL, NULL, NULL, NULL);
498 1.4.2.2 tls if (menu_no == -1)
499 1.4.2.2 tls return -1;
500 1.4.2.2 tls msg_display(MSG_ask_disk, doingwhat);
501 1.4.2.2 tls process_menu(menu_no, &selected_disk);
502 1.4.2.2 tls free_menu(menu_no);
503 1.4.2.2 tls }
504 1.4.2.2 tls if (partman_go < 0 && selected_disk == numdisks) {
505 1.4.2.2 tls partman_go = 1;
506 1.4.2.2 tls return -2;
507 1.4.2.2 tls } else
508 1.4.2.2 tls partman_go = 0;
509 1.4.2.2 tls if (selected_disk < 0 || selected_disk >= numdisks)
510 1.4.2.2 tls return -1;
511 1.4.2.2 tls }
512 1.4.2.2 tls
513 1.4.2.2 tls /* Fill pm struct with device(s) info */
514 1.4.2.2 tls for (i = 0; i < numdisks; i++) {
515 1.4.2.2 tls if (! partman_go)
516 1.4.2.2 tls disk = disks + selected_disk;
517 1.4.2.2 tls else {
518 1.4.2.2 tls disk = disks + i;
519 1.4.2.2 tls already_found = 0;
520 1.4.2.2 tls SLIST_FOREACH(pm_i, &pm_head, l) {
521 1.4.2.2 tls pm_last = pm_i;
522 1.4.2.2 tls if (!already_found &&
523 1.4.2.2 tls strcmp(pm_i->diskdev, disk->dd_name) == 0) {
524 1.4.2.2 tls pm_i->found = 1;
525 1.4.2.2 tls break;
526 1.4.2.2 tls }
527 1.4.2.2 tls }
528 1.4.2.2 tls if (pm_i != NULL && pm_i->found)
529 1.4.2.2 tls /* We already added this device, skipping */
530 1.4.2.2 tls continue;
531 1.4.2.2 tls }
532 1.4.2.2 tls pm = pm_new;
533 1.4.2.2 tls pm->found = 1;
534 1.4.2.2 tls pm->bootable = 0;
535 1.4.2.2 tls pm->pi.menu_no = -1;
536 1.4.2.2 tls pm->disktype = "unknown";
537 1.4.2.2 tls pm->doessf = "";
538 1.4.2.2 tls strlcpy(pm->diskdev, disk->dd_name, sizeof pm->diskdev);
539 1.4.2.2 tls strlcpy(pm->diskdev_descr, disk->dd_descr, sizeof pm->diskdev_descr);
540 1.4.2.2 tls /* Use as a default disk if the user has the sets on a local disk */
541 1.4.2.2 tls strlcpy(localfs_dev, disk->dd_name, sizeof localfs_dev);
542 1.4.2.2 tls
543 1.4.2.2 tls pm->gpt = is_gpt(pm->diskdev);
544 1.4.2.2 tls pm->no_mbr = disk->dd_no_mbr || pm->gpt;
545 1.4.2.2 tls pm->sectorsize = disk->dd_secsize;
546 1.4.2.2 tls pm->dlcyl = disk->dd_cyl;
547 1.4.2.2 tls pm->dlhead = disk->dd_head;
548 1.4.2.2 tls pm->dlsec = disk->dd_sec;
549 1.4.2.2 tls pm->dlsize = disk->dd_totsec;
550 1.4.2.2 tls if (pm->dlsize == 0)
551 1.4.2.2 tls pm->dlsize = disk->dd_cyl * disk->dd_head * disk->dd_sec;
552 1.4.2.2 tls if (pm->dlsize > UINT32_MAX && ! partman_go) {
553 1.4.2.2 tls if (logfp)
554 1.4.2.2 tls fprintf(logfp, "Cannot process disk %s: too big size (%d)\n",
555 1.4.2.2 tls pm->diskdev, (int)pm->dlsize);
556 1.4.2.2 tls msg_display(MSG_toobigdisklabel);
557 1.4.2.2 tls process_menu(MENU_ok, NULL);
558 1.4.2.2 tls return -1;
559 1.4.2.2 tls }
560 1.4.2.2 tls pm->dlcylsize = pm->dlhead * pm->dlsec;
561 1.4.2.2 tls
562 1.4.2.2 tls label_read();
563 1.4.2.2 tls if (partman_go) {
564 1.4.2.2 tls pm_getrefdev(pm_new);
565 1.4.2.2 tls if (SLIST_EMPTY(&pm_head) || pm_last == NULL)
566 1.4.2.2 tls SLIST_INSERT_HEAD(&pm_head, pm_new, l);
567 1.4.2.2 tls else
568 1.4.2.2 tls SLIST_INSERT_AFTER(pm_last, pm_new, l);
569 1.4.2.2 tls pm_new = malloc(sizeof (pm_devs_t));
570 1.4.2.2 tls memset(pm_new, 0, sizeof *pm_new);
571 1.4.2.2 tls } else
572 1.4.2.2 tls /* We is not in partman and do not want to process all devices, exit */
573 1.4.2.2 tls break;
574 1.4.2.2 tls }
575 1.4.2.2 tls
576 1.4.2.2 tls return numdisks;
577 1.4.2.2 tls }
578 1.4.2.2 tls
579 1.4.2.2 tls void
580 1.4.2.2 tls label_read(void)
581 1.4.2.2 tls {
582 1.4.2.2 tls /* Get existing/default label */
583 1.4.2.2 tls memset(&pm->oldlabel, 0, sizeof pm->oldlabel);
584 1.4.2.2 tls if (! pm->gpt)
585 1.4.2.2 tls incorelabel(pm->diskdev, pm->oldlabel);
586 1.4.2.2 tls else
587 1.4.2.2 tls incoregpt(pm, pm->oldlabel);
588 1.4.2.2 tls /* Set 'target' label to current label in case we don't change it */
589 1.4.2.2 tls memcpy(&pm->bsdlabel, &pm->oldlabel, sizeof pm->bsdlabel);
590 1.4.2.2 tls #ifndef NO_DISKLABEL
591 1.4.2.2 tls if (! pm->gpt)
592 1.4.2.2 tls savenewlabel(pm->oldlabel, getmaxpartitions());
593 1.4.2.2 tls #endif
594 1.4.2.2 tls }
595 1.4.2.2 tls
596 1.4.2.2 tls void
597 1.4.2.2 tls fmt_fspart(menudesc *m, int ptn, void *arg)
598 1.4.2.2 tls {
599 1.4.2.2 tls unsigned int poffset, psize, pend;
600 1.4.2.2 tls const char *desc;
601 1.4.2.2 tls static const char *Yes;
602 1.4.2.2 tls partinfo *p = pm->bsdlabel + ptn;
603 1.4.2.2 tls
604 1.4.2.2 tls if (Yes == NULL)
605 1.4.2.2 tls Yes = msg_string(MSG_Yes);
606 1.4.2.2 tls
607 1.4.2.2 tls poffset = p->pi_offset / sizemult;
608 1.4.2.2 tls psize = p->pi_size / sizemult;
609 1.4.2.2 tls if (psize == 0)
610 1.4.2.2 tls pend = 0;
611 1.4.2.2 tls else
612 1.4.2.2 tls pend = (p->pi_offset + p->pi_size) / sizemult - 1;
613 1.4.2.2 tls
614 1.4.2.2 tls if (p->pi_fstype == FS_BSDFFS)
615 1.4.2.2 tls if (p->pi_flags & PIF_FFSv2)
616 1.4.2.2 tls desc = "FFSv2";
617 1.4.2.2 tls else
618 1.4.2.2 tls desc = "FFSv1";
619 1.4.2.2 tls else
620 1.4.2.2 tls desc = getfslabelname(p->pi_fstype);
621 1.4.2.2 tls
622 1.4.2.2 tls #ifdef PART_BOOT
623 1.4.2.2 tls if (ptn == PART_BOOT)
624 1.4.2.2 tls desc = msg_string(MSG_Boot_partition_cant_change);
625 1.4.2.2 tls #endif
626 1.4.2.2 tls if (ptn == getrawpartition())
627 1.4.2.2 tls desc = msg_string(MSG_Whole_disk_cant_change);
628 1.4.2.2 tls else {
629 1.4.2.2 tls if (ptn == PART_C)
630 1.4.2.2 tls desc = msg_string(MSG_NetBSD_partition_cant_change);
631 1.4.2.2 tls }
632 1.4.2.2 tls
633 1.4.2.2 tls wprintw(m->mw, msg_string(MSG_fspart_row),
634 1.4.2.2 tls poffset, pend, psize, desc,
635 1.4.2.2 tls p->pi_flags & PIF_NEWFS ? Yes : "",
636 1.4.2.2 tls p->pi_flags & PIF_MOUNT ? Yes : "",
637 1.4.2.2 tls p->pi_mount);
638 1.4.2.2 tls }
639 1.4.2.2 tls
640 1.4.2.2 tls /*
641 1.4.2.2 tls * Label a disk using an MD-specific string DISKLABEL_CMD for
642 1.4.2.2 tls * to invoke disklabel.
643 1.4.2.2 tls * if MD code does not define DISKLABEL_CMD, this is a no-op.
644 1.4.2.2 tls *
645 1.4.2.2 tls * i386 port uses "/sbin/disklabel -w -r", just like i386
646 1.4.2.2 tls * miniroot scripts, though this may leave a bogus incore label.
647 1.4.2.2 tls *
648 1.4.2.2 tls * Sun ports should use DISKLABEL_CMD "/sbin/disklabel -w"
649 1.4.2.2 tls * to get incore to ondisk inode translation for the Sun proms.
650 1.4.2.2 tls */
651 1.4.2.2 tls int
652 1.4.2.2 tls write_disklabel (void)
653 1.4.2.2 tls {
654 1.4.2.2 tls
655 1.4.2.2 tls #ifdef DISKLABEL_CMD
656 1.4.2.2 tls /* disklabel the disk */
657 1.4.2.2 tls return run_program(RUN_DISPLAY, "%s -f /tmp/disktab %s '%s'",
658 1.4.2.2 tls DISKLABEL_CMD, pm->diskdev, pm->bsddiskname);
659 1.4.2.2 tls #else
660 1.4.2.2 tls return 0;
661 1.4.2.2 tls #endif
662 1.4.2.2 tls }
663 1.4.2.2 tls
664 1.4.2.2 tls
665 1.4.2.2 tls static int
666 1.4.2.2 tls ptn_sort(const void *a, const void *b)
667 1.4.2.2 tls {
668 1.4.2.2 tls return strcmp(pm->bsdlabel[*(const int *)a].pi_mount,
669 1.4.2.2 tls pm->bsdlabel[*(const int *)b].pi_mount);
670 1.4.2.2 tls }
671 1.4.2.2 tls
672 1.4.2.2 tls int
673 1.4.2.2 tls make_filesystems(void)
674 1.4.2.2 tls {
675 1.4.2.2 tls unsigned int i;
676 1.4.2.2 tls int ptn;
677 1.4.2.2 tls int ptn_order[nelem(pm->bsdlabel)];
678 1.4.2.2 tls int error = 0;
679 1.4.2.2 tls unsigned int maxpart = getmaxpartitions();
680 1.4.2.2 tls char *newfs = NULL, *dev = NULL, *devdev = NULL;
681 1.4.2.2 tls partinfo *lbl;
682 1.4.2.2 tls
683 1.4.2.2 tls if (maxpart > nelem(pm->bsdlabel))
684 1.4.2.2 tls maxpart = nelem(pm->bsdlabel);
685 1.4.2.2 tls
686 1.4.2.2 tls /* Making new file systems and mounting them */
687 1.4.2.2 tls
688 1.4.2.2 tls /* sort to ensure /usr/local is mounted after /usr (etc) */
689 1.4.2.2 tls for (i = 0; i < maxpart; i++)
690 1.4.2.2 tls ptn_order[i] = i;
691 1.4.2.2 tls qsort(ptn_order, maxpart, sizeof ptn_order[0], ptn_sort);
692 1.4.2.2 tls
693 1.4.2.2 tls for (i = 0; i < maxpart; i++) {
694 1.4.2.2 tls /*
695 1.4.2.2 tls * newfs and mount. For now, process only BSD filesystems.
696 1.4.2.2 tls * but if this is the mounted-on root, has no mount
697 1.4.2.2 tls * point defined, or is marked preserve, don't touch it!
698 1.4.2.2 tls */
699 1.4.2.2 tls ptn = ptn_order[i];
700 1.4.2.2 tls lbl = pm->bsdlabel + ptn;
701 1.4.2.2 tls
702 1.4.2.2 tls if (is_active_rootpart(pm->diskdev, ptn))
703 1.4.2.2 tls continue;
704 1.4.2.2 tls
705 1.4.2.2 tls if (*lbl->pi_mount == 0)
706 1.4.2.2 tls /* No mount point */
707 1.4.2.2 tls continue;
708 1.4.2.2 tls
709 1.4.2.2 tls if (pm->isspecial) {
710 1.4.2.2 tls asprintf(&dev, "%s", pm->diskdev);
711 1.4.2.2 tls ptn = 0 - 'a';
712 1.4.2.2 tls } else {
713 1.4.2.2 tls asprintf(&dev, "%s%c", pm->diskdev, 'a' + ptn);
714 1.4.2.2 tls }
715 1.4.2.2 tls if (dev == NULL)
716 1.4.2.2 tls return (ENOMEM);
717 1.4.2.2 tls asprintf(&devdev, "/dev/%s", dev);
718 1.4.2.2 tls if (devdev == NULL)
719 1.4.2.2 tls return (ENOMEM);
720 1.4.2.2 tls
721 1.4.2.2 tls newfs = NULL;
722 1.4.2.2 tls lbl->mnt_opts = NULL;
723 1.4.2.2 tls lbl->fsname = NULL;
724 1.4.2.2 tls switch (lbl->pi_fstype) {
725 1.4.2.2 tls case FS_APPLEUFS:
726 1.4.2.2 tls asprintf(&newfs, "/sbin/newfs %s%.0d",
727 1.4.2.2 tls lbl->pi_isize != 0 ? "-i" : "", lbl->pi_isize);
728 1.4.2.2 tls lbl->mnt_opts = "-tffs -o async";
729 1.4.2.2 tls lbl->fsname = "ffs";
730 1.4.2.2 tls break;
731 1.4.2.2 tls case FS_BSDFFS:
732 1.4.2.2 tls asprintf(&newfs,
733 1.4.2.2 tls "/sbin/newfs -V2 -O %d -b %d -f %d%s%.0d",
734 1.4.2.2 tls lbl->pi_flags & PIF_FFSv2 ? 2 : 1,
735 1.4.2.2 tls lbl->pi_fsize * lbl->pi_frag, lbl->pi_fsize,
736 1.4.2.2 tls lbl->pi_isize != 0 ? " -i " : "", lbl->pi_isize);
737 1.4.2.2 tls if (lbl->pi_flags & PIF_LOG)
738 1.4.2.2 tls lbl->mnt_opts = "-tffs -o log";
739 1.4.2.2 tls else
740 1.4.2.2 tls lbl->mnt_opts = "-tffs -o async";
741 1.4.2.2 tls lbl->fsname = "ffs";
742 1.4.2.2 tls break;
743 1.4.2.2 tls case FS_BSDLFS:
744 1.4.2.2 tls asprintf(&newfs, "/sbin/newfs_lfs -b %d",
745 1.4.2.2 tls lbl->pi_fsize * lbl->pi_frag);
746 1.4.2.2 tls lbl->mnt_opts = "-tlfs";
747 1.4.2.2 tls lbl->fsname = "lfs";
748 1.4.2.2 tls break;
749 1.4.2.2 tls case FS_MSDOS:
750 1.4.2.2 tls #ifdef USE_NEWFS_MSDOS
751 1.4.2.2 tls asprintf(&newfs, "/sbin/newfs_msdos");
752 1.4.2.2 tls #endif
753 1.4.2.2 tls lbl->mnt_opts = "-tmsdos";
754 1.4.2.2 tls lbl->fsname = "msdos";
755 1.4.2.2 tls break;
756 1.4.2.2 tls #ifdef USE_SYSVBFS
757 1.4.2.2 tls case FS_SYSVBFS:
758 1.4.2.2 tls asprintf(&newfs, "/sbin/newfs_sysvbfs");
759 1.4.2.2 tls lbl->mnt_opts = "-tsysvbfs";
760 1.4.2.2 tls lbl->fsname = "sysvbfs";
761 1.4.2.2 tls break;
762 1.4.2.2 tls #endif
763 1.4.2.2 tls #ifdef USE_EXT2FS
764 1.4.2.2 tls case FS_EX2FS:
765 1.4.2.2 tls asprintf(&newfs, "/sbin/newfs_ext2fs");
766 1.4.2.2 tls lbl->mnt_opts = "-text2fs";
767 1.4.2.2 tls lbl->fsname = "ext2fs";
768 1.4.2.2 tls break;
769 1.4.2.2 tls #endif
770 1.4.2.2 tls }
771 1.4.2.2 tls if (lbl->pi_flags & PIF_NEWFS && newfs != NULL) {
772 1.4.2.2 tls #ifdef USE_NEWFS_MSDOS
773 1.4.2.2 tls if (lbl->pi_fstype == FS_MSDOS) {
774 1.4.2.2 tls /* newfs only if mount fails */
775 1.4.2.2 tls if (run_program(RUN_SILENT | RUN_ERROR_OK,
776 1.4.2.2 tls "mount -rt msdos /dev/%s /mnt2", dev) != 0)
777 1.4.2.2 tls error = run_program(
778 1.4.2.2 tls RUN_DISPLAY | RUN_PROGRESS,
779 1.4.2.2 tls "%s /dev/r%s",
780 1.4.2.2 tls newfs, dev);
781 1.4.2.2 tls else {
782 1.4.2.2 tls run_program(RUN_SILENT | RUN_ERROR_OK,
783 1.4.2.2 tls "umount /mnt2");
784 1.4.2.2 tls error = 0;
785 1.4.2.2 tls }
786 1.4.2.2 tls } else
787 1.4.2.2 tls #endif
788 1.4.2.2 tls error = run_program(RUN_DISPLAY | RUN_PROGRESS,
789 1.4.2.2 tls "%s /dev/r%s", newfs, dev);
790 1.4.2.2 tls } else {
791 1.4.2.2 tls /* We'd better check it isn't dirty */
792 1.4.2.2 tls error = fsck_preen(pm->diskdev, ptn, lbl->fsname);
793 1.4.2.2 tls }
794 1.4.2.2 tls free(newfs);
795 1.4.2.2 tls if (error != 0)
796 1.4.2.2 tls return error;
797 1.4.2.2 tls
798 1.4.2.2 tls lbl->pi_flags ^= PIF_NEWFS;
799 1.4.2.2 tls md_pre_mount();
800 1.4.2.2 tls
801 1.4.2.2 tls if (partman_go == 0 && lbl->pi_flags & PIF_MOUNT &&
802 1.4.2.2 tls lbl->mnt_opts != NULL) {
803 1.4.2.2 tls make_target_dir(lbl->pi_mount);
804 1.4.2.2 tls error = target_mount_do(lbl->mnt_opts, devdev, lbl->pi_mount);
805 1.4.2.2 tls if (error) {
806 1.4.2.2 tls msg_display(MSG_mountfail, dev, ' ', lbl->pi_mount);
807 1.4.2.2 tls process_menu(MENU_ok, NULL);
808 1.4.2.2 tls return error;
809 1.4.2.2 tls }
810 1.4.2.2 tls }
811 1.4.2.2 tls free(devdev);
812 1.4.2.2 tls free(dev);
813 1.4.2.2 tls }
814 1.4.2.2 tls return 0;
815 1.4.2.2 tls }
816 1.4.2.2 tls
817 1.4.2.2 tls int
818 1.4.2.2 tls make_fstab(void)
819 1.4.2.2 tls {
820 1.4.2.2 tls FILE *f;
821 1.4.2.2 tls int i, swap_dev = -1;
822 1.4.2.2 tls const char *dump_dev;
823 1.4.2.2 tls char *dev = NULL;
824 1.4.2.2 tls pm_devs_t *pm_i;
825 1.4.2.2 tls #ifndef HAVE_TMPFS
826 1.4.2.2 tls pm_devs_t *pm_with_swap = NULL;
827 1.4.2.2 tls #endif
828 1.4.2.2 tls
829 1.4.2.2 tls /* Create the fstab. */
830 1.4.2.2 tls make_target_dir("/etc");
831 1.4.2.2 tls f = target_fopen("/etc/fstab", "w");
832 1.4.2.2 tls scripting_fprintf(NULL, "cat <<EOF >%s/etc/fstab\n", target_prefix());
833 1.4.2.2 tls
834 1.4.2.2 tls if (logfp)
835 1.4.2.2 tls (void)fprintf(logfp,
836 1.4.2.2 tls "Making %s/etc/fstab (%s).\n", target_prefix(), pm->diskdev);
837 1.4.2.2 tls
838 1.4.2.2 tls if (f == NULL) {
839 1.4.2.2 tls msg_display(MSG_createfstab);
840 1.4.2.2 tls if (logfp)
841 1.4.2.2 tls (void)fprintf(logfp, "Failed to make /etc/fstab!\n");
842 1.4.2.2 tls process_menu(MENU_ok, NULL);
843 1.4.2.2 tls #ifndef DEBUG
844 1.4.2.2 tls return 1;
845 1.4.2.2 tls #else
846 1.4.2.2 tls f = stdout;
847 1.4.2.2 tls #endif
848 1.4.2.2 tls }
849 1.4.2.2 tls
850 1.4.2.2 tls scripting_fprintf(f, "# NetBSD %s/etc/fstab\n# See /usr/share/examples/"
851 1.4.2.2 tls "fstab/ for more examples.\n", target_prefix());
852 1.4.2.2 tls if (! partman_go) {
853 1.4.2.2 tls /* We want to process only one disk... */
854 1.4.2.2 tls pm_i = pm;
855 1.4.2.2 tls goto onlyonediskinfstab;
856 1.4.2.2 tls }
857 1.4.2.2 tls SLIST_FOREACH(pm_i, &pm_head, l) {
858 1.4.2.2 tls onlyonediskinfstab:
859 1.4.2.2 tls for (i = 0; i < getmaxpartitions(); i++) {
860 1.4.2.2 tls const char *s = "";
861 1.4.2.2 tls const char *mp = pm_i->bsdlabel[i].pi_mount;
862 1.4.2.2 tls const char *fstype = "ffs";
863 1.4.2.2 tls int fsck_pass = 0, dump_freq = 0;
864 1.4.2.2 tls
865 1.4.2.2 tls if (dev != NULL)
866 1.4.2.2 tls free(dev);
867 1.4.2.2 tls if (pm_i->isspecial)
868 1.4.2.2 tls asprintf(&dev, "%s", pm_i->diskdev);
869 1.4.2.2 tls else
870 1.4.2.2 tls asprintf(&dev, "%s%c", pm_i->diskdev, 'a' + i);
871 1.4.2.2 tls if (dev == NULL)
872 1.4.2.2 tls return (ENOMEM);
873 1.4.2.2 tls
874 1.4.2.2 tls if (!*mp) {
875 1.4.2.2 tls /*
876 1.4.2.2 tls * No mount point specified, comment out line and
877 1.4.2.2 tls * use /mnt as a placeholder for the mount point.
878 1.4.2.2 tls */
879 1.4.2.2 tls s = "# ";
880 1.4.2.2 tls mp = "/mnt";
881 1.4.2.2 tls }
882 1.4.2.2 tls
883 1.4.2.2 tls switch (pm_i->bsdlabel[i].pi_fstype) {
884 1.4.2.2 tls case FS_UNUSED:
885 1.4.2.2 tls continue;
886 1.4.2.2 tls case FS_BSDLFS:
887 1.4.2.2 tls /* If there is no LFS, just comment it out. */
888 1.4.2.2 tls if (!check_lfs_progs())
889 1.4.2.2 tls s = "# ";
890 1.4.2.2 tls fstype = "lfs";
891 1.4.2.2 tls /* FALLTHROUGH */
892 1.4.2.2 tls case FS_BSDFFS:
893 1.4.2.2 tls fsck_pass = (strcmp(mp, "/") == 0) ? 1 : 2;
894 1.4.2.2 tls dump_freq = 1;
895 1.4.2.2 tls break;
896 1.4.2.2 tls case FS_MSDOS:
897 1.4.2.2 tls fstype = "msdos";
898 1.4.2.2 tls break;
899 1.4.2.2 tls case FS_SWAP:
900 1.4.2.2 tls if (pm_i->isspecial)
901 1.4.2.2 tls continue;
902 1.4.2.2 tls if (swap_dev == -1) {
903 1.4.2.2 tls swap_dev = i;
904 1.4.2.2 tls dump_dev = ",dp";
905 1.4.2.2 tls #ifndef HAVE_TMPFS
906 1.4.2.2 tls pm_with_swap = pm_i;
907 1.4.2.2 tls #endif
908 1.4.2.2 tls } else {
909 1.4.2.2 tls dump_dev = "";
910 1.4.2.2 tls }
911 1.4.2.2 tls scripting_fprintf(f, "/dev/%s\t\tnone\tswap\tsw%s\t\t 0 0\n",
912 1.4.2.2 tls dev, dump_dev);
913 1.4.2.2 tls continue;
914 1.4.2.2 tls #ifdef USE_SYSVBFS
915 1.4.2.2 tls case FS_SYSVBFS:
916 1.4.2.2 tls fstype = "sysvbfs";
917 1.4.2.2 tls make_target_dir("/stand");
918 1.4.2.2 tls break;
919 1.4.2.2 tls #endif
920 1.4.2.2 tls default:
921 1.4.2.2 tls fstype = "???";
922 1.4.2.2 tls s = "# ";
923 1.4.2.2 tls break;
924 1.4.2.2 tls }
925 1.4.2.2 tls /* The code that remounts root rw doesn't check the partition */
926 1.4.2.2 tls if (strcmp(mp, "/") == 0 && !(pm_i->bsdlabel[i].pi_flags & PIF_MOUNT))
927 1.4.2.2 tls s = "# ";
928 1.4.2.2 tls
929 1.4.2.2 tls scripting_fprintf(f,
930 1.4.2.2 tls "%s/dev/%s\t\t%s\t%s\trw%s%s%s%s%s%s%s%s\t\t %d %d\n",
931 1.4.2.2 tls s, dev, mp, fstype,
932 1.4.2.2 tls pm_i->bsdlabel[i].pi_flags & PIF_LOG ? ",log" : "",
933 1.4.2.2 tls pm_i->bsdlabel[i].pi_flags & PIF_MOUNT ? "" : ",noauto",
934 1.4.2.2 tls pm_i->bsdlabel[i].pi_flags & PIF_ASYNC ? ",async" : "",
935 1.4.2.2 tls pm_i->bsdlabel[i].pi_flags & PIF_NOATIME ? ",noatime" : "",
936 1.4.2.2 tls pm_i->bsdlabel[i].pi_flags & PIF_NODEV ? ",nodev" : "",
937 1.4.2.2 tls pm_i->bsdlabel[i].pi_flags & PIF_NODEVMTIME ? ",nodevmtime" : "",
938 1.4.2.2 tls pm_i->bsdlabel[i].pi_flags & PIF_NOEXEC ? ",noexec" : "",
939 1.4.2.2 tls pm_i->bsdlabel[i].pi_flags & PIF_NOSUID ? ",nosuid" : "",
940 1.4.2.2 tls dump_freq, fsck_pass);
941 1.4.2.2 tls if (pm_i->isspecial)
942 1.4.2.2 tls /* Special device (such as dk*) have only one partition */
943 1.4.2.2 tls break;
944 1.4.2.2 tls }
945 1.4.2.2 tls /* Simple install, only one disk */
946 1.4.2.2 tls if (!partman_go)
947 1.4.2.2 tls break;
948 1.4.2.2 tls }
949 1.4.2.2 tls if (tmp_ramdisk_size != 0) {
950 1.4.2.2 tls #ifdef HAVE_TMPFS
951 1.4.2.2 tls scripting_fprintf(f, "tmpfs\t\t/tmp\ttmpfs\trw,-m=1777,-s=%"
952 1.4.2.2 tls PRIi64 "\n",
953 1.4.2.2 tls tmp_ramdisk_size * 512);
954 1.4.2.2 tls #else
955 1.4.2.2 tls if (swap_dev != -1 && pm_with_swap != NULL)
956 1.4.2.2 tls scripting_fprintf(f, "/dev/%s%c\t\t/tmp\tmfs\trw,-s=%"
957 1.4.2.2 tls PRIi64 "\n",
958 1.4.2.2 tls pm_with_swap->diskdev, 'a' + swap_dev, tmp_ramdisk_size);
959 1.4.2.2 tls else
960 1.4.2.2 tls scripting_fprintf(f, "swap\t\t/tmp\tmfs\trw,-s=%"
961 1.4.2.2 tls PRIi64 "\n",
962 1.4.2.2 tls tmp_ramdisk_size);
963 1.4.2.2 tls #endif
964 1.4.2.2 tls }
965 1.4.2.2 tls
966 1.4.2.2 tls /* Add /kern, /proc and /dev/pts to fstab and make mountpoint. */
967 1.4.2.2 tls scripting_fprintf(f, "kernfs\t\t/kern\tkernfs\trw\n");
968 1.4.2.2 tls scripting_fprintf(f, "ptyfs\t\t/dev/pts\tptyfs\trw\n");
969 1.4.2.2 tls scripting_fprintf(f, "procfs\t\t/proc\tprocfs\trw\n");
970 1.4.2.2 tls scripting_fprintf(f, "/dev/%s\t\t/cdrom\tcd9660\tro,noauto\n",
971 1.4.2.2 tls get_default_cdrom());
972 1.4.2.2 tls scripting_fprintf(f, "%stmpfs\t\t/var/shm\ttmpfs\trw,-m1777,-sram%%25\n",
973 1.4.2.2 tls tmpfs_on_var_shm() ? "" : "#");
974 1.4.2.2 tls make_target_dir("/kern");
975 1.4.2.2 tls make_target_dir("/proc");
976 1.4.2.2 tls make_target_dir("/dev/pts");
977 1.4.2.2 tls make_target_dir("/cdrom");
978 1.4.2.2 tls make_target_dir("/var/shm");
979 1.4.2.2 tls
980 1.4.2.2 tls scripting_fprintf(NULL, "EOF\n");
981 1.4.2.2 tls
982 1.4.2.2 tls if (dev != NULL)
983 1.4.2.2 tls free(dev);
984 1.4.2.2 tls fclose(f);
985 1.4.2.2 tls fflush(NULL);
986 1.4.2.2 tls return 0;
987 1.4.2.2 tls }
988 1.4.2.2 tls
989 1.4.2.2 tls
990 1.4.2.2 tls
991 1.4.2.2 tls static int
992 1.4.2.2 tls /*ARGSUSED*/
993 1.4.2.2 tls foundffs(struct data *list, size_t num)
994 1.4.2.2 tls {
995 1.4.2.2 tls int error;
996 1.4.2.2 tls
997 1.4.2.2 tls if (num < 2 || strcmp(list[1].u.s_val, "/") == 0 ||
998 1.4.2.2 tls strstr(list[2].u.s_val, "noauto") != NULL)
999 1.4.2.2 tls return 0;
1000 1.4.2.2 tls
1001 1.4.2.2 tls error = fsck_preen(list[0].u.s_val, ' '-'a', "ffs");
1002 1.4.2.2 tls if (error != 0)
1003 1.4.2.2 tls return error;
1004 1.4.2.2 tls
1005 1.4.2.2 tls error = target_mount("", list[0].u.s_val, ' '-'a', list[1].u.s_val);
1006 1.4.2.2 tls if (error != 0) {
1007 1.4.2.2 tls msg_display(MSG_mount_failed, list[0].u.s_val);
1008 1.4.2.2 tls process_menu(MENU_noyes, NULL);
1009 1.4.2.2 tls if (!yesno)
1010 1.4.2.2 tls return error;
1011 1.4.2.2 tls }
1012 1.4.2.2 tls return 0;
1013 1.4.2.2 tls }
1014 1.4.2.2 tls
1015 1.4.2.2 tls #ifdef USE_SYSVBFS
1016 1.4.2.2 tls static int
1017 1.4.2.2 tls /*ARGSUSED*/
1018 1.4.2.2 tls foundsysvbfs(struct data *list, size_t num)
1019 1.4.2.2 tls {
1020 1.4.2.2 tls int error;
1021 1.4.2.2 tls
1022 1.4.2.2 tls if (num < 2 || strcmp(list[1].u.s_val, "/") == 0 ||
1023 1.4.2.2 tls strstr(list[2].u.s_val, "noauto") != NULL)
1024 1.4.2.2 tls return 0;
1025 1.4.2.2 tls
1026 1.4.2.2 tls error = target_mount("", list[0].u.s_val, ' '-'a', list[1].u.s_val);
1027 1.4.2.2 tls if (error != 0)
1028 1.4.2.2 tls return error;
1029 1.4.2.2 tls return 0;
1030 1.4.2.2 tls }
1031 1.4.2.2 tls #endif
1032 1.4.2.2 tls
1033 1.4.2.2 tls /*
1034 1.4.2.2 tls * Do an fsck. On failure, inform the user by showing a warning
1035 1.4.2.2 tls * message and doing menu_ok() before proceeding.
1036 1.4.2.2 tls * Returns 0 on success, or nonzero return code from fsck() on failure.
1037 1.4.2.2 tls */
1038 1.4.2.2 tls static int
1039 1.4.2.2 tls fsck_preen(const char *disk, int ptn, const char *fsname)
1040 1.4.2.2 tls {
1041 1.4.2.2 tls char *prog;
1042 1.4.2.2 tls int error;
1043 1.4.2.2 tls
1044 1.4.2.2 tls ptn = (ptn < 0)? 0 : 'a' + ptn;
1045 1.4.2.2 tls if (fsname == NULL)
1046 1.4.2.2 tls return 0;
1047 1.4.2.2 tls /* first, check if fsck program exists, if not, assume ok */
1048 1.4.2.2 tls asprintf(&prog, "/sbin/fsck_%s", fsname);
1049 1.4.2.2 tls if (prog == NULL)
1050 1.4.2.2 tls return 0;
1051 1.4.2.2 tls if (access(prog, X_OK) != 0)
1052 1.4.2.2 tls return 0;
1053 1.4.2.2 tls if (!strcmp(fsname,"ffs"))
1054 1.4.2.2 tls fixsb(prog, disk, ptn);
1055 1.4.2.2 tls error = run_program(0, "%s -p -q /dev/r%s%c", prog, disk, ptn);
1056 1.4.2.2 tls free(prog);
1057 1.4.2.2 tls if (error != 0) {
1058 1.4.2.2 tls msg_display(MSG_badfs, disk, ptn, error);
1059 1.4.2.2 tls process_menu(MENU_noyes, NULL);
1060 1.4.2.2 tls if (yesno)
1061 1.4.2.2 tls error = 0;
1062 1.4.2.2 tls /* XXX at this point maybe we should run a full fsck? */
1063 1.4.2.2 tls }
1064 1.4.2.2 tls return error;
1065 1.4.2.2 tls }
1066 1.4.2.2 tls
1067 1.4.2.2 tls /* This performs the same function as the etc/rc.d/fixsb script
1068 1.4.2.2 tls * which attempts to correct problems with ffs1 filesystems
1069 1.4.2.2 tls * which may have been introduced by booting a netbsd-current kernel
1070 1.4.2.2 tls * from between April of 2003 and January 2004. For more information
1071 1.4.2.2 tls * This script was developed as a response to NetBSD pr install/25138
1072 1.4.2.2 tls * Additional prs regarding the original issue include:
1073 1.4.2.2 tls * bin/17910 kern/21283 kern/21404 port-macppc/23925 port-macppc/23926
1074 1.4.2.2 tls */
1075 1.4.2.2 tls static void
1076 1.4.2.2 tls fixsb(const char *prog, const char *disk, char ptn)
1077 1.4.2.2 tls {
1078 1.4.2.2 tls int fd;
1079 1.4.2.2 tls int rval;
1080 1.4.2.2 tls union {
1081 1.4.2.2 tls struct fs fs;
1082 1.4.2.2 tls char buf[SBLOCKSIZE];
1083 1.4.2.2 tls } sblk;
1084 1.4.2.2 tls struct fs *fs = &sblk.fs;
1085 1.4.2.2 tls
1086 1.4.2.2 tls snprintf(sblk.buf, sizeof(sblk.buf), "/dev/r%s%c",
1087 1.4.2.2 tls disk, ptn == ' ' ? 0 : ptn);
1088 1.4.2.2 tls fd = open(sblk.buf, O_RDONLY);
1089 1.4.2.2 tls if (fd == -1)
1090 1.4.2.2 tls return;
1091 1.4.2.2 tls
1092 1.4.2.2 tls /* Read ffsv1 main superblock */
1093 1.4.2.2 tls rval = pread(fd, sblk.buf, sizeof sblk.buf, SBLOCK_UFS1);
1094 1.4.2.2 tls close(fd);
1095 1.4.2.2 tls if (rval != sizeof sblk.buf)
1096 1.4.2.2 tls return;
1097 1.4.2.2 tls
1098 1.4.2.2 tls if (fs->fs_magic != FS_UFS1_MAGIC &&
1099 1.4.2.2 tls fs->fs_magic != FS_UFS1_MAGIC_SWAPPED)
1100 1.4.2.2 tls /* Not FFSv1 */
1101 1.4.2.2 tls return;
1102 1.4.2.2 tls if (fs->fs_old_flags & FS_FLAGS_UPDATED)
1103 1.4.2.2 tls /* properly updated fslevel 4 */
1104 1.4.2.2 tls return;
1105 1.4.2.2 tls if (fs->fs_bsize != fs->fs_maxbsize)
1106 1.4.2.2 tls /* not messed up */
1107 1.4.2.2 tls return;
1108 1.4.2.2 tls
1109 1.4.2.2 tls /*
1110 1.4.2.2 tls * OK we have a munged fs, first 'upgrade' to fslevel 4,
1111 1.4.2.2 tls * We specify -b16 in order to stop fsck bleating that the
1112 1.4.2.2 tls * sb doesn't match the first alternate.
1113 1.4.2.2 tls */
1114 1.4.2.2 tls run_program(RUN_DISPLAY | RUN_PROGRESS,
1115 1.4.2.2 tls "%s -p -b 16 -c 4 /dev/r%s%c", prog, disk, ptn);
1116 1.4.2.2 tls /* Then downgrade to fslevel 3 */
1117 1.4.2.2 tls run_program(RUN_DISPLAY | RUN_PROGRESS,
1118 1.4.2.2 tls "%s -p -c 3 /dev/r%s%c", prog, disk, ptn);
1119 1.4.2.2 tls }
1120 1.4.2.2 tls
1121 1.4.2.2 tls /*
1122 1.4.2.2 tls * fsck and mount the root partition.
1123 1.4.2.2 tls */
1124 1.4.2.2 tls static int
1125 1.4.2.2 tls mount_root(void)
1126 1.4.2.2 tls {
1127 1.4.2.2 tls int error;
1128 1.4.2.2 tls int ptn = (pm->isspecial)? 0 - 'a' : pm->rootpart;
1129 1.4.2.2 tls
1130 1.4.2.2 tls error = fsck_preen(pm->diskdev, ptn, "ffs");
1131 1.4.2.2 tls if (error != 0)
1132 1.4.2.2 tls return error;
1133 1.4.2.2 tls
1134 1.4.2.2 tls md_pre_mount();
1135 1.4.2.2 tls
1136 1.4.2.2 tls /* Mount /dev/<diskdev>a on target's "".
1137 1.4.2.2 tls * If we pass "" as mount-on, Prefixing will DTRT.
1138 1.4.2.2 tls * for now, use no options.
1139 1.4.2.2 tls * XXX consider -o remount in case target root is
1140 1.4.2.2 tls * current root, still readonly from single-user?
1141 1.4.2.2 tls */
1142 1.4.2.2 tls return target_mount("", pm->diskdev, ptn, "");
1143 1.4.2.2 tls }
1144 1.4.2.2 tls
1145 1.4.2.2 tls /* Get information on the file systems mounted from the root filesystem.
1146 1.4.2.2 tls * Offer to convert them into 4.4BSD inodes if they are not 4.4BSD
1147 1.4.2.2 tls * inodes. Fsck them. Mount them.
1148 1.4.2.2 tls */
1149 1.4.2.2 tls
1150 1.4.2.2 tls int
1151 1.4.2.2 tls mount_disks(void)
1152 1.4.2.2 tls {
1153 1.4.2.2 tls char *fstab;
1154 1.4.2.2 tls int fstabsize;
1155 1.4.2.2 tls int error;
1156 1.4.2.2 tls
1157 1.4.2.2 tls static struct lookfor fstabbuf[] = {
1158 1.4.2.2 tls {"/dev/", "/dev/%s %s ffs %s", "c", NULL, 0, 0, foundffs},
1159 1.4.2.2 tls {"/dev/", "/dev/%s %s ufs %s", "c", NULL, 0, 0, foundffs},
1160 1.4.2.2 tls #ifdef USE_SYSVBFS
1161 1.4.2.2 tls {"/dev/", "/dev/%s %s sysvbfs %s", "c", NULL, 0, 0,
1162 1.4.2.2 tls foundsysvbfs},
1163 1.4.2.2 tls #endif
1164 1.4.2.2 tls };
1165 1.4.2.2 tls static size_t numfstabbuf = sizeof(fstabbuf) / sizeof(struct lookfor);
1166 1.4.2.2 tls
1167 1.4.2.2 tls /* First the root device. */
1168 1.4.2.2 tls if (target_already_root())
1169 1.4.2.2 tls /* avoid needing to call target_already_root() again */
1170 1.4.2.2 tls targetroot_mnt[0] = 0;
1171 1.4.2.2 tls else {
1172 1.4.2.2 tls error = mount_root();
1173 1.4.2.2 tls if (error != 0 && error != EBUSY)
1174 1.4.2.2 tls return -1;
1175 1.4.2.2 tls }
1176 1.4.2.2 tls
1177 1.4.2.2 tls /* Check the target /etc/fstab exists before trying to parse it. */
1178 1.4.2.2 tls if (target_dir_exists_p("/etc") == 0 ||
1179 1.4.2.2 tls target_file_exists_p("/etc/fstab") == 0) {
1180 1.4.2.2 tls msg_display(MSG_noetcfstab, pm->diskdev);
1181 1.4.2.2 tls process_menu(MENU_ok, NULL);
1182 1.4.2.2 tls return -1;
1183 1.4.2.2 tls }
1184 1.4.2.2 tls
1185 1.4.2.2 tls
1186 1.4.2.2 tls /* Get fstab entries from the target-root /etc/fstab. */
1187 1.4.2.2 tls fstabsize = target_collect_file(T_FILE, &fstab, "/etc/fstab");
1188 1.4.2.2 tls if (fstabsize < 0) {
1189 1.4.2.2 tls /* error ! */
1190 1.4.2.2 tls msg_display(MSG_badetcfstab, pm->diskdev);
1191 1.4.2.2 tls process_menu(MENU_ok, NULL);
1192 1.4.2.2 tls return -2;
1193 1.4.2.2 tls }
1194 1.4.2.2 tls error = walk(fstab, (size_t)fstabsize, fstabbuf, numfstabbuf);
1195 1.4.2.2 tls free(fstab);
1196 1.4.2.2 tls
1197 1.4.2.2 tls return error;
1198 1.4.2.2 tls }
1199 1.4.2.2 tls
1200 1.4.2.2 tls int
1201 1.4.2.2 tls set_swap(const char *disk, partinfo *pp)
1202 1.4.2.2 tls {
1203 1.4.2.2 tls int i;
1204 1.4.2.2 tls char *cp;
1205 1.4.2.2 tls int rval;
1206 1.4.2.2 tls
1207 1.4.2.2 tls if (pp == NULL)
1208 1.4.2.2 tls pp = pm->oldlabel;
1209 1.4.2.2 tls
1210 1.4.2.2 tls for (i = 0; i < MAXPARTITIONS; i++) {
1211 1.4.2.2 tls if (pp[i].pi_fstype != FS_SWAP)
1212 1.4.2.2 tls continue;
1213 1.4.2.2 tls asprintf(&cp, "/dev/%s%c", disk, 'a' + i);
1214 1.4.2.2 tls rval = swapctl(SWAP_ON, cp, 0);
1215 1.4.2.2 tls free(cp);
1216 1.4.2.2 tls if (rval != 0)
1217 1.4.2.2 tls return -1;
1218 1.4.2.2 tls }
1219 1.4.2.2 tls
1220 1.4.2.2 tls return 0;
1221 1.4.2.2 tls }
1222 1.4.2.2 tls
1223 1.4.2.2 tls int
1224 1.4.2.2 tls check_swap(const char *disk, int remove_swap)
1225 1.4.2.2 tls {
1226 1.4.2.2 tls struct swapent *swap;
1227 1.4.2.2 tls char *cp;
1228 1.4.2.2 tls int nswap;
1229 1.4.2.2 tls int l;
1230 1.4.2.2 tls int rval = 0;
1231 1.4.2.2 tls
1232 1.4.2.2 tls nswap = swapctl(SWAP_NSWAP, 0, 0);
1233 1.4.2.2 tls if (nswap <= 0)
1234 1.4.2.2 tls return 0;
1235 1.4.2.2 tls
1236 1.4.2.2 tls swap = malloc(nswap * sizeof *swap);
1237 1.4.2.2 tls if (swap == NULL)
1238 1.4.2.2 tls return -1;
1239 1.4.2.2 tls
1240 1.4.2.2 tls nswap = swapctl(SWAP_STATS, swap, nswap);
1241 1.4.2.2 tls if (nswap < 0)
1242 1.4.2.2 tls goto bad_swap;
1243 1.4.2.2 tls
1244 1.4.2.2 tls l = strlen(disk);
1245 1.4.2.2 tls while (--nswap >= 0) {
1246 1.4.2.2 tls /* Should we check the se_dev or se_path? */
1247 1.4.2.2 tls cp = swap[nswap].se_path;
1248 1.4.2.2 tls if (memcmp(cp, "/dev/", 5) != 0)
1249 1.4.2.2 tls continue;
1250 1.4.2.2 tls if (memcmp(cp + 5, disk, l) != 0)
1251 1.4.2.2 tls continue;
1252 1.4.2.2 tls if (!isalpha(*(unsigned char *)(cp + 5 + l)))
1253 1.4.2.2 tls continue;
1254 1.4.2.2 tls if (cp[5 + l + 1] != 0)
1255 1.4.2.2 tls continue;
1256 1.4.2.2 tls /* ok path looks like it is for this device */
1257 1.4.2.2 tls if (!remove_swap) {
1258 1.4.2.2 tls /* count active swap areas */
1259 1.4.2.2 tls rval++;
1260 1.4.2.2 tls continue;
1261 1.4.2.2 tls }
1262 1.4.2.2 tls if (swapctl(SWAP_OFF, cp, 0) == -1)
1263 1.4.2.2 tls rval = -1;
1264 1.4.2.2 tls }
1265 1.4.2.2 tls
1266 1.4.2.2 tls done:
1267 1.4.2.2 tls free(swap);
1268 1.4.2.2 tls return rval;
1269 1.4.2.2 tls
1270 1.4.2.2 tls bad_swap:
1271 1.4.2.2 tls rval = -1;
1272 1.4.2.2 tls goto done;
1273 1.4.2.2 tls }
1274 1.4.2.2 tls
1275 1.4.2.2 tls #ifdef HAVE_BOOTXX_xFS
1276 1.4.2.2 tls char *
1277 1.4.2.2 tls bootxx_name(void)
1278 1.4.2.2 tls {
1279 1.4.2.2 tls int fstype;
1280 1.4.2.2 tls const char *bootxxname;
1281 1.4.2.2 tls char *bootxx;
1282 1.4.2.2 tls
1283 1.4.2.2 tls /* check we have boot code for the root partition type */
1284 1.4.2.2 tls fstype = pm->bsdlabel[pm->rootpart].pi_fstype;
1285 1.4.2.2 tls switch (fstype) {
1286 1.4.2.2 tls #if defined(BOOTXX_FFSV1) || defined(BOOTXX_FFSV2)
1287 1.4.2.2 tls case FS_BSDFFS:
1288 1.4.2.2 tls if (pm->bsdlabel[pm->rootpart].pi_flags & PIF_FFSv2) {
1289 1.4.2.2 tls #ifdef BOOTXX_FFSV2
1290 1.4.2.2 tls bootxxname = BOOTXX_FFSV2;
1291 1.4.2.2 tls #else
1292 1.4.2.2 tls bootxxname = NULL;
1293 1.4.2.2 tls #endif
1294 1.4.2.2 tls } else {
1295 1.4.2.2 tls #ifdef BOOTXX_FFSV1
1296 1.4.2.2 tls bootxxname = BOOTXX_FFSV1;
1297 1.4.2.2 tls #else
1298 1.4.2.2 tls bootxxname = NULL;
1299 1.4.2.2 tls #endif
1300 1.4.2.2 tls }
1301 1.4.2.2 tls break;
1302 1.4.2.2 tls #endif
1303 1.4.2.2 tls #ifdef BOOTXX_LFS
1304 1.4.2.2 tls case FS_BSDLFS:
1305 1.4.2.2 tls bootxxname = BOOTXX_LFS;
1306 1.4.2.2 tls break;
1307 1.4.2.2 tls #endif
1308 1.4.2.2 tls default:
1309 1.4.2.2 tls bootxxname = NULL;
1310 1.4.2.2 tls break;
1311 1.4.2.2 tls }
1312 1.4.2.2 tls
1313 1.4.2.2 tls if (bootxxname == NULL)
1314 1.4.2.2 tls return NULL;
1315 1.4.2.2 tls
1316 1.4.2.2 tls asprintf(&bootxx, "%s/%s", BOOTXXDIR, bootxxname);
1317 1.4.2.2 tls return bootxx;
1318 1.4.2.2 tls }
1319 1.4.2.2 tls #endif
1320 1.4.2.2 tls
1321 1.4.2.2 tls static int
1322 1.4.2.2 tls get_fsid_by_gptuuid(const char *str)
1323 1.4.2.2 tls {
1324 1.4.2.2 tls int i;
1325 1.4.2.2 tls uuid_t uuid;
1326 1.4.2.2 tls uint32_t status;
1327 1.4.2.2 tls
1328 1.4.2.2 tls uuid_from_string(str, &uuid, &status);
1329 1.4.2.2 tls if (status == uuid_s_ok) {
1330 1.4.2.2 tls for (i = 0; gpt_filesystems[i].id > 0; i++)
1331 1.4.2.2 tls if (uuid_equal(&uuid, &(gpt_filesystems[i].uuid), NULL))
1332 1.4.2.2 tls return gpt_filesystems[i].id;
1333 1.4.2.2 tls }
1334 1.4.2.2 tls return FS_OTHER;
1335 1.4.2.2 tls }
1336 1.4.2.2 tls
1337 1.4.2.2 tls const char *
1338 1.4.2.2 tls get_gptfs_by_id(int filesystem)
1339 1.4.2.2 tls {
1340 1.4.2.2 tls int i;
1341 1.4.2.2 tls for (i = 0; gpt_filesystems[i].id > 0; i++)
1342 1.4.2.2 tls if (filesystem == gpt_filesystems[i].id)
1343 1.4.2.2 tls return gpt_filesystems[i].name;
1344 1.4.2.2 tls return NULL;
1345 1.4.2.2 tls }
1346 1.4.2.2 tls
1347 1.4.2.2 tls /* from dkctl.c */
1348 1.4.2.2 tls static int
1349 1.4.2.2 tls get_dkwedges_sort(const void *a, const void *b)
1350 1.4.2.2 tls {
1351 1.4.2.2 tls const struct dkwedge_info *dkwa = a, *dkwb = b;
1352 1.4.2.2 tls const daddr_t oa = dkwa->dkw_offset, ob = dkwb->dkw_offset;
1353 1.4.2.2 tls return (oa < ob) ? -1 : (oa > ob) ? 1 : 0;
1354 1.4.2.2 tls }
1355 1.4.2.2 tls
1356 1.4.2.2 tls int
1357 1.4.2.2 tls get_dkwedges(struct dkwedge_info **dkw, const char *diskdev)
1358 1.4.2.2 tls {
1359 1.4.2.2 tls int fd;
1360 1.4.2.2 tls char buf[STRSIZE];
1361 1.4.2.2 tls size_t bufsize;
1362 1.4.2.2 tls struct dkwedge_list dkwl;
1363 1.4.2.2 tls
1364 1.4.2.2 tls *dkw = NULL;
1365 1.4.2.2 tls dkwl.dkwl_buf = *dkw;
1366 1.4.2.2 tls dkwl.dkwl_bufsize = 0;
1367 1.4.2.2 tls fd = opendisk(diskdev, O_RDONLY, buf, STRSIZE, 0);
1368 1.4.2.2 tls if (fd < 0)
1369 1.4.2.2 tls return -1;
1370 1.4.2.2 tls
1371 1.4.2.2 tls for (;;) {
1372 1.4.2.2 tls if (ioctl(fd, DIOCLWEDGES, &dkwl) == -1)
1373 1.4.2.2 tls return -2;
1374 1.4.2.2 tls if (dkwl.dkwl_nwedges == dkwl.dkwl_ncopied)
1375 1.4.2.2 tls break;
1376 1.4.2.2 tls bufsize = dkwl.dkwl_nwedges * sizeof(**dkw);
1377 1.4.2.2 tls if (dkwl.dkwl_bufsize < bufsize) {
1378 1.4.2.2 tls *dkw = realloc(dkwl.dkwl_buf, bufsize);
1379 1.4.2.2 tls if (*dkw == NULL)
1380 1.4.2.2 tls return -3;
1381 1.4.2.2 tls dkwl.dkwl_buf = *dkw;
1382 1.4.2.2 tls dkwl.dkwl_bufsize = bufsize;
1383 1.4.2.2 tls }
1384 1.4.2.2 tls }
1385 1.4.2.2 tls
1386 1.4.2.2 tls if (dkwl.dkwl_nwedges > 0)
1387 1.4.2.2 tls qsort(*dkw, dkwl.dkwl_nwedges, sizeof(**dkw), get_dkwedges_sort);
1388 1.4.2.2 tls
1389 1.4.2.2 tls close(fd);
1390 1.4.2.2 tls return dkwl.dkwl_nwedges;
1391 1.4.2.2 tls }
1392 1.4.2.2 tls
1393 1.4.2.2 tls /* XXX: rewrite */
1394 1.4.2.2 tls static int
1395 1.4.2.2 tls incoregpt(pm_devs_t *pm_cur, partinfo *lp)
1396 1.4.2.2 tls {
1397 1.4.2.2 tls int i, num;
1398 1.4.2.2 tls unsigned int p_num;
1399 1.4.2.2 tls uint64_t p_start, p_size;
1400 1.4.2.2 tls char *textbuf, *t, *tt, p_type[STRSIZE];
1401 1.4.2.2 tls struct dkwedge_info *dkw;
1402 1.4.2.2 tls
1403 1.4.2.2 tls num = get_dkwedges(&dkw, pm_cur->diskdev);
1404 1.4.2.2 tls if (dkw != NULL) {
1405 1.4.2.2 tls for (i = 0; i < num && i < MAX_WEDGES; i++)
1406 1.4.2.2 tls run_program(RUN_SILENT, "dkctl %s delwedge %s",
1407 1.4.2.2 tls pm_cur->diskdev, dkw[i].dkw_devname);
1408 1.4.2.2 tls free (dkw);
1409 1.4.2.2 tls }
1410 1.4.2.2 tls
1411 1.4.2.2 tls if (collect(T_OUTPUT, &textbuf, "gpt show -u %s 2>/dev/null", pm_cur->diskdev) < 1)
1412 1.4.2.2 tls return -1;
1413 1.4.2.2 tls
1414 1.4.2.2 tls (void)strtok(textbuf, "\n"); /* ignore first line */
1415 1.4.2.2 tls while ((t = strtok(NULL, "\n")) != NULL) {
1416 1.4.2.2 tls i = 0; p_start = 0; p_size = 0; p_num = 0; strcpy(p_type, ""); /* init */
1417 1.4.2.2 tls while ((tt = strsep(&t, " \t")) != NULL) {
1418 1.4.2.2 tls if (strlen(tt) == 0)
1419 1.4.2.2 tls continue;
1420 1.4.2.2 tls if (i == 0)
1421 1.4.2.2 tls p_start = strtouq(tt, NULL, 10);
1422 1.4.2.2 tls if (i == 1)
1423 1.4.2.2 tls p_size = strtouq(tt, NULL, 10);
1424 1.4.2.2 tls if (i == 2)
1425 1.4.2.2 tls p_num = strtouq(tt, NULL, 10);
1426 1.4.2.2 tls if (i > 2 || (i == 2 && p_num == 0))
1427 1.4.2.2 tls if (
1428 1.4.2.2 tls strcmp(tt, "GPT") &&
1429 1.4.2.2 tls strcmp(tt, "part") &&
1430 1.4.2.2 tls strcmp(tt, "-")
1431 1.4.2.2 tls )
1432 1.4.2.2 tls strncat(p_type, tt, STRSIZE);
1433 1.4.2.2 tls i++;
1434 1.4.2.2 tls }
1435 1.4.2.2 tls if (p_start == 0 || p_size == 0)
1436 1.4.2.2 tls continue;
1437 1.4.2.2 tls else if (! strcmp(p_type, "Pritable"))
1438 1.4.2.2 tls pm_cur->ptstart = p_start + p_size;
1439 1.4.2.2 tls else if (! strcmp(p_type, "Sectable"))
1440 1.4.2.2 tls pm_cur->ptsize = p_start - pm_cur->ptstart - 1;
1441 1.4.2.2 tls else if (p_num == 0 && strlen(p_type) > 0)
1442 1.4.2.2 tls /* Utilitary entry (PMBR, etc) */
1443 1.4.2.2 tls continue;
1444 1.4.2.2 tls else if (p_num == 0) {
1445 1.4.2.2 tls /* Free space */
1446 1.4.2.2 tls continue;
1447 1.4.2.2 tls } else {
1448 1.4.2.2 tls /* Usual partition */
1449 1.4.2.2 tls lp[p_num].pi_size = p_size;
1450 1.4.2.2 tls lp[p_num].pi_offset = p_start;
1451 1.4.2.2 tls lp[p_num].pi_fstype = get_fsid_by_gptuuid(p_type);
1452 1.4.2.2 tls }
1453 1.4.2.2 tls }
1454 1.4.2.2 tls free(textbuf);
1455 1.4.2.2 tls
1456 1.4.2.2 tls return 0;
1457 1.4.2.2 tls }
1458 1.4.2.2 tls
1459 1.4.2.2 tls static int
1460 1.4.2.2 tls is_gpt(const char *dev)
1461 1.4.2.2 tls {
1462 1.4.2.2 tls return ! run_program(RUN_SILENT | RUN_ERROR_OK,
1463 1.4.2.2 tls "sh -c 'gpt show %s |grep -e Pri\\ GPT\\ table'", dev);
1464 1.4.2.2 tls }
1465