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 *
20 ### handling breakpoints in the startup process
21 import select, sys, string
23 ### global debug settings
24 # NOTE. when BREAKPOINT_MODE turns out enabled,
25 # you have to attend the boot phase, that would hang otherwise
27 # enabling this will cause the node to ask for breakpoint-mode at startup
28 # production code should read False/False
30 # default for when prompt is turned off, or it's on but the timeout triggers
34 # in seconds : if no input, proceed
37 def prompt_for_breakpoint_mode ():
39 global BREAKPOINT_MODE
41 default_answer=BREAKPOINT_MODE
47 sys.stdout.write ("Want to run in breakpoint mode ? %s "%display)
49 r,w,e = select.select ([sys.stdin],[],[],PROMPT_TIMEOUT)
51 answer = string.strip(sys.stdin.readline())
53 sys.stdout.write("\nTimed-out (%d s)"%PROMPT_TIMEOUT)
55 BREAKPOINT_MODE = ( answer == "y" or answer == "Y")
57 BREAKPOINT_MODE = default_answer
61 sys.stdout.write("\nCurrent BREAKPOINT_MODE is %s\n"%label)
63 def breakpoint (message, cmd = None):
69 message=message+" -- Entering bash - type ^D to proceed"
75 ########################################
78 from python docs for os.makedirs:
79 Throws an error exception if the leaf directory
80 already exists or cannot be created.
82 That is real useful. Instead, we'll create the directory, then use a
83 separate function to test for its existance.
85 Return 1 if the directory exists and/or has been created, a BootManagerException
86 otherwise. Does not test the writability of said directory.
95 raise BootManagerException, "Unable to create directory tree: %s" % path
101 def removedir( path ):
103 remove a directory tree, return 1 if successful, a BootManagerException
112 shutil.rmtree( path )
113 except OSError, desc:
114 raise BootManagerException, "Unable to remove directory tree: %s" % path
120 def sysexec( cmd, log= None ):
122 execute a system command, output the results to the logger
125 return 1 if command completed (return code of non-zero),
126 0 if failed. A BootManagerException is raised if the command
127 was unable to execute or was interrupted by the user with Ctrl+C
130 print ("sysexec >>> %s" % cmd)
131 prog= popen2.Popen4( cmd, 0 )
133 raise BootManagerException, \
134 "Unable to create instance of popen2.Popen4 " \
135 "for command: %s" % cmd
139 for line in prog.fromchild:
141 except KeyboardInterrupt:
142 raise BootManagerException, "Interrupted by user"
144 returncode= prog.wait()
146 raise BootManagerException, "Running %s failed (rc=%d)" % (cmd,returncode)
152 def sysexec_noerr( cmd, log= None ):
154 same as sysexec, but capture boot manager exceptions
158 rc= sysexec( cmd, log )
159 except BootManagerException, e:
168 change to a directory, return 1 if successful, a BootManagerException if failure
173 raise BootManagerException, "Unable to change to directory: %s" % dir
179 def removefile( filepath ):
181 removes a file, return 1 if successful, 0 if failure
184 os.remove( filepath )
186 raise BootManagerException, "Unable to remove file: %s" % filepath
192 # from: http://forums.devshed.com/archive/t-51149/
193 # Ethernet-card-address-Through-Python-or-C
196 return "%02x" % (ord(n))
198 def get_mac_from_interface(ifname):
200 given a device name, like eth0, return its mac_address.
201 return None if the device doesn't exist.
204 SIOCGIFHWADDR = 0x8927 # magic number
206 s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
207 ifname = string.strip(ifname)
208 ifr = ifname + '\0'*(32-len(ifname))
211 r= fcntl.ioctl(s.fileno(),SIOCGIFHWADDR,ifr)
212 addr = map(hexy,r[18:24])
213 ret = (':'.join(map(str, addr)))