9892 Most consumers of be_list() do not need snapshots
[unleashed.git] / usr / src / cmd / boot / bootadm / bootadm_loader.c
blobb9cc9ccd212db13747cbd7e4d7225aec61416f52
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2012 Milan Jurik. All rights reserved.
27 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
28 * Copyright 2016 Toomas Soome <tsoome@me.com>
29 * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
33 * Loader menu management.
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <wchar.h>
40 #include <errno.h>
41 #include <limits.h>
42 #include <alloca.h>
43 #include <unistd.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <sys/queue.h>
47 #include <libbe.h>
48 #include <ficl.h>
49 #include <ficlplatform/emu.h>
50 #include <ofmt.h>
52 #include "bootadm.h"
54 extern int bam_rootlen;
55 extern int bam_alt_root;
56 extern char *rootbuf;
57 extern char *bam_root;
59 #define BOOT_DIR "/boot"
60 #define CONF_DIR BOOT_DIR "/conf.d"
61 #define MENU BOOT_DIR "/menu.lst"
62 #define TRANSIENT BOOT_DIR "/transient.conf"
63 #define XEN_CONFIG CONF_DIR "/xen"
65 typedef struct menu_entry {
66 int me_idx;
67 boolean_t me_active;
68 char *me_title;
69 char *me_type;
70 char *me_bootfs;
71 STAILQ_ENTRY(menu_entry) me_next;
72 } menu_entry_t;
73 STAILQ_HEAD(menu_lst, menu_entry);
75 static error_t set_option(struct menu_lst *, char *, char *);
76 static error_t list_entry(struct menu_lst *, char *, char *);
77 static error_t update_entry(struct menu_lst *, char *, char *);
78 static error_t update_temp(struct menu_lst *, char *, char *);
79 static error_t list_setting(struct menu_lst *menu, char *, char *);
80 static error_t disable_hyper(struct menu_lst *, char *, char *);
81 static error_t enable_hyper(struct menu_lst *, char *, char *);
83 /* Menu related sub commands */
84 static subcmd_defn_t menu_subcmds[] = {
85 { "set_option", OPT_ABSENT, set_option, 0 }, /* PUB */
86 { "list_entry", OPT_OPTIONAL, list_entry, 1 }, /* PUB */
87 { "update_entry", OPT_REQ, update_entry, 0 }, /* menu */
88 { "update_temp", OPT_OPTIONAL, update_temp, 0 }, /* reboot */
89 { "list_setting", OPT_OPTIONAL, list_setting, 1 }, /* menu */
90 { "disable_hypervisor", OPT_ABSENT, disable_hyper, 0 }, /* menu */
91 { "enable_hypervisor", OPT_ABSENT, enable_hyper, 0 }, /* menu */
92 { NULL, 0, NULL, 0 } /* must be last */
95 #define NUM_COLS (5)
97 static boolean_t
98 print_menu_cb(ofmt_arg_t *ofarg, char *buf, uint_t bufsize)
100 menu_entry_t *entry = ofarg->ofmt_cbarg;
102 switch (ofarg->ofmt_id) {
103 case 0:
104 (void) snprintf(buf, bufsize, "%d", entry->me_idx);
105 break;
106 case 1:
107 (void) snprintf(buf, bufsize, "%s", entry->me_title);
108 break;
109 case 2:
110 (void) snprintf(buf, bufsize, "%s", entry->me_bootfs);
111 break;
112 case 3:
113 (void) snprintf(buf, bufsize, "%s", entry->me_type);
114 break;
115 case 4:
116 if (entry->me_active == B_TRUE)
117 (void) snprintf(buf, bufsize, " *");
118 else
119 (void) snprintf(buf, bufsize, " -");
120 break;
121 default:
122 return (B_FALSE);
124 return (B_TRUE);
127 static void
128 init_hdr_cols(ofmt_field_t *hdr)
130 uint_t i;
132 for (i = 0; i < NUM_COLS; i++) {
133 char *name = NULL;
135 switch (i) {
136 case 0:
137 name = _("INDEX");
138 break;
139 case 1:
140 name = _("NAME");
141 break;
142 case 2:
143 name = _("DEVICE");
144 break;
145 case 3:
146 name = _("TYPE");
147 break;
148 case 4:
149 name = _("DEFAULT");
150 break;
153 hdr[i].of_name = name;
154 hdr[i].of_id = i;
155 hdr[i].of_cb = print_menu_cb;
157 if (name != NULL) {
158 wchar_t wname[128];
159 size_t sz = mbstowcs(wname, name, sizeof (wname) /
160 sizeof (wchar_t));
161 if (sz > 0) {
162 int wcsw = wcswidth(wname, sz);
163 if (wcsw > 0)
164 hdr[i].of_width = wcsw;
165 else
166 hdr[i].of_width = sz;
167 } else {
168 hdr[i].of_width = strlen(name);
174 static void
175 menu_update_widths(ofmt_field_t *hdr, struct menu_lst *menu)
177 size_t len[NUM_COLS];
178 menu_entry_t *entry;
179 int i;
181 for (i = 0; i < NUM_COLS; i++)
182 len[i] = hdr[i].of_width + 1;
184 STAILQ_FOREACH(entry, menu, me_next) {
185 size_t entry_len;
187 entry_len = strlen(entry->me_title) + 1;
188 if (entry_len > len[1])
189 len[1] = entry_len;
191 entry_len = strlen(entry->me_bootfs) + 1;
192 if (entry_len > len[2])
193 len[2] = entry_len;
195 entry_len = strlen(entry->me_type) + 1;
196 if (entry_len > len[3])
197 len[3] = entry_len;
200 for (i = 0; i < NUM_COLS; i++)
201 hdr[i].of_width = len[i];
204 static ofmt_field_t *
205 init_menu_template(struct menu_lst *menu)
207 ofmt_field_t *temp;
209 if ((temp = calloc(NUM_COLS + 1, sizeof (ofmt_field_t))) == NULL)
210 return (temp);
212 init_hdr_cols(temp);
213 menu_update_widths(temp, menu);
214 return (temp);
217 static void
218 print_nodes(boolean_t parsable, struct menu_lst *menu)
220 ofmt_status_t oferr;
221 ofmt_handle_t ofmt;
222 uint_t ofmtflags = 0;
223 ofmt_field_t *menu_template;
224 menu_entry_t *entry;
226 if (parsable == B_TRUE)
227 ofmtflags = OFMT_PARSABLE;
229 menu_template = init_menu_template(menu);
230 oferr = ofmt_open(NULL, menu_template, ofmtflags, 0, &ofmt);
232 if (oferr != OFMT_SUCCESS) {
233 char buf[OFMT_BUFSIZE];
235 (void) ofmt_strerror(ofmt, oferr, buf, sizeof (buf));
236 (void) printf("bootadm: %s\n", buf);
237 free(menu_template);
238 return;
241 STAILQ_FOREACH(entry, menu, me_next)
242 ofmt_print(ofmt, entry);
244 ofmt_close(ofmt);
245 free(menu_template);
249 * Get the be_active_on_boot for bootfs.
251 static boolean_t
252 menu_active_on_boot(be_node_list_t *be_nodes, const char *bootfs)
254 be_node_list_t *be_node;
255 boolean_t rv = B_FALSE;
257 for (be_node = be_nodes; be_node != NULL;
258 be_node = be_node->be_next_node) {
259 if (strcmp(be_node->be_root_ds, bootfs) == 0) {
260 rv = be_node->be_active_on_boot;
261 break;
265 return (rv);
268 error_t
269 menu_read(struct menu_lst *menu, char *menu_path)
271 FILE *fp;
272 be_node_list_t *be_nodes;
273 menu_entry_t *mp;
274 char buf[PATH_MAX];
275 char *title;
276 char *bootfs;
277 char *type;
278 char *key, *value;
279 int i = 0;
280 int ret = BAM_SUCCESS;
282 fp = fopen(menu_path, "r");
283 if (fp == NULL)
284 return (BAM_ERROR);
286 if (be_list(NULL, &be_nodes, BE_LIST_DEFAULT) != BE_SUCCESS)
287 be_nodes = NULL;
290 * menu.lst entry is on two lines, one for title, one for bootfs
291 * so we process both lines in succession.
293 title = NULL;
294 type = NULL;
295 bootfs = NULL;
296 do {
297 if (fgets(buf, PATH_MAX, fp) == NULL) {
298 if (!feof(fp))
299 ret = BAM_ERROR;
300 goto done;
302 key = strtok(buf, " \n");
303 if (strcmp(key, "title") != 0) {
304 ret = BAM_ERROR;
305 goto done;
307 value = strtok(NULL, " \n");
308 if ((title = strdup(value)) == NULL) {
309 ret = BAM_ERROR;
310 goto done;
313 if (fgets(buf, PATH_MAX, fp) == NULL) {
314 ret = BAM_ERROR;
315 goto done;
318 key = strtok(buf, " \n");
319 if ((type = strdup(key)) == NULL) {
320 ret = BAM_ERROR;
321 goto done;
323 value = strtok(NULL, " \n");
324 if ((bootfs = strdup(value)) == NULL) {
325 ret = BAM_ERROR;
326 goto done;
328 if ((mp = malloc(sizeof (menu_entry_t))) == NULL) {
329 ret = BAM_ERROR;
330 goto done;
332 mp->me_idx = i++;
333 mp->me_title = title;
334 mp->me_type = type;
335 mp->me_bootfs = bootfs;
336 mp->me_active = menu_active_on_boot(be_nodes, bootfs);
337 STAILQ_INSERT_TAIL(menu, mp, me_next);
339 title = NULL;
340 type = NULL;
341 bootfs = NULL;
342 } while (feof(fp) == 0);
344 done:
345 free(title);
346 free(type);
347 free(bootfs);
348 (void) fclose(fp);
349 be_free_list(be_nodes);
350 return (ret);
353 void
354 menu_free(struct menu_lst *menu)
356 menu_entry_t *entry;
357 STAILQ_FOREACH(entry, menu, me_next) {
358 STAILQ_REMOVE_HEAD(menu, me_next);
359 free(entry->me_title);
360 free(entry->me_type);
361 free(entry->me_bootfs);
362 free(entry);
366 error_t
367 bam_loader_menu(char *subcmd, char *opt, int largc, char *largv[])
369 error_t ret;
370 char menu_path[PATH_MAX];
371 char clean_menu_root[PATH_MAX];
372 char menu_root[PATH_MAX];
373 struct stat sb;
374 error_t (*f)(struct menu_lst *, char *, char *);
375 char *special;
376 char *pool = NULL;
377 zfs_mnted_t zmnted;
378 char *zmntpt;
379 char *osdev;
380 char *osroot;
381 const char *fcn = "bam_loader_menu()";
382 struct menu_lst menu = {0};
384 STAILQ_INIT(&menu);
387 * Check arguments
389 ret = check_subcmd_and_options(subcmd, opt, menu_subcmds, &f);
390 if (ret == BAM_ERROR) {
391 return (BAM_ERROR);
394 assert(bam_root);
396 (void) strlcpy(menu_root, bam_root, sizeof (menu_root));
397 osdev = osroot = NULL;
399 if (strcmp(subcmd, "update_entry") == 0) {
400 assert(opt);
402 osdev = strtok(opt, ",");
403 assert(osdev);
404 osroot = strtok(NULL, ",");
405 if (osroot) {
406 /* fixup bam_root so that it points at osroot */
407 if (realpath(osroot, rootbuf) == NULL) {
408 bam_error(_("cannot resolve path %s: %s\n"),
409 osroot, strerror(errno));
410 return (BAM_ERROR);
412 bam_alt_root = 1;
413 bam_root = rootbuf;
414 bam_rootlen = strlen(rootbuf);
418 if (stat(menu_root, &sb) == -1) {
419 bam_error(_("cannot find menu\n"));
420 return (BAM_ERROR);
423 if (!is_zfs(menu_root)) {
424 bam_error(_("only ZFS root is supported\n"));
425 return (BAM_ERROR);
428 assert(strcmp(menu_root, bam_root) == 0);
429 special = get_special(menu_root);
430 INJECT_ERROR1("Z_MENU_GET_SPECIAL", special = NULL);
431 if (special == NULL) {
432 bam_error(_("cant find special file for mount-point %s\n"),
433 menu_root);
434 return (BAM_ERROR);
436 pool = strtok(special, "/");
437 INJECT_ERROR1("Z_MENU_GET_POOL", pool = NULL);
438 if (pool == NULL) {
439 free(special);
440 bam_error(_("cant find pool for mount-point %s\n"), menu_root);
441 return (BAM_ERROR);
443 BAM_DPRINTF(("%s: derived pool=%s from special\n", fcn, pool));
445 zmntpt = mount_top_dataset(pool, &zmnted);
446 INJECT_ERROR1("Z_MENU_MOUNT_TOP_DATASET", zmntpt = NULL);
447 if (zmntpt == NULL) {
448 bam_error(_("cannot mount pool dataset for pool: %s\n"), pool);
449 free(special);
450 return (BAM_ERROR);
452 BAM_DPRINTF(("%s: top dataset mountpoint=%s\n", fcn, zmntpt));
454 (void) strlcpy(menu_root, zmntpt, sizeof (menu_root));
455 BAM_DPRINTF(("%s: zfs menu_root=%s\n", fcn, menu_root));
457 elide_trailing_slash(menu_root, clean_menu_root,
458 sizeof (clean_menu_root));
460 BAM_DPRINTF(("%s: cleaned menu root is <%s>\n", fcn, clean_menu_root));
462 (void) strlcpy(menu_path, clean_menu_root, sizeof (menu_path));
463 (void) strlcat(menu_path, MENU, sizeof (menu_path));
465 BAM_DPRINTF(("%s: menu path is: %s\n", fcn, menu_path));
468 * update_entry is special case, its used by installer
469 * and needs to create menu.lst file for loader
471 if (menu_read(&menu, menu_path) == BAM_ERROR &&
472 strcmp(subcmd, "update_entry") != 0) {
473 bam_error(_("cannot find menu file: %s\n"), menu_path);
474 if (special != NULL)
475 free(special);
476 return (BAM_ERROR);
480 * If listing the menu, display the menu location
482 if (strcmp(subcmd, "list_entry") == 0)
483 bam_print(_("the location for the active menu is: %s\n"),
484 menu_path);
487 * We already checked the following case in
488 * check_subcmd_and_suboptions() above. Complete the
489 * final step now.
491 if (strcmp(subcmd, "set_option") == 0) {
492 assert(largc == 1 && largv[0] && largv[1] == NULL);
493 opt = largv[0];
494 } else if ((strcmp(subcmd, "enable_hypervisor") != 0) &&
495 (strcmp(subcmd, "list_setting") != 0)) {
496 assert(largc == 0 && largv == NULL);
500 * Once the sub-cmd handler has run
501 * only the line field is guaranteed to have valid values
503 if (strcmp(subcmd, "update_entry") == 0) {
504 ret = f(&menu, menu_root, osdev);
505 } else if (strcmp(subcmd, "upgrade") == 0) {
506 ret = f(&menu, bam_root, menu_root);
507 } else if (strcmp(subcmd, "list_entry") == 0) {
508 ret = f(&menu, menu_path, opt);
509 } else if (strcmp(subcmd, "list_setting") == 0) {
510 ret = f(&menu, ((largc > 0) ? largv[0] : ""),
511 ((largc > 1) ? largv[1] : ""));
512 } else if (strcmp(subcmd, "disable_hypervisor") == 0) {
513 ret = f(&menu, bam_root, NULL);
514 } else if (strcmp(subcmd, "enable_hypervisor") == 0) {
515 char *extra_args = NULL;
518 * Compress all arguments passed in the largv[] array
519 * into one string that can then be appended to the
520 * end of the kernel$ string the routine to enable the
521 * hypervisor will build.
523 * This allows the caller to supply arbitrary unparsed
524 * arguments, such as dom0 memory settings or APIC
525 * options.
527 * This concatenation will be done without ANY syntax
528 * checking whatsoever, so it's the responsibility of
529 * the caller to make sure the arguments are valid and
530 * do not duplicate arguments the conversion routines
531 * may create.
533 if (largc > 0) {
534 int extra_len, i;
536 for (extra_len = 0, i = 0; i < largc; i++)
537 extra_len += strlen(largv[i]);
540 * Allocate space for argument strings,
541 * intervening spaces and terminating NULL.
543 extra_args = alloca(extra_len + largc);
545 (void) strcpy(extra_args, largv[0]);
547 for (i = 1; i < largc; i++) {
548 (void) strcat(extra_args, " ");
549 (void) strcat(extra_args, largv[i]);
553 ret = f(&menu, bam_root, extra_args);
554 } else
555 ret = f(&menu, NULL, opt);
557 if (ret == BAM_WRITE) {
558 BAM_DPRINTF(("%s: writing menu to clean-menu-root: <%s>\n",
559 fcn, clean_menu_root));
560 /* ret = menu_write(clean_menu_root, menu); */
563 INJECT_ERROR1("POOL_SET", pool = "/pooldata");
564 assert((is_zfs(menu_root)) ^ (pool == NULL));
565 if (pool) {
566 (void) umount_top_dataset(pool, zmnted, zmntpt);
567 free(special);
570 menu_free(&menu);
571 return (ret);
575 * To suppress output from ficl. We do not want to see messages
576 * from interpreting loader config.
579 /*ARGSUSED*/
580 static void
581 ficlTextOutSilent(ficlCallback *cb, char *text)
585 /*ARGSUSED*/
586 static error_t
587 set_option(struct menu_lst *menu, char *dummy, char *opt)
589 char path[PATH_MAX];
590 char *val;
591 char *rest;
592 int optval;
593 menu_entry_t *entry;
594 nvlist_t *be_attrs;
595 FILE *fp;
596 int rv, ret = BAM_SUCCESS;
598 assert(menu);
599 assert(opt);
600 assert(dummy == NULL);
602 val = strchr(opt, '=');
603 if (val != NULL) {
604 *val++ = '\0';
607 if (strcmp(opt, "default") == 0) {
608 errno = 0;
609 optval = strtol(val, &rest, 10);
610 if (errno != 0 || *rest != '\0') {
611 bam_error(_("invalid boot entry number: %s\n"), val);
612 return (BAM_ERROR);
614 STAILQ_FOREACH(entry, menu, me_next) {
615 if (entry->me_idx == optval)
616 break;
618 if (entry == NULL) {
619 bam_error(_("invalid boot entry number: %s\n"), val);
620 return (BAM_ERROR);
622 if (nvlist_alloc(&be_attrs, NV_UNIQUE_NAME, 0) != 0) {
623 bam_error(_("out of memory\n"));
624 return (BAM_ERROR);
626 if (nvlist_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME,
627 entry->me_title) != 0) {
628 bam_error(_("out of memory\n"));
629 nvlist_free(be_attrs);
630 return (BAM_ERROR);
632 ret = be_activate(be_attrs);
633 nvlist_free(be_attrs);
634 if (ret != 0)
635 ret = BAM_ERROR;
636 return (ret);
637 } else if (strcmp(opt, "timeout") == 0) {
638 errno = 0;
639 optval = strtol(val, &rest, 10);
640 if (errno != 0 || *rest != '\0') {
641 bam_error(_("invalid timeout: %s\n"), val);
642 return (BAM_ERROR);
645 (void) snprintf(path, PATH_MAX, "%s" CONF_DIR "/timeout",
646 bam_root);
648 fp = fopen(path, "w");
649 if (fp == NULL) {
650 bam_error(_("failed to open file: %s: %s\n"),
651 path, strerror(errno));
652 return (BAM_ERROR);
655 * timeout=-1 is to disable auto boot in illumos, but
656 * loader needs "NO" to disable auto boot.
658 if (optval == -1)
659 rv = fprintf(fp, "autoboot_delay=\"NO\"\n");
660 else
661 rv = fprintf(fp, "autoboot_delay=\"%d\"\n", optval);
663 if (rv < 0) {
664 bam_error(_("write to file failed: %s: %s\n"),
665 path, strerror(errno));
666 (void) fclose(fp);
667 ret = BAM_ERROR;
668 } else
669 rv = fclose(fp);
671 if (rv < 0) {
672 bam_error(_("failed to close file: %s: %s\n"),
673 path, strerror(errno));
674 ret = BAM_ERROR;
676 if (ret == BAM_ERROR)
677 (void) unlink(path);
679 return (BAM_SUCCESS);
682 bam_error(_("invalid option: %s\n"), opt);
683 return (BAM_ERROR);
686 static int
687 bam_mount_be(menu_entry_t *entry, char **dir)
689 nvlist_t *be_attrs = NULL;
690 const char *tmpdir = getenv("TMPDIR");
691 const char *tmpname = "bam.XXXXXX";
692 be_node_list_t *be_node, *be_nodes = NULL;
693 int ret;
695 *dir = NULL;
696 if (tmpdir == NULL)
697 tmpdir = "/tmp";
699 ret = asprintf(dir, "%s/%s", tmpdir, tmpname);
700 if (ret < 0) {
701 return (BE_ERR_NOMEM);
703 *dir = mkdtemp(*dir);
705 if (nvlist_alloc(&be_attrs, NV_UNIQUE_NAME, 0) != 0) {
706 ret = BE_ERR_NOMEM;
707 goto out;
710 ret = be_list(NULL, &be_nodes, BE_LIST_DEFAULT);
711 if (ret != BE_SUCCESS) {
712 goto out;
715 for (be_node = be_nodes; be_node;
716 be_node = be_node->be_next_node)
717 if (strcmp(be_node->be_root_ds, entry->me_bootfs) == 0)
718 break;
720 if (nvlist_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME,
721 be_node->be_node_name) != 0) {
722 ret = BE_ERR_NOMEM;
723 goto out;
726 if (nvlist_add_string(be_attrs, BE_ATTR_MOUNTPOINT, *dir) != 0) {
727 ret = BE_ERR_NOMEM;
728 goto out;
731 ret = be_mount(be_attrs);
732 if (ret == BE_ERR_MOUNTED) {
734 * if BE is mounted, dir does not point to correct directory
736 (void) rmdir(*dir);
737 free(*dir);
738 *dir = NULL;
740 out:
741 if (be_nodes != NULL)
742 be_free_list(be_nodes);
743 nvlist_free(be_attrs);
744 return (ret);
747 static int
748 bam_umount_be(char *dir)
750 nvlist_t *be_attrs;
751 int ret;
753 if (dir == NULL) /* nothing to do */
754 return (BE_SUCCESS);
756 if (nvlist_alloc(&be_attrs, NV_UNIQUE_NAME, 0) != 0)
757 return (BE_ERR_NOMEM);
759 if (nvlist_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME, dir) != 0) {
760 ret = BE_ERR_NOMEM;
761 goto out;
764 ret = be_unmount(be_attrs);
765 out:
766 nvlist_free(be_attrs);
767 return (ret);
771 * display details of menu entry or single property
773 static error_t
774 list_menu_entry(menu_entry_t *entry, char *setting)
776 int ret = BAM_SUCCESS;
777 char *ptr, *dir;
778 char buf[MAX_INPUT];
779 ficlVm *vm;
780 int mounted;
782 if (strcmp(entry->me_type, "bootfs") != 0 ||
783 strchr(entry->me_bootfs, ':') != NULL) {
784 (void) printf("\nTitle: %s\n", entry->me_title);
785 (void) printf("Type: %s\n", entry->me_type);
786 (void) printf("Device: %s\n", entry->me_bootfs);
787 return (ret);
790 mounted = bam_mount_be(entry, &dir);
791 if (mounted != BE_SUCCESS && mounted != BE_ERR_MOUNTED) {
792 if (dir != NULL) {
793 (void) rmdir(dir);
794 free(dir);
796 bam_error(_("%s is not mounted\n"), entry->me_title);
797 return (BAM_ERROR);
800 vm = bf_init("", ficlTextOutSilent);
801 if (vm == NULL) {
802 bam_error(_("error setting up forth interpreter\n"));
803 ret = BAM_ERROR;
804 goto done;
807 /* should only get FICL_VM_STATUS_OUT_OF_TEXT */
808 (void) snprintf(buf, MAX_INPUT, "set currdev=zfs:%s:",
809 entry->me_bootfs);
810 ret = ficlVmEvaluate(vm, buf);
811 if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
812 bam_error(_("error interpreting boot config\n"));
813 ret = BAM_ERROR;
814 goto done;
816 (void) snprintf(buf, MAX_INPUT, "include /boot/forth/loader.4th");
817 ret = ficlVmEvaluate(vm, buf);
818 if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
819 bam_error(_("error interpreting boot config\n"));
820 ret = BAM_ERROR;
821 goto done;
823 (void) snprintf(buf, MAX_INPUT, "start");
824 ret = ficlVmEvaluate(vm, buf);
825 if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
826 bam_error(_("error interpreting boot config\n"));
827 ret = BAM_ERROR;
828 goto done;
830 (void) snprintf(buf, MAX_INPUT, "boot");
831 ret = ficlVmEvaluate(vm, buf);
832 if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
833 bam_error(_("error interpreting boot config\n"));
834 ret = BAM_ERROR;
835 goto done;
838 ret = BAM_SUCCESS;
839 if (*setting == '\0')
840 (void) printf("\nTitle: %s\n", entry->me_title);
841 else if (strcasecmp(setting, "title") == 0) {
842 (void) printf("%s\n", entry->me_title);
843 goto done;
846 ptr = getenv("autoboot_delay");
847 if (ptr != NULL) {
848 char *timeout = "-1";
850 if (strcasecmp(ptr, "NO") != 0)
851 timeout = ptr;
853 if (*setting == '\0')
854 (void) printf("Timeout: %s\n", timeout);
855 else if (strcasecmp(setting, "timeout") == 0) {
856 (void) printf("%s\n", timeout);
857 goto done;
861 ptr = getenv("console");
862 if (ptr != NULL) {
863 if (*setting == '\0')
864 (void) printf("Console: %s\n", ptr);
865 else if (strcasecmp(setting, "console") == 0) {
866 (void) printf("%s\n", ptr);
867 goto done;
871 if (*setting == '\0')
872 (void) printf("Bootfs: %s\n", entry->me_bootfs);
873 else if (strcasecmp(setting, "bootfs") == 0) {
874 (void) printf("%s\n", entry->me_bootfs);
875 goto done;
878 ptr = getenv("xen_kernel");
879 if (ptr != NULL) {
880 if (*setting == '\0') {
881 (void) printf("Xen kernel: %s\n", ptr);
882 } else if (strcasecmp(setting, "xen_kernel") == 0) {
883 (void) printf("%s\n", ptr);
884 goto done;
887 if (*setting == '\0') {
888 (void) printf("Xen args: \"%s\"\n",
889 getenv("xen_cmdline"));
890 } else if (strcasecmp(setting, "xen_cmdline") == 0) {
891 (void) printf("%s\n", getenv("xen_cmdline"));
892 goto done;
895 if (*setting == '\0') {
896 (void) printf("Kernel: %s\n",
897 getenv("bootfile"));
898 } if (strcasecmp(setting, "kernel") == 0) {
899 (void) printf("%s\n", getenv("bootfile"));
900 goto done;
902 } else {
903 ptr = getenv("kernelname");
904 if (ptr != NULL) {
905 if (*setting == '\0') {
906 (void) printf("Kernel: %s\n", ptr);
907 } else if (strcasecmp(setting, "kernel") == 0) {
908 (void) printf("%s\n", ptr);
909 goto done;
914 ptr = getenv("boot-args");
915 if (ptr != NULL) {
916 if (*setting == '\0') {
917 (void) printf("Boot-args: \"%s\"\n", ptr);
918 } else if (strcasecmp(setting, "boot-args") == 0) {
919 (void) printf("%s\n", ptr);
920 goto done;
924 if (*setting == '\0' || strcasecmp(setting, "modules") == 0) {
925 (void) printf("\nModules:\n");
926 ficlVmSetTextOut(vm, ficlCallbackDefaultTextOut);
927 (void) snprintf(buf, MAX_INPUT, "show-module-options");
928 ret = ficlVmEvaluate(vm, buf);
929 if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
930 bam_error(_("error interpreting boot config\n"));
931 ret = BAM_ERROR;
932 goto done;
934 ret = BAM_SUCCESS;
935 goto done;
938 /* if we got here with setting string, its unknown property */
939 if (*setting != '\0') {
940 bam_error(_("unknown property: %s\n"), setting);
941 ret = BAM_ERROR;
942 } else
943 ret = BAM_SUCCESS;
944 done:
945 bf_fini();
946 if (mounted != BE_ERR_MOUNTED) {
947 (void) bam_umount_be(dir);
950 if (dir != NULL) {
951 (void) rmdir(dir);
952 free(dir);
955 return (ret);
958 /*ARGSUSED*/
959 static error_t
960 list_entry(struct menu_lst *menu, char *menu_root, char *opt)
962 error_t ret = BAM_SUCCESS;
963 menu_entry_t *entry;
964 char *ptr, *title = NULL;
965 int i, e = -1;
967 if (opt == NULL) {
968 print_nodes(B_FALSE, menu);
969 return (ret);
972 if ((ptr = strchr(opt, '=')) == NULL) {
973 bam_error(_("invalid option: %s\n"), opt);
974 return (BAM_ERROR);
977 i = ptr - opt;
978 if (strncmp(opt, "entry", i) == 0) {
979 e = atoi(ptr+1);
980 } else if (strncmp(opt, "title", i) == 0) {
981 title = ptr+1;
982 } else {
983 bam_error(_("invalid option: %s\n"), opt);
984 return (BAM_ERROR);
987 STAILQ_FOREACH(entry, menu, me_next) {
988 if (title != NULL) {
989 if (strcmp(title, entry->me_title) == 0)
990 break;
991 } else if (entry->me_idx == e)
992 break;
995 if (entry == NULL) {
996 bam_error(_("no matching entry found\n"));
997 return (BAM_ERROR);
1000 return (list_menu_entry(entry, ""));
1004 * For now this is just stub entry to support grub interface, the
1005 * known consumer is installer ict.py code, calling as:
1006 * bootadm update-menu -R /a -Z -o rdisk
1007 * Later this can be converted to do something useful.
1009 /*ARGSUSED*/
1010 static error_t
1011 update_entry(struct menu_lst *menu, char *menu_root, char *osdev)
1013 char path[PATH_MAX];
1014 char *pool = menu_root + 1;
1015 be_node_list_t *be_nodes, *be_node;
1016 int rv;
1017 FILE *fp;
1019 (void) snprintf(path, PATH_MAX, "%s%s", menu_root, MENU);
1020 rv = be_list(NULL, &be_nodes, BE_LIST_DEFAULT);
1022 if (rv != BE_SUCCESS)
1023 return (BAM_ERROR);
1025 fp = fopen(path, "w");
1026 if (fp == NULL) {
1027 be_free_list(be_nodes);
1028 return (BAM_ERROR);
1031 for (be_node = be_nodes; be_node; be_node = be_node->be_next_node) {
1032 if (strcmp(be_node->be_rpool, pool) == 0) {
1033 (void) fprintf(fp, "title %s\n", be_node->be_node_name);
1034 (void) fprintf(fp, "bootfs %s\n", be_node->be_root_ds);
1038 be_free_list(be_nodes);
1039 (void) fclose(fp);
1040 return (BAM_SUCCESS);
1043 /*ARGSUSED*/
1044 static error_t
1045 update_temp(struct menu_lst *menu, char *dummy, char *opt)
1047 error_t ret = BAM_ERROR;
1048 char path[PATH_MAX];
1049 char buf[MAX_INPUT];
1050 struct mnttab mpref = { 0 };
1051 struct mnttab mp = { 0 };
1052 ficlVm *vm;
1053 char *env, *o;
1054 FILE *fp;
1056 (void) snprintf(path, PATH_MAX, "%s" TRANSIENT, bam_root);
1058 * if opt == NULL, remove transient config
1060 if (opt == NULL) {
1061 (void) unlink(path);
1062 return (BAM_SUCCESS);
1065 fp = fopen(MNTTAB, "r");
1066 if (fp == NULL)
1067 return (BAM_ERROR);
1069 mpref.mnt_mountp = "/";
1070 if (getmntany(fp, &mp, &mpref) != 0) {
1071 (void) fclose(fp);
1072 return (BAM_ERROR);
1074 (void) fclose(fp);
1076 vm = bf_init("", ficlTextOutSilent);
1077 if (vm == NULL) {
1078 bam_error(_("Error setting up forth interpreter\n"));
1079 return (ret);
1083 * need to check current boot config, so fire up the ficl
1084 * if its xen setup, we add option to boot-args list, not replacing it.
1086 (void) snprintf(buf, MAX_INPUT, "set currdev=zfs:%s:", mp.mnt_special);
1087 ret = ficlVmEvaluate(vm, buf);
1088 if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
1089 bam_error(_("Error interpreting boot config\n"));
1090 bf_fini();
1091 return (BAM_ERROR);
1093 (void) snprintf(buf, MAX_INPUT, "include /boot/forth/loader.4th");
1094 ret = ficlVmEvaluate(vm, buf);
1095 if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
1096 bam_error(_("Error interpreting boot config\n"));
1097 bf_fini();
1098 return (BAM_ERROR);
1100 (void) snprintf(buf, MAX_INPUT, "start");
1101 ret = ficlVmEvaluate(vm, buf);
1102 if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
1103 bam_error(_("Error interpreting boot config\n"));
1104 bf_fini();
1105 return (BAM_ERROR);
1107 (void) snprintf(buf, MAX_INPUT, "boot");
1108 ret = ficlVmEvaluate(vm, buf);
1109 if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
1110 bam_error(_("Error interpreting boot config\n"));
1111 bf_fini();
1112 return (BAM_ERROR);
1114 bf_fini();
1116 if (opt[0] == '-') {
1117 env = getenv("xen_kernel");
1118 fp = fopen(path, "w");
1119 if (fp == NULL)
1120 return (BAM_ERROR);
1122 if (env != NULL) {
1123 env = getenv("boot-args");
1124 (void) fprintf(fp, "boot-args=\"%s %s\"\n", env, opt);
1125 } else
1126 (void) fprintf(fp, "boot-args=\"%s\"\n", opt);
1127 (void) fclose(fp);
1128 return (BAM_SUCCESS);
1132 * it should be the case with "kernel args"
1133 * so, we split the opt at first space
1134 * and store bootfile= and boot-args=
1136 env = getenv("xen_kernel");
1138 o = strchr(opt, ' ');
1139 if (o == NULL) {
1140 fp = fopen(path, "w");
1141 if (fp == NULL)
1142 return (BAM_ERROR);
1143 (void) fprintf(fp, "bootfile=\"%s;unix\"\n", opt);
1144 (void) fclose(fp);
1145 return (BAM_SUCCESS);
1147 *o++ = '\0';
1148 fp = fopen(path, "w");
1149 if (fp == NULL)
1150 return (BAM_ERROR);
1151 (void) fprintf(fp, "bootfile=\"%s;unix\"\n", opt);
1153 if (env != NULL) {
1154 env = getenv("boot-args");
1155 (void) fprintf(fp, "boot-args=\"%s %s\"\n", env, opt);
1156 } else
1157 (void) fprintf(fp, "boot-args=\"%s\"\n", o);
1159 (void) fflush(fp);
1160 (void) fclose(fp);
1161 return (ret);
1164 static error_t
1165 list_setting(struct menu_lst *menu, char *which, char *setting)
1167 int entry = -1;
1168 menu_entry_t *m;
1169 be_node_list_t *be_nodes, *be_node = NULL;
1170 int ret;
1172 assert(which);
1173 assert(setting);
1176 * which can be:
1177 * "" - list default entry
1178 * number - use for entry number
1179 * property name
1181 if (*which != '\0') {
1182 if (isdigit(*which)) {
1183 char *rest;
1184 errno = 0;
1185 entry = strtol(which, &rest, 10);
1186 if (errno != 0 || *rest != '\0') {
1187 bam_error(_("invalid boot entry number: %s\n"),
1188 which);
1189 return (BAM_ERROR);
1191 } else
1192 setting = which;
1195 /* find default entry */
1196 if (entry == -1) {
1197 ret = be_list(NULL, &be_nodes, BE_LIST_DEFAULT);
1198 if (ret != BE_SUCCESS) {
1199 bam_error(_("No BE's found\n"));
1200 return (BAM_ERROR);
1202 STAILQ_FOREACH(m, menu, me_next) {
1203 entry++;
1204 for (be_node = be_nodes; be_node;
1205 be_node = be_node->be_next_node) {
1206 if (strcmp(be_node->be_root_ds,
1207 m->me_bootfs) == 0)
1208 break;
1210 if (be_node != NULL &&
1211 be_node->be_active_on_boot == B_TRUE)
1212 break; /* found active node */
1214 be_free_list(be_nodes);
1215 if (be_node == NULL) {
1216 bam_error(_("None of BE nodes is marked active\n"));
1217 return (BAM_ERROR);
1219 } else {
1220 STAILQ_FOREACH(m, menu, me_next)
1221 if (m->me_idx == entry)
1222 break;
1224 if (m == NULL) {
1225 bam_error(_("no matching entry found\n"));
1226 return (BAM_ERROR);
1230 return (list_menu_entry(m, setting));
1233 /*ARGSUSED*/
1234 static error_t
1235 disable_hyper(struct menu_lst *menu, char *osroot, char *opt)
1237 char path[PATH_MAX];
1239 (void) snprintf(path, PATH_MAX, "%s" XEN_CONFIG, bam_root);
1240 (void) unlink(path);
1241 return (BAM_SUCCESS);
1244 /*ARGSUSED*/
1245 static error_t
1246 enable_hyper(struct menu_lst *menu, char *osroot, char *opt)
1248 ficlVm *vm;
1249 char path[PATH_MAX];
1250 char buf[MAX_INPUT];
1251 char *env;
1252 FILE *fp;
1253 struct mnttab mpref = { 0 };
1254 struct mnttab mp = { 0 };
1255 int ret;
1257 fp = fopen(MNTTAB, "r");
1258 if (fp == NULL)
1259 return (BAM_ERROR);
1261 mpref.mnt_mountp = "/";
1262 if (getmntany(fp, &mp, &mpref) != 0) {
1263 (void) fclose(fp);
1264 return (BAM_ERROR);
1266 (void) fclose(fp);
1268 vm = bf_init("", ficlTextOutSilent);
1269 if (vm == NULL) {
1270 bam_error(_("Error setting up forth interpreter\n"));
1271 return (BAM_ERROR);
1275 * need to check current boot config, so fire up the ficl
1276 * if its xen setup, we add option to boot-args list, not replacing it.
1278 (void) snprintf(buf, MAX_INPUT, "set currdev=zfs:%s:", mp.mnt_special);
1279 ret = ficlVmEvaluate(vm, buf);
1280 if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
1281 bam_error(_("Error interpreting boot config\n"));
1282 bf_fini();
1283 return (BAM_ERROR);
1285 (void) snprintf(buf, MAX_INPUT, "include /boot/forth/loader.4th");
1286 ret = ficlVmEvaluate(vm, buf);
1287 if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
1288 bam_error(_("Error interpreting boot config\n"));
1289 bf_fini();
1290 return (BAM_ERROR);
1292 (void) snprintf(buf, MAX_INPUT, "start");
1293 ret = ficlVmEvaluate(vm, buf);
1294 if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
1295 bam_error(_("Error interpreting boot config\n"));
1296 bf_fini();
1297 return (BAM_ERROR);
1299 (void) snprintf(buf, MAX_INPUT, "boot");
1300 ret = ficlVmEvaluate(vm, buf);
1301 if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
1302 bam_error(_("Error interpreting boot config\n"));
1303 bf_fini();
1304 return (BAM_ERROR);
1306 bf_fini();
1308 (void) mkdir(CONF_DIR, 0755);
1309 (void) snprintf(path, PATH_MAX, "%s" XEN_CONFIG, bam_root);
1310 fp = fopen(path, "w");
1311 if (fp == NULL) {
1312 return (BAM_ERROR); /* error, cant write config */
1315 errno = 0;
1317 * on write error, remove file to ensure we have bootable config.
1318 * note we dont mind if config exists, it will get updated
1320 (void) fprintf(fp, "xen_kernel=\"/boot/${ISADIR}/xen\"\n");
1321 if (errno != 0)
1322 goto error;
1325 * really simple and stupid console conversion.
1326 * it really has to be gone, it belongs to milestone/xvm properties.
1328 env = getenv("console");
1329 if (env != NULL) {
1330 if (strcmp(env, "ttya") == 0)
1331 (void) fprintf(fp, "xen_cmdline=\"console=com1 %s\"\n",
1332 opt);
1333 else if (strcmp(env, "ttyb") == 0)
1334 (void) fprintf(fp, "xen_cmdline=\"console=com2 %s\"\n",
1335 opt);
1336 else
1337 (void) fprintf(fp, "xen_cmdline=\"console=vga %s\"\n",
1338 opt);
1339 } else
1340 (void) fprintf(fp, "xen_cmdline=\"%s\"\n", opt);
1341 if (errno != 0)
1342 goto error;
1344 (void) fprintf(fp,
1345 "bootfile=\"/platform/i86xpv/kernel/${ISADIR}/unix\"\n");
1346 if (errno != 0)
1347 goto error;
1349 (void) fprintf(fp,
1350 "boot-args=\"/platform/i86xpv/kernel/${ISADIR}/unix\"\n");
1351 if (errno != 0)
1352 goto error;
1354 (void) fclose(fp);
1355 if (errno != 0) {
1356 (void) unlink(path);
1357 return (BAM_ERROR);
1359 return (BAM_SUCCESS);
1360 error:
1361 (void) fclose(fp);
1362 (void) unlink(path);
1363 return (BAM_ERROR);