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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
#!/usr/bin/env python
# hardware-reinstall - Prepare a physical box in FI for re-install.
# (c) 2012 Red Hat, Inc.
# Ricky Elrod <codeblock@fedoraproject.org>
# GPLv2+
import os
import sys
import urllib
import socket
import subprocess
import shlex
import platform
from optparse import OptionParser
parser = OptionParser(
description='Prepare a physical box in FI for re-install.')
parser.add_option('-n',
'--noop',
action='store_true',
help="Don't actually modify/download anything, just "
"output stuff.")
parser.add_option('-y',
'--yes',
action='store_true',
default=False,
dest="yes",
help="Don't prompt to confirm, just do it.")
parser.add_option('--ip',
help="Override the IP of the box (passed to Grubby)")
parser.add_option('--gw',
help="Override the Gateway of the box (passed to Grubby)",
dest='gateway')
parser.add_option('--nm',
help="Override the Netmask of the box (passed to Grubby)",
dest='netmask')
parser.add_option('--dns',
help="Comma-delimited list of DNS resolvers (passed to "
"Grubby)",
dest='dns_resolvers')
parser.add_option('--ks-file',
help="Set the kickstart file to use (default:"
"hardware-rhel-6-nohd)",
default='hardware-rhel-6-nohd',
dest='ks_file')
(options, args) = parser.parse_args()
if options.yes and options.noop:
print "Don't ask AND don't do anything? Cmon"
sys.exit(1)
# 0. Get our hostname/primary ip
# Get our primary IP by resolving our hostname.
if options.ip:
if not options.netmask:
print 'You gave a custom IP and should specify a custom netmask too.'
sys.exit(1)
primary_ip = options.ip
else:
primary_ip = socket.gethostbyname(socket.gethostname())
# so - anaconda sometimes doesn't seem to listen to our dns
# when fetching kickstarts, etc - so if we give the ip of the host
# if we're in 10.5.X network (phx2) then things just work.
if primary_ip.startswith('10.5.'):
basehost = "http://10.5.126.23/"
else:
basehost = "http://infrastructure.fedoraproject.org/"
arch = platform.machine()
VMLINUZ_URL = '%srepo/rhel/RHEL6-%s/images/pxeboot/vmlinuz' % (basehost, arch)
INITRD_URL = '%srepo/rhel/RHEL6-%s/images/pxeboot/initrd.img' % (basehost,
arch)
# 1. Grab initrd and vmlinuz and throw them in /boot
# FIXME - more error catching here
if not options.noop:
print 'Fetching vmlinuz'
urllib.urlretrieve(VMLINUZ_URL, "/boot/vmlinuz-install")
print 'Fetching initrd'
urllib.urlretrieve(INITRD_URL, "/boot/initrd-install.img")
# 2. Find our network info.
if options.netmask:
primary_netmask = options.netmask
# We still have to get the MAC address, of the primary NIC
# even if we specify a custom IP/NM.
cmd = subprocess.Popen('/sbin/ifconfig', stdout=subprocess.PIPE)
stdout = cmd.communicate()[0]
i = 0
lines = stdout.split("\n")
for line in lines:
if socket.gethostbyname(socket.gethostname()) in line:
# Somewhere between EL6 and F17, ifconfig output has changed.
# We accommodate for both.
if ':' in line:
# We are EL6
if not options.netmask:
# inet addr:10.5.127.51 Bcast:10.5.127.255 Mask:255.255.255.0
primary_netmask = line.split('Mask:')[1]
# On EL6 MAC addr is always one line before the IP address line
primary_mac = lines[i - 1].split('HWaddr ')[1]
else:
# We are likely something newer
if not options.netmask:
# inet 10.10.10.113 netmask 255.255.255.0 broadcast
# 10.10.10.255 # (cont. from above comment)
primary_netmask = line.split('netmask ')[1].split(' ')[0]
# On newer things, life gets harder. We have to continue
# parsing lines until we get one with 'ether ' in it.
# The range is the line we're on now -> the last line.
for y in xrange(i, len(lines) - 1):
if 'ether ' in lines[y]:
primary_mac = lines[y].split('ether ')[1].split(' ')[0]
break
break
i += 1
# Gateway
if options.gateway:
primary_gateway = options.gateway
else:
cmd = subprocess.Popen(['/sbin/ip', 'route'], stdout=subprocess.PIPE)
stdout = cmd.communicate()[0]
for line in stdout.split("\n"):
if 'default' in line:
# default via 10.10.10.1 dev wlan0 proto static
primary_gateway = line.split('via ')[1].split(' ')[0]
break
# And DNS servers
if options.dns_resolvers:
dns_resolvers = options.dns_resolvers
else:
dns_servers = []
with open('/etc/resolv.conf', 'r') as f:
for line in f.readlines():
if 'nameserver' in line:
dns = line.split(' ')
if len(dns) == 2:
dns_servers.append(dns[1].strip())
dns_resolvers = ','.join(dns_servers)
print '-' * 30
print 'Primary IP: ' + primary_ip
print 'Primary Netmask: ' + primary_netmask
print 'Primary Gateway: ' + primary_gateway
print 'Primary MAC Address: ' + primary_mac
print 'DNS Resolvers: ' + dns_resolvers
print '-' * 30
# 3. Construct the grubby line.
# grubby --add-kernel=/boot/vmlinuz-install \
# --args="ks=http://infrastructure.fedoraproject.org/\
# repo/rhel/ks/hardware-rhel-6-nohd \
# repo=http://infrastructure.fedoraproject.org/repo/rhel/RHEL6-x86_64/ \
# ksdevice=link ip=$IP gateway=$GATEWAY netmask=$NETMASK dns=$DNS" \
# --title="install el6" --initrd=/boot/initrd-install.img
grubby_command = '/sbin/grubby --add-kernel=/boot/vmlinuz-install ' \
'--args="ks=%srepo/rhel/ks/%s ksdevice=%s ' \
'ip=%s gateway=%s netmask=%s dns=%s repo=%srepo/rhel/RHEL6-x86_64/" ' \
'--title="install el6" --initrd=/boot/initrd-install.img' % (basehost,
options.ks_file,
primary_mac,
primary_ip,
primary_gateway,
primary_netmask,
dns_resolvers,
basehost)
print 'This grubby command seems like it will work:'
print '-' * 30
print grubby_command
print '-' * 30
print 'Check the command and be sure that it looks correct.'
if not options.noop:
if not options.yes:
print 'Type yes to continue, anything else to abort.'
print 'By continuing, I will run the above command.'
if raw_input('> ') != 'yes':
print 'Removing downloaded files.'
os.unlink('/boot/vmlinuz-install')
os.unlink('/boot/initrd-install.img')
print 'Aborting.'
sys.exit(1)
cmd = subprocess.Popen(shlex.split(grubby_command),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = cmd.communicate()
if stdout:
print stdout
if stderr:
print "[STDERR output]"
print stderr
if not options.yes:
raw_input(
'Examine the above output, if it looks sane, press enter to '
'continue.')
print 'The next command I will run is:'
print 'echo "savedefault --default=0 --once" | grub --batch'
if not options.noop:
cmd = subprocess.Popen(['/sbin/grub', '--batch'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
stdout = cmd.communicate(input='savedefault --default=0 --once\n')
print stdout[0]
print 'Done.'
print 'When you are ready, run: `shutdown -r now` to reboot.'
print 'Go here:'
print 'http://infrastructure.fedoraproject.org/infra/docs/kickstarts.txt'
print 'And control-f for "Installation" (no quotes). Continue from there.'
if options.noop:
print '-' * 30
print 'Script was run in "no-op" mode - none of the above commands ' \
'actually ran.'
print '-' * 30
|