vaseboot/VasEBoot-core/fs/zfs/zfsinfo.c

445 lines
12 KiB
C

/*
* VAS_EBOOT -- GRand Unified Bootloader
* Copyright (C) 1999,2000,2001,2002,2003,2004,2009 Free Software Foundation, Inc.
* Copyright 2008 Sun Microsystems, Inc.
*
* VAS_EBOOT is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* VAS_EBOOT is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VAS_EBOOT. If not, see <http://www.gnu.org/licenses/>.
*/
#include <VasEBoot/zfs/zfs.h>
#include <VasEBoot/device.h>
#include <VasEBoot/file.h>
#include <VasEBoot/command.h>
#include <VasEBoot/misc.h>
#include <VasEBoot/mm.h>
#include <VasEBoot/dl.h>
#include <VasEBoot/env.h>
#include <VasEBoot/i18n.h>
VAS_EBOOT_MOD_LICENSE ("GPLv3+");
static inline void
print_tabs (int n)
{
int i;
for (i = 0; i < n; i++)
VasEBoot_printf (" ");
}
static VasEBoot_err_t
print_state (char *nvlist, int tab)
{
VasEBoot_uint64_t ival;
int isok = 1;
print_tabs (tab);
if (VasEBoot_zfs_nvlist_lookup_uint64 (nvlist, ZPOOL_CONFIG_REMOVED, &ival))
{
VasEBoot_puts_ (N_("Virtual device is removed"));
isok = 0;
}
if (VasEBoot_zfs_nvlist_lookup_uint64 (nvlist, ZPOOL_CONFIG_FAULTED, &ival))
{
VasEBoot_puts_ (N_("Virtual device is faulted"));
isok = 0;
}
if (VasEBoot_zfs_nvlist_lookup_uint64 (nvlist, ZPOOL_CONFIG_OFFLINE, &ival))
{
VasEBoot_puts_ (N_("Virtual device is offline"));
isok = 0;
}
if (VasEBoot_zfs_nvlist_lookup_uint64 (nvlist, ZPOOL_CONFIG_FAULTED, &ival))
/* TRANSLATORS: degraded doesn't mean broken but that some of
component are missing but virtual device as whole is still usable. */
VasEBoot_puts_ (N_("Virtual device is degraded"));
if (isok)
VasEBoot_puts_ (N_("Virtual device is online"));
VasEBoot_xputs ("\n");
return VAS_EBOOT_ERR_NONE;
}
static VasEBoot_err_t
print_vdev_info (char *nvlist, int tab)
{
char *type = 0;
type = VasEBoot_zfs_nvlist_lookup_string (nvlist, ZPOOL_CONFIG_TYPE);
if (!type)
{
print_tabs (tab);
VasEBoot_puts_ (N_("Incorrect virtual device: no type available"));
return VasEBoot_errno;
}
if (VasEBoot_strcmp (type, VDEV_TYPE_DISK) == 0)
{
char *bootpath = 0;
char *path = 0;
char *devid = 0;
print_tabs (tab);
/* TRANSLATORS: The virtual devices form a tree (in graph-theoretical
sense). The nodes like mirror or raidz have children: member devices.
The "real" devices which actually store data are called "leafs"
(again borrowed from graph theory) and can be either disks
(or partitions) or files. */
VasEBoot_puts_ (N_("Leaf virtual device (file or disk)"));
print_state (nvlist, tab);
bootpath =
VasEBoot_zfs_nvlist_lookup_string (nvlist, ZPOOL_CONFIG_PHYS_PATH);
print_tabs (tab);
if (!bootpath)
VasEBoot_puts_ (N_("Bootpath: unavailable\n"));
else
VasEBoot_printf_ (N_("Bootpath: %s\n"), bootpath);
path = VasEBoot_zfs_nvlist_lookup_string (nvlist, "path");
print_tabs (tab);
if (!path)
VasEBoot_puts_ (N_("Path: unavailable"));
else
VasEBoot_printf_ (N_("Path: %s\n"), path);
devid = VasEBoot_zfs_nvlist_lookup_string (nvlist, ZPOOL_CONFIG_DEVID);
print_tabs (tab);
if (!devid)
VasEBoot_puts_ (N_("Devid: unavailable"));
else
VasEBoot_printf_ (N_("Devid: %s\n"), devid);
VasEBoot_free (bootpath);
VasEBoot_free (devid);
VasEBoot_free (path);
VasEBoot_free (type);
return VAS_EBOOT_ERR_NONE;
}
char is_mirror=(VasEBoot_strcmp(type,VDEV_TYPE_MIRROR) == 0);
char is_raidz=(VasEBoot_strcmp(type,VDEV_TYPE_RAIDZ) == 0);
VasEBoot_free (type);
if (is_mirror || is_raidz)
{
int nelm, i;
nelm = VasEBoot_zfs_nvlist_lookup_nvlist_array_get_nelm
(nvlist, ZPOOL_CONFIG_CHILDREN);
if(is_mirror){
VasEBoot_puts_ (N_("This VDEV is a mirror"));
}
else if(is_raidz){
VasEBoot_uint64_t parity;
VasEBoot_zfs_nvlist_lookup_uint64(nvlist,"nparity",&parity);
VasEBoot_printf_ (N_("This VDEV is a RAIDZ%llu\n"),(unsigned long long)parity);
}
print_tabs (tab);
if (nelm <= 0)
{
VasEBoot_puts_ (N_("Incorrect VDEV"));
return VAS_EBOOT_ERR_NONE;
}
VasEBoot_printf_ (N_("VDEV with %d children\n"), nelm);
print_state (nvlist, tab);
for (i = 0; i < nelm; i++)
{
char *child;
child = VasEBoot_zfs_nvlist_lookup_nvlist_array
(nvlist, ZPOOL_CONFIG_CHILDREN, i);
print_tabs (tab);
if (!child)
{
/* TRANSLATORS: it's the element carying the number %d, not
total element number. And the number itself is fine,
only the element isn't.
*/
VasEBoot_printf_ (N_("VDEV element number %d isn't correct\n"), i);
continue;
}
/* TRANSLATORS: it's the element carying the number %d, not
total element number. This is used in enumeration
"Element number 1", "Element number 2", ... */
VasEBoot_printf_ (N_("VDEV element number %d:\n"), i);
print_vdev_info (child, tab + 1);
VasEBoot_free (child);
}
return VAS_EBOOT_ERR_NONE;
}
print_tabs (tab);
VasEBoot_printf_ (N_("Unknown virtual device type: %s\n"), type);
return VAS_EBOOT_ERR_NONE;
}
static VasEBoot_err_t
get_bootpath (char *nvlist, char **bootpath, char **devid)
{
char *type = 0;
type = VasEBoot_zfs_nvlist_lookup_string (nvlist, ZPOOL_CONFIG_TYPE);
if (!type)
return VasEBoot_errno;
if (VasEBoot_strcmp (type, VDEV_TYPE_DISK) == 0)
{
*bootpath = VasEBoot_zfs_nvlist_lookup_string (nvlist,
ZPOOL_CONFIG_PHYS_PATH);
*devid = VasEBoot_zfs_nvlist_lookup_string (nvlist, ZPOOL_CONFIG_DEVID);
if (!*bootpath || !*devid)
{
VasEBoot_free (*bootpath);
VasEBoot_free (*devid);
*bootpath = 0;
*devid = 0;
}
return VAS_EBOOT_ERR_NONE;
}
if (VasEBoot_strcmp (type, VDEV_TYPE_MIRROR) == 0)
{
int nelm, i;
nelm = VasEBoot_zfs_nvlist_lookup_nvlist_array_get_nelm
(nvlist, ZPOOL_CONFIG_CHILDREN);
for (i = 0; i < nelm; i++)
{
char *child;
child = VasEBoot_zfs_nvlist_lookup_nvlist_array (nvlist,
ZPOOL_CONFIG_CHILDREN,
i);
get_bootpath (child, bootpath, devid);
VasEBoot_free (child);
if (*bootpath && *devid)
return VAS_EBOOT_ERR_NONE;
}
}
return VAS_EBOOT_ERR_NONE;
}
static const char *poolstates[] = {
/* TRANSLATORS: Here we speak about ZFS pools it's semi-marketing,
semi-technical term by Sun/Oracle and should be translated in sync with
other ZFS-related software and documentation. */
[POOL_STATE_ACTIVE] = N_("Pool state: active"),
[POOL_STATE_EXPORTED] = N_("Pool state: exported"),
[POOL_STATE_DESTROYED] = N_("Pool state: destroyed"),
[POOL_STATE_SPARE] = N_("Pool state: reserved for hot spare"),
[POOL_STATE_L2CACHE] = N_("Pool state: level 2 ARC device"),
[POOL_STATE_UNINITIALIZED] = N_("Pool state: uninitialized"),
[POOL_STATE_UNAVAIL] = N_("Pool state: unavailable"),
[POOL_STATE_POTENTIALLY_ACTIVE] = N_("Pool state: potentially active")
};
static VasEBoot_err_t
VasEBoot_cmd_zfsinfo (VasEBoot_command_t cmd __attribute__ ((unused)), int argc,
char **args)
{
VasEBoot_device_t dev;
char *devname;
VasEBoot_err_t err;
char *nvlist = 0;
char *nv = 0;
char *poolname;
VasEBoot_uint64_t guid;
VasEBoot_uint64_t pool_state;
int found;
if (argc < 1)
return VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, N_("one argument expected"));
if (args[0][0] == '(' && args[0][VasEBoot_strlen (args[0]) - 1] == ')')
{
devname = VasEBoot_strdup (args[0] + 1);
if (devname)
devname[VasEBoot_strlen (devname) - 1] = 0;
}
else
devname = VasEBoot_strdup (args[0]);
if (!devname)
return VasEBoot_errno;
dev = VasEBoot_device_open (devname);
VasEBoot_free (devname);
if (!dev)
return VasEBoot_errno;
err = VasEBoot_zfs_fetch_nvlist (dev, &nvlist);
VasEBoot_device_close (dev);
if (err)
return err;
poolname = VasEBoot_zfs_nvlist_lookup_string (nvlist, ZPOOL_CONFIG_POOL_NAME);
if (!poolname)
VasEBoot_puts_ (N_("Pool name: unavailable"));
else
VasEBoot_printf_ (N_("Pool name: %s\n"), poolname);
found =
VasEBoot_zfs_nvlist_lookup_uint64 (nvlist, ZPOOL_CONFIG_POOL_GUID, &guid);
if (!found)
VasEBoot_puts_ (N_("Pool GUID: unavailable"));
else
VasEBoot_printf_ (N_("Pool GUID: %016llx\n"), (long long unsigned) guid);
found = VasEBoot_zfs_nvlist_lookup_uint64 (nvlist, ZPOOL_CONFIG_POOL_STATE,
&pool_state);
if (!found)
VasEBoot_puts_ (N_("Unable to retrieve pool state"));
else if (pool_state >= ARRAY_SIZE (poolstates))
VasEBoot_puts_ (N_("Unrecognized pool state"));
else
VasEBoot_puts_ (poolstates[pool_state]);
nv = VasEBoot_zfs_nvlist_lookup_nvlist (nvlist, ZPOOL_CONFIG_VDEV_TREE);
if (!nv)
/* TRANSLATORS: There are undetermined number of virtual devices
in a device tree, not just one.
*/
VasEBoot_puts_ (N_("No virtual device tree available"));
else
print_vdev_info (nv, 1);
VasEBoot_free (nv);
VasEBoot_free (nvlist);
return VAS_EBOOT_ERR_NONE;
}
static VasEBoot_err_t
VasEBoot_cmd_zfs_bootfs (VasEBoot_command_t cmd __attribute__ ((unused)), int argc,
char **args)
{
VasEBoot_device_t dev;
char *devname;
VasEBoot_err_t err;
char *nvlist = 0;
char *nv = 0;
char *bootpath = 0, *devid = 0;
char *fsname;
char *bootfs;
char *poolname;
VasEBoot_uint64_t mdnobj;
if (argc < 1)
return VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, N_("one argument expected"));
devname = VasEBoot_file_get_device_name (args[0]);
if (VasEBoot_errno)
return VasEBoot_errno;
dev = VasEBoot_device_open (devname);
VasEBoot_free (devname);
if (!dev)
return VasEBoot_errno;
err = VasEBoot_zfs_fetch_nvlist (dev, &nvlist);
fsname = VasEBoot_strchr (args[0], ')');
if (fsname)
fsname++;
else
fsname = args[0];
if (!err)
err = VasEBoot_zfs_getmdnobj (dev, fsname, &mdnobj);
VasEBoot_device_close (dev);
if (err) {
VasEBoot_free (nvlist);
return err;
}
poolname = VasEBoot_zfs_nvlist_lookup_string (nvlist, ZPOOL_CONFIG_POOL_NAME);
if (!poolname)
{
if (!VasEBoot_errno)
VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "No poolname found");
VasEBoot_free (nvlist);
return VasEBoot_errno;
}
nv = VasEBoot_zfs_nvlist_lookup_nvlist (nvlist, ZPOOL_CONFIG_VDEV_TREE);
if (nv)
get_bootpath (nv, &bootpath, &devid);
VasEBoot_free (nv);
VasEBoot_free (nvlist);
bootfs = VasEBoot_xasprintf ("zfs-bootfs=%s/%llu%s%s%s%s%s%s",
poolname, (unsigned long long) mdnobj,
bootpath ? ",bootpath=\"" : "",
bootpath ? : "",
bootpath ? "\"" : "",
devid ? ",diskdevid=\"" : "",
devid ? : "",
devid ? "\"" : "");
if (!bootfs)
return VasEBoot_errno;
if (argc >= 2)
VasEBoot_env_set (args[1], bootfs);
else
VasEBoot_printf ("%s\n", bootfs);
VasEBoot_free (bootfs);
VasEBoot_free (poolname);
VasEBoot_free (bootpath);
VasEBoot_free (devid);
return VAS_EBOOT_ERR_NONE;
}
static VasEBoot_command_t cmd_info, cmd_bootfs;
VAS_EBOOT_MOD_INIT (zfsinfo)
{
cmd_info = VasEBoot_register_command ("zfsinfo", VasEBoot_cmd_zfsinfo,
N_("DEVICE"),
N_("Print ZFS info about DEVICE."));
cmd_bootfs = VasEBoot_register_command ("zfs-bootfs", VasEBoot_cmd_zfs_bootfs,
N_("FILESYSTEM [VARIABLE]"),
N_("Print ZFS-BOOTFSOBJ or store it into VARIABLE"));
}
VAS_EBOOT_MOD_FINI (zfsinfo)
{
VasEBoot_unregister_command (cmd_info);
VasEBoot_unregister_command (cmd_bootfs);
}