bootadm.c (348:5841de0daf2d) bootadm.c (621:37445ada73e4)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *

--- 121 unchanged lines hidden (view full) ---

130#define ALL_ENTRIES -2 /* selects all boot entries */
131
132#define GRUB_DIR "/boot/grub"
133#define MULTI_BOOT "/platform/i86pc/multiboot"
134#define BOOT_ARCHIVE "/platform/i86pc/boot_archive"
135#define GRUB_MENU "/boot/grub/menu.lst"
136#define MENU_TMP "/boot/grub/menu.lst.tmp"
137#define RAMDISK_SPECIAL "/ramdisk"
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *

--- 121 unchanged lines hidden (view full) ---

130#define ALL_ENTRIES -2 /* selects all boot entries */
131
132#define GRUB_DIR "/boot/grub"
133#define MULTI_BOOT "/platform/i86pc/multiboot"
134#define BOOT_ARCHIVE "/platform/i86pc/boot_archive"
135#define GRUB_MENU "/boot/grub/menu.lst"
136#define MENU_TMP "/boot/grub/menu.lst.tmp"
137#define RAMDISK_SPECIAL "/ramdisk"
138#define STUBBOOT "/stubboot"
138
139/* lock related */
140#define BAM_LOCK_FILE "/var/run/bootadm.lock"
141#define LOCK_FILE_PERMS (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
142
143#define CREATE_RAMDISK "/boot/solaris/bin/create_ramdisk"
144#define CREATE_DISKMAP "/boot/solaris/bin/create_diskmap"
145#define GRUBDISK_MAP "/var/run/solaris_grubdisk.map"
146
147#define GRUB_slice "/etc/lu/GRUB_slice"
148#define GRUB_root "/etc/lu/GRUB_root"
149#define GRUB_backup_menu "/etc/lu/GRUB_backup_menu"
150#define GRUB_slice_mntpt "/tmp/GRUB_slice_mntpt"
151#define LU_ACTIVATE_FILE "/etc/lu/DelayUpdate/activate.sh"
152
153#define INSTALLGRUB "/sbin/installgrub"
154#define STAGE1 "/boot/grub/stage1"
155#define STAGE2 "/boot/grub/stage2"
156
139
140/* lock related */
141#define BAM_LOCK_FILE "/var/run/bootadm.lock"
142#define LOCK_FILE_PERMS (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
143
144#define CREATE_RAMDISK "/boot/solaris/bin/create_ramdisk"
145#define CREATE_DISKMAP "/boot/solaris/bin/create_diskmap"
146#define GRUBDISK_MAP "/var/run/solaris_grubdisk.map"
147
148#define GRUB_slice "/etc/lu/GRUB_slice"
149#define GRUB_root "/etc/lu/GRUB_root"
150#define GRUB_backup_menu "/etc/lu/GRUB_backup_menu"
151#define GRUB_slice_mntpt "/tmp/GRUB_slice_mntpt"
152#define LU_ACTIVATE_FILE "/etc/lu/DelayUpdate/activate.sh"
153
154#define INSTALLGRUB "/sbin/installgrub"
155#define STAGE1 "/boot/grub/stage1"
156#define STAGE2 "/boot/grub/stage2"
157
157#define QUIET 1
158
159
160/*
161 * Default file attributes
162 */
163#define DEFAULT_DEV_MODE 0644 /* default permissions */
164#define DEFAULT_DEV_UID 0 /* user root */
165#define DEFAULT_DEV_GID 3 /* group sys */
166
167/*

--- 46 unchanged lines hidden (view full) ---

214
215
216/* Globals */
217static char *prog;
218static subcmd_t bam_cmd;
219static char *bam_root;
220static int bam_rootlen;
221static int bam_root_readonly;
158/*
159 * Default file attributes
160 */
161#define DEFAULT_DEV_MODE 0644 /* default permissions */
162#define DEFAULT_DEV_UID 0 /* user root */
163#define DEFAULT_DEV_GID 3 /* group sys */
164
165/*

--- 46 unchanged lines hidden (view full) ---

212
213
214/* Globals */
215static char *prog;
216static subcmd_t bam_cmd;
217static char *bam_root;
218static int bam_rootlen;
219static int bam_root_readonly;
220static int bam_alt_root;
222static char *bam_subcmd;
223static char *bam_opt;
224static int bam_debug;
225static char **bam_argv;
226static int bam_argc;
227static int bam_force;
228static int bam_verbose;
229static int bam_check;

--- 302 unchanged lines hidden (view full) ---

532 bam_error(DUP_OPT, c);
533 break;
534 } else if (realpath(optarg, rootbuf) == NULL) {
535 error = 1;
536 bam_error(CANT_RESOLVE, optarg,
537 strerror(errno));
538 break;
539 }
221static char *bam_subcmd;
222static char *bam_opt;
223static int bam_debug;
224static char **bam_argv;
225static int bam_argc;
226static int bam_force;
227static int bam_verbose;
228static int bam_check;

--- 302 unchanged lines hidden (view full) ---

531 bam_error(DUP_OPT, c);
532 break;
533 } else if (realpath(optarg, rootbuf) == NULL) {
534 error = 1;
535 bam_error(CANT_RESOLVE, optarg,
536 strerror(errno));
537 break;
538 }
539 bam_alt_root = 1;
540 bam_root = rootbuf;
541 if (rootbuf[strlen(rootbuf) - 1] != '/')
542 (void) strcat(rootbuf, "/");
543 bam_rootlen = strlen(rootbuf);
544 break;
545 case '?':
546 error = 1;
547 bam_error(BAD_OPT, optopt);

--- 80 unchanged lines hidden (view full) ---

628
629 *fp = table[i].handler;
630
631 return (BAM_SUCCESS);
632}
633
634
635static char *
540 bam_root = rootbuf;
541 if (rootbuf[strlen(rootbuf) - 1] != '/')
542 (void) strcat(rootbuf, "/");
543 bam_rootlen = strlen(rootbuf);
544 break;
545 case '?':
546 error = 1;
547 bam_error(BAD_OPT, optopt);

--- 80 unchanged lines hidden (view full) ---

628
629 *fp = table[i].handler;
630
631 return (BAM_SUCCESS);
632}
633
634
635static char *
636mount_grub_slice(int *mnted, char **physlice, int mode)
636mount_grub_slice(int *mnted, char **physlice, char **logslice, char **fs_type)
637{
638 struct extmnttab mnt;
639 struct stat sb;
640 char buf[BAM_MAXLINE], dev[PATH_MAX], phys[PATH_MAX], fstype[32];
641 char cmd[PATH_MAX];
642 char *mntpt;
643 int p, l, f;
644 FILE *fp;
645
646 assert(mnted);
647 *mnted = 0;
648
649 /*
637{
638 struct extmnttab mnt;
639 struct stat sb;
640 char buf[BAM_MAXLINE], dev[PATH_MAX], phys[PATH_MAX], fstype[32];
641 char cmd[PATH_MAX];
642 char *mntpt;
643 int p, l, f;
644 FILE *fp;
645
646 assert(mnted);
647 *mnted = 0;
648
649 /*
650 * physlice arg may be NULL
650 * physlice, logslice, fs_type args may be NULL
651 */
652 if (physlice)
653 *physlice = NULL;
651 */
652 if (physlice)
653 *physlice = NULL;
654 if (logslice)
655 *logslice = NULL;
656 if (fs_type)
657 *fs_type = NULL;
654
655 if (stat(GRUB_slice, &sb) != 0) {
656 bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno));
657 return (NULL);
658 }
659
660 fp = fopen(GRUB_slice, "r");
661 if (fp == NULL) {

--- 21 unchanged lines hidden (view full) ---

683 }
684 (void) fclose(fp);
685
686 if (dev[0] == '\0' || fstype[0] == '\0' || phys[0] == '\0') {
687 bam_error(BAD_SLICE_FILE, GRUB_slice);
688 return (NULL);
689 }
690
658
659 if (stat(GRUB_slice, &sb) != 0) {
660 bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno));
661 return (NULL);
662 }
663
664 fp = fopen(GRUB_slice, "r");
665 if (fp == NULL) {

--- 21 unchanged lines hidden (view full) ---

687 }
688 (void) fclose(fp);
689
690 if (dev[0] == '\0' || fstype[0] == '\0' || phys[0] == '\0') {
691 bam_error(BAD_SLICE_FILE, GRUB_slice);
692 return (NULL);
693 }
694
691 if (mode != QUIET)
692 bam_print(USING_GRUB_SLICE, dev);
693
694 if (physlice) {
695 *physlice = s_strdup(phys);
696 }
695 if (physlice) {
696 *physlice = s_strdup(phys);
697 }
698 if (logslice) {
699 *logslice = s_strdup(dev);
700 }
701 if (fs_type) {
702 *fs_type = s_strdup(fstype);
703 }
697
698 /*
699 * Check if the slice is already mounted
700 */
701 fp = fopen(MNTTAB, "r");
702 if (fp == NULL) {
703 bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
704
705 /*
706 * Check if the slice is already mounted
707 */
708 fp = fopen(MNTTAB, "r");
709 if (fp == NULL) {
710 bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
704 if (physlice) {
705 free(*physlice);
706 *physlice = NULL;
707 }
708 return (NULL);
711 goto error;
709 }
710
711 resetmnttab(fp);
712
713 mntpt = NULL;
714 while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
715 if (strcmp(mnt.mnt_special, dev) == 0) {
716 mntpt = s_strdup(mnt.mnt_mountp);

--- 12 unchanged lines hidden (view full) ---

729 * GRUB slice is not mounted, we need to mount it now.
730 * First create the mountpoint
731 */
732 mntpt = s_calloc(1, PATH_MAX);
733 (void) snprintf(mntpt, PATH_MAX, "%s.%d", GRUB_slice_mntpt, getpid());
734 if (mkdir(mntpt, 0755) == -1 && errno != EEXIST) {
735 bam_error(MKDIR_FAILED, mntpt, strerror(errno));
736 free(mntpt);
712 }
713
714 resetmnttab(fp);
715
716 mntpt = NULL;
717 while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
718 if (strcmp(mnt.mnt_special, dev) == 0) {
719 mntpt = s_strdup(mnt.mnt_mountp);

--- 12 unchanged lines hidden (view full) ---

732 * GRUB slice is not mounted, we need to mount it now.
733 * First create the mountpoint
734 */
735 mntpt = s_calloc(1, PATH_MAX);
736 (void) snprintf(mntpt, PATH_MAX, "%s.%d", GRUB_slice_mntpt, getpid());
737 if (mkdir(mntpt, 0755) == -1 && errno != EEXIST) {
738 bam_error(MKDIR_FAILED, mntpt, strerror(errno));
739 free(mntpt);
737 if (physlice) {
738 free(*physlice);
739 *physlice = NULL;
740 }
741 return (NULL);
740 goto error;
742 }
743
744 (void) snprintf(cmd, sizeof (cmd), "/sbin/mount -F %s %s %s",
745 fstype, dev, mntpt);
746
747 if (exec_cmd(cmd, NULL, 0) != 0) {
741 }
742
743 (void) snprintf(cmd, sizeof (cmd), "/sbin/mount -F %s %s %s",
744 fstype, dev, mntpt);
745
746 if (exec_cmd(cmd, NULL, 0) != 0) {
748 bam_error(MOUNT_FAILED, dev, fstype, mntpt);
747 bam_error(MOUNT_FAILED, dev, fstype);
749 if (rmdir(mntpt) != 0) {
750 bam_error(RMDIR_FAILED, mntpt, strerror(errno));
751 }
752 free(mntpt);
748 if (rmdir(mntpt) != 0) {
749 bam_error(RMDIR_FAILED, mntpt, strerror(errno));
750 }
751 free(mntpt);
753 if (physlice) {
754 free(*physlice);
755 *physlice = NULL;
756 }
757 return (NULL);
752 goto error;
758 }
759
760 *mnted = 1;
761 return (mntpt);
753 }
754
755 *mnted = 1;
756 return (mntpt);
757
758error:
759 if (physlice) {
760 free(*physlice);
761 *physlice = NULL;
762 }
763 if (logslice) {
764 free(*logslice);
765 *logslice = NULL;
766 }
767 if (fs_type) {
768 free(*fs_type);
769 *fs_type = NULL;
770 }
771 return (NULL);
762}
763
764static void
772}
773
774static void
765umount_grub_slice(int mnted, char *mntpt, char *physlice)
775umount_grub_slice(
776 int mnted,
777 char *mntpt,
778 char *physlice,
779 char *logslice,
780 char *fs_type)
766{
767 char cmd[PATH_MAX];
768
769 /*
770 * If we have not dealt with GRUB slice
771 * we have nothing to do - just return.
772 */
773 if (mntpt == NULL)

--- 9 unchanged lines hidden (view full) ---

783 mntpt);
784 if (exec_cmd(cmd, NULL, 0) != 0) {
785 bam_error(UMOUNT_FAILED, mntpt);
786 }
787 if (rmdir(mntpt) != 0) {
788 bam_error(RMDIR_FAILED, mntpt, strerror(errno));
789 }
790 }
781{
782 char cmd[PATH_MAX];
783
784 /*
785 * If we have not dealt with GRUB slice
786 * we have nothing to do - just return.
787 */
788 if (mntpt == NULL)

--- 9 unchanged lines hidden (view full) ---

798 mntpt);
799 if (exec_cmd(cmd, NULL, 0) != 0) {
800 bam_error(UMOUNT_FAILED, mntpt);
801 }
802 if (rmdir(mntpt) != 0) {
803 bam_error(RMDIR_FAILED, mntpt, strerror(errno));
804 }
805 }
806
791 if (physlice)
792 free(physlice);
807 if (physlice)
808 free(physlice);
809 if (logslice)
810 free(logslice);
811 if (fs_type)
812 free(fs_type);
813
793 free(mntpt);
794}
795
814 free(mntpt);
815}
816
817static char *
818use_stubboot(void)
819{
820 int mnted;
821 struct stat sb;
822 struct extmnttab mnt;
823 FILE *fp;
824 char cmd[PATH_MAX];
825
826 if (stat(STUBBOOT, &sb) != 0) {
827 bam_error(STUBBOOT_DIR_NOT_FOUND);
828 return (NULL);
829 }
830
831 /*
832 * Check if stubboot is mounted. If not, mount it
833 */
834 fp = fopen(MNTTAB, "r");
835 if (fp == NULL) {
836 bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
837 return (NULL);
838 }
839
840 resetmnttab(fp);
841
842 mnted = 0;
843 while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
844 if (strcmp(mnt.mnt_mountp, STUBBOOT) == 0) {
845 mnted = 1;
846 break;
847 }
848 }
849
850 (void) fclose(fp);
851
852 if (mnted)
853 return (STUBBOOT);
854
855 /*
856 * Stubboot is not mounted, mount it now.
857 * It should exist in /etc/vfstab
858 */
859 (void) snprintf(cmd, sizeof (cmd), "/sbin/mount %s",
860 STUBBOOT);
861 if (exec_cmd(cmd, NULL, 0) != 0) {
862 bam_error(MOUNT_MNTPT_FAILED, STUBBOOT);
863 return (NULL);
864 }
865
866 return (STUBBOOT);
867}
868
869static void
870disp_active_menu_locn(char *menu_path, char *logslice, char *fstype, int mnted)
871{
872 /*
873 * Check if we did a temp mount of an unmounted device.
874 * If yes, print the block device and fstype for that device
875 * else it is already mounted, so we print the path to the GRUB menu.
876 */
877 if (mnted) {
878 bam_print(GRUB_MENU_DEVICE, logslice);
879 bam_print(GRUB_MENU_FSTYPE, fstype);
880 } else {
881 bam_print(GRUB_MENU_PATH, menu_path);
882 }
883}
884
885/*
886 * NOTE: A single "/" is also considered a trailing slash and will
887 * be deleted.
888 */
889static void
890elide_trailing_slash(const char *src, char *dst, size_t dstsize)
891{
892 size_t dstlen;
893
894 assert(src);
895 assert(dst);
896
897 (void) strlcpy(dst, src, dstsize);
898
899 dstlen = strlen(dst);
900 if (dst[dstlen - 1] == '/') {
901 dst[dstlen - 1] = '\0';
902 }
903}
904
796static error_t
797bam_menu(char *subcmd, char *opt, int largc, char *largv[])
798{
799 error_t ret;
800 char menu_path[PATH_MAX];
801 menu_t *menu;
905static error_t
906bam_menu(char *subcmd, char *opt, int largc, char *largv[])
907{
908 error_t ret;
909 char menu_path[PATH_MAX];
910 menu_t *menu;
802 char *mntpt, *menu_root;
911 char *mntpt, *menu_root, *logslice, *fstype;
803 struct stat sb;
804 int mnted; /* set if we did a mount */
912 struct stat sb;
913 int mnted; /* set if we did a mount */
805 int mode;
806 error_t (*f)(menu_t *mp, char *menu_path, char *opt);
807
808 /*
809 * Check arguments
810 */
811 ret = check_subcmd_and_options(subcmd, opt, menu_subcmds, &f);
812 if (ret == BAM_ERROR) {
813 return (BAM_ERROR);
814 }
815
914 error_t (*f)(menu_t *mp, char *menu_path, char *opt);
915
916 /*
917 * Check arguments
918 */
919 ret = check_subcmd_and_options(subcmd, opt, menu_subcmds, &f);
920 if (ret == BAM_ERROR) {
921 return (BAM_ERROR);
922 }
923
816 if (strcmp(subcmd, "update_temp") == 0)
817 mode = QUIET;
818 else
819 mode = 0;
820
821 mntpt = NULL;
822 mnted = 0;
924 mntpt = NULL;
925 mnted = 0;
823 if (stat(GRUB_slice, &sb) == 0) {
824 mntpt = mount_grub_slice(&mnted, NULL, mode);
825 if (mntpt == NULL) {
826 return (BAM_ERROR);
827 }
926 logslice = fstype = NULL;
927
928 /*
929 * If the user provides an alternate root, we
930 * assume they know what they are doing and we
931 * use it. Else we check if there is an
932 * alternate location (other than /boot/grub)
933 * for the GRUB menu
934 */
935 if (bam_alt_root) {
936 menu_root = bam_root;
937 } else if (stat(GRUB_slice, &sb) == 0) {
938 mntpt = mount_grub_slice(&mnted, NULL, &logslice, &fstype);
828 menu_root = mntpt;
939 menu_root = mntpt;
940 } else if (stat(STUBBOOT, &sb) == 0) {
941 menu_root = use_stubboot();
829 } else {
830 menu_root = bam_root;
831 }
832
942 } else {
943 menu_root = bam_root;
944 }
945
833 (void) snprintf(menu_path, sizeof (menu_path), "%s%s",
834 menu_root, GRUB_MENU);
946 if (menu_root == NULL) {
947 bam_error(CANNOT_LOCATE_GRUB_MENU);
948 return (BAM_ERROR);
949 }
835
950
951 elide_trailing_slash(menu_root, menu_path, sizeof (menu_path));
952 (void) strlcat(menu_path, GRUB_MENU, sizeof (menu_path));
953
954 /*
955 * If listing the menu, display the active menu
956 * location
957 */
958 if (strcmp(subcmd, "list_entry") == 0) {
959 disp_active_menu_locn(menu_path, logslice, fstype, mnted);
960 }
961
836 menu = menu_read(menu_path);
837 assert(menu);
838
839 /*
840 * Special handling for setting timeout and default
841 */
842 if (strcmp(subcmd, "set_option") == 0) {
843 if (largc != 1 || largv[0] == NULL) {
844 usage();
962 menu = menu_read(menu_path);
963 assert(menu);
964
965 /*
966 * Special handling for setting timeout and default
967 */
968 if (strcmp(subcmd, "set_option") == 0) {
969 if (largc != 1 || largv[0] == NULL) {
970 usage();
845 umount_grub_slice(mnted, mntpt, NULL);
971 menu_free(menu);
972 umount_grub_slice(mnted, mntpt, NULL, logslice, fstype);
846 return (BAM_ERROR);
847 }
848 opt = largv[0];
849 } else if (largc != 0) {
850 usage();
973 return (BAM_ERROR);
974 }
975 opt = largv[0];
976 } else if (largc != 0) {
977 usage();
851 umount_grub_slice(mnted, mntpt, NULL);
978 menu_free(menu);
979 umount_grub_slice(mnted, mntpt, NULL, logslice, fstype);
852 return (BAM_ERROR);
853 }
854
855 /*
856 * Once the sub-cmd handler has run
857 * only the line field is guaranteed to have valid values
858 */
859 if (strcmp(subcmd, "update_entry") == 0)
860 ret = f(menu, bam_root, opt);
861 else
862 ret = f(menu, menu_path, opt);
863 if (ret == BAM_WRITE) {
864 ret = menu_write(menu_root, menu);
865 }
866
867 menu_free(menu);
868
980 return (BAM_ERROR);
981 }
982
983 /*
984 * Once the sub-cmd handler has run
985 * only the line field is guaranteed to have valid values
986 */
987 if (strcmp(subcmd, "update_entry") == 0)
988 ret = f(menu, bam_root, opt);
989 else
990 ret = f(menu, menu_path, opt);
991 if (ret == BAM_WRITE) {
992 ret = menu_write(menu_root, menu);
993 }
994
995 menu_free(menu);
996
869 umount_grub_slice(mnted, mntpt, NULL);
997 umount_grub_slice(mnted, mntpt, NULL, logslice, fstype);
870
871 return (ret);
872}
873
874
875static error_t
876bam_archive(
877 char *subcmd,

--- 969 unchanged lines hidden (view full) ---

1847 * handle GRUB/DCA installation
1848 */
1849 if (stat(LU_ACTIVATE_FILE, &sb) == 0) {
1850 return;
1851 }
1852
1853 mnted = 0;
1854 physlice = NULL;
998
999 return (ret);
1000}
1001
1002
1003static error_t
1004bam_archive(
1005 char *subcmd,

--- 969 unchanged lines hidden (view full) ---

1975 * handle GRUB/DCA installation
1976 */
1977 if (stat(LU_ACTIVATE_FILE, &sb) == 0) {
1978 return;
1979 }
1980
1981 mnted = 0;
1982 physlice = NULL;
1855 mntpt = mount_grub_slice(&mnted, &physlice, QUIET);
1983 mntpt = mount_grub_slice(&mnted, &physlice, NULL, NULL);
1856 if (mntpt == NULL) {
1857 bam_error(CANNOT_RESTORE_GRUB_SLICE);
1858 return;
1859 }
1860
1861 (void) snprintf(menupath, sizeof (menupath), "%s%s", mntpt, GRUB_MENU);
1862 if (stat(menupath, &sb) == 0) {
1984 if (mntpt == NULL) {
1985 bam_error(CANNOT_RESTORE_GRUB_SLICE);
1986 return;
1987 }
1988
1989 (void) snprintf(menupath, sizeof (menupath), "%s%s", mntpt, GRUB_MENU);
1990 if (stat(menupath, &sb) == 0) {
1863 umount_grub_slice(mnted, mntpt, physlice);
1991 umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
1864 return;
1865 }
1866
1867 /*
1868 * The menu is missing - we need to do a restore
1869 */
1870 bam_print(RESTORING_GRUB);
1871
1872 (void) snprintf(cmd, sizeof (cmd), "%s %s %s %s",
1873 INSTALLGRUB, STAGE1, STAGE2, physlice);
1874
1875 if (exec_cmd(cmd, NULL, 0) != 0) {
1876 bam_error(RESTORE_GRUB_FAILED);
1992 return;
1993 }
1994
1995 /*
1996 * The menu is missing - we need to do a restore
1997 */
1998 bam_print(RESTORING_GRUB);
1999
2000 (void) snprintf(cmd, sizeof (cmd), "%s %s %s %s",
2001 INSTALLGRUB, STAGE1, STAGE2, physlice);
2002
2003 if (exec_cmd(cmd, NULL, 0) != 0) {
2004 bam_error(RESTORE_GRUB_FAILED);
1877 umount_grub_slice(mnted, mntpt, physlice);
2005 umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
1878 return;
1879 }
1880
1881 if (stat(GRUB_backup_menu, &sb) != 0) {
1882 bam_error(MISSING_BACKUP_MENU,
1883 GRUB_backup_menu, strerror(errno));
2006 return;
2007 }
2008
2009 if (stat(GRUB_backup_menu, &sb) != 0) {
2010 bam_error(MISSING_BACKUP_MENU,
2011 GRUB_backup_menu, strerror(errno));
1884 umount_grub_slice(mnted, mntpt, physlice);
2012 umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
1885 return;
1886 }
1887
1888 (void) snprintf(cmd, sizeof (cmd), "/bin/cp %s %s",
1889 GRUB_backup_menu, menupath);
1890
1891 if (exec_cmd(cmd, NULL, 0) != 0) {
1892 bam_error(RESTORE_MENU_FAILED, menupath);
2013 return;
2014 }
2015
2016 (void) snprintf(cmd, sizeof (cmd), "/bin/cp %s %s",
2017 GRUB_backup_menu, menupath);
2018
2019 if (exec_cmd(cmd, NULL, 0) != 0) {
2020 bam_error(RESTORE_MENU_FAILED, menupath);
1893 umount_grub_slice(mnted, mntpt, physlice);
2021 umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
1894 return;
1895 }
1896
1897 /* Success */
2022 return;
2023 }
2024
2025 /* Success */
1898 umount_grub_slice(mnted, mntpt, physlice);
2026 umount_grub_slice(mnted, mntpt, physlice, NULL, NULL);
1899}
1900
1901static error_t
1902update_all(char *root, char *opt)
1903{
1904 struct extmnttab mnt;
1905 struct stat sb;
1906 FILE *fp;
1907 char multibt[PATH_MAX];
1908 error_t ret = BAM_SUCCESS;
1909
2027}
2028
2029static error_t
2030update_all(char *root, char *opt)
2031{
2032 struct extmnttab mnt;
2033 struct stat sb;
2034 FILE *fp;
2035 char multibt[PATH_MAX];
2036 error_t ret = BAM_SUCCESS;
2037
1910 assert(bam_rootlen == 1 && root[0] == '/');
2038 assert(root);
1911 assert(opt == NULL);
1912
2039 assert(opt == NULL);
2040
2041 if (bam_rootlen != 1 || *root != '/') {
2042 elide_trailing_slash(root, multibt, sizeof (multibt));
2043 bam_error(ALT_ROOT_INVALID, multibt);
2044 return (BAM_ERROR);
2045 }
2046
1913 /*
1914 * First update archive for current root
1915 */
1916 if (update_archive(root, opt) != BAM_SUCCESS)
1917 ret = BAM_ERROR;
1918
1919 /*
1920 * Now walk the mount table, performing archive update

--- 158 unchanged lines hidden (view full) ---

2079 lp->flags = (*entryNum == ENTRY_INIT) ? BAM_GLOBAL : BAM_ENTRY;
2080 }
2081
2082 append_line(mp, lp);
2083
2084 prev = lp;
2085}
2086
2047 /*
2048 * First update archive for current root
2049 */
2050 if (update_archive(root, opt) != BAM_SUCCESS)
2051 ret = BAM_ERROR;
2052
2053 /*
2054 * Now walk the mount table, performing archive update

--- 158 unchanged lines hidden (view full) ---

2213 lp->flags = (*entryNum == ENTRY_INIT) ? BAM_GLOBAL : BAM_ENTRY;
2214 }
2215
2216 append_line(mp, lp);
2217
2218 prev = lp;
2219}
2220
2221static void
2222update_numbering(menu_t *mp)
2223{
2224 int lineNum;
2225 int entryNum;
2226 int old_default_value;
2227 line_t *lp, *prev, *default_lp, *default_entry;
2228 char buf[PATH_MAX];
2229
2230 if (mp->start == NULL) {
2231 return;
2232 }
2233
2234 lineNum = LINE_INIT;
2235 entryNum = ENTRY_INIT;
2236 old_default_value = ENTRY_INIT;
2237 lp = default_lp = default_entry = NULL;
2238
2239 prev = NULL;
2240 for (lp = mp->start; lp; prev = lp, lp = lp->next) {
2241 lp->lineNum = ++lineNum;
2242
2243 /*
2244 * Get the value of the default command
2245 */
2246 if (lp->entryNum == ENTRY_INIT && lp->cmd &&
2247 strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0 &&
2248 lp->arg) {
2249 old_default_value = atoi(lp->arg);
2250 default_lp = lp;
2251 }
2252
2253 /*
2254 * If not boot entry, nothing else to fix for this
2255 * entry
2256 */
2257 if (lp->entryNum == ENTRY_INIT)
2258 continue;
2259
2260 /*
2261 * Record the position of the default entry.
2262 * The following works because global
2263 * commands like default and timeout should precede
2264 * actual boot entries, so old_default_value
2265 * is already known (or default cmd is missing).
2266 */
2267 if (default_entry == NULL &&
2268 old_default_value != ENTRY_INIT &&
2269 lp->entryNum == old_default_value) {
2270 default_entry = lp;
2271 }
2272
2273 /*
2274 * Now fixup the entry number
2275 */
2276 if (lp->cmd && strcmp(lp->cmd, menu_cmds[TITLE_CMD]) == 0) {
2277 lp->entryNum = ++entryNum;
2278 /* fixup the bootadm header */
2279 if (prev && prev->flags == BAM_COMMENT &&
2280 prev->arg && strcmp(prev->arg, BAM_HDR) == 0) {
2281 prev->entryNum = lp->entryNum;
2282 }
2283 } else {
2284 lp->entryNum = entryNum;
2285 }
2286 }
2287
2288 /*
2289 * No default command in menu, simply return
2290 */
2291 if (default_lp == NULL) {
2292 return;
2293 }
2294
2295 free(default_lp->arg);
2296 free(default_lp->line);
2297
2298 if (default_entry == NULL) {
2299 default_lp->arg = s_strdup("0");
2300 } else {
2301 (void) snprintf(buf, sizeof (buf), "%d",
2302 default_entry->entryNum);
2303 default_lp->arg = s_strdup(buf);
2304 }
2305
2306 /*
2307 * The following is required since only the line field gets
2308 * written back to menu.lst
2309 */
2310 (void) snprintf(buf, sizeof (buf), "%s%s%s",
2311 menu_cmds[DEFAULT_CMD], menu_cmds[SEP_CMD], default_lp->arg);
2312 default_lp->line = s_strdup(buf);
2313}
2314
2315
2087static menu_t *
2088menu_read(char *menu_path)
2089{
2090 FILE *fp;
2091 char buf[BAM_MAXLINE], *cp;
2092 menu_t *mp;
2093 int line, entry, len, n;
2094

--- 322 unchanged lines hidden (view full) ---

2417 deleted = 1;
2418 }
2419
2420 if (!deleted && entryNum != ALL_ENTRIES) {
2421 bam_error(NO_BOOTADM_MATCH);
2422 return (BAM_ERROR);
2423 }
2424
2316static menu_t *
2317menu_read(char *menu_path)
2318{
2319 FILE *fp;
2320 char buf[BAM_MAXLINE], *cp;
2321 menu_t *mp;
2322 int line, entry, len, n;
2323

--- 322 unchanged lines hidden (view full) ---

2646 deleted = 1;
2647 }
2648
2649 if (!deleted && entryNum != ALL_ENTRIES) {
2650 bam_error(NO_BOOTADM_MATCH);
2651 return (BAM_ERROR);
2652 }
2653
2654 /*
2655 * Now that we have deleted an entry, update
2656 * the entry numbering and the default cmd.
2657 */
2658 update_numbering(mp);
2659
2425 return (BAM_SUCCESS);
2426}
2427
2428static error_t
2429delete_entry(menu_t *mp, char *menu_path, char *opt)
2430{
2431 int entry = ENTRY_INIT;
2432 char *title = NULL;

--- 909 unchanged lines hidden ---
2660 return (BAM_SUCCESS);
2661}
2662
2663static error_t
2664delete_entry(menu_t *mp, char *menu_path, char *opt)
2665{
2666 int entry = ENTRY_INIT;
2667 char *title = NULL;

--- 909 unchanged lines hidden ---