libgrub_fs.c revision 753a6d457b330b1b29b2d3eefcd0831116ce950d
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * CDDL HEADER START
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * The contents of this file are subject to the terms of the
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Common Development and Distribution License (the "License").
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * You may not use this file except in compliance with the License.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * See the License for the specific language governing permissions
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * and limitations under the License.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * When distributing Covered Code, include this CDDL HEADER in each
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * If applicable, add the following below this CDDL HEADER, with the
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * fields enclosed by brackets "[]" replaced with your own identifying
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * information: Portions Copyright [yyyy] [name of copyright owner]
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * CDDL HEADER END
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Use is subject to license terms.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * This file contains all the functions that manipualte the file
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * system where the GRUB menu resides.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore const char *pos;
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore return ((pos = strrchr(physpath, slice)) == NULL ||
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Returns zero if path contains ufs
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore const char *id;
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if ((ret = fstyp_init(fd, 0, NULL, &hdl)) == 0) {
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore (void) snprintf(rdev, sizeof (rdev), "/devices%s,raw", physpath);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore /* parse fdisk table */
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore for (i = 0; i < FD_NUMPART; ++i) {
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if (ipart[i].systid == SUNIXOS || ipart[i].systid == SUNIXOS2)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Get physpath, topfs and bootfs for ZFS root dataset.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Return 0 on success, non-zero (not errno) on failure.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Mooreget_zfs_root(zfs_handle_t *zfh, grub_fs_t *fs, grub_root_t *root)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore const char *name;
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if (zfs_get_type(zfh) != ZFS_TYPE_FILESYSTEM ||
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore return (-1);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if ((ret = zpool_get_physpath(zph, root->gr_physpath,
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore sizeof (root->gr_fs[GRBM_ZFS_BOOTFS].gfs_dev), NULL)) == 0) {
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore (void) strlcpy(root->gr_fs[GRBM_ZFS_TOPFS].gfs_dev, name,
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore (void) grub_fsd_get_mountp(root->gr_fs + GRBM_ZFS_BOOTFS,
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore (void) grub_fsd_get_mountp(root->gr_fs + GRBM_ZFS_TOPFS,
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * On entry physpath parameter supposed to contain:
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * <disk_physpath>[<space><disk_physpath>]*.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Retireives first <disk_physpath> that matches both partition and slice.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * If any partition and slice is acceptable, first <disk_physpath> is returned.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Mooreget_one_physpath(char *physpath, uint_t prtnum, uint_t slcnum)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if (!IS_SLCNUM_VALID(slcnum) && !IS_PRTNUM_VALID(prtnum)) {
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore for (tok = strtok(tmp, " "); tok != NULL; tok = strtok(NULL, " ")) {
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if (get_zfs_root(zfh, &menu->gm_fs, &barg->gb_root) != 0 ||
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore get_one_physpath(barg->gb_root.gr_physpath, barg->gb_prtnum,
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * if top zfs dataset is not mounted, mount it now
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if (barg->gb_root.gr_fs[GRBM_ZFS_TOPFS].gfs_mountp[0] == 0) {
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore /* check that bootsign exists and it is a regular file */
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore barg->gb_root.gr_fs[GRBM_ZFS_TOPFS].gfs_mountp,
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if (lstat(path, &st) != 0 || S_ISREG(st.st_mode) == 0 ||
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore (void) strlcpy(barg->gb_root.gr_fstyp, MNTTYPE_ZFS,
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore /* LINTED: E_CONSTANT_CONDITION */
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore } while (0);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore grub_fsd_umount_tmp(barg->gb_root.gr_fs + GRBM_ZFS_TOPFS);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore /* return non-zero to terminate the walk */
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore const char *path;
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore (void) strlcpy(barg->gb_root.gr_fs[GRBM_UFS].gfs_dev, path,
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore sizeof (barg->gb_root.gr_fs[GRBM_UFS].gfs_dev));
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore /* get /dev/dsk link */
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if (di_devlink_walk(mp->gm_fs.gf_dvlh, "^dsk/",
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore barg->gb_root.gr_physpath, DI_PRIMARY_LINK, barg, get_devlink) != 0)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * if disk is not mounted, mount it now
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if (grub_fsd_get_mountp(barg->gb_root.gr_fs + GRBM_UFS,
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore slice_ufs(barg->gb_root.gr_fs[GRBM_UFS].gfs_dev)) != 0 ||
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore (ret = grub_fsd_mount_tmp(barg->gb_root.gr_fs + GRBM_UFS,
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore barg->gb_root.gr_fs[GRBM_UFS].gfs_mountp, barg->gb_bootsign);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if (lstat(path, &st) == 0 && S_ISREG(st.st_mode) &&
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore (void) strlcpy(barg->gb_root.gr_fstyp, MNTTYPE_UFS,
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore grub_fsd_umount_tmp(barg->gb_root.gr_fs + GRBM_UFS);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Mooreufs_bootsign(di_node_t node, di_minor_t minor, void *arg)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if (name[0] != barg->gb_slcnum || name[1] != 0)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore sizeof (barg->gb_root.gr_physpath), "%s:%c", path, barg->gb_slcnum);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore prtnum = get_sol_prtnum(barg->gb_root.gr_physpath);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * check only specified partition, slice
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if (prtnum != barg->gb_prtnum || ufs_bootsign_check(barg) != 0)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Walk through all slices in found solaris partition
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore while ((minor = di_minor_next(node, minor)) != DI_MINOR_NIL) {
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore path = strrchr(barg->gb_root.gr_physpath, ':');
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Differs from what GRUB is doing: GRUB searchs through all disks seen by bios
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * for bootsign, if bootsign is found on ufs slice GRUB sets it as a root,
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * if on zfs, then GRUB uses zfs slice as root only if bootsign wasn't found
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * on other slices.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * That function first searches through all top datasets of active zpools,
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * then if bootsign still not found walks through all disks and tries to
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * find ufs slice with the bootsign.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore /* try to find bootsign over zfs pools */
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore (void) zfs_iter_root(mp->gm_fs.gf_lzfh, zfs_bootsign, barg);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore /* try ufs now */
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if (barg->gb_walkret != 0 && di_walk_minor(mp->gm_fs.gf_diroot,
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Get current root file system.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Return 0 on success, errno code on failure.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Mooregrub_current_root(grub_fs_t *fs, grub_root_t *root)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * getmntany returns non-zero for failure, and sets errno
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore (void) strlcpy(root->gr_fstyp, mp.mnt_fstype, sizeof (root->gr_fstyp));
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if (strcmp(root->gr_fstyp, MNTTYPE_ZFS) == 0) {
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore (void) strlcpy(buf, mp.mnt_special, sizeof (buf));
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if ((zfh = zfs_open(fs->gf_lzfh, name, ZFS_TYPE_FILESYSTEM)) ==
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * get_zfs_root returns non-zero on failure, not
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore } else if (strcmp(mp.mnt_fstype, MNTTYPE_UFS) == 0) {
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore (void) strlcpy(root->gr_fs[GRBM_UFS].gfs_dev, mp.mnt_special,
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore (void) strlcpy(root->gr_fs[GRBM_UFS].gfs_mountp, mp.mnt_mountp,
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore else if (strcmp(MNTTYPE_ZFS, root->gr_fstyp) == 0)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore fsd = (grub_fsdesc_t *)root->gr_fs + GRBM_ZFS_BOOTFS;
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Gets file systems mount point if any.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Return 0 if filesystem is mounted, errno on failure.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Mooregrub_fsd_get_mountp(grub_fsdesc_t *fsd, char *fstyp)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moorestatic const char tmp_mountp[] = "/tmp/.libgrubmgmt.%s.XXXXXX";
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Mount file system at tmp_mountp.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Return 0 on success, errno on failure.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Mooregrub_fsd_mount_tmp(grub_fsdesc_t *fsd, const char *fstyp)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore const char *pos;
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore struct ufs_args ufs_args = {UFSMNT_LARGEFILES};
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore (void) strlcpy(mntopts, MNTOPT_LARGEFILES, sizeof (mntopts));
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore /* construct name for temporary mount point */
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore (void) snprintf(fsd->gfs_mountp, sizeof (fsd->gfs_mountp),
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore fstyp, data, dtsz, mntopts, sizeof (mntopts))) != 0) {
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * mount failed, collect errno and remove temp dir
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Note that valid values for gfs_is_tmp_mounted are 0,1.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Any other value indicates that something bad happened.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Probably grub_fsd_umount_tmp() wasn't called or didn't
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * work as expected.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Unmount file system at tmp_mountp.