def rename(opts): """ Description: Rename the name of a Boot Environment. The following is the subcommand, options and args that make up the opts object passed in: rename beName newBeName Parameters: opts - A object containing the mount subcommand and all the options and arguments passed in on the command line mentioned above. Returns: 0 - Success 1 - Failure """ be = BootEnvironment() try: be_names = getopt.getopt(opts, "")[1] except getopt.GetoptError: msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1) usage() if len(be_names) != 2: msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1) usage() if lb.beVerifyBEName(be_names[0]) != 0: msg.printMsg(msg.Msgs.BEADM_ERR_BENAME, None, -1) return 1 if lb.beVerifyBEName(be_names[1]) != 0: msg.printMsg(msg.Msgs.BEADM_ERR_BENAME, None, -1) return 1 rc = lb.beRename(be_names[0], be_names[1]) if rc == 0: return 0 be.msg_buf["0"] = be_names[0] if rc == msg.Msgs.BE_ERR_BE_NOENT: be.msg_buf["1"] = msg.getMsg(msg.Msgs.BEADM_ERR_BE_DOES_NOT_EXIST, be_names[0]) elif rc == msg.Msgs.BE_ERR_PERM or rc == msg.Msgs.BE_ERR_ACCESS: be.msg_buf["1"] = msg.getMsg(msg.Msgs.BEADM_ERR_PERMISSIONS, rc) msg.printMsg(msg.Msgs.BEADM_ERR_RENAME, be.msg_buf, -1) return 1 else: be.msg_buf["1"] = lb.beGetErrDesc(rc) if be.msg_buf["1"] == None: be.msg_buf["1"] = msg.getMsg(msg.Msgs.BEADM_ERR_NO_MSG, rc) msg.printMsg(msg.Msgs.BEADM_ERR_RENAME, be.msg_buf, -1) return 1
def activate(opts): """ Function: activate Description: Activate a Boot Environment.The following is the subcommand, options and args that make up the opts object passed in: Parameters: opts - A string containing the active subcommand Returns: 0 - Success 1 - Failure """ if len(opts) != 1: msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1) usage() be = BootEnvironment() if lb.beVerifyBEName(opts[0]) != 0: msg.printMsg(msg.Msgs.BEADM_ERR_BENAME, None, -1) return 1 rc = lb.beActivate(opts[0]) if rc == 0: return 0 be.msg_buf["0"] = opts[0] if rc == msg.Msgs.BE_ERR_BE_NOENT: be.msg_buf["1"] = \ msg.getMsg(msg.Msgs.BEADM_ERR_BE_DOES_NOT_EXIST, opts[0]) elif rc == msg.Msgs.BE_ERR_PERM or rc == msg.Msgs.BE_ERR_ACCESS: be.msg_buf["1"] = msg.getMsg(msg.Msgs.BEADM_ERR_PERMISSIONS, rc) msg.printMsg(msg.Msgs.BEADM_ERR_ACTIVATE, be.msg_buf, -1) return 1 else: be.msg_buf["1"] = lb.beGetErrDesc(rc) if be.msg_buf["1"] == None: be.msg_buf["1"] = \ msg.getMsg(msg.Msgs.BEADM_ERR_NO_MSG, rc) msg.printMsg(msg.Msgs.BEADM_ERR_ACTIVATE, be.msg_buf, -1) return 1
def activate(opts): """ Function: activate Description: Activate a Boot Environment.The following is the subcommand, options and args that make up the opts object passed in: Parameters: opts - A string containing the active subcommand Returns: 0 - Success 1 - Failure """ if len(opts) != 1: msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1) usage() be = BootEnvironment() if lb.beVerifyBEName(opts[0]) != 0: msg.printMsg(msg.Msgs.BEADM_ERR_BENAME, None, -1) return 1 rc = lb.beActivate(opts[0]) if rc == 0: return 0 be.msg_buf["0"] = opts[0] if rc == msg.Msgs.BE_ERR_BE_NOENT: be.msg_buf["1"] = msg.getMsg(msg.Msgs.BEADM_ERR_BE_DOES_NOT_EXIST, opts[0]) elif rc == msg.Msgs.BE_ERR_PERM or rc == msg.Msgs.BE_ERR_ACCESS: be.msg_buf["1"] = msg.getMsg(msg.Msgs.BEADM_ERR_PERMISSIONS, rc) msg.printMsg(msg.Msgs.BEADM_ERR_ACTIVATE, be.msg_buf, -1) return 1 else: be.msg_buf["1"] = lb.beGetErrDesc(rc) if be.msg_buf["1"] == None: be.msg_buf["1"] = msg.getMsg(msg.Msgs.BEADM_ERR_NO_MSG, rc) msg.printMsg(msg.Msgs.BEADM_ERR_ACTIVATE, be.msg_buf, -1) return 1
def unmount(opts): """ Description: Unmount a Boot Environment. The following is the subcommand, options and args that make up the opts object passed in: unmount [-f] beName Parameters: opts - A object containing the unmount subcommand and all the options and arguments passed in on the command line mentioned above. Returns: 0 - Success 1 - Failure """ be = BootEnvironment() force_unmount = 0 # Counter for detecting multiple options. # e.g. beadm unmount -f -f newbe num_f_opts = 0 try: optlist, args = getopt.getopt(opts, "f") except getopt.GetoptError: msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1) usage() for opt, arg in optlist: if opt == "-f": force_unmount = 1 num_f_opts += 1 if num_f_opts > 1: msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1) usage() if len(args) != 1: msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1) usage() if lb.beVerifyBEName(args[0]) != 0: msg.printMsg(msg.Msgs.BEADM_ERR_BENAME, None, -1) return 1 rc = lb.beUnmount(args[0], force_unmount) if rc == 0: return 0 be.msg_buf["0"] = args[0] if rc == msg.Msgs.BE_ERR_UMOUNT_CURR_BE: be.msg_buf["1"] = msg.getMsg(msg.Msgs.BEADM_ERR_UNMOUNT_ACTIVE, args[0]) elif rc == msg.Msgs.BE_ERR_UMOUNT_SHARED: be.msg_buf["1"] = msg.getMsg(msg.Msgs.BEADM_ERR_SHARED_FS, args[0]) elif rc == msg.Msgs.BE_ERR_PERM or rc == msg.Msgs.BE_ERR_ACCESS: be.msg_buf["1"] = msg.getMsg(msg.Msgs.BEADM_ERR_PERMISSIONS, rc) msg.printMsg(msg.Msgs.BEADM_ERR_UNMOUNT, be.msg_buf, -1) return 1 else: be.msg_buf["1"] = lb.beGetErrDesc(rc) if be.msg_buf["1"] == None: be.msg_buf["1"] = msg.getMsg(msg.Msgs.BEADM_ERR_NO_MSG, rc) msg.printMsg(msg.Msgs.BEADM_ERR_UNMOUNT, be.msg_buf, -1) return 1
def mount(opts): """ Description: Mount a Boot Environment on a directory. The following is the subcommand, options and args that make up the opts object passed in: mount beName [mountpoint] Parameters: opts - A object containing the mount subcommand and all the options and arguments passed in on the command line mentioned above. Returns: 0 - Success 1 - Failure """ be = BootEnvironment() mountpoint = None try: be_name_mnt_point = getopt.getopt(opts, "")[1] except getopt.GetoptError: msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1) usage() mnt_point_len = len(be_name_mnt_point) if mnt_point_len != 2: msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1) usage() else: # Check for leading / in mount point mountpoint = be_name_mnt_point[1] if not mountpoint.startswith("/"): msg.printMsg(msg.Msgs.BEADM_ERR_MOUNTPOINT, mountpoint, -1) return 1 if lb.beVerifyBEName(be_name_mnt_point[0]) != 0: msg.printMsg(msg.Msgs.BEADM_ERR_BENAME, None, -1) return 1 rc = lb.beMount(be_name_mnt_point[0], mountpoint) if rc == 0: return 0 be.msg_buf["0"] = be_name_mnt_point[0] if rc == msg.Msgs.BE_ERR_MOUNTED: be.msg_buf["1"] = msg.getMsg(msg.Msgs.BEADM_ERR_MOUNT_EXISTS, be_name_mnt_point[0]) elif rc == msg.Msgs.BE_ERR_BE_NOENT: be.msg_buf["1"] = msg.getMsg(msg.Msgs.BEADM_ERR_BE_DOES_NOT_EXIST, be_name_mnt_point[0]) elif rc == msg.Msgs.BE_ERR_PERM or rc == msg.Msgs.BE_ERR_ACCESS: be.msg_buf["1"] = msg.getMsg(msg.Msgs.BEADM_ERR_PERMISSIONS, rc) msg.printMsg(msg.Msgs.BEADM_ERR_MOUNT, be.msg_buf, -1) return 1 else: be.msg_buf["1"] = lb.beGetErrDesc(rc) if be.msg_buf["1"] == None: be.msg_buf["1"] = msg.getMsg(msg.Msgs.BEADM_ERR_NO_MSG, rc) msg.printMsg(msg.Msgs.BEADM_ERR_MOUNT, be.msg_buf, -1) return 1
def destroy(opts): """ Function: destroy Description: Destroy a Boot Environment. The following is the subcommand, options and args that make up the opts object passed in: destroy [-fF] beName | beName@snapshot Parameters: opts - A object containing the destroy subcommand and all the options and arguments passed in on the command line mentioned above. Returns: 0 - Success 1 - Failure """ force_unmount = 0 suppress_prompt = False be_active_on_boot = None be = BootEnvironment() try: opts_args, be.trgt_be_name_or_snapshot = getopt.getopt(opts, "fF") except getopt.GetoptError: msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1) usage() # Counters for detecting multiple options. # e.g. beadm destroy -f -f newbe num_f_opts = 0 num_sf_opts = 0 for opt, arg in opts_args: if opt == "-f": force_unmount = 1 num_sf_opts += 1 elif opt == "-F": suppress_prompt = True num_f_opts += 1 if num_sf_opts > 1 or num_f_opts > 1: msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1) usage() if len(be.trgt_be_name_or_snapshot) != 1: msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1) usage() is_snapshot = False if "@" in be.trgt_be_name_or_snapshot[0]: is_snapshot = True be_name, snap_name = be.trgt_be_name_or_snapshot[0].split("@") if lb.beVerifyBEName(be_name) != 0: msg.printMsg(msg.Msgs.BEADM_ERR_BENAME, None, -1) return 1 else: if lb.beVerifyBEName(be.trgt_be_name_or_snapshot[0]) != 0: msg.printMsg(msg.Msgs.BEADM_ERR_BENAME, None, -1) return 1 # Get the 'active' BE and the 'active on boot' BE. be_active, be_active_on_boot = getActiveBEAndActiveOnBootBE() # If the user is trying to destroy the 'active' BE then quit now. if not is_snapshot and be_active == be.trgt_be_name_or_snapshot[0]: be.msg_buf["0"] = be.msg_buf["1"] = be_active msg.printMsg(msg.Msgs.BEADM_ERR_DESTROY_ACTIVE, be.msg_buf, -1) return 1 if not suppress_prompt: # Display a destruction question and wait for user response. # Quit if negative user response. if not displayDestructionQuestion(be): return 0 if is_snapshot: # Destroy a snapshot. rc = lb.beDestroySnapshot(be_name, snap_name) else: # Destroy a BE. Passing in 1 for the second arg destroys # any snapshots the BE may have as well. rc = lb.beDestroy(be.trgt_be_name_or_snapshot[0], 1, force_unmount) # Check if the BE that was just destroyed was the # 'active on boot' BE. If it was, display a message letting # the user know that the 'active' BE is now also the # 'active on boot' BE. if be_active_on_boot == be.trgt_be_name_or_snapshot[0] and rc == 0: msg.printMsg(msg.Msgs.BEADM_MSG_ACTIVE_ON_BOOT, be_active, -1) if rc == 0: try: shutil.rmtree("/var/log/beadm/" + be.trgt_be_name_or_snapshot[0], True) except: msg.printMsg(msg.Msgs.BEADM_ERR_LOG_RM, "/var/log/beadm/" + be.trgt_be_name_or_snapshot[0], -1) return 0 be.msg_buf["0"] = be.trgt_be_name_or_snapshot[0] if rc == msg.Msgs.BE_ERR_MOUNTED: be.msg_buf["1"] = be.msg_buf["2"] = be.trgt_be_name_or_snapshot[0] msg.printMsg(msg.Msgs.BEADM_ERR_MOUNTED, be.msg_buf, -1) return 1 elif rc == msg.Msgs.BE_ERR_DESTROY_CURR_BE: msg.printMsg(msg.Msgs.BEADM_ERR_DESTROY_ACTIVE, be.msg_buf["0"], -1) return 1 elif rc == msg.Msgs.BE_ERR_ZONES_UNMOUNT: be.msg_buf["1"] = be.trgt_be_name_or_snapshot[0] msg.printMsg(msg.Msgs.BE_ERR_ZONES_UNMOUNT, be.msg_buf, -1) return 1 elif rc == msg.Msgs.BE_ERR_PERM or rc == msg.Msgs.BE_ERR_ACCESS: be.msg_buf["1"] = msg.getMsg(msg.Msgs.BEADM_ERR_PERMISSIONS, rc) msg.printMsg(msg.Msgs.BEADM_ERR_DESTROY, be.msg_buf, -1) return 1 else: be.msg_buf["1"] = lb.beGetErrDesc(rc) if be.msg_buf["1"] == None: be.msg_buf["1"] = msg.getMsg(msg.Msgs.BEADM_ERR_NO_MSG, rc) msg.printMsg(msg.Msgs.BEADM_ERR_DESTROY, be.msg_buf, -1) return 1
def create(opts): """ Function: create Description: Create a Boot Environment. The following is the subcommand, options and args that make up the opts object passed in: create [-a] [-d description] [-e non-activeBeName | beName@Snapshot] [-o property=value] ... [-p zpool] beName create beName@Snapshot Parameters: opts - A object containing the create subcommand and all the options and arguments passed in on the command line mentioned above. Returns: 0 - Success 1 - Failure """ be = BootEnvironment() activate = False try: opts_args, be.trgt_be_name_or_snapshot = getopt.getopt(opts, "ad:e:o:p:") except getopt.GetoptError: msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1) usage() # Counters for detecting multiple options. # e.g. beadm create -p rpool -p rpool2 newbe num_a_opts = 0 num_e_opts = 0 num_p_opts = 0 num_d_opts = 0 for opt, arg in opts_args: if opt == "-a": activate = True num_a_opts += 1 elif opt == "-e": be.src_be_name_or_snapshot = arg num_e_opts += 1 elif opt == "-o": key, value = arg.split("=") be.properties[key] = value elif opt == "-p": be.trgt_rpool = arg num_p_opts += 1 elif opt == "-d": be.description = arg num_d_opts += 1 if num_a_opts > 1 or num_e_opts > 1 or num_p_opts > 1 or num_d_opts > 1: msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1) usage() # Check that all info provided from the user is legitimate. if verifyCreateOptionsArgs(be) != 0: usage() if initBELog("create", be) != 0: return 1 msg.printMsg(msg.Msgs.BEADM_MSG_BE_CREATE_START, be.trgt_be_name_or_snapshot[0], be.log_id) if "@" in be.trgt_be_name_or_snapshot[0]: # Create a snapshot rc = createSnapshot(be) else: if lb.beVerifyBEName(be.trgt_be_name_or_snapshot[0]) != 0: msg.printMsg(msg.Msgs.BEADM_ERR_BENAME, None, -1) return 1 # Create a BE based on a snapshot if be.src_be_name_or_snapshot is not None and "@" in be.src_be_name_or_snapshot: # Create a BE from a snapshot rc = createBEFromSnapshot(be) else: rc = createBE(be) # Activate the BE if the user chose to. if activate and rc == 0: rc = activateBE(be) cleanupBELog(be) return rc
def rename(opts): """ Description: Rename the name of a Boot Environment. The following is the subcommand, options and args that make up the opts object passed in: rename beName newBeName Parameters: opts - A object containing the mount subcommand and all the options and arguments passed in on the command line mentioned above. Returns: 0 - Success 1 - Failure """ be = BootEnvironment() try: be_names = getopt.getopt(opts, "")[1] except getopt.GetoptError: msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1) usage() if len(be_names) != 2: msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1) usage() if lb.beVerifyBEName(be_names[0]) != 0: msg.printMsg(msg.Msgs.BEADM_ERR_BENAME, None, -1) return 1 if lb.beVerifyBEName(be_names[1]) != 0: msg.printMsg(msg.Msgs.BEADM_ERR_BENAME, None, -1) return 1 rc = lb.beRename(be_names[0], be_names[1]) if rc == 0: return 0 be.msg_buf["0"] = be_names[0] if rc == msg.Msgs.BE_ERR_BE_NOENT: be.msg_buf["1"] = \ msg.getMsg(msg.Msgs.BEADM_ERR_BE_DOES_NOT_EXIST, be_names[0]) elif rc == msg.Msgs.BE_ERR_PERM or rc == msg.Msgs.BE_ERR_ACCESS: be.msg_buf["1"] = msg.getMsg(msg.Msgs.BEADM_ERR_PERMISSIONS, rc) msg.printMsg(msg.Msgs.BEADM_ERR_RENAME, be.msg_buf, -1) return 1 else: be.msg_buf["1"] = lb.beGetErrDesc(rc) if be.msg_buf["1"] == None: be.msg_buf["1"] = \ msg.getMsg(msg.Msgs.BEADM_ERR_NO_MSG, rc) msg.printMsg(msg.Msgs.BEADM_ERR_RENAME, be.msg_buf, -1) return 1
def unmount(opts): """ Description: Unmount a Boot Environment. The following is the subcommand, options and args that make up the opts object passed in: unmount [-f] beName Parameters: opts - A object containing the unmount subcommand and all the options and arguments passed in on the command line mentioned above. Returns: 0 - Success 1 - Failure """ be = BootEnvironment() force_unmount = 0 # Counter for detecting multiple options. # e.g. beadm unmount -f -f newbe num_f_opts = 0 try: optlist, args = getopt.getopt(opts, "f") except getopt.GetoptError: msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1) usage() for opt, arg in optlist: if opt == "-f": force_unmount = 1 num_f_opts += 1 if num_f_opts > 1: msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1) usage() if len(args) != 1: msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1) usage() if lb.beVerifyBEName(args[0]) != 0: msg.printMsg(msg.Msgs.BEADM_ERR_BENAME, None, -1) return 1 rc = lb.beUnmount(args[0], force_unmount) if rc == 0: return 0 be.msg_buf["0"] = args[0] if rc == msg.Msgs.BE_ERR_UMOUNT_CURR_BE: be.msg_buf["1"] = \ msg.getMsg(msg.Msgs.BEADM_ERR_UNMOUNT_ACTIVE, args[0]) elif rc == msg.Msgs.BE_ERR_UMOUNT_SHARED: be.msg_buf["1"] = \ msg.getMsg(msg.Msgs.BEADM_ERR_SHARED_FS, args[0]) elif rc == msg.Msgs.BE_ERR_PERM or rc == msg.Msgs.BE_ERR_ACCESS: be.msg_buf["1"] = msg.getMsg(msg.Msgs.BEADM_ERR_PERMISSIONS, rc) msg.printMsg(msg.Msgs.BEADM_ERR_UNMOUNT, be.msg_buf, -1) return 1 else: be.msg_buf["1"] = lb.beGetErrDesc(rc) if be.msg_buf["1"] == None: be.msg_buf["1"] = \ msg.getMsg(msg.Msgs.BEADM_ERR_NO_MSG, rc) msg.printMsg(msg.Msgs.BEADM_ERR_UNMOUNT, be.msg_buf, -1) return 1
def mount(opts): """ Description: Mount a Boot Environment on a directory. The following is the subcommand, options and args that make up the opts object passed in: mount beName [mountpoint] Parameters: opts - A object containing the mount subcommand and all the options and arguments passed in on the command line mentioned above. Returns: 0 - Success 1 - Failure """ be = BootEnvironment() mountpoint = None try: be_name_mnt_point = getopt.getopt(opts, "")[1] except getopt.GetoptError: msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1) usage() mnt_point_len = len(be_name_mnt_point) if mnt_point_len != 2: msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1) usage() else: # Check for leading / in mount point mountpoint = be_name_mnt_point[1] if not mountpoint.startswith('/'): msg.printMsg(msg.Msgs.BEADM_ERR_MOUNTPOINT, mountpoint, -1) return 1 if lb.beVerifyBEName(be_name_mnt_point[0]) != 0: msg.printMsg(msg.Msgs.BEADM_ERR_BENAME, None, -1) return 1 rc = lb.beMount(be_name_mnt_point[0], mountpoint) if rc == 0: return 0 be.msg_buf["0"] = be_name_mnt_point[0] if rc == msg.Msgs.BE_ERR_MOUNTED: be.msg_buf["1"] = \ msg.getMsg(msg.Msgs.BEADM_ERR_MOUNT_EXISTS, be_name_mnt_point[0]) elif rc == msg.Msgs.BE_ERR_BE_NOENT: be.msg_buf["1"] = \ msg.getMsg(msg.Msgs.BEADM_ERR_BE_DOES_NOT_EXIST, be_name_mnt_point[0]) elif rc == msg.Msgs.BE_ERR_PERM or rc == msg.Msgs.BE_ERR_ACCESS: be.msg_buf["1"] = msg.getMsg(msg.Msgs.BEADM_ERR_PERMISSIONS, rc) msg.printMsg(msg.Msgs.BEADM_ERR_MOUNT, be.msg_buf, -1) return 1 else: be.msg_buf["1"] = lb.beGetErrDesc(rc) if be.msg_buf["1"] == None: be.msg_buf["1"] = \ msg.getMsg(msg.Msgs.BEADM_ERR_NO_MSG, rc) msg.printMsg(msg.Msgs.BEADM_ERR_MOUNT, be.msg_buf, -1) return 1
def list(opts): """ Description: List the attributes of a Boot Environment. The following is the subcommand, options and args that make up the opts object passed in: list [[-a] | [-d] [-s]] [-H] [beName] -a displays all info -d displays BE info plus dataset info -s displays BE info plus snapshot info -H displays info parsable by a computer Parameters: opts - A object containing the list subcommand and all the options and arguments passed in on the command line mentioned above. Returns: 0 - Success 1 - Failure """ be = BootEnvironment() list_all_attrs = "" list_datasets = "" list_snapshots = "" dont_display_headers = False be_name = None be_list = None # Counters for detecting multiple options. # e.g. beadm list -a -a newbe num_a_opts = 0 num_d_opts = 0 num_s_opts = 0 num_h_opts = 0 try: opts_args, be.trgt_be_name_or_snapshot = getopt.getopt(opts, "adHs") except getopt.GetoptError: msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1) usage() for opt, arg in opts_args: if opt == "-a": list_all_attrs = opt num_a_opts += 1 elif opt == "-d": list_datasets = opt num_d_opts += 1 elif opt == "-s": list_snapshots = opt num_s_opts += 1 elif opt == "-H": dont_display_headers = True num_h_opts += 1 if num_a_opts > 1 or num_d_opts > 1 or num_s_opts > 1 or num_h_opts > 1: msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1) usage() if len(be.trgt_be_name_or_snapshot) > 1: msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1) usage() if len(be.trgt_be_name_or_snapshot) == 1: be_name = be.trgt_be_name_or_snapshot[0] if lb.beVerifyBEName(be_name) != 0: msg.printMsg(msg.Msgs.BEADM_ERR_BENAME, None, -1) return 1 if (list_all_attrs == "-a" and (list_datasets == "-d" \ or list_snapshots == "-s")): msg.printMsg(msg.Msgs.BEADM_ERR_MUTUALLY_EXCL, list_all_attrs + " " + list_datasets + " " + list_snapshots, -1) usage() list_options = "" # When zones are implemented add "listZones == "-z" below # Coelesce options to pass to displayBEs if (list_datasets == "-d" and list_snapshots == "-s" or \ list_all_attrs == "-a"): list_options = "-a" elif list_datasets != "" or list_snapshots != "" or list_all_attrs != "": list_options = list_datasets + " " + list_snapshots rc, be_list = lb.beList() if rc != 0: if rc == msg.Msgs.BE_ERR_BE_NOENT: if be_name == None: msg.printMsg(msg.Msgs.BEADM_ERR_NO_BES_EXIST, None, -1) return 1 string = \ msg.getMsg(msg.Msgs.BEADM_ERR_BE_DOES_NOT_EXIST, be_name) else: string = lb.beGetErrDesc(rc) if string == None: string = \ msg.getMsg(msg.Msgs.BEADM_ERR_NO_MSG, rc) msg.printMsg(msg.Msgs.BEADM_ERR_LIST, string, -1) return 1 # classify according to command line options if list_options.find("-a") != -1 or \ (list_options.find("-d") != -1 and list_options.find("-s") != -1): list_object = CompleteList(dont_display_headers) #all elif list_options.find("-d") != -1: list_object = DatasetList(dont_display_headers) #dataset elif list_options.find("-s") != -1: list_object = SnapshotList(dont_display_headers) #snapshot else: list_object = BEList(dont_display_headers) #only BE # use list method for object if list_object.list(be_list, dont_display_headers, be_name) != 0: msg.printMsg(msg.Msgs.BEADM_ERR_LIST_DATA, None, -1) return 1 return 0
def destroy(opts): """ Function: destroy Description: Destroy a Boot Environment. The following is the subcommand, options and args that make up the opts object passed in: destroy [-fF] beName | beName@snapshot Parameters: opts - A object containing the destroy subcommand and all the options and arguments passed in on the command line mentioned above. Returns: 0 - Success 1 - Failure """ force_unmount = 0 suppress_prompt = False be_active_on_boot = None be = BootEnvironment() try: opts_args, be.trgt_be_name_or_snapshot = getopt.getopt(opts, "fF") except getopt.GetoptError: msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1) usage() # Counters for detecting multiple options. # e.g. beadm destroy -f -f newbe num_f_opts = 0 num_sf_opts = 0 for opt, arg in opts_args: if opt == "-f": force_unmount = 1 num_sf_opts += 1 elif opt == "-F": suppress_prompt = True num_f_opts += 1 if num_sf_opts > 1 or num_f_opts > 1: msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1) usage() if len(be.trgt_be_name_or_snapshot) != 1: msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1) usage() is_snapshot = False if "@" in be.trgt_be_name_or_snapshot[0]: is_snapshot = True be_name, snap_name = be.trgt_be_name_or_snapshot[0].split("@") if lb.beVerifyBEName(be_name) != 0: msg.printMsg(msg.Msgs.BEADM_ERR_BENAME, None, -1) return 1 else: if lb.beVerifyBEName(be.trgt_be_name_or_snapshot[0]) != 0: msg.printMsg(msg.Msgs.BEADM_ERR_BENAME, None, -1) return 1 # Get the 'active' BE and the 'active on boot' BE. be_active, be_active_on_boot = getActiveBEAndActiveOnBootBE() # If the user is trying to destroy the 'active' BE then quit now. if not is_snapshot and be_active == be.trgt_be_name_or_snapshot[0]: be.msg_buf["0"] = be.msg_buf["1"] = be_active msg.printMsg(msg.Msgs.BEADM_ERR_DESTROY_ACTIVE, be.msg_buf, -1) return 1 if not suppress_prompt: # Display a destruction question and wait for user response. # Quit if negative user response. if not displayDestructionQuestion(be): return 0 if is_snapshot: # Destroy a snapshot. rc = lb.beDestroySnapshot(be_name, snap_name) else: # Destroy a BE. Passing in 1 for the second arg destroys # any snapshots the BE may have as well. rc = lb.beDestroy(be.trgt_be_name_or_snapshot[0], 1, force_unmount) # Check if the BE that was just destroyed was the # 'active on boot' BE. If it was, display a message letting # the user know that the 'active' BE is now also the # 'active on boot' BE. if be_active_on_boot == be.trgt_be_name_or_snapshot[0] and rc == 0: msg.printMsg(msg.Msgs.BEADM_MSG_ACTIVE_ON_BOOT, be_active, -1) if rc == 0: try: shutil.rmtree("/var/log/beadm/" + \ be.trgt_be_name_or_snapshot[0], True) except: msg.printMsg(msg.Msgs.BEADM_ERR_LOG_RM, "/var/log/beadm/" + be.trgt_be_name_or_snapshot[0], -1) return 0 be.msg_buf["0"] = be.trgt_be_name_or_snapshot[0] if rc == msg.Msgs.BE_ERR_MOUNTED: be.msg_buf["1"] = be.msg_buf["2"] = be.trgt_be_name_or_snapshot[0] msg.printMsg(msg.Msgs.BEADM_ERR_MOUNTED, be.msg_buf, -1) return 1 elif rc == msg.Msgs.BE_ERR_DESTROY_CURR_BE: msg.printMsg(msg.Msgs.BEADM_ERR_DESTROY_ACTIVE, \ be.msg_buf["0"], -1) return 1 elif rc == msg.Msgs.BE_ERR_ZONES_UNMOUNT: be.msg_buf["1"] = be.trgt_be_name_or_snapshot[0] msg.printMsg(msg.Msgs.BE_ERR_ZONES_UNMOUNT, be.msg_buf, -1) return 1 elif rc == msg.Msgs.BE_ERR_PERM or rc == msg.Msgs.BE_ERR_ACCESS: be.msg_buf["1"] = msg.getMsg(msg.Msgs.BEADM_ERR_PERMISSIONS, rc) msg.printMsg(msg.Msgs.BEADM_ERR_DESTROY, be.msg_buf, -1) return 1 else: be.msg_buf["1"] = lb.beGetErrDesc(rc) if be.msg_buf["1"] == None: be.msg_buf["1"] = \ msg.getMsg(msg.Msgs.BEADM_ERR_NO_MSG, rc) msg.printMsg(msg.Msgs.BEADM_ERR_DESTROY, be.msg_buf, -1) return 1
def create(opts): """ Function: create Description: Create a Boot Environment. The following is the subcommand, options and args that make up the opts object passed in: create [-a] [-d description] [-e non-activeBeName | beName@Snapshot] [-o property=value] ... [-p zpool] beName create beName@Snapshot Parameters: opts - A object containing the create subcommand and all the options and arguments passed in on the command line mentioned above. Returns: 0 - Success 1 - Failure """ be = BootEnvironment() activate = False try: opts_args, be.trgt_be_name_or_snapshot = getopt.getopt(opts, "ad:e:o:p:") except getopt.GetoptError: msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1) usage() # Counters for detecting multiple options. # e.g. beadm create -p rpool -p rpool2 newbe num_a_opts = 0 num_e_opts = 0 num_p_opts = 0 num_d_opts = 0 for opt, arg in opts_args: if opt == "-a": activate = True num_a_opts += 1 elif opt == "-e": be.src_be_name_or_snapshot = arg num_e_opts += 1 elif opt == "-o": key, value = arg.split("=") be.properties[key] = value elif opt == "-p": be.trgt_rpool = arg num_p_opts += 1 elif opt == "-d": be.description = arg num_d_opts += 1 if num_a_opts > 1 or num_e_opts > 1 or num_p_opts > 1 or num_d_opts > 1: msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1) usage() # Check that all info provided from the user is legitimate. if (verifyCreateOptionsArgs(be) != 0): usage() if initBELog("create", be) != 0: return 1 msg.printMsg(msg.Msgs.BEADM_MSG_BE_CREATE_START, be.trgt_be_name_or_snapshot[0], be.log_id) if '@' in be.trgt_be_name_or_snapshot[0]: # Create a snapshot rc = createSnapshot(be) else: if lb.beVerifyBEName(be.trgt_be_name_or_snapshot[0]) != 0: msg.printMsg(msg.Msgs.BEADM_ERR_BENAME, None, -1) return 1 # Create a BE based on a snapshot if be.src_be_name_or_snapshot is not None and \ '@' in be.src_be_name_or_snapshot: # Create a BE from a snapshot rc = createBEFromSnapshot(be) else: rc = createBE(be) # Activate the BE if the user chose to. if activate and rc == 0: rc = activateBE(be) cleanupBELog(be) return rc