3 # Copyright (c) 2003 Intel Corporation
6 # Copyright (c) 2004-2006 The Trustees of Princeton University
8 # expected /proc/partitions format
10 from __future__ import print_function
12 import os, sys, shutil
20 from Exceptions import *
23 # the simplest way to debug is to let the node take off,
24 # ssh into it as root using the debug ssh key in /etc/planetlab
25 # then go to /tmp/source
26 # edit this file locally to turn on breakpoints if needed, then run
30 ### handling breakpoints in the startup process
31 import select, sys, string
33 ### global debugging settings
35 # enabling this will cause the node to ask for breakpoint-mode at startup
36 # production code should read False/False
38 # default for when prompt is turned off, or it's on but the timeout triggers
39 BREAKPOINT_MODE = False
41 # verbose mode is just fine
43 # in seconds : if no input, proceed
46 def prompt_for_breakpoint_mode():
48 global BREAKPOINT_MODE
50 default_answer = BREAKPOINT_MODE
56 sys.stdout.write ("Want to run in breakpoint mode ? {} ".format(display))
58 r, w, e = select.select ([sys.stdin], [], [], PROMPT_TIMEOUT)
60 answer = string.strip(sys.stdin.readline())
62 sys.stdout.write("\nTimed-out ({}s)".format(PROMPT_TIMEOUT))
64 BREAKPOINT_MODE = (answer == "y" or answer == "Y")
66 BREAKPOINT_MODE = default_answer
70 sys.stdout.write("\nCurrent BREAKPOINT_MODE is {}\n".format(label))
72 def breakpoint (message, cmd = None):
78 message = message + " -- Entering bash - type ^D to proceed"
84 print("Ignoring breakpoint (BREAKPOINT_MODE=False) : {}".format(message))
87 ########################################
90 from python docs for os.makedirs:
91 Throws an error exception if the leaf directory
92 already exists or cannot be created.
94 That is real useful. Instead, we'll create the directory, then use a
95 separate function to test for its existance.
97 Return 1 if the directory exists and/or has been created, a BootManagerException
98 otherwise. Does not test the writability of said directory.
107 raise BootManagerException("Unable to create directory tree: {}".format(path))
115 remove a directory tree, return 1 if successful, a BootManagerException
125 except OSError as desc:
126 raise BootManagerException("Unable to remove directory tree: {}".format(path))
131 def sysexec(cmd, log=None, fsck=False, shell=False):
133 execute a system command, output the results to the logger
136 return 1 if command completed (return code of non-zero),
137 0 if failed. A BootManagerException is raised if the command
138 was unable to execute or was interrupted by the user with Ctrl+C
141 # Thierry - Jan. 6 2011
142 # would probably make sense to look for | here as well
143 # however this is fragile and hard to test thoroughly
144 # let the caller set 'shell' when that is desirable
145 if shell or cmd.__contains__(">"):
146 prog = subprocess.Popen(cmd, shell=True)
148 log.write("sysexec (shell mode) >>> {}".format(cmd))
150 print("sysexec (shell mode) >>> {}".format(cmd))
152 prog = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
154 log.write("sysexec >>> {}\n".format(cmd))
156 print("sysexec >>> {}".format(cmd))
158 raise BootManagerException(
159 "Unable to create instance of subprocess.Popen "
160 "for command: {}".format(cmd))
162 (stdoutdata, stderrdata) = prog.communicate()
163 except KeyboardInterrupt:
164 raise BootManagerException("Interrupted by user")
166 # log stdout & stderr
169 log.write("==========stdout\n" + stdoutdata)
171 log.write("==========stderr\n" + stderrdata)
173 returncode = prog.wait()
176 # The exit code returned by fsck is the sum of the following conditions:
178 # 1 - File system errors corrected
179 # 2 - System should be rebooted
180 # 4 - File system errors left uncorrected
181 # 8 - Operational error
182 # 16 - Usage or syntax error
183 # 32 - Fsck canceled by user request
184 # 128 - Shared library error
185 if returncode != 0 and returncode != 1:
186 raise BootManagerException("Running {} failed (rc={})".format(cmd, returncode))
189 raise BootManagerException("Running {} failed (rc={})".format(cmd, returncode))
195 def sysexec_chroot(path, cmd, log=None, shell=False):
197 same as sysexec, but inside a chroot
200 release = os.uname()[2]
201 # 2.6.12 kernels need this
202 if release[:5] == "2.6.1":
203 library = "{}/lib/libc-opendir-hack.so".format(path)
204 if not os.path.exists(library):
205 shutil.copy("./libc-opendir-hack.so", library)
206 preload = "/bin/env LD_PRELOAD=/lib/libc-opendir-hack.so"
207 sysexec("chroot {} {} {}".format(path, preload, cmd), log, shell=shell)
210 def sysexec_chroot_noerr(path, cmd, log=None, shell=False):
212 same as sysexec_chroot, but capture boot manager exceptions
216 rc = sysexec_chroot(path, cmd, log, shell=shell)
217 except BootManagerException as e:
223 def sysexec_noerr(cmd, log=None, shell=False):
225 same as sysexec, but capture boot manager exceptions
229 rc = sysexec(cmd, log, shell=shell)
230 except BootManagerException as e:
239 change to a directory, return 1 if successful, a BootManagerException if failure
244 raise BootManagerException("Unable to change to directory: {}".format(dir))
250 def removefile(filepath):
252 removes a file, return 1 if successful, 0 if failure
257 raise BootManagerException("Unable to remove file: {}".format(filepath))
263 # from: http://forums.devshed.com/archive/t-51149/
264 # Ethernet-card-address-Through-Python-or-C
266 def get_mac_from_interface(ifname):
268 given a device name, like eth0, return its mac_address.
269 return None if the device doesn't exist.
272 SIOCGIFHWADDR = 0x8927 # magic number
274 s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
275 ifname = string.strip(ifname)
276 ifr = ifname + '\0'*(32-len(ifname))
279 r = fcntl.ioctl(s.fileno(), SIOCGIFHWADDR,ifr)
280 ret = ':'.join(["{:02x}".format(ord(n)) for n in r[18:24]])
286 def check_file_hash(filename, hash_filename):
287 """Check the file's integrity with a given hash."""
288 return sha1_file(filename) == open(hash_filename).read().split()[0].strip()
290 def sha1_file(filename):
291 """Calculate sha1 hash of file."""
299 f = file(filename, 'rb')
301 # 256 KB seems ideal for speed/memory tradeoff
302 # It wont get much faster with bigger blocks, but
304 block = f.read(256 * 1024)
309 # Simple trick to keep total heap even lower
310 # Delete the previous block, so while next one is read
311 # we wont have two allocated blocks with same size
315 raise BootManagerException("Cannot calculate SHA1 hash of {}".format(filename))
317 def display_disks_status(PARTITIONS, message, log):
318 log.write("{} - PARTITIONS status - BEG\n".format(message))
319 sysexec_noerr("vgdisplay", log)
320 sysexec_noerr("pvdisplay", log)
321 for name, path in PARTITIONS.items():
322 log.write("PARTITIONS[{}]={}\n".format(name,path))
323 sysexec_noerr("ls -l {}".format(path), log)
324 log.write("{} - PARTITIONS status - END\n".format(message))