summaryrefslogtreecommitdiffstats
path: root/pyanaconda/image.py
blob: 6f8cc5f39516113de0f483ca00b3d2e35eba5144 (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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#
# image.py: Support methods for CD/DVD and ISO image installations.
#
# 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 isys, iutil
import os, os.path, stat, sys
from constants import *

from errors import *

import gettext
_ = lambda x: gettext.ldgettext("anaconda", x)

import logging
log = logging.getLogger("anaconda")

_arch = iutil.getArch()

def findFirstIsoImage(path):
    """
    Find the first iso image in path
    This also supports specifying a specific .iso image

    Returns the basename of the image
    """
    flush = os.stat(path)
    arch = _arch

    if os.path.isfile(path) and path.endswith(".iso"):
        files = [os.path.basename(path)]
        path = os.path.dirname(path)
    else:
        files = os.listdir(path)

    for fn in files:
        what = path + '/' + fn
        log.debug("Checking %s" % (what))
        if not isys.isIsoImage(what):
            continue

        log.debug("mounting %s on /mnt/install/cdimage", what)
        try:
            isys.mount(what, "/mnt/install/cdimage", fstype="iso9660", readOnly=True)
        except SystemError:
            continue

        if not os.access("/mnt/install/cdimage/.discinfo", os.R_OK):
            isys.umount("/mnt/install/cdimage", removeDir=False)
            continue

        log.debug("Reading .discinfo")
        f = open("/mnt/install/cdimage/.discinfo")
        f.readline() # skip timestamp
        f.readline() # skip release description
        discArch = f.readline().strip() # read architecture
        f.close()

        log.debug("discArch = %s" % discArch)
        if discArch != arch:
            log.warning("findFirstIsoImage: architectures mismatch: %s, %s" %
                        (discArch, arch))
            isys.umount("/mnt/install/cdimage", removeDir=False)
            continue

        # If there's no repodata, there's no point in trying to
        # install from it.
        if not os.access("/mnt/install/cdimage/repodata", os.R_OK):
            log.warning("%s doesn't have repodata, skipping" %(what,))
            isys.umount("/mnt/install/cdimage", removeDir=False)
            continue

        # warn user if images appears to be wrong size
        if os.stat(what)[stat.ST_SIZE] % 2048:
            log.warning("%s appears to be corrupted" % what)
            exn = InvalidImageSizeError("size is not a multiple of 2048 bytes")
            if errorHandler.cb(exn) == ERROR_RAISE:
                raise exn

        log.info("Found disc at %s" % fn)
        isys.umount("/mnt/install/cdimage", removeDir=False)
        return fn

    return None

def getMediaId(path):
    if os.access("%s/.discinfo" % path, os.R_OK):
        f = open("%s/.discinfo" % path)
        newStamp = f.readline().strip()
        f.close()

        return newStamp
    else:
        return None

# This mounts the directory containing the iso images on ISO_DIR.
def mountImageDirectory(method, storage):
    # No need to mount it again.
    if os.path.ismount(ISO_DIR):
        return

    if method.method == "harddrive":
        if method.biospart:
            log.warning("biospart support is not implemented")
            devspec = method.biospart
        else:
            devspec = method.partition

        # FIXME: teach DeviceTree.resolveDevice about biospart
        device = storage.devicetree.resolveDevice(devspec)

        while True:
            try:
                device.setup()
                device.format.setup(mountpoint=ISO_DIR)
            except StorageError as e:
                log.error("couldn't mount ISO source directory: %s" % e)
                exn = MediaMountError(str(e))
                if errorHandler.cb(exn) == ERROR_RAISE:
                    raise exn
    elif methodstr.startswith("nfsiso:"):
        # XXX what if we mount it on ISO_DIR and then create a symlink
        #     if there are no isos instead of the remount?

        # mount the specified directory
        path = method.dir
        if method.dir.endswith(".iso"):
            path = os.path.dirname(method.dir)

        url = "%s:%s" % (method.server, path)

        while True:
            try:
                isys.mount(url, ISO_DIR, options=method.options)
            except SystemError as e:
                log.error("couldn't mount ISO source directory: %s" % e)
                exn = MediaMountError(str(e))
                if errorHandler.cb(exn) == ERROR_RAISE:
                    raise exn

def mountImage(isodir, tree):
    while True:
        if os.path.isfile(isodir):
            image = isodir
        else:
            image = findFirstIsoImage(isodir)
            if image is None:
                exn = MissingImageError()
                if errorHandler.cb(exn) == ERROR_RAISE:
                    raise exn
                else:
                    continue

            image = os.path.normpath("%s/%s" % (isodir, image))

        try:
            isys.mount(image, tree, fstype = 'iso9660', readOnly = True)
        except SystemError:
            exn = MissingImageError()
            if errorHandler.cb(exn) == ERROR_RAISE:
                raise exn
            else:
                continue
        else:
            break

# Return the first Device instance containing valid optical install media
# for this product.
def opticalInstallMedia(devicetree, mountpoint=INSTALL_TREE):
    retval = None

    for dev in devicetree.getDevicesByType("cdrom"):
        devicetree.updateDeviceFormat(dev)
        try:
            dev.format.mount(mountpoint=mountpoint)
        except Exception:
            continue

        if not verifyMedia(mountpoint):
            dev.format.unmount()
            continue

        dev.format.unmount()
        retval = dev
        break

    return retval

# Return a list of Device instances that may have HDISO install media
# somewhere.  Candidate devices are simply any that we can mount.
def potentialHdisoSources(devicetree):
    return filter(lambda d: d.format.exists and d.format.mountable, devicetree.getDevicesByType("partition"))

def umountImage(tree):
    if os.path.ismount(tree):
        isys.umount(tree, removeDir=False)

def unmountCD(dev):
    if not dev:
        return

    while True:
        try:
            dev.format.unmount()
        except Exception as e:
            log.error("exception in _unmountCD: %s" %(e,))
            exn = MediaUnmountError()
            errorHandler.cb(exn, dev)
        else:
            break

def verifyMedia(tree, timestamp=None):
    if os.access("%s/.discinfo" % tree, os.R_OK):
        f = open("%s/.discinfo" % tree)

        newStamp = f.readline().strip()
        descr = f.readline().strip()
        arch = f.readline().strip()
        f.close()

        if timestamp is not None:
            if newStamp == timestamp and arch == _arch:
                return True
        else:
            if arch == _arch:
                return True

    return False