zone-vnc-console revision 4070
3809N/A#!/usr/bin/python2.6
3809N/A
4070N/A# Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
4070N/A#
4070N/A# Licensed under the Apache License, Version 2.0 (the "License"); you may
4070N/A# not use this file except in compliance with the License. You may obtain
4070N/A# a copy of the License at
4070N/A#
4070N/A# http://www.apache.org/licenses/LICENSE-2.0
4070N/A#
4070N/A# Unless required by applicable law or agreed to in writing, software
4070N/A# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
4070N/A# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
4070N/A# License for the specific language governing permissions and limitations
4070N/A# under the License.
4070N/A
3809N/Aimport errno
3809N/Aimport os
3809N/Aimport pwd
3809N/Aimport smf_include
3809N/Aimport subprocess
3809N/Aimport sys
4070N/Aimport time
3809N/A
3809N/Afrom subprocess import CalledProcessError, check_call, Popen
3809N/Afrom tempfile import mkstemp
3809N/A
4070N/AGTF = "/usr/bin/gtf"
3809N/ASVCCFG = "/usr/sbin/svccfg"
3809N/ASVCPROP = "/usr/bin/svcprop"
3809N/AVNCSERVER = "/usr/bin/vncserver"
4070N/AXRANDR = "/usr/bin/xrandr"
3809N/AXSTARTUPHDR = "# WARNING: THIS FILE GENERATED BY SMF.\n" + \
3809N/A "# DO NOT EDIT THIS FILE. EDITS WILL BE LOST.\n"
4070N/AXRESOURCES = "[[ -f ~/.Xresources ]] && /usr/bin/xrdb -merge ~/.Xresources\n"
3809N/AXTERM = "/usr/bin/xterm"
4070N/A# Borderless, Monospsce font, point size 14, white foreground on black
4070N/A# background are reasonable defaults.
4070N/AXTERMOPTS = ' -b 0 -fa Monospace -fs 14 -fg white -bg black -title ' + \
4070N/A '"Zone Console: $ZONENAME"'
4070N/AXWININFO = "/usr/bin/xwininfo"
3809N/A# Enclose command in comments to prevent xterm consuming zlogin opts
3809N/AZLOGINOPTS = ' -e "/usr/bin/pfexec /usr/sbin/zlogin -C -E $ZONENAME"\n'
4070N/AXSTARTUP = XSTARTUPHDR + XRESOURCES + XTERM + XTERMOPTS + ZLOGINOPTS
3809N/A
3809N/A
3809N/Adef start():
3809N/A check_vncserver()
3809N/A homedir = os.environ.get('HOME')
3809N/A if not homedir:
3809N/A homedir = pwd.getpwuid(os.getuid()).pw_dir
3809N/A os.putenv("HOME", homedir)
3809N/A set_xstartup(homedir)
3809N/A
3809N/A try:
3809N/A fmri = os.environ['SMF_FMRI']
3809N/A zonename = fmri.rsplit(':', 1)[1]
3809N/A os.putenv("ZONENAME", zonename)
3809N/A desktop_name = zonename + ' console'
3809N/A # NOTE: 'geometry' below is that which matches the size of standard
3809N/A # 80 character undecorated xterm window using font style specified in
4070N/A # XTERMOPTS. The geometry doesn't matter too much because the display
4070N/A # will be resized using xrandr once the xterm geometry is established.
3809N/A cmd = [VNCSERVER, "-name", desktop_name, "-SecurityTypes=None",
3809N/A "-geometry", "964x580", "-localhost", "-autokill"]
3809N/A vnc = Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
3809N/A env=None)
3809N/A out, err = vnc.communicate()
3809N/A vncret = vnc.wait()
3809N/A if vncret != 0:
3809N/A print "Error starting VNC server: " + err
3809N/A return smf_include.SMF_EXIT_ERR_FATAL
3809N/A except Exception as e:
3809N/A print e
3809N/A return smf_include.SMF_EXIT_ERR_FATAL
3809N/A
3809N/A output = err.splitlines()
3809N/A for line in output:
3809N/A if line.startswith("New '%s' desktop is" % desktop_name):
3809N/A display = line.rpartition(' ')[2]
3809N/A host, display_num = display.split(':', 1)
3809N/A # set host prop
3809N/A port = 5900 + int(display_num)
3809N/A print "VNC port: %d" % port
3809N/A # set port num prop
3809N/A cmd = [SVCCFG, '-s', fmri, 'setprop', 'vnc/port', '=', 'integer:',
3809N/A str(port)]
3809N/A
3809N/A svccfg = subprocess.Popen(cmd, stdout=subprocess.PIPE,
3809N/A stderr=subprocess.PIPE)
3809N/A out, err = svccfg.communicate()
3809N/A retcode = svccfg.wait()
3809N/A if retcode != 0:
3809N/A print "Error updating 'vnc/port' property: " + err
3809N/A return smf_include.SMF_EXIT_ERR_FATAL
4070N/A resize_xserver(display, zonename)
4070N/A
3809N/A return smf_include.SMF_EXIT_OK
3809N/A
3809N/A
3809N/Adef stop():
3809N/A try:
3809N/A # first kill the SMF contract
3809N/A check_call(["/usr/bin/pkill", "-c", sys.argv[2]])
3809N/A except CalledProcessError as cpe:
3809N/A # 1 is returncode if no SMF contract processes were matched,
3809N/A # meaning they have already terminated.
3809N/A if cpe.returncode != 1:
3809N/A print "failed to kill the SMF contract: %s" % cpe
3809N/A return smf_include.SMF_EXIT_ERR_FATAL
3809N/A
3809N/A try:
3809N/A fmri = os.environ['SMF_FMRI']
3809N/A # reset port num prop to initial zero value
3809N/A cmd = [SVCCFG, '-s', fmri, 'setprop', 'vnc/port', '=', 'integer:',
3809N/A '0']
3809N/A svccfg = subprocess.Popen(cmd, stdout=subprocess.PIPE,
3809N/A stderr=subprocess.PIPE,)
3809N/A out, err = svccfg.communicate()
3809N/A retcode = svccfg.wait()
3809N/A if retcode != 0:
3809N/A print "Error resetting 'vnc/port' property: " + err
3809N/A return smf_include.SMF_EXIT_ERR_FATAL
3809N/A except Exception as e:
3809N/A print e
3809N/A return smf_include.SMF_EXIT_ERR_FATAL
3809N/A
3809N/A
3809N/Adef check_vncserver():
3809N/A if not os.path.exists(VNCSERVER):
3809N/A print("VNC console service not available on this compute node. "
3809N/A "%s is missing. Run 'pkg install x11/server/xvnc'"
3809N/A % VNCSERVER)
3809N/A return smf_include.SMF_EXIT_ERR_FATAL
3809N/A if not os.path.exists(XTERM):
3809N/A print("VNC console service not available on this compute node. "
3809N/A "%s is missing. Run 'pkg install terminal/xterm'"
3809N/A % XTERM)
3809N/A return smf_include.SMF_EXIT_ERR_FATAL
3809N/A
3809N/A
3809N/Adef set_xstartup(homedir):
3809N/A vncdir = os.path.join(homedir, '.vnc')
3809N/A xstartup_path = os.path.join(vncdir, 'xstartup')
3809N/A
3809N/A try:
3809N/A os.mkdir(vncdir)
3809N/A except OSError as ose:
3809N/A if ose.errno != errno.EEXIST:
3809N/A raise
3809N/A
3809N/A # Always clobber xstartup
3809N/A # stemp tuple = [fd, path]
3809N/A stemp = mkstemp(dir=vncdir)
3809N/A os.write(stemp[0], XSTARTUP)
3809N/A os.close(stemp[0])
3809N/A os.chmod(stemp[1], 0700)
3809N/A os.rename(stemp[1], xstartup_path)
3809N/A
3809N/A
4070N/Adef resize_xserver(display, zonename):
4070N/A """ Try to determine xterm window geometry and resize the Xvnc display
4070N/A to match using XRANDR. Treat failure as non-fatal since an
4070N/A incorrectly sized console is arguably better than none.
4070N/A """
4070N/A class UninitializedWindowError(Exception):
4070N/A pass
4070N/A
4070N/A class UnmappedWindowError(Exception):
4070N/A pass
4070N/A
4070N/A def _get_window_geometry(display, windowname):
4070N/A """ Find the xterm xwindow by name/title and extract its geometry
4070N/A Returns: tuple of window [width, height]
4070N/A Raises:
4070N/A UninitializedWindowError if window not yet initialized
4070N/A UnmappedWindowError if window is not viewable/mapped
4070N/A """
4070N/A cmd = [XWININFO, '-d', display, '-name', windowname]
4070N/A xwininfo = subprocess.Popen(cmd, stdout=subprocess.PIPE,
4070N/A stderr=subprocess.PIPE)
4070N/A out, err = xwininfo.communicate()
4070N/A retcode = xwininfo.wait()
4070N/A if retcode != 0:
4070N/A print "Error finding console xwindow info: " + err
4070N/A raise UninitializedWindowError
4070N/A
4070N/A width = None
4070N/A height = None
4070N/A mapped = False
4070N/A for line in out.splitlines():
4070N/A line = line.strip()
4070N/A if line.startswith("Map State:"):
4070N/A if line.split()[-1] != "IsViewable":
4070N/A # Window is not mapped yet.
4070N/A raise UnmappedWindowError
4070N/A else:
4070N/A mapped = True
4070N/A if line.startswith("Width:"):
4070N/A width = int(line.split()[1])
4070N/A elif line.startswith("Height:"):
4070N/A height = int(line.split()[1])
4070N/A if width and height and mapped:
4070N/A return [width, height]
4070N/A else:
4070N/A # What, no width and height???
4070N/A print "No window geometry info returned by " + XWINFINFO
4070N/A raise UnmappedWindowError
4070N/A
4070N/A retries = 10
4070N/A sleep = 1
4070N/A uninit_count = 0
4070N/A unmap_count = 0
4070N/A width = 0
4070N/A height = 0
4070N/A while uninit_count < retries and unmap_count < retries:
4070N/A try:
4070N/A width, height = _get_window_geometry(display,
4070N/A 'Zone Console: ' + zonename)
4070N/A print "Discovered xterm geometry: %d x %d" % (width, height)
4070N/A break
4070N/A except UninitializedWindowError:
4070N/A if uninit_count < retries:
4070N/A print "xterm window not initialized yet. Retrying in %ds" \
4070N/A % sleep
4070N/A uninit_count += 1
4070N/A time.sleep(sleep)
4070N/A continue
4070N/A else:
4070N/A print "xterm window is taking too long to initialize"
4070N/A break
4070N/A except UnmappedWindowError:
4070N/A if unmap_count < retries:
4070N/A print "Discovered xterm not mapped yet. Retrying in %ds" \
4070N/A % sleep
4070N/A unmap_count += 1
4070N/A time.sleep(sleep)
4070N/A continue
4070N/A else:
4070N/A print "Discovered xterm window is taking too long to map"
4070N/A break
4070N/A else:
4070N/A print "Too many failed attempts to discover xterm window geometry"
4070N/A return
4070N/A
4070N/A # Generate a mode line for width and height, with a refresh of 60.0Hz
4070N/A cmd = [GTF, str(width), str(height), '60.0', '-x']
4070N/A gtf = subprocess.Popen(cmd, stdout=subprocess.PIPE,
4070N/A stderr=subprocess.PIPE)
4070N/A out, err = gtf.communicate()
4070N/A retcode = gtf.wait()
4070N/A if retcode != 0:
4070N/A print "Error creating new modeline for VNC display: " + err
4070N/A return
4070N/A
4070N/A for line in out.splitlines():
4070N/A line = line.strip()
4070N/A if line.startswith('Modeline'):
4070N/A modeline = line.split('Modeline')[1]
4070N/A print "New optimal modeline for Xvnc server: " + modeline
4070N/A mode = modeline.split()
4070N/A break
4070N/A
4070N/A # Create a new mode for the Xvnc server using the modeline generated by gtf
4070N/A cmd = [XRANDR, '-d', display, '--newmode']
4070N/A cmd.extend(mode)
4070N/A newmode = subprocess.Popen(cmd, stdout=subprocess.PIPE,
4070N/A stderr=subprocess.PIPE)
4070N/A out, err = newmode.communicate()
4070N/A retcode = newmode.wait()
4070N/A if retcode != 0:
4070N/A print "Error creating new xrandr modeline for VNC display: " + err
4070N/A return
4070N/A
4070N/A # Add the new mode to the default display output
4070N/A modename = mode[0]
4070N/A cmd = [XRANDR, '-d', display, '--addmode', 'default', modename]
4070N/A addmode = subprocess.Popen(cmd, stdout=subprocess.PIPE,
4070N/A stderr=subprocess.PIPE)
4070N/A out, err = addmode.communicate()
4070N/A retcode = addmode.wait()
4070N/A if retcode != 0:
4070N/A print "Error adding new xrandr modeline for VNC display: " + err
4070N/A return
4070N/A
4070N/A # Activate the new mode on the default display output
4070N/A cmd = [XRANDR, '-d', display, '--output', 'default', '--mode', modename]
4070N/A addmode = subprocess.Popen(cmd, stdout=subprocess.PIPE,
4070N/A stderr=subprocess.PIPE)
4070N/A out, err = addmode.communicate()
4070N/A retcode = addmode.wait()
4070N/A if retcode != 0:
4070N/A print "Error setting new xrandr modeline for VNC display: " + err
4070N/A return
4070N/A
3809N/Aif __name__ == "__main__":
3809N/A os.putenv("LC_ALL", "C")
3809N/A smf_include.smf_main()