14784aef9SWarner Losh /*-
2*f86e6000SWarner Losh * Copyright (c) 2018 M. Warner Losh <imp@FreeBSD.org>
34784aef9SWarner Losh *
44784aef9SWarner Losh * Redistribution and use in source and binary forms, with or without
54784aef9SWarner Losh * modification, are permitted provided that the following conditions
64784aef9SWarner Losh * are met:
74784aef9SWarner Losh * 1. Redistributions of source code must retain the above copyright
84784aef9SWarner Losh * notice, this list of conditions and the following disclaimer.
94784aef9SWarner Losh * 2. Redistributions in binary form must reproduce the above copyright
104784aef9SWarner Losh * notice, this list of conditions and the following disclaimer in the
114784aef9SWarner Losh * documentation and/or other materials provided with the distribution.
124784aef9SWarner Losh *
134784aef9SWarner Losh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
144784aef9SWarner Losh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
154784aef9SWarner Losh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
164784aef9SWarner Losh * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
174784aef9SWarner Losh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
184784aef9SWarner Losh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
194784aef9SWarner Losh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
204784aef9SWarner Losh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
214784aef9SWarner Losh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
224784aef9SWarner Losh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
234784aef9SWarner Losh * SUCH DAMAGE.
244784aef9SWarner Losh */
254784aef9SWarner Losh
264784aef9SWarner Losh /*
274784aef9SWarner Losh * MD bootstrap main() and assorted miscellaneous
284784aef9SWarner Losh * commands.
294784aef9SWarner Losh */
304784aef9SWarner Losh
314784aef9SWarner Losh #include <stand.h>
324784aef9SWarner Losh #include <stddef.h>
334784aef9SWarner Losh #include <sys/disk.h>
344784aef9SWarner Losh #include <sys/reboot.h>
354784aef9SWarner Losh
364784aef9SWarner Losh #include "bootstrap.h"
37007b82d7SWarner Losh #include "libzfs.h"
384784aef9SWarner Losh
394784aef9SWarner Losh COMMAND_SET(lszfs, "lszfs", "list child datasets of a zfs dataset",
404784aef9SWarner Losh command_lszfs);
414784aef9SWarner Losh
424784aef9SWarner Losh static int
command_lszfs(int argc,char * argv[])434784aef9SWarner Losh command_lszfs(int argc, char *argv[])
444784aef9SWarner Losh {
454784aef9SWarner Losh int err;
464784aef9SWarner Losh
474784aef9SWarner Losh if (argc != 2) {
484784aef9SWarner Losh command_errmsg = "a single dataset must be supplied";
494784aef9SWarner Losh return (CMD_ERROR);
504784aef9SWarner Losh }
514784aef9SWarner Losh
524784aef9SWarner Losh err = zfs_list(argv[1]);
534784aef9SWarner Losh if (err != 0) {
544784aef9SWarner Losh command_errmsg = strerror(err);
554784aef9SWarner Losh return (CMD_ERROR);
564784aef9SWarner Losh }
574784aef9SWarner Losh return (CMD_OK);
584784aef9SWarner Losh }
594784aef9SWarner Losh
604784aef9SWarner Losh COMMAND_SET(reloadbe, "reloadbe", "refresh the list of ZFS Boot Environments",
614784aef9SWarner Losh command_reloadbe);
624784aef9SWarner Losh
634784aef9SWarner Losh static int
command_reloadbe(int argc,char * argv[])644784aef9SWarner Losh command_reloadbe(int argc, char *argv[])
654784aef9SWarner Losh {
664784aef9SWarner Losh int err;
674784aef9SWarner Losh char *root;
684784aef9SWarner Losh
694784aef9SWarner Losh if (argc > 2) {
704784aef9SWarner Losh command_errmsg = "wrong number of arguments";
714784aef9SWarner Losh return (CMD_ERROR);
724784aef9SWarner Losh }
734784aef9SWarner Losh
744784aef9SWarner Losh if (argc == 2) {
754784aef9SWarner Losh err = zfs_bootenv(argv[1]);
764784aef9SWarner Losh } else {
774784aef9SWarner Losh root = getenv("zfs_be_root");
784784aef9SWarner Losh if (root == NULL) {
794784aef9SWarner Losh /* There does not appear to be a ZFS pool here, exit without error */
804784aef9SWarner Losh return (CMD_OK);
814784aef9SWarner Losh }
824784aef9SWarner Losh err = zfs_bootenv(root);
834784aef9SWarner Losh }
844784aef9SWarner Losh
854784aef9SWarner Losh if (err != 0) {
864784aef9SWarner Losh command_errmsg = strerror(err);
874784aef9SWarner Losh return (CMD_ERROR);
884784aef9SWarner Losh }
894784aef9SWarner Losh
904784aef9SWarner Losh return (CMD_OK);
914784aef9SWarner Losh }
924784aef9SWarner Losh
934784aef9SWarner Losh uint64_t
ldi_get_size(void * priv)944784aef9SWarner Losh ldi_get_size(void *priv)
954784aef9SWarner Losh {
964784aef9SWarner Losh int fd = (uintptr_t) priv;
974784aef9SWarner Losh uint64_t size;
984784aef9SWarner Losh
994784aef9SWarner Losh ioctl(fd, DIOCGMEDIASIZE, &size);
1004784aef9SWarner Losh return (size);
1014784aef9SWarner Losh }
102