3 # Copyright (c) 2003 Intel Corporation
6 # Copyright (c) 2004-2006 The Trustees of Princeton University
8 # expected /proc/partitions format
10 import os, sys, shutil
17 from Exceptions import *
19 ### handling breakpoints in the startup process
20 import select, sys, string
22 ### global debug settings
23 # NOTE. when BREAKPOINT_MODE turns out enabled,
24 # you have to attend the boot phase, that would hang otherwise
26 # enabling this will cause the node to ask for breakpoint-mode at startup
27 # production code should read False/False
29 # default for when prompt is turned off, or it's on but the timeout triggers
33 # in seconds : if no input, proceed
36 def prompt_for_breakpoint_mode ():
38 global BREAKPOINT_MODE
40 default_answer=BREAKPOINT_MODE
46 sys.stdout.write ("Want to run in breakpoint mode ? %s "%display)
48 r,w,e = select.select ([sys.stdin],[],[],PROMPT_TIMEOUT)
50 answer = string.strip(sys.stdin.readline())
52 sys.stdout.write("\nTimed-out (%d s)"%PROMPT_TIMEOUT)
54 BREAKPOINT_MODE = ( answer == "y" or answer == "Y")
56 BREAKPOINT_MODE = default_answer
60 sys.stdout.write("\nCurrent BREAKPOINT_MODE is %s\n"%label)
62 def breakpoint (message, cmd = None):
68 message=message+" -- Entering bash - type ^D to proceed"
73 ##############################
76 from python docs for os.makedirs:
77 Throws an error exception if the leaf directory
78 already exists or cannot be created.
80 That is real useful. Instead, we'll create the directory, then use a
81 separate function to test for its existance.
83 Return 1 if the directory exists and/or has been created, a BootManagerException
84 otherwise. Does not test the writability of said directory.
93 raise BootManagerException, "Unable to create directory tree: %s" % path
99 def removedir( path ):
101 remove a directory tree, return 1 if successful, a BootManagerException
110 shutil.rmtree( path )
111 except OSError, desc:
112 raise BootManagerException, "Unable to remove directory tree: %s" % path
118 def sysexec( cmd, log= None ):
120 execute a system command, output the results to the logger
123 return 1 if command completed (return code of non-zero),
124 0 if failed. A BootManagerException is raised if the command
125 was unable to execute or was interrupted by the user with Ctrl+C
128 print ("sysexec >>> %s" % cmd)
129 prog= popen2.Popen4( cmd, 0 )
131 raise BootManagerException, \
132 "Unable to create instance of popen2.Popen4 " \
133 "for command: %s" % cmd
137 for line in prog.fromchild:
139 except KeyboardInterrupt:
140 raise BootManagerException, "Interrupted by user"
142 returncode= prog.wait()
144 raise BootManagerException, "Running %s failed (rc=%d)" % (cmd,returncode)
150 globals()['_chroot_lib_copied'] = False
151 def sysexec_chroot( path, cmd, log= None ):
153 same as sysexec, but inside a chroot
156 release = os.uname()[2]
157 # 2.6.12 kernels need this
158 if release[:5] == "2.6.1":
159 library = "/lib/libc-opendir-hack.so"
160 if not globals()['_chroot_lib_copied']:
161 shutil.copy("./libc-opendir-hack.so", "%s%s" % (path, library))
162 globals()['_chroot_lib_copied'] = True
163 preload = "/bin/env LD_PRELOAD=%s" % library
164 return sysexec("chroot %s %s %s" % (path, preload, cmd), log)
167 def sysexec_chroot_noerr( path, cmd, log= None ):
169 same as sysexec_chroot, but capture boot manager exceptions
173 rc= sysexec_chroot( cmd, log )
174 except BootManagerException, e:
180 def sysexec_noerr( cmd, log= None ):
182 same as sysexec, but capture boot manager exceptions
186 rc= sysexec( cmd, log )
187 except BootManagerException, e:
196 change to a directory, return 1 if successful, a BootManagerException if failure
201 raise BootManagerException, "Unable to change to directory: %s" % dir
207 def removefile( filepath ):
209 removes a file, return 1 if successful, 0 if failure
212 os.remove( filepath )
214 raise BootManagerException, "Unable to remove file: %s" % filepath
220 # from: http://forums.devshed.com/archive/t-51149/
221 # Ethernet-card-address-Through-Python-or-C
224 return "%02x" % (ord(n))
226 def get_mac_from_interface(ifname):
228 given a device name, like eth0, return its mac_address.
229 return None if the device doesn't exist.
232 SIOCGIFHWADDR = 0x8927 # magic number
234 s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
235 ifname = string.strip(ifname)
236 ifr = ifname + '\0'*(32-len(ifname))
239 r= fcntl.ioctl(s.fileno(),SIOCGIFHWADDR,ifr)
240 addr = map(hexy,r[18:24])
241 ret = (':'.join(map(str, addr)))