def _zonename(): """Get the zonname of the current system.""" cmd = DebugValues.get_value("bin_zonename") # pylint: disable=E1120 if cmd is not None: cmd = [cmd] else: cmd = ["/bin/zonename"] # if the command doesn't exist then bail. if not li.path_exists(cmd[0]): return # open a temporary file in text mode for compatible string handling fout = tempfile.TemporaryFile(mode="w+") ferrout = tempfile.TemporaryFile(mode="w+") p = pkg.pkgsubprocess.Popen(cmd, stdout=fout, stderr=ferrout) p.wait() if p.returncode != 0: cmd = " ".join(cmd) ferrout.seek(0) errout = "".join(ferrout.readlines()) ferrout.close() raise apx.LinkedImageException(cmd_failed=(p.returncode, cmd, errout)) # parse the command output fout.seek(0) l = fout.readlines()[0].rstrip() fout.close() return l
def _zoneadm_list_parse(line, cmd, output): """Parse zoneadm list -p output. It's possible for zonepath to contain a ":". If it does it will be escaped to be "\\:". (But note that if the zonepath contains a "\" it will not be escaped, which is argubaly a bug.)""" # zoneadm list output should never contain a NUL char, so # temporarily replace any escaped colons with a NUL, split the string # on any remaining colons, and then switch any NULs back to colons. tmp_char = "\0" fields = [ field.replace(tmp_char, ":") for field in line.replace(r"\:", tmp_char).split(":") ] try: # Unused variable; pylint: disable=W0612 z_id, z_name, z_state, z_path, z_uuid, z_brand, z_iptype = \ fields[:7] # pylint: enable=W0612 except ValueError: raise apx.LinkedImageException( cmd_output_invalid=(cmd, output)) return z_name, z_state, z_path, z_brand
def _list_zones(root, path_transform): """Get the zones associated with the image located at 'root'. We return a dictionary where the keys are zone names and the values are tuples containing zone root path and current state. The global zone is excluded from the results. Solaris10 branded zones are excluded from the results.""" rv = dict() cmd = DebugValues.get_value("bin_zoneadm") # pylint: disable=E1120 if cmd is not None: cmd = [cmd] else: cmd = ["/usr/sbin/zoneadm"] # if the command doesn't exist then bail. if not li.path_exists(cmd[0]): return rv # make sure "root" has a trailing '/' root = root.rstrip(os.sep) + os.sep # create the zoneadm command line cmd.extend(["-R", str(root), "list", "-cp"]) # execute zoneadm and save its output to a file # open a temporary file in text mode for compatible string handling fout = tempfile.TemporaryFile(mode="w+") ferrout = tempfile.TemporaryFile(mode="w+") p = pkg.pkgsubprocess.Popen(cmd, stdout=fout, stderr=ferrout) p.wait() if p.returncode != 0: cmd = " ".join(cmd) ferrout.seek(0) errout = "".join(ferrout.readlines()) ferrout.close() raise apx.LinkedImageException(cmd_failed=(p.returncode, cmd, errout)) # parse the command output fout.seek(0) output = fout.readlines() fout.close() for l in output: l = l.rstrip() z_name, z_state, z_path, z_brand = \ _zoneadm_list_parse(l, cmd, output) # skip brands that we don't care about # W0511 XXX / FIXME Comments; pylint: disable=W0511 # XXX: don't hard code brand names, use a brand attribute # pylint: enable=W0511 if z_brand not in ["lipkg", "solaris", "sn1", "labeled", "sparse"]: continue # we don't care about the global zone. if z_name == "global": continue # append "/root" to zonepath z_rootpath = os.path.join(z_path, "root") assert z_rootpath.startswith(root), \ "zone path '{0}' doesn't begin with '{1}".format( z_rootpath, root) # If there is a current path transform in effect then revert # the path reported by zoneadm to the original zone path. if li.path_transform_applied(z_rootpath, path_transform): z_rootpath = li.path_transform_revert(z_rootpath, path_transform) # we only care about zones that have been installed if z_state not in zone_installed_states: continue rv[z_name] = (z_rootpath, z_state) return rv