summaryrefslogtreecommitdiffstats
path: root/scripts/mk-rescueimage.i386
blob: fdc8810dffc65f3548b3599c7256e39cf595db7a (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/python
#
# mk-rescueimage.i386 - builds a rescue image from an existing tree
#                       which can be converted into a isolinux bootable
#                       iso and used as a sysadmin rescue image.
#
#
# Usage: mk-rescueimage.i386 <src-tree> <dest-dir> <productname>
#
# the rescue image will be created as <dest-dir>/i386-rescueimage
#

import os
import sys
import string

def usage():
    print "usage: mk-rescueimage.i386 <toplevel> <dest-dir> <productname> [<productpath>]"
    sys.exit(0)


if len(sys.argv) < 5:
    usage()
    sys.exit(1)
    
if not os.access("%s/i386" % (sys.argv[2],), os.F_OK):
    print "ERROR - Destination directory %s does not contain an i386 directory!" % (sys.argv[2],)
    sys.exit(1)

srcdir  = sys.argv[1]
destdir = sys.argv[2]+"/i386-rescueimage"
productname = sys.argv[3]

if len(sys.argv) > 4:
    productpath = sys.argv[4]

# clean and create destination directory
os.system("rm -rf %s" % (destdir,))
os.system("mkdir %s" % (destdir,))

# copy documentation
for pat in ["*eula*", "*EULA*", "README*", "RELEASE*", "GPL", "RPM-*"]:
    os.system("cp -p %s/%s       %s/ 2>/dev/null" % (srcdir, pat, destdir))

# cp isolinux boot image
os.system("cp -a %s/isolinux     %s/" % (srcdir, destdir))

# cp stage image
os.system("mkdir -p %s/%s/base" % (destdir,productpath))
os.system("cp -a %s/%s/base/stage2.img %s/%s/base" % (srcdir, productpath, destdir, productpath))

# munge syslinux config to have a default of rescue mode
cfgfile = open("%s/isolinux/isolinux.cfg" % (destdir,), "r")
cfglines = cfgfile.readlines()
cfgfile.close()

# backup old one
os.system("cp %s/isolinux/isolinux.cfg %s/isolinux/isolinux.cfg.backup" % (destdir,destdir))

cfgfile = open("%s/isolinux/isolinux.cfg" % (destdir,), "w+")
fndit = 0
indefault = 0
defaultimg = None
for l in cfglines:
    if string.find(l, "default ") != -1:
	defaultimg = string.strip(string.split(l,' ')[1])
	cfgfile.write("default rescue\n")
	continue
    elif string.find(l, "label ") != -1:
	# see if we've left default stanza and entered a new one
	if fndit and indefault:
	    indefault = 0
	    cfgfile.write(rescuestanza)
	    
	if defaultimg is not None:
	    curimg = string.strip(string.split(l, ' ')[1])
	    if curimg == defaultimg:
		fndit = 1
		indefault = 1

		# build up the rescue stanza based on whats in the
		# default stanza as we echo it out
		rescuestanza = "label rescue\n"

	# if we fall through then write it out
	cfgfile.write(l)
    else:
	# we're writing out the matching stanza - if we hit the append
	# line put rescue on the end of it, otherwise just echo line
	if fndit and indefault:
	    if string.find(l, "append ") != -1:
		rescuestanza = rescuestanza + string.rstrip(l)+" rescue\n"
	    else:
		rescuestanza = rescuestanza + l

	cfgfile.write(l)

cfgfile.close()

if not fndit:
    print "Could not find default stanza, did not modify isolinux.cfg"
    os.system("mv -f %s/isolinux/isolinux.cfg.backup %s/isolinux/isolinux.cfg" % (destdir,destdir))
else:
    os.system("rm -f %s/isolinux/isolinux.cfg.backup" % (destdir,))

# synthesize a new syslinux screen
#

msgfile = open("%s/isolinux/boot.msg" % (destdir,), "w+")

msgfile.write(" \n")
msgfile.write("splash.lss\n")

# make "Red Hat" RED!
idx = string.find(productname, "Red Hat")
if idx != -1:
    productstr = productname[:idx]
    productstr = productstr + "02Red Hat07"
    productstr = productstr + productname[idx+7:]
else:
    productstr = productname

msgfile.write("This is the %s i386 Rescue CD.\n" % (productstr,))
msgfile.write("\n")
msgfile.write(" -  To enter rescue mode press the 0f<ENTER>07 key.\n")
msgfile.write("\n")
msgfile.write(" -  To install or upgrade %s type \"linux\" and then the 0f<ENTER>07 key.\n" % (productstr, ))
msgfile.write("\n")
msgfile.write(" -  Use the function keys listed below for more information.\n")
msgfile.write("\n")
msgfile.write("02[F1-Main] [F2-Options] [F3-General] [F4-Kernel] [F5-Rescue]07\n")
msgfile.close()