1 ##########################################################################
2 # Copyright (c) 2009-2016 ETH Zurich.
5 # This file is distributed under the terms in the attached LICENSE file.
6 # If you do not find this file, copies can be found by writing to:
7 # ETH Zurich D-INFK, Universitaetstr 6, CH-8092 Zurich. Attn: Systems Group.
8 ##########################################################################
10 import os, getpass, subprocess, socket, pty
11 import debug, eth_machinedata
12 from machines import Machine, MachineLockedError, MachineFactory,\
15 from subprocess_timeout import wait_or_terminate
17 TFTP_PATH='/home/netos/tftpboot'
18 TOOLS_PATH='/home/netos/tools/bin'
19 RACKBOOT=os.path.join(TOOLS_PATH, 'rackboot.sh')
20 RACKPOWER=os.path.join(TOOLS_PATH, 'rackpower')
22 class ETHBaseMachine(Machine):
25 def __init__(self, options,
27 serial_binary='serial_pc16550d',
30 super(ETHBaseMachine, self).__init__(options, operations,
31 serial_binary=serial_binary,
34 def get_perfcount_type(self):
35 return self._perfcount_type
37 class ETHBaseMachineOperations(MachineOperations):
39 def __init__(self, machine):
40 super(ETHBaseMachineOperations, self).__init__(machine)
41 self.lockprocess = None
44 def _get_console_status(self):
45 raise NotImplementedError
48 """Use conserver to lock the machine."""
50 # find out current status of console
51 cstate = self._get_console_status()
52 # check that nobody else has it open for writing
53 myuser = getpass.getuser()
54 parts = cstate.strip().split(':')
55 conname, child, contype, details, users, state = parts[:6]
57 for userinfo in users.split(','):
58 mode, username, host, port = userinfo.split('@')[:4]
59 if 'w' in mode and username != myuser:
60 raise MachineLockedError # Machine is not free
62 # run a console in the background to 'hold' the lock and read output
63 debug.verbose('starting "console %s"' % self._machine.get_machine_name())
64 # run on a PTY to work around terminal mangling code in console
65 (self.masterfd, slavefd) = pty.openpty()
66 self.lockprocess = subprocess.Popen(["console", self._machine.get_machine_name()],
68 stdout=slavefd, stdin=slavefd)
70 # XXX: open in binary mode with no buffering
71 # otherwise select.select() may block when there is data in the buffer
72 self.console_out = os.fdopen(self.masterfd, 'rb', 0)
75 if self.lockprocess is None:
77 debug.verbose('quitting console process (%d)' % self.lockprocess.pid)
78 # os.kill(self.lockprocess.pid, signal.SIGTERM)
79 os.write(self.masterfd, "\x05c.")
80 wait_or_terminate(self.lockprocess)
81 self.lockprocess = None
84 # this expects a pexpect object for `consolectrl`
85 def force_write(self, consolectrl):
87 consolectrl.send('\x05cf')
89 print "Unable to force write control through consolectrl, trying masterfd"
90 os.write(self.masterfd, "\x05cf")
93 return self.console_out
96 class ETHMachine(ETHBaseMachine):
97 _machines = eth_machinedata.machines
99 def __init__(self, options, **kwargs):
100 super(ETHMachine, self).__init__(options, ETHMachineOperations(self), **kwargs)
102 def get_buildall_target(self):
103 return self.get_bootarch().upper() + "_Full"
105 def get_xphi_ncores(self):
106 if 'xphi_ncores' in self._machines[self.name] :
107 return self._machines[self.name]['xphi_ncores']
111 def get_xphi_ncards(self):
112 if 'xphi_ncards' in self._machines[self.name] :
113 return self._machines[self.name]['xphi_ncards']
117 def get_xphi_ram_gb(self):
118 if 'xphi_ram_gb' in self._machines[self.name] :
119 return self._machines[self.name]['xphi_ram_gb']
123 def get_xphi_tickrate(self):
124 if 'xphi_tickrate' in self._machines[self.name] :
125 return self._machines[self.name]['xphi_tickrate']
129 def get_hostname(self):
130 return self.get_machine_name() + '.in.barrelfish.org'
133 return socket.gethostbyname(self.get_hostname())
135 class ETHMachineOperations(ETHBaseMachineOperations):
137 def __init__(self, machine):
138 super(ETHMachineOperations, self).__init__(machine)
140 def get_tftp_dir(self):
141 user = getpass.getuser()
142 return os.path.join(TFTP_PATH, user, self._machine.name + "_harness")
144 def get_tftp_subdir(self):
145 user = getpass.getuser()
146 return os.path.join(user, self._machine.name + "_harness")
148 def _write_menu_lst(self, data, path):
149 debug.verbose('writing %s' % path)
151 with open(path, 'w') as f:
155 def _get_menu_lst_name(self):
156 if self._machine.get_bootarch() == "armv8":
161 def _set_menu_lst(self, relpath):
162 ip_menu_name = os.path.join(TFTP_PATH, self._get_menu_lst_name() + "." + self._machine.get_ip())
163 debug.verbose('relinking %s to %s' % (ip_menu_name, relpath))
164 os.remove(ip_menu_name)
165 os.symlink(relpath, ip_menu_name)
167 def set_bootmodules(self, modules):
168 fullpath = os.path.join(self.get_tftp_dir(), self._get_menu_lst_name())
169 relpath = os.path.relpath(fullpath, TFTP_PATH)
170 tftppath = '/' + os.path.relpath(self.get_tftp_dir(), TFTP_PATH)
171 self._write_menu_lst(modules.get_menu_data(tftppath), fullpath)
172 self._set_menu_lst(relpath)
174 def _get_console_status(self):
175 debug.verbose('executing "console -i %s" to check state' %
176 self._machine.get_machine_name())
177 proc = subprocess.Popen(["console", "-i", self._machine.get_machine_name()],
178 stdout=subprocess.PIPE)
179 line = proc.communicate()[0]
180 assert(proc.returncode == 0)
183 def __rackboot(self, args):
184 debug.checkcmd([RACKBOOT] + args + [self._machine.get_machine_name()])
187 if self._machine.get_bootarch() == "armv8":
188 self.__rackboot(["-b", "-H", "-n"])
190 self.__rackboot(["-b", "-n"])
192 def __rackpower(self, arg):
194 debug.checkcmd([RACKPOWER, arg, self._machine.get_machine_name()])
195 except subprocess.CalledProcessError:
196 debug.warning("rackpower %s %s failed" %
197 (arg, self._machine.get_machine_name()))
200 self.__rackpower('-r')
203 self.__rackpower('-d')
206 for n in sorted(ETHMachine._machines.keys()):
207 class TmpMachine(ETHMachine):
209 MachineFactory.addMachine(n, TmpMachine, **ETHMachine._machines[n])