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/types.h>
41 #include <sys/sysctl.h>
46 #include "libaura/dict.h"
50 #include "functions.h"
52 static int fgets_chomp(char *, int, FILE *);
53 static int parse_geometry_info(char *, int *, int *, int *);
54 static int parse_slice_info(char *, int *,
55 unsigned long *, unsigned long *, int *, int *);
58 * Get a line from a file. Remove any trailing EOL's.
59 * Return 1 if we did not hit EOF, 0 if we did.
62 fgets_chomp(char *line
, int size
, FILE *f
)
64 if (fgets(line
, size
, f
) == NULL
)
66 while (strlen(line
) > 0 && line
[strlen(line
) - 1] == '\n')
67 line
[strlen(line
) - 1] = '\0';
72 * Given a geometry line from fdisk's summary output, return the
73 * number of cylinders, heads, and sectors.
76 parse_geometry_info(char *line
, int *cyl
, int *head
, int *sec
)
81 * /dev/ad3: 2112 cyl 16 hd 63 sec
83 if ((word
= strtok(line
, " \t")) == NULL
) /* /dev/ad3: */
85 if ((word
= strtok(NULL
, " \t")) == NULL
) /* 2112 */
88 if ((word
= strtok(NULL
, " \t")) == NULL
) /* cyl */
90 if ((word
= strtok(NULL
, " \t")) == NULL
) /* 16 */
93 if ((word
= strtok(NULL
, " \t")) == NULL
) /* hd */
95 if ((word
= strtok(NULL
, " \t")) == NULL
) /* 63 */
103 * Given a slice description line from fdisk's summary output, return
104 * the number of the slice, and its start, size, type, and flags.
107 parse_slice_info(char *line
, int *slice
,
108 unsigned long *start
, unsigned long *size
,
109 int *type
, int *flags
)
114 * Part Start Size Type Flags
115 * 1: 63 2128833 0xa5 0x80
117 if ((word
= strtok(line
, " \t")) == NULL
) /* 1: */
120 if ((word
= strtok(NULL
, " \t")) == NULL
) /* 63 */
122 *start
= strtoul(word
, NULL
, 10);
123 if ((word
= strtok(NULL
, " \t")) == NULL
) /* 2128833 */
125 *size
= strtoul(word
, NULL
, 10);
126 if ((word
= strtok(NULL
, " \t")) == NULL
) /* 0xa5 */
128 if (!hex_to_int(word
, type
))
130 if ((word
= strtok(NULL
, " \t")) == NULL
) /* 0x80 */
132 if (!hex_to_int(word
, flags
))
139 * Survey storage capacity of this system.
142 survey_storage(struct i_fn_args
*a
)
144 unsigned long mem
= 0;
145 char disks
[256], line
[256];
146 char *disk
, *disk_ptr
;
147 struct commands
*cmds
;
151 struct disk
*d
= NULL
;
155 struct aura_dict
*di
;
162 if (sysctlbyname("hw.physmem", &mem
, &len
, NULL
, 0) < 0) {
165 storage_set_memsize(a
->s
, next_power_of_two(mem
>> 20));
168 if (sysctlbyname("kern.disks", disks
, &len
, NULL
, 0) < 0) {
173 di
= aura_dict_new(1, AURA_DICT_SORTED_LIST
);
174 while (!failure
&& (disk
= strsep(&disk_ptr
, " ")) != NULL
) {
179 * If the disk is a memory disk, floppy or CD-ROM, skip it.
181 if (strncmp(disk
, "md", 2) == 0 ||
182 strncmp(disk
, "cd", 2) == 0 ||
183 strncmp(disk
, "acd", 3) == 0 ||
184 strncmp(disk
, "fd", 2) == 0)
187 aura_dict_store(di
, disk
, strlen(disk
) + 1, "", 1);
190 cmds
= commands_new();
191 cmd
= command_add(cmds
, "%s%s -n '' >%ssurvey.txt",
192 a
->os_root
, cmd_name(a
, "ECHO"), a
->tmp
);
193 command_set_log_mode(cmd
, COMMAND_LOG_SILENT
);
195 aura_dict_rewind(di
);
196 while (!aura_dict_eof(di
)) {
197 aura_dict_get_current_key(di
, &rk
, &rk_len
),
201 cmd
= command_add(cmds
, "%s%s '@DISK' >>%ssurvey.txt",
202 a
->os_root
, cmd_name(a
, "ECHO"), a
->tmp
);
203 command_set_log_mode(cmd
, COMMAND_LOG_SILENT
);
204 cmd
= command_add(cmds
, "%s%s '%s' >>%ssurvey.txt",
205 a
->os_root
, cmd_name(a
, "ECHO"), disk
, a
->tmp
);
206 command_set_log_mode(cmd
, COMMAND_LOG_SILENT
);
209 * Look for descriptions of this disk.
211 cmd
= command_add(cmds
, "%s%s '@DESC' >>%ssurvey.txt",
212 a
->os_root
, cmd_name(a
, "ECHO"), a
->tmp
);
213 command_set_log_mode(cmd
, COMMAND_LOG_SILENT
);
214 cmd
= command_add(cmds
, "%s%s -w '^%s: [0-9]*MB' %s%s >>%ssurvey.txt || %s%s '%s' >>%ssurvey.txt",
215 a
->os_root
, cmd_name(a
, "GREP"),
217 a
->os_root
, cmd_name(a
, "DMESG_BOOT"),
219 a
->os_root
, cmd_name(a
, "ECHO"),
222 cmd
= command_add(cmds
, "%s%s '@END' >>%ssurvey.txt",
223 a
->os_root
, cmd_name(a
, "ECHO"), a
->tmp
);
224 command_set_log_mode(cmd
, COMMAND_LOG_SILENT
);
227 * Look for the disk's serial number.
229 cmd
= command_add(cmds
, "%s%s '@SERNO' >>%ssurvey.txt",
230 a
->os_root
, cmd_name(a
, "ECHO"), a
->tmp
);
231 command_set_log_mode(cmd
, COMMAND_LOG_SILENT
);
232 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",
233 a
->os_root
, cmd_name(a
, "TEST"),
234 a
->os_root
, cmd_name(a
, "LS"),
235 a
->os_root
, cmd_name(a
, "GREP"),
236 a
->os_root
, cmd_name(a
, "LS"),
238 a
->os_root
, cmd_name(a
, "AWK"),
239 a
->os_root
, cmd_name(a
, "AWK"),
241 cmd
= command_add(cmds
, "%s%s '@END' >>%ssurvey.txt",
242 a
->os_root
, cmd_name(a
, "ECHO"), a
->tmp
);
243 command_set_log_mode(cmd
, COMMAND_LOG_SILENT
);
246 * Probe the disk with fdisk.
248 cmd
= command_add(cmds
, "%s%s '@SLICES' >>%ssurvey.txt",
249 a
->os_root
, cmd_name(a
, "ECHO"), a
->tmp
);
250 command_set_log_mode(cmd
, COMMAND_LOG_SILENT
);
251 cmd
= command_add(cmds
, "%s%s -s %s 2>/dev/null >>%ssurvey.txt || %s%s '' >>%ssurvey.txt",
252 a
->os_root
, cmd_name(a
, "FDISK"),
255 a
->os_root
, cmd_name(a
, "ECHO"),
257 cmd
= command_add(cmds
, "%s%s '@END' >>%ssurvey.txt",
258 a
->os_root
, cmd_name(a
, "ECHO"), a
->tmp
);
259 command_set_log_mode(cmd
, COMMAND_LOG_SILENT
);
264 cmd
= command_add(cmds
, "%s%s '.' >>%ssurvey.txt",
265 a
->os_root
, cmd_name(a
, "ECHO"), a
->tmp
);
266 command_set_log_mode(cmd
, COMMAND_LOG_SILENT
);
268 if (!commands_execute(a
, cmds
))
271 temp_file_add(a
, "survey.txt");
276 * Now read in and parse the file that those commands just created.
278 asprintf(&filename
, "%ssurvey.txt", a
->tmp
);
279 if ((f
= fopen(filename
, "r")) == NULL
)
283 while (!failure
&& fgets_chomp(line
, 255, f
)) {
284 if (strcmp(line
, "@DISK") == 0) {
285 if (fgets_chomp(line
, 255, f
)) {
286 d
= disk_new(a
->s
, line
);
287 disk_set_number(d
, number
++);
289 } else if (strcmp(line
, "@DESC") == 0) {
290 while (d
!= NULL
&& strcmp(line
, "@END") != 0 && fgets_chomp(line
, 255, f
)) {
291 disk_set_desc(d
, line
);
293 } else if (strcmp(line
, "@SERNO") == 0) {
294 fgets_chomp(line
, 255, f
);
295 if (line
[0] != '\0' && strcmp(line
, "@END") != 0)
296 disk_set_serno(d
, line
);
297 } else if (strcmp(line
, "@SLICES") == 0) {
299 int number
, type
, flags
;
300 unsigned long start
, size
;
303 * /dev/ad3: 2112 cyl 16 hd 63 sec
304 * Part Start Size Type Flags
305 * 1: 63 2128833 0xa5 0x80
307 while (d
!= NULL
&& strcmp(line
, "@END") != 0 && fgets_chomp(line
, 255, f
)) {
308 if (strncmp(line
, "/dev/", 5) == 0) {
309 parse_geometry_info(line
, &cyl
, &hd
, &sec
);
310 disk_set_geometry(d
, cyl
, hd
, sec
);
311 } else if (strncmp(line
, "Part", 4) == 0) {
314 if (parse_slice_info(line
, &number
, &start
, &size
,
317 fprintfo(log, "| Found slice #%d, sysid %d, "
318 "start %ld, size %ld\n", number, type, start, size);
320 slice_new(d
, number
, type
, flags
, start
, size
);
331 * Fix up any disk descriptions that didn't make it.
333 for (d
= storage_disk_first(a
->s
); d
!= NULL
; d
= disk_next(d
)) {
334 if (disk_get_desc(d
) == NULL
)
335 disk_set_desc(d
, disk_get_device_name(d
));