3 # Copyright (c) 2003 Intel Corporation
6 # Copyright (c) 2004-2006 The Trustees of Princeton University
11 from Exceptions import *
18 ---------------------------------------------------------
19 This machine has entered a temporary debug state, so
20 Planetlab Support can login and fix any problems that
23 Please do not reboot this machine at this point, unless
24 specifically asked to.
27 ---------------------------------------------------------
31 # either at the end of the bm logic, because something failed (last_resort = True)
32 # and/or it can be invoked as a fallback very early in the bootmanager logic,
33 # so we can reach the node regardless of what happens (e.g. bm sometimes hangs)
35 def Run( vars, log, last_resort = True):
38 Bring up sshd inside the boot cd environment for debug purposes.
40 Once its running, touch the file /tmp/SSHD_RUNNING so future
41 calls to this function don't do anything.
43 Expect the following variables in vars to be set:
44 BM_SOURCE_DIR The source dir for the boot manager sources that
45 we are currently running from
46 BOOT_CD_VERSION A tuple of the current bootcd version
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"
63 BOOT_CD_VERSION= vars["BOOT_CD_VERSION"]
64 if BOOT_CD_VERSION == "":
65 raise ValueError, "BOOT_CD_VERSION"
68 raise BootManagerException, "Missing variable in vars: %s\n" % var
69 except ValueError, var:
70 raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
73 ssh_source_files= "%s/debug_files/" % BM_SOURCE_DIR
75 ssh_home= "/root/.ssh"
76 cancel_boot_flag= "/tmp/CANCEL_BOOT"
77 sshd_started_flag= "/tmp/SSHD_RUNNING"
79 # create host keys if needed
80 if not os.path.isdir (ssh_dir):
81 utils.makedirs (ssh_dir)
82 key=ssh_dir+"/ssh_host_key"
83 if not os.path.isfile (key):
84 log.write("Creating host rsa1 key %s\n"%key)
85 utils.sysexec( "ssh-keygen -t rsa1 -b 1024 -f %s -N ''" % key, log )
86 key=ssh_dir+"/ssh_host_rsa_key"
87 if not os.path.isfile (key):
88 log.write("Creating host rsa key %s\n"%key)
89 utils.sysexec( "ssh-keygen -t rsa -f %s -N ''" % key, log )
90 key=ssh_dir+"/ssh_host_dsa_key"
91 if not os.path.isfile (key):
92 log.write("Creating host dsa key %s\n"%key)
93 utils.sysexec( "ssh-keygen -d -f %s -N ''" % key, log )
95 if BOOT_CD_VERSION[0] >= 3:
96 utils.sysexec( "cp -f %s/sshd_config_v3 %s/sshd_config" % (ssh_source_files,ssh_dir), log )
98 utils.sysexec( "cp -f %s/sshd_config_v2 %s/sshd_config" % (ssh_source_files,ssh_dir), log )
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 if BOOT_CD_VERSION[0] == 2: utils.sysexec( "/usr/sbin/sshd", log )
112 else: utils.sysexec( "service sshd start", log )
113 # flag that ssh is running
114 utils.sysexec( "touch %s" % sshd_started_flag, log )
116 # it is expected that sshd is already running when last_resort==True
118 log.write( "sshd is already running\n" )
121 # this will make the initial script stop requesting scripts from PLC
122 utils.sysexec( "touch %s" % cancel_boot_flag, log )
124 # for ease of use, setup lvm on 2.x cds
125 if BOOT_CD_VERSION[0] == 2:
126 compatibility.setup_lvm_2x_cd(vars,log)
129 print warning_message