2 * Copyright (c)2004 The DragonFly Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * Neither the name of the DragonFly Project nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
36 * Survey the storage capacity of the system.
37 * $Id: survey.c,v 1.17 2005/02/06 21:05:18 cpressey Exp $
40 #include <sys/param.h>
41 #include <sys/diskslice.h>
42 #include <sys/sysctl.h>
49 #include "libaura/dict.h"
53 #include "functions.h"
55 static int fgets_chomp(char *, int, FILE *);
56 static int parse_geometry_info(char *, int *, int *, int *);
57 static int parse_slice_info(char *, int *,
58 unsigned long *, unsigned long *, int *, int *);
61 * Get a line from a file. Remove any trailing EOL's.
62 * Return 1 if we did not hit EOF, 0 if we did.
65 fgets_chomp(char *line
, int size
, FILE *f
)
67 if (fgets(line
, size
, f
) == NULL
)
69 while (strlen(line
) > 0 && line
[strlen(line
) - 1] == '\n')
70 line
[strlen(line
) - 1] = '\0';
75 * Given a geometry line from fdisk's summary output, return the
76 * number of cylinders, heads, and sectors.
79 parse_geometry_info(char *line
, int *cyl
, int *head
, int *sec
)
84 * /dev/ad3: 2112 cyl 16 hd 63 sec
86 if ((word
= strtok(line
, " \t")) == NULL
) /* /dev/ad3: */
88 if ((word
= strtok(NULL
, " \t")) == NULL
) /* 2112 */
91 if ((word
= strtok(NULL
, " \t")) == NULL
) /* cyl */
93 if ((word
= strtok(NULL
, " \t")) == NULL
) /* 16 */
96 if ((word
= strtok(NULL
, " \t")) == NULL
) /* hd */
98 if ((word
= strtok(NULL
, " \t")) == NULL
) /* 63 */
106 * Given a slice description line from fdisk's summary output, return
107 * the number of the slice, and its start, size, type, and flags.
110 parse_slice_info(char *line
, int *slice
,
111 unsigned long *start
, unsigned long *size
,
112 int *type
, int *flags
)
117 * Part Start Size Type Flags
118 * 1: 63 2128833 0xa5 0x80
120 if ((word
= strtok(line
, " \t")) == NULL
) /* 1: */
123 if ((word
= strtok(NULL
, " \t")) == NULL
) /* 63 */
125 *start
= strtoul(word
, NULL
, 10);
126 if ((word
= strtok(NULL
, " \t")) == NULL
) /* 2128833 */
128 *size
= strtoul(word
, NULL
, 10);
129 if ((word
= strtok(NULL
, " \t")) == NULL
) /* 0xa5 */
131 if (!hex_to_int(word
, type
))
133 if ((word
= strtok(NULL
, " \t")) == NULL
) /* 0x80 */
135 if (!hex_to_int(word
, flags
))
142 * Survey storage capacity of this system.
145 survey_storage(struct i_fn_args
*a
)
147 unsigned long mem
= 0;
148 char disks
[256], line
[256];
149 char *disk
, *disk_ptr
;
150 struct commands
*cmds
;
154 struct disk
*d
= NULL
;
160 struct aura_dict
*di
;
163 struct partinfo diskpart
;
164 char diskpath
[PATH_MAX
];
169 if (sysctlbyname("hw.physmem", &mem
, &len
, NULL
, 0) < 0) {
172 storage_set_memsize(a
->s
, next_power_of_two(mem
>> 20));
175 if (sysctlbyname("kern.disks", disks
, &len
, NULL
, 0) < 0) {
180 di
= aura_dict_new(1, AURA_DICT_SORTED_LIST
);
181 while (!failure
&& (disk
= strsep(&disk_ptr
, " ")) != NULL
) {
186 * If the disk is a memory disk, floppy or CD-ROM, skip it.
188 if (strncmp(disk
, "md", 2) == 0 ||
189 strncmp(disk
, "cd", 2) == 0 ||
190 strncmp(disk
, "acd", 3) == 0 ||
191 strncmp(disk
, "fd", 2) == 0)
194 aura_dict_store(di
, disk
, strlen(disk
) + 1, "", 1);
201 cmds
= commands_new();
202 cmd
= command_add(cmds
, "%s%s -n '' >%ssurvey.txt",
203 a
->os_root
, cmd_name(a
, "ECHO"), a
->tmp
);
204 command_set_log_mode(cmd
, COMMAND_LOG_SILENT
);
206 for (aura_dict_rewind(di
); !aura_dict_eof(di
); aura_dict_next(di
) ) {
207 aura_dict_get_current_key(di
, &rk
, &rk_len
);
211 * Attempt to get media information from the disk. This information
212 * might be used later on for partitioning. Any disk that does not
213 * provide the information will be discarded as not suitable for
216 bzero(&diskpart
, sizeof(diskpart
));
217 snprintf(diskpath
, PATH_MAX
, "/dev/%s", disk
);
218 if ((fd
= open(diskpath
, O_RDONLY
)) < 0)
220 if (ioctl(fd
, DIOCGPART
, &diskpart
) < 0)
223 cmd
= command_add(cmds
, "%s%s '@DISK' >>%ssurvey.txt",
224 a
->os_root
, cmd_name(a
, "ECHO"), a
->tmp
);
225 command_set_log_mode(cmd
, COMMAND_LOG_SILENT
);
226 cmd
= command_add(cmds
, "%s%s '%s' >>%ssurvey.txt",
227 a
->os_root
, cmd_name(a
, "ECHO"), disk
, a
->tmp
);
228 command_set_log_mode(cmd
, COMMAND_LOG_SILENT
);
231 * Look for descriptions of this disk.
233 cmd
= command_add(cmds
, "%s%s '@DESC' >>%ssurvey.txt",
234 a
->os_root
, cmd_name(a
, "ECHO"), a
->tmp
);
235 command_set_log_mode(cmd
, COMMAND_LOG_SILENT
);
236 cmd
= command_add(cmds
, "%s%s '%s: %luMB' >>%ssurvey.txt",
237 a
->os_root
, cmd_name(a
, "ECHO"),
239 diskpart
.media_size
/ 1024 / 1024,
242 cmd
= command_add(cmds
, "%s%s '@END' >>%ssurvey.txt",
243 a
->os_root
, cmd_name(a
, "ECHO"), a
->tmp
);
244 command_set_log_mode(cmd
, COMMAND_LOG_SILENT
);
247 * Look for the disk's serial number.
249 cmd
= command_add(cmds
, "%s%s '@SERNO' >>%ssurvey.txt",
250 a
->os_root
, cmd_name(a
, "ECHO"), a
->tmp
);
251 command_set_log_mode(cmd
, COMMAND_LOG_SILENT
);
252 cmd
= command_add(cmds
, "if %s%s -d /dev/serno; then %s%s -l /dev/serno | %s%s \"`%s%s -l /dev/%s | %s%s '{print $5, $6;}'`\" | %s%s '{print $10;}' >>%ssurvey.txt; fi",
253 a
->os_root
, cmd_name(a
, "TEST"),
254 a
->os_root
, cmd_name(a
, "LS"),
255 a
->os_root
, cmd_name(a
, "GREP"),
256 a
->os_root
, cmd_name(a
, "LS"),
258 a
->os_root
, cmd_name(a
, "AWK"),
259 a
->os_root
, cmd_name(a
, "AWK"),
261 cmd
= command_add(cmds
, "%s%s '@END' >>%ssurvey.txt",
262 a
->os_root
, cmd_name(a
, "ECHO"), a
->tmp
);
263 command_set_log_mode(cmd
, COMMAND_LOG_SILENT
);
266 * Probe the disk with fdisk.
268 cmd
= command_add(cmds
, "%s%s '@SLICES' >>%ssurvey.txt",
269 a
->os_root
, cmd_name(a
, "ECHO"), a
->tmp
);
270 command_set_log_mode(cmd
, COMMAND_LOG_SILENT
);
271 cmd
= command_add(cmds
, "%s%s -s %s 2>/dev/null >>%ssurvey.txt || %s%s '' >>%ssurvey.txt",
272 a
->os_root
, cmd_name(a
, "FDISK"),
275 a
->os_root
, cmd_name(a
, "ECHO"),
277 cmd
= command_add(cmds
, "%s%s '@END' >>%ssurvey.txt",
278 a
->os_root
, cmd_name(a
, "ECHO"), a
->tmp
);
279 command_set_log_mode(cmd
, COMMAND_LOG_SILENT
);
282 cmd
= command_add(cmds
, "%s%s '.' >>%ssurvey.txt",
283 a
->os_root
, cmd_name(a
, "ECHO"), a
->tmp
);
284 command_set_log_mode(cmd
, COMMAND_LOG_SILENT
);
286 if (!commands_execute(a
, cmds
))
289 temp_file_add(a
, "survey.txt");
294 * Now read in and parse the file that those commands just created.
296 asprintf(&filename
, "%ssurvey.txt", a
->tmp
);
297 if ((f
= fopen(filename
, "r")) == NULL
)
301 while (!failure
&& fgets_chomp(line
, 255, f
)) {
302 if (strcmp(line
, "@DISK") == 0) {
303 if (fgets_chomp(line
, 255, f
)) {
304 d
= disk_new(a
->s
, line
);
305 disk_set_number(d
, number
++);
307 } else if (strcmp(line
, "@DESC") == 0) {
308 while (d
!= NULL
&& strcmp(line
, "@END") != 0 && fgets_chomp(line
, 255, f
)) {
309 disk_set_desc(d
, line
);
311 } else if (strcmp(line
, "@SERNO") == 0) {
312 fgets_chomp(line
, 255, f
);
313 if (line
[0] != '\0' && strcmp(line
, "@END") != 0)
314 disk_set_serno(d
, line
);
315 } else if (strcmp(line
, "@SLICES") == 0) {
317 int sliceno
, type
, flags
;
318 unsigned long start
, size
;
321 * /dev/ad3: 2112 cyl 16 hd 63 sec
322 * Part Start Size Type Flags
323 * 1: 63 2128833 0xa5 0x80
325 while (d
!= NULL
&& strcmp(line
, "@END") != 0 && fgets_chomp(line
, 255, f
)) {
326 if (strncmp(line
, "/dev/", 5) == 0) {
328 parse_geometry_info(line
, &cyl
, &hd
, &sec
);
329 disk_set_geometry(d
, cyl
, hd
, sec
);
330 } else if (strncmp(line
, "Part", 4) == 0) {
333 if (parse_slice_info(line
, &sliceno
, &start
, &size
,
336 fprintfo(log, "| Found slice #%d, sysid %d, "
337 "start %ld, size %ld\n", sliceno, type, start, size);
339 slice_new(d
, sliceno
, type
, flags
, start
, size
);
350 * Fix up any disk descriptions that didn't make it.
352 for (d
= storage_disk_first(a
->s
); d
!= NULL
; d
= disk_next(d
)) {
353 if (disk_get_desc(d
) == NULL
)
354 disk_set_desc(d
, disk_get_device_name(d
));