zoneadm.c revision 20c8013f7d176d8bb197f289e6b14411470350cf
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* zoneadm is a command interpreter for zone administration. It is all in
* main() calls parse_and_run() which calls cmd_match(), then invokes the
* appropriate command's handler function. The rest of the program is the
* handler functions and their helper functions.
*
* Some of the helper functions are used largely to simplify I18N: reducing
* the need for translation notes. This is particularly true of many of
* the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather
* than zerror(gettext("foo failed")) with a translation note indicating
* that "foo" need not be translated.
*/
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <stdarg.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <wait.h>
#include <zone.h>
#include <priv.h>
#include <locale.h>
#include <libintl.h>
#include <libzonecfg.h>
#include <assert.h>
#include <limits.h>
#include <libzfs.h>
#include <fcntl.h>
#include <door.h>
#include <macros.h>
#include <libgen.h>
#include <fnmatch.h>
#include <pool.h>
#define MAXARGS 8
/* Reflects kernel zone entries */
typedef struct zone_entry {
char zname[ZONENAME_MAX];
char *zstate_str;
char zroot[MAXPATHLEN];
} zone_entry_t;
static zone_entry_t *zents;
#if !defined(TEXT_DOMAIN) /* should be defined by cc -D */
#endif
#define Z_ERR 1
#define Z_USAGE 2
/* 0755 is the default directory mode. */
#define DEFAULT_DIR_MODE \
#define CMD_HELP 0
#define CMD_BOOT 1
#define CMD_HALT 2
#define CMD_READY 3
#define CMD_REBOOT 4
#define CMD_LIST 5
#define CMD_VERIFY 6
#define CMD_INSTALL 7
#define CMD_UNINSTALL 8
#define CMD_MOUNT 9
#define CMD_UNMOUNT 10
#define CMD_CLONE 11
#define CMD_MOVE 12
#define SINGLE_USER_RETRY 30
struct cmd {
char *cmd_name; /* command name */
char *short_usage; /* short form help */
};
#define SHELP_HELP "help"
#define SHELP_BOOT "boot [-s]"
#define SHELP_HALT "halt"
#define SHELP_READY "ready"
#define SHELP_REBOOT "reboot"
#define SHELP_LIST "list [-cipv]"
#define SHELP_VERIFY "verify"
#define SHELP_INSTALL "install"
#define SHELP_UNINSTALL "uninstall [-F]"
#define SHELP_CLONE "clone [-m method] zonename"
#define SHELP_MOVE "move zonepath"
static int verify_details(int);
};
/* global variables */
/* set early in main(), never modified thereafter, used all over the place */
static char *execname;
static char *target_zone;
static char *locale;
/* used in do_subproc() and signal handler */
static volatile boolean_t child_killed;
static char *
cmd_to_str(int cmd_num)
{
}
/* This is a separate function because of gettext() wrapping. */
static char *
{
switch (cmd_num) {
case CMD_HELP:
return (gettext("Print usage message."));
case CMD_BOOT:
return (gettext("Activates (boots) specified zone. "
"The -s flag can be used\n\tto boot the zone in "
"the single-user state."));
case CMD_HALT:
return (gettext("Halts specified zone, bypassing "
"shutdown scripts and removing runtime\n\t"
"resources of the zone."));
case CMD_READY:
return (gettext("Prepares a zone for running "
"applications but does not start any user\n\t"
"processes in the zone."));
case CMD_REBOOT:
return (gettext("Restarts the zone (equivalent to a "
"halt / boot sequence).\n\tFails if the zone is "
"not active."));
case CMD_LIST:
return (gettext("Lists the current zones, or a "
"specific zone if indicated. By default,\n\tall "
"running zones are listed, though this can be "
"expanded to all\n\tinstalled zones with the -i "
"option or all configured zones with the\n\t-c "
"option. When used with the general -z <zone> "
"option, lists only the\n\tspecified zone, but "
"lists it regardless of its state, and the -i "
"and -c\n\toptions are disallowed. The -v option "
"can be used to display verbose\n\tinformation: "
"zone name, id, current state, root directory and "
"options.\n\tThe -p option can be used to request "
"machine-parsable output. The -v\n\tand -p "
"options are mutually exclusive. If neither -v "
"nor -p is used,\n\tjust the zone name is "
"listed."));
case CMD_VERIFY:
return (gettext("Check to make sure the configuration "
"can safely be instantiated\n\ton the machine: "
"physical network interfaces exist, etc."));
case CMD_INSTALL:
return (gettext("Install the configuration on to the "
"system."));
case CMD_UNINSTALL:
return (gettext("Uninstall the configuration from the "
"system. The -F flag can be used\n\tto force the "
"action."));
case CMD_CLONE:
return (gettext("Clone the installation of another "
"zone."));
case CMD_MOVE:
return (gettext("Move the zone to a new zonepath."));
default:
return ("");
}
/* NOTREACHED */
return (NULL);
}
/*
* Called with explicit B_TRUE when help is explicitly requested, B_FALSE for
* unexpected errors.
*/
static int
{
int i;
gettext("subcommand"));
continue;
if (explicit)
}
if (!explicit)
return (Z_USAGE);
}
static void
{
}
/*
* zperror() is like perror(3c) except that this also prints the executable
* name at the start of the message, and takes a boolean indicating whether
* to call libc'c strerror() or that from libzonecfg.
*/
static void
{
}
/*
* zperror2() is very similar to zperror() above, except it also prints a
* supplied zone name after the executable.
*
* All current consumers of this function want libzonecfg's strerror() rather
* than libc's; if this ever changes, this function can be made more generic
* like zperror() above.
*/
static void
{
}
/* PRINTFLIKE1 */
static void
{
if (target_zone != NULL)
}
static void *
{
if (r == NULL) {
}
return (r);
}
static void
{
"NAME", "STATUS", "PATH");
}
if (!verbose) {
if (!parsable) {
return;
}
(void) printf("-");
else
return;
}
else
}
}
static int
{
char root[MAXPATHLEN];
int err;
Z_OK) {
return (Z_ERR);
}
return (Z_ERR);
}
return (Z_OK);
}
/*
* fetch_zents() calls zone_list(2) to find out how many zones are running
* (which is stored in the global nzents), then calls zone_list(2) again
* to fetch the list of running zones (stored in the global zents). This
* function may be called multiple times, so if zents is already set, we
* return immediately to save work.
*/
static int
fetch_zents(void)
{
int i, retv;
if (nzents > 0)
return (Z_OK);
return (Z_ERR);
}
if (nzents == 0)
return (Z_OK);
return (Z_ERR);
}
if (nzents != nzents_saved) {
/* list changed, try again */
goto again;
}
if (inaltroot)
else
for (i = 0; i < nzents; i++) {
char name[ZONENAME_MAX];
char altname[ZONENAME_MAX];
continue;
}
if (zonecfg_is_scratch(name)) {
/* Ignore scratch zones by default */
if (!inaltroot)
continue;
"zone %s"), name);
continue;
}
} else {
/* Ignore non-scratch when in an alternate root */
continue;
}
continue;
}
zentp++;
}
return (retv);
}
static int
{
int i;
char *name;
/*
* First get the list of running zones from the kernel and print them.
* If that is all we need, then return.
*/
if ((i = fetch_zents()) != Z_OK) {
/*
* No need for error messages; fetch_zents() has already taken
* care of this.
*/
return (i);
}
for (i = 0; i < nzents; i++)
if (min_state >= ZONE_STATE_RUNNING)
return (Z_OK);
/*
* Next, get the full list of zones from the configuration, skipping
* any we have already printed.
*/
cookie = setzoneent();
for (i = 0; i < nzents; i++) {
break;
}
if (i < nzents) {
continue;
}
continue;
}
}
return (Z_OK);
}
static zone_entry_t *
lookup_running_zone(char *str)
{
char *cp;
int i;
if (fetch_zents() != Z_OK)
return (NULL);
for (i = 0; i < nzents; i++) {
return (&zents[i]);
}
errno = 0;
return (NULL);
for (i = 0; i < nzents; i++) {
return (&zents[i]);
}
return (NULL);
}
/*
* Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if
* B_FALSE, it should be off. Return B_TRUE if the mode is bad (incorrect).
*/
static boolean_t
{
char *str;
/*
* TRANSLATION_NOTE
* The strings below will be used as part of a larger message,
* either:
* (file name) must be (owner|group|world) (read|writ|execut)able
* or
* (file name) must not be (owner|group|world) (read|writ|execut)able
*/
switch (bit) {
case S_IRUSR:
break;
case S_IWUSR:
break;
case S_IXUSR:
break;
case S_IRGRP:
break;
case S_IWGRP:
break;
case S_IXGRP:
break;
case S_IROTH:
break;
case S_IWOTH:
break;
case S_IXOTH:
break;
}
/*
* TRANSLATION_NOTE
* The first parameter below is a file name; the second
* is one of the "(owner|group|world) (read|writ|execut)able"
* strings from above.
*/
/*
* The code below could be simplified but not in a way
* that would easily translate to non-English locales.
*/
if (on) {
} else {
}
return (B_TRUE);
}
return (B_FALSE);
}
/*
* We want to make sure that no zone has its zone path as a child node
* (in the directory sense) of any other. We do that by comparing this
* zone's path to the path of all other (non-global) zones. The comparison
* in each case is simple: add '/' to the end of the path, then do a
* strncmp() of the two paths, using the length of the shorter one.
*/
static int
crosscheck_zonepaths(char *path)
{
cookie = setzoneent();
/* Skip zones which are not installed. */
continue;
}
/* Skip the global zone and the current target zone. */
continue;
}
/* old index file without path, fall back */
gettext("could not get zone path"));
continue;
}
}
if (res == -1) {
return (Z_ERR);
}
"zonepath", path_copy);
continue;
}
/*
* TRANSLATION_NOTE
* zonepath is a literal that should not be translated.
*/
"%s zonepath (%s) overlap.\n"),
return (Z_ERR);
}
}
return (Z_OK);
}
static int
{
if (path[0] != '/') {
return (Z_ERR);
}
return (Z_ERR);
}
if (cmd_num == CMD_VERIFY) {
/*
* TRANSLATION_NOTE
* zoneadm is a literal that should not be translated.
*/
"exist, so it could not be verified.\nWhen "
"'zoneadm %s' is run, '%s' will try to create\n%s, "
"and '%s' will be tried again,\nbut the '%s' may "
"fail if:\nthe parent directory of %s is group- or "
"other-writable\nor\n%s overlaps with any other "
"installed zones.\n"), path,
return (Z_OK);
}
/*
* The zonepath is supposed to be mode 700 but its
* parent(s) 755. So use 755 on the mkdirp() then
* chmod() the zonepath itself to 700.
*/
return (Z_ERR);
}
/*
* If the chmod() fails, report the error, but might
* as well continue the verify procedure.
*/
/*
* Since the mkdir() succeeded, we should not have to
* worry about a subsequent ENOENT, thus this should
* only recurse once.
*/
}
return (Z_ERR);
}
return (Z_ERR);
}
rpath);
return (Z_ERR);
}
"file-system.\n"), rpath);
}
return (Z_ERR);
/*
* Try to collect and report as many minor errors as possible
* before returning, so the user can learn everything that needs
* to be fixed up front.
*/
rpath);
}
return (Z_ERR);
}
return (Z_ERR);
}
/* theoretically impossible */
rppath);
return (Z_ERR);
}
rppath);
}
rppath);
}
return (Z_ERR);
}
/*
* TRANSLATION_NOTE
* Zonepath and NFS are literals that should not be translated.
*/
"mounted file-system.\n"
"\tA local file-system must be used.\n"), rpath);
return (Z_ERR);
}
/*
* TRANSLATION_NOTE
* Zonepath and nosuid are literals that should not be
* translated.
*/
"file-system.\n"), rpath);
return (Z_ERR);
}
return (Z_ERR);
}
/*
* The existence of the root path is only bad in the configured state,
* as it is *supposed* to be there at the installed and later states.
*/
if (state == ZONE_STATE_CONFIGURED) {
sizeof (rootpath)) {
/*
* TRANSLATION_NOTE
* Zonepath is a literal that should not be translated.
*/
return (Z_ERR);
}
"remove or move aside prior to %s.\n"), rootpath,
return (Z_ERR);
}
}
}
static void
release_lock_file(int lockfd)
{
}
static int
{
ZONES_TMPDIR) >= sizeof (pathbuf)) {
return (Z_ERR);
}
return (Z_ERR);
}
/*
* One of these lock files is created for each zone (when needed).
* The lock files are not cleaned up (except on system reboot),
* but since there is only one per zone, there is no resource
* starvation issue.
*/
return (Z_ERR);
}
return (Z_ERR);
}
/*
* Lock the file to synchronize with other zoneadmds
*/
return (Z_ERR);
}
return (Z_OK);
}
static boolean_t
{
}
/*
* system daemons are not audited. For the global zone, this occurs
* "naturally" since init is started with the default audit
* characteristics. Since zoneadmd is a system daemon and it starts
* init for a zone, it is necessary to clear out the audit
* characteristics inherited from whomever started zoneadmd. This is
* indicated by the audit id, which is set from the ruid parameter of
* adt_set_user(), below.
*/
static void
{
return;
}
(void) adt_end_session(ah);
return;
}
if (adt_set_proc(ah))
(void) adt_end_session(ah);
}
static int
start_zoneadmd(const char *zone_name)
{
return (Z_ERR);
return (Z_ERR);
/*
* Now that we have the lock, re-confirm that the daemon is
* *not* up and working fine. If it is still down, we have a green
* light to start it.
*/
goto out;
}
} else {
goto out;
}
}
goto out;
} else if (child_pid == 0) {
/* child process */
*ap++ = "zoneadmd";
*ap++ = "-z";
if (zonecfg_in_alt_root()) {
*ap++ = "-R";
*ap++ = zonecfg_get_root();
}
/*
* TRANSLATION_NOTE
* zoneadmd is a literal that should not be translated.
*/
} else {
/* parent process */
int pstatus = 0;
do {
WEXITSTATUS(pstatus) != 0)) {
goto out;
}
}
out:
return (error);
}
static int
ping_zoneadmd(const char *zone_name)
{
int doorfd;
return (Z_ERR);
return (Z_ERR);
}
return (Z_OK);
}
return (Z_ERR);
}
static int
{
rlen = getpagesize();
return (-1);
}
sizeof (uniqid));
}
return (-1);
}
/*
* Loop trying to start zoneadmd; if something goes seriously
* wrong we break out and fail.
*/
for (;;) {
break;
break;
}
/*
* We'll get EBADF if the door has been revoked.
*/
break;
}
continue; /* take another lap */
}
/* Door server is going away; kick it again. */
continue;
}
while (*errbuf != '\0') {
/*
* Remove any newlines since zerror()
* will append one automatically.
*/
*cp = '\0';
break;
}
return (result);
}
return (-1);
}
static int
{
int arg;
if (zonecfg_in_alt_root()) {
return (Z_ERR);
}
optind = 0;
switch (arg) {
case '?':
default:
return (Z_USAGE);
}
}
return (Z_USAGE);
}
return (Z_ERR);
return (Z_ERR);
return (Z_ERR);
}
return (Z_OK);
}
static int
{
int arg;
if (zonecfg_in_alt_root()) {
return (Z_ERR);
}
/*
* At the current time, the only supported subargument to the
* "boot" subcommand is "-s" which specifies a single-user boot.
* In the future, other boot arguments should be supported
* including "-m" for specifying alternate smf(5) milestones.
*/
optind = 0;
switch (arg) {
case '?':
case 's':
break;
default:
return (Z_USAGE);
}
}
return (Z_USAGE);
}
return (Z_ERR);
return (Z_ERR);
return (Z_ERR);
}
return (Z_OK);
}
static void
{
/*
* Since we're looking up our own (non-global) zone name,
* we can be assured that it will succeed.
*/
}
static int
{
if (target_zone == NULL) {
/* all zones: default view to running but allow override */
optind = 0;
switch (arg) {
case '?':
/*
* The 'i' and 'c' options are not mutually
* exclusive so if 'c' is given, then min_state
* is set to 0 (ZONE_STATE_CONFIGURED) which is
* the lowest possible state. If 'i' is given,
* then min_state is set to be the lowest state
* so far.
*/
case 'c':
break;
case 'i':
break;
case 'p':
break;
case 'v':
break;
default:
return (Z_USAGE);
}
}
return (Z_ERR);
}
if (zone_id == GLOBAL_ZONEID) {
} else {
}
return (retv);
}
/*
* Specific target zone: disallow -i/-c suboptions.
*/
optind = 0;
switch (arg) {
case '?':
case 'p':
break;
case 'v':
break;
default:
return (Z_USAGE);
}
}
return (Z_ERR);
}
return (Z_USAGE);
}
if (zone_id != GLOBAL_ZONEID) {
/*
* main() will issue a Z_NO_ZONE error if it cannot get an
* id for target_zone, which in a non-global zone should
* happen for any zone name except `zonename`. Thus we
* assert() that here but don't otherwise check.
*/
}
}
static void
{
/*
* Ignore SIG{INT,TERM}, so we don't end up in an infinite loop,
* then propagate the signal to our process group.
*/
}
static int
do_subproc(char *cmdbuf)
{
/*
* We use popen(3c) to launch child processes for [un]install;
* this library call does not return a PID, so we have to kill
* the whole process group. To avoid killing our parent, we
* become a process group leader here. But doing so can wreak
* havoc with reading from stdin when launched by a non-job-control
*/
(void) close(STDIN_FILENO);
(void) setpgid(0, 0);
for (;;) {
break;
}
}
static int
{
if (exit_code == 0)
return (Z_OK);
} else if (WIFSIGNALED(status)) {
char sigstr[SIG2STR_MAX];
sigstr);
} else {
cmd);
}
} else {
}
return (Z_ERR);
}
/*
* Various sanity checks; make sure:
* 1. We're in the global zone.
* 2. The calling user has sufficient privilege.
* 3. The target zone is neither the global zone nor anything starting with
* "SUNW".
* 4a. If we're looking for a 'not running' (i.e., configured or installed)
* zone, the name service knows about it.
* 4b. For some operations which expect a zone not to be running, that it is
* not already running (or ready).
*/
static int
{
char kernzone[ZONENAME_MAX];
if (getzoneid() != GLOBAL_ZONEID) {
return (Z_ERR);
}
return (Z_ERR);
}
return (Z_ERR);
}
return (Z_ERR);
}
return (Z_ERR);
}
return (Z_ERR);
}
return (Z_ERR);
}
if (!zonecfg_in_alt_root()) {
} else {
else
}
/*
* Look up from the kernel for 'running' zones.
*/
if (running) {
return (Z_ERR);
}
} else {
int err;
/* check whether the zone is ready or running */
gettext("could not get state"));
/* can't tell, so hedge */
} else {
zent->zstate_str =
}
return (Z_ERR);
}
return (Z_ERR);
}
switch (cmd_num) {
case CMD_UNINSTALL:
if (state == ZONE_STATE_CONFIGURED) {
return (Z_ERR);
}
break;
case CMD_CLONE:
case CMD_INSTALL:
if (state == ZONE_STATE_INSTALLED) {
return (Z_ERR);
} else if (state == ZONE_STATE_INCOMPLETE) {
return (Z_ERR);
}
break;
case CMD_MOVE:
case CMD_READY:
case CMD_BOOT:
case CMD_MOUNT:
if (state < ZONE_STATE_INSTALLED) {
return (Z_ERR);
}
break;
case CMD_VERIFY:
if (state == ZONE_STATE_INCOMPLETE) {
return (Z_ERR);
}
break;
case CMD_UNMOUNT:
if (state != ZONE_STATE_MOUNTED) {
return (Z_ERR);
}
break;
}
}
return (Z_OK);
}
static int
{
int arg;
if (zonecfg_in_alt_root()) {
return (Z_ERR);
}
optind = 0;
switch (arg) {
case '?':
default:
return (Z_USAGE);
}
}
return (Z_USAGE);
}
/*
* zoneadmd should be the one to decide whether or not to proceed,
* so even though it seems that the fourth parameter below should
* perhaps be B_TRUE, it really shouldn't be.
*/
return (Z_ERR);
}
static int
{
int arg;
if (zonecfg_in_alt_root()) {
return (Z_ERR);
}
optind = 0;
switch (arg) {
case '?':
default:
return (Z_USAGE);
}
}
if (argc > 0) {
return (Z_USAGE);
}
/*
* zoneadmd should be the one to decide whether or not to proceed,
* so even though it seems that the fourth parameter below should
* perhaps be B_TRUE, it really shouldn't be.
*/
return (Z_ERR);
return (Z_ERR);
}
static int
{
struct zone_rctltab rctltab;
return (Z_NOMEM);
}
return (error);
}
struct zone_rctlvaltab *rctlval;
if (!zonecfg_is_rctl(name)) {
"'%s'."), name);
continue;
}
!= Z_OK) {
"(priv=%s,limit=%s,action%s)"),
goto out;
}
"is not a valid value for rctl '%s'"),
name);
goto out;
}
}
}
out:
(void) zonecfg_endrctlent(handle);
return (error);
}
static int
{
char poolname[MAXPATHLEN];
int status;
int error;
/*
* This ends up being very similar to the check done in zoneadmd.
*/
/*
* No pool specified.
*/
return (0);
}
"configuration"), B_TRUE);
return (error);
}
/*
* Don't do anything if pools aren't enabled.
*/
"zone will not be bound to pool '%s'."), poolname);
return (Z_OK);
}
/*
* Try to provide a sane error message if the requested pool doesn't
* exist. It isn't clear that pools-related failures should
* necessarily translate to a failure to verify the zone configuration,
* hence they are not considered errors.
*/
"using default pool"));
return (Z_OK);
}
PO_SUCCESS) {
"using default pool"));
return (Z_OK);
}
(void) pool_conf_close(poolconf);
"using default pool"), poolname);
}
return (Z_OK);
}
static int
{
int return_code = Z_OK;
struct zone_fstab fstab;
char specdir[MAXPATHLEN];
/*
* TRANSLATION_NOTE
* inherit-pkg-dirs is a literal that should not be translated.
*/
"inherit-pkg-dirs: unable to enumerate mounts\n"));
return (Z_ERR);
}
/*
* Verify fs_dir exists.
*/
/*
* TRANSLATION_NOTE
* inherit-pkg-dir is a literal that should not be
* translated.
*/
"inherit-pkg-dir %s: %s\n"),
return_code = Z_ERR;
}
/*
* TRANSLATION_NOTE
* inherit-pkg-dir and NFS are literals that should
* not be translated.
*/
"inherit-pkg-dir %s: NFS mounted file-system.\n"
"\tA local file-system must be used.\n"),
return_code = Z_ERR;
}
}
(void) zonecfg_endipdent(handle);
return (return_code);
}
/* ARGSUSED */
static void
{
/*
* Do nothing - do not print the libzfs error messages.
*/
}
/*
* Verify that the ZFS dataset exists, and its mountpoint
* property is set to "legacy".
*/
static int
{
char propbuf[ZFS_MAXPROPLEN];
"could not access zfs dataset '%s'\n"),
return (Z_ERR);
}
"'%s' is not a filesystem\n"),
return (Z_ERR);
}
"mountpoint is not \"legacy\"\n"),
return (Z_ERR);
}
return (Z_OK);
}
/*
* Verify that the special device/filesystem exists and is valid.
*/
static int
{
return (verify_fs_zfs(fstab));
return (Z_ERR);
}
/*
* TRANSLATION_NOTE
* fs and NFS are literals that should
* not be translated.
*/
"fs %s: NFS mounted file-system.\n"
"\tA local file-system must be used.\n"),
return (Z_ERR);
}
return (Z_OK);
}
static int
{
int return_code = Z_OK;
struct zone_fstab fstab;
char cmdbuf[MAXPATHLEN];
/*
* No need to verify inherit-pkg-dir fs types, as their type is
* implicitly lofs, which is known. Therefore, the types are only
* verified for regular filesystems below.
*
* Since the actual mount point is not known until the dependent mounts
* are performed, we don't attempt any path validation here: that will
* happen later when zoneadmd actually does the mounts.
*/
"unable to enumerate mounts\n"));
return (Z_ERR);
}
return_code = Z_ERR;
goto next_fs;
}
/*
*/
return_code = Z_ERR;
goto next_fs;
}
return_code = Z_ERR;
goto next_fs;
}
"%s: %s is not a regular file\n"),
return_code = Z_ERR;
goto next_fs;
}
/*
* set.
*/
return_code = Z_ERR;
goto next_fs;
}
"%s: must specify 'raw' device for %s "
"file-systems\n"),
return_code = Z_ERR;
goto next_fs;
}
"'raw' device specified but "
"no fsck executable exists for %s\n"),
return_code = Z_ERR;
goto next_fs;
}
/* Verify fs_special. */
goto next_fs;
/* Verify fs_raw. */
/*
* TRANSLATION_NOTE
* fs is a literal that should not be translated.
*/
return_code = Z_ERR;
goto next_fs;
}
}
(void) zonecfg_endfsent(handle);
return (return_code);
}
const char *current_dataset;
/*
* Custom error handler for errors incurred as part of the checks below. We
* want to trim off the leading 'cannot open ...' to create a better error
* message. The only other way this can fail is if we fail to set the 'zoned'
* property. In this case we just pass the error on verbatim.
*/
static void
{
char buf[1024];
/*
* TRANSLATION_NOTE
* zfs and dataset are literals that should not be translated.
*/
else
}
/* ARGSUSED */
static int
{
int ret;
/*
* TRANSLATION_NOTE
* zfs and dataset are literals that should not be translated.
*/
"volumes cannot be specified as a zone dataset resource\n"),
zfs_get_name(zhp));
ret = -1;
} else {
}
return (ret);
}
/*
* Validate that the given dataset exists on the system, and that neither it nor
* its children are zvols.
*
* Note that we don't do anything with the 'zoned' property here. All
* management is done in zoneadmd when the zone is actually rebooted. This
* allows us to automatically set the zoned property even when a zone is
* rebooted by the administrator.
*/
static int
{
int return_code = Z_OK;
struct zone_dstab dstab;
char propbuf[ZFS_MAXPROPLEN];
char source[ZFS_MAXNAMELEN];
/*
* TRANSLATION_NOTE
* zfs and dataset are literals that should not be translated.
*/
"unable to enumerate datasets\n"));
return (Z_ERR);
}
return_code = Z_ERR;
continue;
}
sizeof (source), 0) == 0 &&
(srctype == ZFS_SRC_INHERITED)) {
"dataset %s: mountpoint cannot be inherited\n"),
return_code = Z_ERR;
continue;
}
"dataset %s: volumes cannot be specified as a "
"zone dataset resource\n"),
return_code = Z_ERR;
}
return_code = Z_ERR;
}
(void) zonecfg_enddsent(handle);
return (return_code);
}
static int
verify_details(int cmd_num)
{
struct zone_nwiftab nwiftab;
int return_code = Z_OK;
int err;
return (Z_ERR);
}
return (Z_ERR);
}
Z_OK) {
return (Z_ERR);
}
/*
* zonecfg_get_zonepath() gets its data from the XML repository.
* Verify this against the index file, which is checked first by
* zone_get_zonepath(). If they don't match, bail out.
*/
return (Z_ERR);
}
/*
* TRANSLATION_NOTE
* XML and zonepath are literals that should not be translated.
*/
"zonepath '%s',\nbut the index file has zonepath '%s'.\n"
"These must match, so fix the incorrect entry.\n"),
return (Z_ERR);
}
"because of the above errors.\n"), zonepath);
return_code = Z_ERR;
}
if (in_alt_root)
goto no_net;
return (Z_ERR);
}
/* skip any loopback interfaces */
continue;
"%s=%s %s=%s: %s\n"), "net", "address",
return_code = Z_ERR;
continue;
}
"%s=%s %s=%s: could not get socket: %s\n"), "net",
return_code = Z_ERR;
continue;
}
gettext("could not verify %s %s=%s %s=%s: %s\n"),
return_code = Z_ERR;
}
}
(void) zonecfg_endnwifent(handle);
return_code = Z_ERR;
return_code = Z_ERR;
return_code = Z_ERR;
return_code = Z_ERR;
return_code = Z_ERR;
if (return_code == Z_ERR)
gettext("%s: zone %s failed to verify\n"),
return (return_code);
}
static int
{
int arg;
optind = 0;
switch (arg) {
case '?':
default:
return (Z_USAGE);
}
}
return (Z_USAGE);
}
return (Z_ERR);
return (verify_details(CMD_VERIFY));
}
#define LUCREATEZONE "/usr/lib/lu/lucreatezone"
static int
{
/* 9: "exec " and " -z " */
int lockfd;
char zonepath[MAXPATHLEN];
int status;
if (zonecfg_in_alt_root()) {
return (Z_ERR);
}
optind = 0;
switch (arg) {
case '?':
default:
return (Z_USAGE);
}
}
return (Z_USAGE);
}
return (Z_ERR);
return (Z_ERR);
"zoneadm");
return (Z_ERR);
}
goto done;
}
/*
* According to the Application Packaging Developer's Guide, a
* "checkinstall" script when included in a package is executed as
* the user "install", if such a user exists, or by the user
* "nobody". In order to support this dubious behavior, the path
* to the zone being constructed is opened up during the life of
* the command laying down the zone's root file system. Once this
* has completed, regardless of whether it was successful, the
* path to the zone is again restricted.
*/
goto done;
}
goto done;
}
/*
* "exec" the command so that the returned status is that of
* LUCREATEZONE and not the shell.
*/
goto done;
}
goto done;
goto done;
}
done:
}
/*
* Check that the inherited pkg dirs are the same for the clone and its source.
* The easiest way to do that is check that the list of ipds is the same
* by matching each one against the other. This algorithm should be fine since
* the list of ipds should not be that long.
*/
static int
{
int err;
int s_cnt = 0;
int t_cnt = 0;
struct zone_fstab s_fstab;
struct zone_fstab t_fstab;
/*
* First check the source of the clone against the target.
*/
"inherit-pkg-dirs"));
return (Z_ERR);
}
s_cnt++;
"inherit-pkg-dirs"));
(void) zonecfg_endipdent(s_handle);
return (Z_ERR);
}
== 0) {
break;
}
}
(void) zonecfg_endipdent(t_handle);
if (!match) {
"'%s' is not configured in zone %s.\n"),
}
}
(void) zonecfg_endipdent(s_handle);
/* skip the next check if we already have errors */
return (res);
/*
* Now check the number of ipds in the target so we can verify
* that the source is not a subset of the target.
*/
"inherit-pkg-dirs"));
return (Z_ERR);
}
t_cnt++;
(void) zonecfg_endipdent(t_handle);
"with inherit-pkg-dirs that are not configured in zone "
}
return (res);
}
static void
{
int err;
struct zone_devtab s_devtab;
struct zone_devtab t_devtab;
return;
}
gettext("could not enumerate devices"));
(void) zonecfg_enddevent(t_handle);
return;
}
/*
* Use fnmatch to catch the case where wildcards
* were used in one zone and the other has an
*/
gettext("WARNING: device '%s' "
"is configured in both zones.\n"),
break;
}
}
(void) zonecfg_enddevent(s_handle);
}
(void) zonecfg_enddevent(t_handle);
}
/*
* Check if the specified mount option (opt) is contained within the
* options string.
*/
static boolean_t
{
char *p;
char *lastp;
return (B_TRUE);
return (B_TRUE);
}
}
return (B_FALSE);
}
#define RW_LOFS "WARNING: read-write lofs file-system on '%s' is configured " \
"in both zones.\n"
static void
{
/*
* It is ok to have shared lofs mounted fs but we want to warn if
* either is rw since this will effect the other zone.
*/
/* The default is rw so no options means rw */
return;
}
return;
}
}
return;
}
}
return;
}
/*
* TRANSLATION_NOTE
* The first variable is the file-system type and the second is
* the file-system special device. For example,
*/
}
static void
{
int err;
struct zone_fstab s_fstab;
struct zone_fstab t_fstab;
gettext("could not enumerate file-systems"));
return;
}
gettext("could not enumerate file-systems"));
(void) zonecfg_endfsent(t_handle);
return;
}
s_fstab.zone_fs_special) == 0) {
break;
}
}
(void) zonecfg_endfsent(s_handle);
}
(void) zonecfg_endfsent(t_handle);
}
/*
* We don't catch the case where you used the same IP address but
* it is not an exact string match. For example, 192.9.0.128 vs. 192.09.0.128.
* However, we're not going to worry about that but we will check for
* a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24)
* and handle that case as a match.
*/
static void
{
int err;
struct zone_nwiftab s_nwiftab;
struct zone_nwiftab t_nwiftab;
gettext("could not enumerate network interfaces"));
return;
}
char *p;
/* remove an (optional) netmask from the address */
*p = '\0';
gettext("could not enumerate network interfaces"));
(void) zonecfg_endnwifent(t_handle);
return;
}
/* remove an (optional) netmask from the address */
!= NULL)
*p = '\0';
s_nwiftab.zone_nwif_address) == 0) {
gettext("WARNING: network address '%s' "
"is configured in both zones.\n"),
break;
}
}
(void) zonecfg_endnwifent(s_handle);
}
(void) zonecfg_endnwifent(t_handle);
}
static void
{
int err;
struct zone_dstab s_dstab;
struct zone_dstab t_dstab;
return;
}
gettext("could not enumerate datasets"));
(void) zonecfg_enddsent(t_handle);
return;
}
s_dstab.zone_dataset_name) == 0) {
gettext("WARNING: dataset '%s' "
"is configured in both zones.\n"),
break;
}
}
(void) zonecfg_enddsent(s_handle);
}
(void) zonecfg_enddsent(t_handle);
}
static int
{
return (Z_ERR);
}
return (Z_ERR);
}
return (Z_ERR);
}
goto done;
}
/* verify new zone has same inherit-pkg-dirs */
/* warn about imported fs's which are the same */
/* warn about imported IP addresses which are the same */
/* warn about imported devices which are the same */
/* warn about imported datasets which are the same */
done:
}
static int
{
int status;
int err;
char *outfile;
}
if (!out_null)
"More information can be found in %s\n"), outfile);
return (err);
}
if (!out_null)
return (Z_OK);
}
/*
* Wait until the target_zone has booted to single-user. Return Z_OK once
* the zone has booted to that level or return Z_BAD_ZONE_STATE if the zone
* has not booted to single-user after the timeout.
*/
static int
{
int retry;
"= \"online\"",
int status;
if (WEXITSTATUS(status) == 0)
return (Z_OK);
(void) sleep(2);
} else {
return (Z_BAD_ZONE_STATE);
}
}
return (Z_BAD_ZONE_STATE);
}
/* ARGSUSED */
int
zfm_print(const char *p, void *r) {
zerror(" %s\n", p);
return (0);
}
static int
{
char cmdbuf[MAXPATHLEN];
char *source_zone = NULL;
int lockfd;
char zonepath[MAXPATHLEN];
char source_zonepath[MAXPATHLEN];
int status;
char *method = "copy";
struct stat unconfig_buf;
if (zonecfg_in_alt_root()) {
return (Z_ERR);
}
optind = 0;
switch (arg) {
case '?':
case 'm':
break;
default:
return (Z_USAGE);
}
}
return (Z_USAGE);
}
return (Z_ERR);
return (Z_ERR);
/*
* We also need to do some extra validation on the source zone.
*/
return (Z_ERR);
}
return (Z_ERR);
}
/* check whether the zone is ready or running */
!= Z_OK) {
/* can't tell, so hedge */
} else {
}
return (Z_ERR);
}
return (Z_ERR);
}
if (state != ZONE_STATE_INSTALLED) {
gettext("%s: zone %s is %s; %s is required.\n"),
return (Z_ERR);
}
/*
* The source zone checks out ok, continue with the clone.
*/
return (Z_ERR);
"zoneadm");
return (Z_ERR);
}
sizeof (source_zonepath))) != Z_OK) {
goto done;
}
!= Z_OK) {
goto done;
}
/* Don't clone the zone if anything is still mounted there */
"subdirectories of %s.\n"), source_zonepath);
goto done;
}
!= Z_OK) {
goto done;
}
goto done;
/*
* We have to set the state of the zone to installed so that we
* can boot it and sys-unconfig it from within the zone. However,
* if something fails during the boot/sys-unconfig, we want to set
* the state back to incomplete. We use the revert flag to keep
* track of this.
*/
goto done;
}
/*
* Check if the zone is already sys-unconfiged. This saves us
* the work of booting the zone so we can unconfigure it.
*/
zonepath);
"for sys-unconfig\n"));
goto done;
}
"for sys-unconfig\n"));
goto done;
}
gettext("\nsys-unconfig failed\n"));
/*
* The sys-unconfig halts the zone but if it failed,
* for some reason, we'll try to halt it now.
*/
goto done;
}
}
done:
(void) printf("\n");
if (revert)
}
static int
{
/* 6: "exec " and " " */
char *new_zonepath = NULL;
int lockfd;
char zonepath[MAXPATHLEN];
struct stat zonepath_buf;
struct stat new_zonepath_buf;
if (zonecfg_in_alt_root()) {
return (Z_ERR);
}
optind = 0;
switch (arg) {
case '?':
default:
return (Z_USAGE);
}
}
return (Z_USAGE);
}
return (Z_ERR);
return (Z_ERR);
/*
* Check out the new zonepath. This has the side effect of creating
* a directory for the new zonepath. We depend on this later when we
* stat to see if we are doing a cross file-system move or not.
*/
return (Z_ERR);
!= Z_OK) {
return (Z_ERR);
}
return (Z_ERR);
}
return (Z_ERR);
}
/* Don't move the zone if anything is still mounted there */
"subdirectories of %s.\n"), zonepath);
return (Z_ERR);
}
/*
* Check if we are moving in the same filesystem and can do a fast
* move or if we are crossing filesystems and have to copy the data.
*/
return (Z_ERR);
}
return (Z_ERR);
}
"zoneadm");
return (Z_ERR);
}
/*
* We're making some file-system changes now so we have to clean up
* the file-system before we are done. This will either clean up the
* new zonepath if the zonecfg update failed or it will clean up the
* old zonepath if everything is ok.
*/
if (fast) {
/* same filesystem, use rename for a quick move */
/*
* Remove the new_zonepath directory that got created above
* during the validation. It gets in the way of the rename.
*/
if (rmdir(new_zonepath) != 0) {
B_FALSE);
return (Z_ERR);
}
/*
* If this fails we don't need to do all of the
* cleanup that happens for the rest of the code
* so just return from this error.
*/
return (Z_ERR);
}
} else {
"Moving across file-systems; copying zonepath %s..."),
zonepath);
(void) printf("\n");
goto done;
}
goto done;
}
goto done;
}
done:
/*
* Clean up the file-system based on how things went. We either
* clean up the new zonepath if the operation failed for some reason
* or we clean up the old zonepath if everything is ok.
*/
if (revert) {
/* The zonecfg update failed, cleanup the new zonepath. */
if (fast) {
B_FALSE);
/*
* err is already != Z_OK since we're reverting
*/
}
} else {
int status;
/*
* "exec" the command so that the returned status is
* that of rm and not the shell.
*/
(void) printf("\n");
"zonepath"), B_TRUE);
} else {
/*
* Because we're reverting we know the mainline
* code failed but we just reused the err
* variable so we reset it back to Z_ERR.
*/
}
}
} else {
/* The move was successful, cleanup the old zonepath. */
if (!fast) {
int status;
(void) printf(
/*
* "exec" the command so that the returned status is
* that of rm and not the shell.
*/
(void) printf("\n");
B_TRUE);
}
}
}
}
/*
* On input, TRUE => yes, FALSE => no.
* On return, TRUE => 1, FALSE => 0, could not ask => -1.
*/
static int
{
if (!isatty(STDIN_FILENO))
return (-1);
for (;;) {
line[0] == '\n')
return (default_answer ? 1 : 0);
return (1);
return (0);
}
}
static int
{
/* 6: "exec " and " " */
int status;
if (zonecfg_in_alt_root()) {
return (Z_ERR);
}
optind = 0;
switch (arg) {
case '?':
case 'F':
break;
default:
return (Z_USAGE);
}
}
return (Z_USAGE);
}
return (Z_ERR);
if (!force) {
gettext("Are you sure you want to %s zone %s"),
return (Z_OK);
} else if (answer == -1) {
"not specified: %s not done."),
return (Z_ERR);
}
}
return (Z_ERR);
}
return (Z_ERR);
}
/*
* If there seems to be a zoneadmd running for this zone, call it
* to tell it that an uninstall is happening; if all goes well it
* will then shut itself down.
*/
/* we don't care too much if this fails... just plow on */
}
"zoneadm");
return (Z_ERR);
}
/* Don't uninstall the zone if anything is mounted there */
if (err) {
"subdirectories of %s.\n"), rootpath);
return (Z_ERR);
}
goto bad;
}
/*
* "exec" the command so that the returned status is that of
* RMCOMMAND and not the shell.
*/
devpath);
goto bad;
rootpath);
goto bad;
}
bad:
return (err);
}
/* ARGSUSED */
static int
{
if (argc > 0)
return (Z_USAGE);
return (Z_ERR);
return (Z_ERR);
return (Z_ERR);
}
return (Z_OK);
}
/* ARGSUSED */
static int
{
if (argc > 0)
return (Z_USAGE);
return (Z_ERR);
return (Z_ERR);
}
return (Z_OK);
}
static int
{
if (argc == 0) {
return (Z_OK);
}
optind = 0;
switch (arg) {
case '?':
default:
return (Z_USAGE);
}
}
/* Private commands have NULL short_usage; omit them */
return (Z_USAGE);
}
optind++;
}
return (Z_OK);
}
/*
* Returns: CMD_MIN thru CMD_MAX on success, -1 on error
*/
static int
{
int i;
/* return only if there is an exact match */
}
return (-1);
}
static int
{
if (i < 0)
}
static char *
get_execbasename(char *execfullname)
{
char *last_slash, *execbasename;
/* guard against '/' at end of command invocation */
for (;;) {
if (last_slash == NULL) {
break;
} else {
if (*execbasename == '\0') {
*last_slash = '\0';
continue;
}
break;
}
}
return (execbasename);
}
int
{
int arg;
locale = "C";
(void) textdomain(TEXT_DOMAIN);
target_zone = NULL;
if (chdir("/") != 0) {
}
switch (arg) {
case '?':
case 'z':
break;
if (*optarg != '/') {
}
gettext("root path must be a directory."));
}
break;
default:
}
}
}
}