summaryrefslogtreecommitdiffstats
path: root/scripts/mk-rescueimage.ppc
blob: 1197fecdfa588f02c6bd8f0bda2151ba776cb3c6 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/python
#
# mk-rescueimage.ppc - builds a rescue image from an existing tree
#                      which can be converted into a yaboot bootable
#                      iso and used as a sysadmin rescue image.
#
#
# Usage: mk-rescueimage.ppc <src-tree> <dest-dir> <productname>
#
# the rescue image will be created as <dest-dir>/ppc-rescueimage
#
#
# Copyright (C) 2007  Red Hat, Inc.  All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import os
import sys
import string

def usage():
    print "usage: mk-rescueimage.ppc <toplevel> <dest-dir> <productname> <productpath>"

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

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

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

# cp boot_image
os.system("cp -a %s/boot_image %s/" % (srcdir, 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 yaboot files
os.system("cp -a %s/ppc     %s/" % (srcdir, destdir))
os.system("cp -a %s/etc     %s/" % (srcdir, destdir))

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

for arch in ("ppc32", "ppc64"):
    # munge various yaboot configs to have a default of rescue mode
    cfgfile = open("%s/ppc/%s/yaboot.conf" % (destdir, arch), "r")
    cfglines = cfgfile.readlines()
    cfgfile.close()

    # backup old one
    os.system("cp %s/ppc/%s/yaboot.conf %s/ppc/%s/yaboot.conf.backup" % (destdir, arch, destdir, arch))

    cfgfile = open("%s/ppc/%s/yaboot.conf" % (destdir, arch), "w+")
    fndit = 0
    for l in cfglines:
        if string.find(l, "Welcome") != -1:
            cfgfile.write(l.replace("installer", "rescue"))
            continue
        if string.find(l, "default=") != -1:
            cfgfile.write("default=rescue\n")
            continue
        # see if we're into the stanza now
        elif string.find(l, "image=") != -1:
            fndit = 1
            # build up the rescue stanza based on whats in the
            # default stanza as we echo it out
            rescuestanza = l + "        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:
                if string.find(l, "read-only") != -1:
                    rescuestanza = rescuestanza + l + "        append=rescue\n"
                else:
                    if not string.find(l, "label=") != -1:
                        rescuestanza = rescuestanza + l
            cfgfile.write(l)
    
    if fndit:
        cfgfile.write('\n' + rescuestanza)

    cfgfile.close()

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

# Modify top level yaboot to indicate rescue CD instead of install CD
#

cfgfile = open("%s/etc/yaboot.conf" % (destdir,), "r")
cfglines = cfgfile.readlines()
cfgfile.close()

cfgfile = open("%s/etc/yaboot.conf" % (destdir,), "w")

origlines = ''
rescuelines = ''

# do some inline replacements of text
for l in cfglines:
    if l.find("init-message") != -1:
        entry = cfglines.index(l)
        cfglines[entry] = cfglines[entry].replace("installer", "rescue")
        cfglines[entry] = cfglines[entry].replace("linux32", "rescue32")
        continue
    if l.find("default=") != -1:
        cfglines[cfglines.index(l)] = l.replace("linux", "rescue")
        continue
    if l.find("image=/ppc/ppc64") != -1:
        origlines = cfglines[cfglines.index(l):]
        break
        
# create a big string that is easier to do replacements on
for l in origlines:
     rescuelines += l

# Do replacements on the big string
rescuelines = rescuelines.replace("label=linux64", "label=rescue64")
rescuelines = rescuelines.replace("alias=linux", "alias=rescue")
rescuelines = rescuelines.replace("label=linux32", "label=rescue32")
rescuelines = rescuelines.replace("read-only", "read-only\n        append=rescue")

for l in cfglines:
    cfgfile.write(l)

cfgfile.write('\n' + rescuelines)