1 |
# |
2 |
# dasd.py - DASD class |
3 |
# |
4 |
# Copyright (C) 2009, 2010 Red Hat, Inc. All rights reserved. |
5 |
# |
6 |
# This program is free software; you can redistribute it and/or modify |
7 |
# it under the terms of the GNU General Public License as published by |
8 |
# the Free Software Foundation; either version 2 of the License, or |
9 |
# (at your option) any later version. |
10 |
# |
11 |
# This program is distributed in the hope that it will be useful, |
12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
# GNU General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License |
17 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
18 |
# |
19 |
# Red Hat Author(s): David Cantrell <dcantrell@redhat.com> |
20 |
# |
21 |
|
22 |
import iutil |
23 |
import sys |
24 |
import os |
25 |
from storage.errors import DasdFormatError |
26 |
from storage.devices import deviceNameToDiskByPath |
27 |
from constants import * |
28 |
from flags import flags |
29 |
from baseudev import udev_trigger |
30 |
|
31 |
import logging |
32 |
log = logging.getLogger("anaconda") |
33 |
|
34 |
import gettext |
35 |
_ = lambda x: gettext.ldgettext("anaconda", x) |
36 |
P_ = lambda x, y, z: gettext.ldngettext("anaconda", x, y, z) |
37 |
|
38 |
class DASD: |
39 |
""" Controlling class for DASD interaction before the storage code in |
40 |
anaconda has initialized. |
41 |
|
42 |
The DASD class can determine if any DASD devices on the system are |
43 |
unformatted and can perform a dasdfmt on them. |
44 |
""" |
45 |
|
46 |
def __init__(self): |
47 |
self._dasdlist = [] |
48 |
self._devices = [] # list of DASDDevice objects |
49 |
self.totalCylinders = 0 |
50 |
self._completedCylinders = 0.0 |
51 |
self._maxFormatJobs = 0 |
52 |
self.dasdfmt = "/sbin/dasdfmt" |
53 |
self.commonArgv = ["-y", "-d", "cdl", "-b", "4096"] |
54 |
self.started = False |
55 |
|
56 |
def __call__(self): |
57 |
return self |
58 |
|
59 |
def startup(self, intf, exclusiveDisks, zeroMbr): |
60 |
""" Look for any unformatted DASDs in the system and offer the user |
61 |
the option for format them with dasdfmt or exit the installer. |
62 |
""" |
63 |
if self.started: |
64 |
return |
65 |
|
66 |
self.started = True |
67 |
out = "/dev/tty5" |
68 |
err = "/dev/tty5" |
69 |
|
70 |
if not iutil.isS390(): |
71 |
return |
72 |
|
73 |
# Trigger udev data about the dasd devices on the system |
74 |
udev_trigger(action="change", name="dasd*") |
75 |
|
76 |
log.info("Checking for unformatted DASD devices:") |
77 |
|
78 |
for device in os.listdir("/sys/block"): |
79 |
if not device.startswith("dasd"): |
80 |
continue |
81 |
|
82 |
statusfile = "/sys/block/%s/device/status" % (device,) |
83 |
if not os.path.isfile(statusfile): |
84 |
continue |
85 |
|
86 |
f = open(statusfile, "r") |
87 |
status = f.read().strip() |
88 |
f.close() |
89 |
|
90 |
if status in ["unformatted"] and device not in exclusiveDisks: |
91 |
bypath = deviceNameToDiskByPath(device) |
92 |
if not bypath: |
93 |
bypath = "/dev/" + device |
94 |
|
95 |
log.info(" %s (%s) status is %s, needs dasdfmt" % (device, |
96 |
bypath, |
97 |
status,)) |
98 |
self._dasdlist.append((device, bypath)) |
99 |
|
100 |
if not len(self._dasdlist): |
101 |
log.info(" no unformatted DASD devices found") |
102 |
return |
103 |
|
104 |
askUser = True |
105 |
|
106 |
if zeroMbr: |
107 |
askUser = False |
108 |
elif not intf and not zeroMbr: |
109 |
log.info(" non-interactive kickstart install without zerombr " |
110 |
"command, unable to run dasdfmt, exiting installer") |
111 |
sys.exit(0) |
112 |
|
113 |
c = len(self._dasdlist) |
114 |
|
115 |
if intf and askUser: |
116 |
devs = '' |
117 |
for dasd, bypath in self._dasdlist: |
118 |
devs += "%s\n" % (bypath,) |
119 |
|
120 |
rc = intf.questionInitializeDASD(c, devs) |
121 |
if rc == 1: |
122 |
log.info(" not running dasdfmt, continuing installation") |
123 |
return |
124 |
|
125 |
# gather total cylinder count |
126 |
argv = ["-t", "-v"] + self.commonArgv |
127 |
for dasd, bypath in self._dasdlist: |
128 |
buf = iutil.execWithCapture(self.dasdfmt, argv + ["/dev/" + dasd], |
129 |
stderr=err) |
130 |
for line in buf.splitlines(): |
131 |
if line.startswith("Drive Geometry: "): |
132 |
# line will look like this: |
133 |
# Drive Geometry: 3339 Cylinders * 15 Heads = 50085 Tracks |
134 |
cyls = long(filter(lambda s: s, line.split(' '))[2]) |
135 |
self.totalCylinders += cyls |
136 |
break |
137 |
|
138 |
# format DASDs |
139 |
argv = ["-P"] + self.commonArgv |
140 |
update = self._updateProgressWindow |
141 |
|
142 |
title = P_("Formatting DASD Device", "Formatting DASD Devices", c) |
143 |
msg = P_("Preparing %d DASD device for use with Linux..." % c, |
144 |
"Preparing %d DASD devices for use with Linux..." % c, c) |
145 |
|
146 |
if intf: |
147 |
if self.totalCylinders: |
148 |
pw = intf.progressWindow(title, msg, 1.0) |
149 |
else: |
150 |
pw = intf.progressWindow(title, msg, 100, pulse=True) |
151 |
|
152 |
for dasd, bypath in self._dasdlist: |
153 |
log.info("Running dasdfmt on %s" % (bypath,)) |
154 |
arglist = argv + ["/dev/" + dasd] |
155 |
|
156 |
try: |
157 |
if intf and self.totalCylinders: |
158 |
rc = iutil.execWithCallback(self.dasdfmt, arglist, |
159 |
stdout=out, stderr=err, |
160 |
callback=update, |
161 |
callback_data=pw, |
162 |
echo=False) |
163 |
elif intf: |
164 |
rc = iutil.execWithPulseProgress(self.dasdfmt, arglist, |
165 |
stdout=out, stderr=err, |
166 |
progress=pw) |
167 |
else: |
168 |
rc = iutil.execWithRedirect(self.dasdfmt, arglist, |
169 |
stdout=out, stderr=err) |
170 |
except Exception as e: |
171 |
raise DasdFormatError(e, bypath) |
172 |
|
173 |
if rc: |
174 |
raise DasdFormatError("dasdfmt failed: %s" % rc, bypath) |
175 |
|
176 |
if intf: |
177 |
pw.pop() |
178 |
|
179 |
def addDASD(self, dasd): |
180 |
""" Adds a DASDDevice to the internal list of DASDs. """ |
181 |
if dasd: |
182 |
self._devices.append(dasd) |
183 |
|
184 |
def clear_device_list(self): |
185 |
""" Clear the device list to force re-populate on next access. """ |
186 |
self._devices = [] |
187 |
|
188 |
def write(self, instPath): |
189 |
""" Write /etc/dasd.conf to target system for all DASD devices |
190 |
configured during installation. |
191 |
""" |
192 |
if self._devices == []: |
193 |
return |
194 |
|
195 |
f = open(os.path.realpath(instPath + "/etc/dasd.conf"), "w") |
196 |
for dasd in self._devices: |
197 |
fields = [dasd.busid] + dasd.getOpts() |
198 |
f.write("%s\n" % (" ".join(fields),)) |
199 |
f.close() |
200 |
|
201 |
def _updateProgressWindow(self, data, callback_data=None): |
202 |
""" Reads progress output from dasdfmt and collects the number of |
203 |
cylinders completed so the progress window can update. |
204 |
""" |
205 |
if not callback_data: |
206 |
return |
207 |
|
208 |
if data == '\n': |
209 |
# each newline we see in this output means one more cylinder done |
210 |
self._completedCylinders += 1.0 |
211 |
callback_data.set(self._completedCylinders / self.totalCylinders) |
212 |
|
213 |
# Create DASD singleton |
214 |
DASD = DASD() |
215 |
|
216 |
# vim:tw=78:ts=4:et:sw=4 |