File: builder

package info (click to toggle)
rootstrap 0.3.24-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 156 kB
  • ctags: 41
  • sloc: sh: 260; python: 185; makefile: 68
file content (96 lines) | stat: -rwxr-xr-x 3,349 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/python

import ConfigParser
import os
import sys

sys.stdout.write("builder running...\n")

# Initialization
os.system("mount -t tmpfs tmpfs /etc")
os.mkdir("/etc/rootstrap")
os.system("mount -t hostfs hostfs -o /etc/rootstrap /etc/rootstrap")
os.system("mount -t tmpfs tmpfs /dev")
os.system("mount -t proc proc /proc")
os.system("mount -t tmpfs tmpfs /tmp")
os.system("mount -t tmpfs tmpfs /lib/modules")
os.system("mount -t hostfs hostfs -o /usr/lib/uml/modules /lib/modules")
os.mkdir("/tmp/target")
os.mkdir("/tmp/host")
os.system("mount -t hostfs hostfs /tmp/host")

# Parse kernel command line
workdir = '~/.rootstrap'
configfile = None
for arg in open('/proc/cmdline').read().split():
    argsplit = arg.split('=')
    if len(argsplit) > 1:
        if argsplit[0] == 'rsworkdir':
            workdir = os.path.expanduser(argsplit[1])
        elif argsplit[0] == 'rsconfig':
            configfile = '/tmp/host' + \
			    os.path.expanduser(os.path.join('/tmp/host',os.path.expanduser(argsplit[1])))

os.system("mount -t hostfs hostfs -o %s /etc/rootstrap/workdir" % workdir)

def dispatch(module, vars):
    "Invoke a rootstrap module"

    for scriptpat in ('/etc/rootstrap/workdir/modules/%s',
                      '/etc/rootstrap/modules/%s',
                      '/usr/lib/rootstrap/modules/%s'):
        script = scriptpat % module

	if os.path.exists(script):
	    # try to ease module debugging
	    #
	    # debug_exit == 0 --> exit and raise exception
	    #               1 --> re-eval script that failed
	    #               2 --> go on with next script
	    #               X --> exit and raise exception
	    while 1:
                print "Using rootstrap module %s from:\n\t%s" % (module,script)
                status = os.spawnle(os.P_WAIT, script, script, vars)

                if status != 0:
	            if "debug" in vars and vars["debug"] == "true":
			print "Module %s failed with status %d: %s" % \
					(module,status,os.strerror(status))
			print "The exit value from the shell will be evaluated:"
			print "   0 --> exit and raise exception"
	                print "   1 --> re-eval script that failed"
	                print "   2 --> go on with next script"
	                print "   N --> exit and raise exception"
	                debug_exit = os.spawnle(os.P_WAIT, "/bin/sh", "/bin/sh", vars)
			if debug_exit == 1:
			    continue
			elif debug_exit == 2:
			    return

		    raise "rootstrap: Module '%s' failed, status %d: %s" % \
				(module,status,os.strerror(status))
		return

    raise "rootstrap: unknown module: %s\n" % module
        
config = ConfigParser.ConfigParser()
config.optionxform = str # case sensitive
config.readfp(open('/etc/rootstrap/rootstrap.conf'))
config.read(('/etc/rootstrap/workdir/rootstrap.conf',))
if configfile:
    config.read(configfile)

globalvars = { 'TARGET' : '/tmp/target', 'WORKDIR' : '/etc/rootstrap/workdir', 'HOST' : '/tmp/host' }
for var in config.options('global'):
    globalvars[var] = config.get('global', var)
    
for module in config.get('global', 'modules').split():
    modulevars = globalvars
    if config.has_section(module):
        for var in config.options(module):
            modulevars[var] = config.get(module,var)

    dispatch(module, modulevars)

os.system("mount -o remount,ro hostfs /")
os.system("/sbin/halt -d -f")