3 # Copyright (c) 2003 Intel Corporation
6 # Copyright (c) 2004-2006 The Trustees of Princeton University
11 from Exceptions import *
17 ---------------------------------------------------------
18 This machine has entered a temporary debug state, so
19 Planetlab Support can login and fix any problems that
22 Please do not reboot this machine at this point, unless
23 specifically asked to.
26 ---------------------------------------------------------
30 # either at the end of the bm logic, because something failed (last_resort = True)
31 # and/or it can be invoked as a fallback very early in the bootmanager logic,
32 # so we can reach the node regardless of what happens (e.g. bm sometimes hangs)
34 def Run( vars, log, last_resort = True):
37 Bring up sshd inside the boot cd environment for debug purposes.
39 Once its running, touch the file /tmp/SSHD_RUNNING so future
40 calls to this function don't do anything.
42 Expect the following variables in vars to be set:
43 BM_SOURCE_DIR The source dir for the boot manager sources that
44 we are currently running from
48 message="Starting debug mode"
50 message="Starting fallback sshd"
53 log.write( "\n\nStep: %s.\n"%message )
55 # make sure we have the variables we need
57 BM_SOURCE_DIR= vars["BM_SOURCE_DIR"]
58 if BM_SOURCE_DIR == "":
59 raise ValueError, "BM_SOURCE_DIR"
62 raise BootManagerException, "Missing variable in vars: %s\n" % var
63 except ValueError, var:
64 raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
67 ssh_source_files= "%s/debug_files/" % BM_SOURCE_DIR
69 ssh_home= "/root/.ssh"
70 cancel_boot_flag= "/tmp/CANCEL_BOOT"
71 sshd_started_flag= "/tmp/SSHD_RUNNING"
74 pre_sshd_script= os.path.join(ssh_source_files, "pre-sshd")
75 if os.path.exists(pre_sshd_script):
76 utils.sysexec_noerr( pre_sshd_script, log )
78 # create host keys if needed
79 if not os.path.isdir (ssh_dir):
80 utils.makedirs (ssh_dir)
81 key=ssh_dir+"/ssh_host_key"
82 if not os.path.isfile (key):
83 log.write("Creating host rsa1 key %s\n"%key)
84 utils.sysexec( "ssh-keygen -t rsa1 -b 1024 -f %s -N ''" % key, log )
85 key=ssh_dir+"/ssh_host_rsa_key"
86 if not os.path.isfile (key):
87 log.write("Creating host rsa key %s\n"%key)
88 utils.sysexec( "ssh-keygen -t rsa -f %s -N ''" % key, log )
89 key=ssh_dir+"/ssh_host_dsa_key"
90 if not os.path.isfile (key):
91 log.write("Creating host dsa key %s\n"%key)
92 # very old versions did 'ssh-keygen -d' instead of 'ssh-keygen -t dsa'
93 utils.sysexec( "ssh-keygen -t dsa -f %s -N ''" % key, log )
95 # (over)write sshd config
96 utils.sysexec( "cp -f %s/sshd_config %s/sshd_config" % (ssh_source_files,ssh_dir), log )
98 ### xxx ### xxx ### xxx ### xxx ### xxx
100 # always update the key, may have changed in this instance of the bootmanager
101 log.write( "Installing debug ssh key for root user\n" )
102 if not os.path.isdir ( ssh_home):
103 utils.makedirs( ssh_home )
104 utils.sysexec( "cp -f %s/debug_root_ssh_key %s/authorized_keys" % (ssh_source_files,ssh_home), log )
105 utils.sysexec( "chmod 700 %s" % ssh_home, log )
106 utils.sysexec( "chmod 600 %s/authorized_keys" % ssh_home, log )
109 if not os.path.isfile(sshd_started_flag):
110 log.write( "Starting sshd\n" )
111 utils.sysexec( "service sshd start", log )
112 # flag that ssh is running
113 utils.sysexec( "touch %s" % sshd_started_flag, log )
115 # it is expected that sshd is already running when last_resort==True
117 log.write( "sshd is already running\n" )
120 # this will make the initial script stop requesting scripts from PLC
121 utils.sysexec( "touch %s" % cancel_boot_flag, log )
124 print warning_message