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 *
22 from python docs for os.makedirs:
23 Throws an error exception if the leaf directory
24 already exists or cannot be created.
26 That is real useful. Instead, we'll create the directory, then use a
27 separate function to test for its existance.
29 Return 1 if the directory exists and/or has been created, a BootManagerException
30 otherwise. Does not test the writability of said directory.
39 raise BootManagerException, "Unable to create directory tree: %s" % path
45 def removedir( path ):
47 remove a directory tree, return 1 if successful, a BootManagerException
58 raise BootManagerException, "Unable to remove directory tree: %s" % path
64 def sysexec( cmd, log= None ):
66 execute a system command, output the results to the logger
69 return 1 if command completed (return code of non-zero),
70 0 if failed. A BootManagerException is raised if the command
71 was unable to execute or was interrupted by the user with Ctrl+C
74 print ("sysexec >>> %s" % cmd)
75 prog= popen2.Popen4( cmd, 0 )
77 raise BootManagerException, \
78 "Unable to create instance of popen2.Popen4 " \
79 "for command: %s" % cmd
83 for line in prog.fromchild:
85 except KeyboardInterrupt:
86 raise BootManagerException, "Interrupted by user"
88 returncode= prog.wait()
90 raise BootManagerException, "Running %s failed (rc=%d)" % (cmd,returncode)
96 def sysexec_chroot( path, cmd, log= None ):
98 same as sysexec, but inside a chroot
101 release = os.uname()[2]
102 # 2.6.12 kernels need this
103 if release[:5] == "2.6.1":
104 library = "%s/lib/libc-opendir-hack.so" % path
105 if not os.path.exists(library):
106 shutil.copy("./libc-opendir-hack.so", library)
107 preload = "/bin/env LD_PRELOAD=/lib/libc-opendir-hack.so"
108 sysexec("chroot %s %s %s" % (path, preload, cmd), log)
111 def sysexec_chroot_noerr( path, cmd, log= None ):
113 same as sysexec_chroot, but capture boot manager exceptions
117 rc= syexec_chroot( cmd, log )
118 except BootManagerException, e:
124 def sysexec_noerr( cmd, log= None ):
126 same as sysexec, but capture boot manager exceptions
130 rc= sysexec( cmd, log )
131 except BootManagerException, e:
140 change to a directory, return 1 if successful, a BootManagerException if failure
145 raise BootManagerException, "Unable to change to directory: %s" % dir
151 def removefile( filepath ):
153 removes a file, return 1 if successful, 0 if failure
156 os.remove( filepath )
158 raise BootManagerException, "Unable to remove file: %s" % filepath
164 # from: http://forums.devshed.com/archive/t-51149/
165 # Ethernet-card-address-Through-Python-or-C
168 return "%02x" % (ord(n))
170 def get_mac_from_interface(ifname):
172 given a device name, like eth0, return its mac_address.
173 return None if the device doesn't exist.
176 SIOCGIFHWADDR = 0x8927 # magic number
178 s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
179 ifname = string.strip(ifname)
180 ifr = ifname + '\0'*(32-len(ifname))
183 r= fcntl.ioctl(s.fileno(),SIOCGIFHWADDR,ifr)
184 addr = map(hexy,r[18:24])
185 ret = (':'.join(map(str, addr)))
191 ### handling breakpoints in the startup process
192 import select, sys, string
194 ### global debug settings
195 # NOTE. when BREAKPOINT_MODE turns out enabled,
196 # you have to attend the boot phase, that would hang otherwise
198 # enabling this will cause the node to ask for breakpoint-mode at startup
199 # production code should read False/False
201 # default for when prompt is turned off, or it's on but the timeout triggers
202 BREAKPOINT_MODE=False
203 # in seconds : if no input, proceed
206 def prompt_for_breakpoint_mode ():
208 global BREAKPOINT_MODE
210 default_answer=BREAKPOINT_MODE
216 sys.stdout.write ("Want to run in breakpoint mode ? %s "%display)
218 r,w,e = select.select ([sys.stdin],[],[],PROMPT_TIMEOUT)
220 answer = string.strip(sys.stdin.readline())
222 sys.stdout.write("\nTimed-out (%d s)"%PROMPT_TIMEOUT)
224 BREAKPOINT_MODE = ( answer == "y" or answer == "Y")
226 BREAKPOINT_MODE = default_answer
230 sys.stdout.write("\nCurrent BREAKPOINT_MODE is %s\n"%label)
232 def breakpoint (message, cmd = None):
238 message=message+" -- Entering bash - type ^D to proceed"