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_noerr( cmd, log= None ):
98 same as sysexec, but capture boot manager exceptions
102 rc= sysexec( cmd, log )
103 except BootManagerException, e:
112 change to a directory, return 1 if successful, a BootManagerException if failure
117 raise BootManagerException, "Unable to change to directory: %s" % dir
123 def removefile( filepath ):
125 removes a file, return 1 if successful, 0 if failure
128 os.remove( filepath )
130 raise BootManagerException, "Unable to remove file: %s" % filepath
136 # from: http://forums.devshed.com/archive/t-51149/
137 # Ethernet-card-address-Through-Python-or-C
140 return "%02x" % (ord(n))
142 def get_mac_from_interface(ifname):
144 given a device name, like eth0, return its mac_address.
145 return None if the device doesn't exist.
148 SIOCGIFHWADDR = 0x8927 # magic number
150 s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
151 ifname = string.strip(ifname)
152 ifr = ifname + '\0'*(32-len(ifname))
155 r= fcntl.ioctl(s.fileno(),SIOCGIFHWADDR,ifr)
156 addr = map(hexy,r[18:24])
157 ret = (':'.join(map(str, addr)))
163 ### handling breakpoints in the startup process
164 import select, sys, string
166 ### global debug settings
167 # NOTE. when BREAKPOINT_MODE turns out enabled,
168 # you have to attend the boot phase, that would hang otherwise
170 # enabling this will cause the node to ask for breakpoint-mode at startup
171 # production code should read False/False
173 # if promt mode enabled, this default is taken
174 BREAKPOINT_MODE=False
175 # in seconds : if no input, proceed
178 def prompt_for_breakpoint_mode ():
180 global BREAKPOINT_MODE
183 sys.stdout.write ("Want to run in breakpoint mode ? y/[n] ")
185 r,w,e = select.select ([sys.stdin],[],[],PROMPT_TIMEOUT)
187 answer = string.strip(sys.stdin.readline())
189 sys.stdout.write("Timed-out is %ds\n"%PROMPT_TIMEOUT)
190 BREAKPOINT_MODE = ( answer == "y" or answer == "Y")
194 sys.stdout.write("Current BREAKPOINT_MODE is %s\n"%label)
196 def breakpoint (message, cmd = None):
202 message=message+" -- Entering bash - type ^D to proceed"