3 # Copyright (c) 2003 Intel Corporation
6 # Copyright (c) 2004-2006 The Trustees of Princeton University
9 from __future__ import print_function
13 from Exceptions import *
19 ---------------------------------------------------------
20 This machine has entered a temporary debug state, so
21 Planetlab Support can login and fix any problems that
24 Please do not reboot this machine at this point, unless
25 specifically asked to.
28 ---------------------------------------------------------
32 # either at the end of the bm logic, because something failed (last_resort = True)
33 # and/or it can be invoked as a fallback very early in the bootmanager logic,
34 # so we can reach the node regardless of what happens (e.g. bm sometimes hangs)
36 def Run(vars, log, last_resort = True):
39 Bring up sshd inside the boot cd environment for debug purposes.
41 Once its running, touch the file /tmp/SSHD_RUNNING so future
42 calls to this function don't do anything.
44 Expect the following variables in vars to be set:
45 BM_SOURCE_DIR The source dir for the boot manager sources that
46 we are currently running from
50 message = "Starting debug mode"
52 message = "Starting fallback sshd"
55 log.write("\n\nStep: %s.\n"%message)
57 # make sure we have the variables we need
59 BM_SOURCE_DIR = vars["BM_SOURCE_DIR"]
60 if BM_SOURCE_DIR == "":
61 raise ValueError, "BM_SOURCE_DIR"
64 raise BootManagerException, "Missing variable in vars: %s\n" % var
65 except ValueError, var:
66 raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
69 ssh_source_files = "%s/debug_files/" % BM_SOURCE_DIR
71 key_gen_prog = "ssh-keygen"
72 ssh_home = "/root/.ssh"
73 cancel_boot_flag = "/tmp/CANCEL_BOOT"
74 sshd_started_flag = "/tmp/SSHD_RUNNING"
77 pre_sshd_script = os.path.join(ssh_source_files, "pre-sshd")
78 if os.path.exists(pre_sshd_script):
79 utils.sysexec_noerr(pre_sshd_script, log)
81 # create host keys if needed
82 if not os.path.isdir (ssh_dir):
83 utils.makedirs (ssh_dir)
85 # original code used to specify -b 1024 for the rsa1 key
86 # fedora23 seems to come with a release of openssh that lacks suppport
87 # for ssh1, and thus rsa1 keys; so we consider that failing to produce
88 # the rsa1 key is not a showstopper
90 ("/etc/ssh/ssh_host_key", 'rsa1', "SSH1 RSA", False),
91 ("/etc/ssh/ssh_host_rsa_key", 'rsa', "SSH2 RSA", True),
92 ("/etc/ssh/ssh_host_dsa_key", 'dsa', "SSH2 DSA", True),
95 for key_file, key_type, label, mandatory in key_specs:
96 if not os.path.exists(key_file):
97 log.write("Creating {} host key {}\n".format(label, key_file))
101 run = utils.sysexec_noerr
102 run("{} -q -t {} -f {} -C '' -N ''"\
103 .format(key_gen_prog, key_type, key_file), log)
104 run("chmod 600 {}".format(key_file), log)
105 run("chmod 644 {}.pub".format(key_file), log)
107 # (over)write sshd config
108 utils.sysexec("cp -f {}/sshd_config {}/sshd_config".format(ssh_source_files, ssh_dir), log)
110 ### xxx ### xxx ### xxx ### xxx ### xxx
112 # always update the key, may have changed in this instance of the bootmanager
113 log.write("Installing debug ssh key for root user\n")
114 if not os.path.isdir (ssh_home):
115 utils.makedirs(ssh_home)
116 utils.sysexec("cp -f {}/debug_root_ssh_key {}/authorized_keys".format(ssh_source_files, ssh_home), log)
117 utils.sysexec("chmod 700 {}".format(ssh_home), log)
118 utils.sysexec("chmod 600 {}/authorized_keys".format(ssh_home), log)
121 if not os.path.isfile(sshd_started_flag):
122 log.write("Starting sshd\n")
123 utils.sysexec("service sshd start", log)
124 # flag that ssh is running
125 utils.sysexec("touch {}".format(sshd_started_flag), log)
127 # it is expected that sshd is already running when last_resort==True
129 log.write("sshd is already running\n")
132 # this will make the initial script stop requesting scripts from PLC
133 utils.sysexec("touch {}".format(cancel_boot_flag), log)
136 print(warning_message)