summaryrefslogtreecommitdiffstats
path: root/pyanaconda/image.py
blob: b590276ad1d1b96850f377f6c4b0667f7070469a (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
243
244
245
#
# 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, string, sys
from constants import *

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

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

_arch = iutil.getArch()

def findFirstIsoImage(path, messageWindow):
    flush = os.stat(path)
    files = os.listdir(path)
    arch = _arch

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

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

            if os.access("/mnt/cdimage/.discinfo", os.R_OK):
                log.debug("Reading .discinfo")
                f = open("/mnt/cdimage/.discinfo")
                try:
                    f.readline() # skip timestamp
                    f.readline() # skip release description
                    discArch = string.strip(f.readline()) # read architecture
                except:
                    discArch = None

                f.close()

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

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

                # warn user if images appears to be wrong size
                if os.stat(what)[stat.ST_SIZE] % 2048:
                    rc = messageWindow(_("Warning"),
                         _("The ISO image %s has a size which is not "
                           "a multiple of 2048 bytes.  This may mean "
                           "it was corrupted on transfer to this computer."
                           "\n\n"
                           "It is recommended that you exit and abort your "
                           "installation, but you can choose to continue if "
                           "you think this is in error.") % (file,),
                           type="custom", custom_icon="warning",
                           custom_buttons= [_("_Exit installer"),
                                            _("_Continue")])
                    if rc == 0:
                        sys.exit(0)

                log.info("Found disc at %s" % file)
                isys.umount("/mnt/cdimage", removeDir=False)
                return file
        except SystemError:
            pass

    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, and places the
# mount point in /mnt/isodir.
def mountDirectory(methodstr, messageWindow):
    if methodstr.startswith("hd:"):
        method = methodstr[3:]
        options = ''
        if method.count(":") == 1:
            (device, path) = method.split(":")
            fstype = "auto"
        else:
            (device, fstype, path) = method.split(":")

        if not device.startswith("/dev/") and not device.startswith("UUID=") \
           and not device.startswith("LABEL="):
            device = "/dev/%s" % device
    elif methodstr.startswith("nfsiso:"):
        (options, host, path) = iutil.parseNfsUrl(methodstr)
        device = "%s:%s" % (host, path)
        fstype = "nfs"
    else:
        return

    # No need to mount it again.
    if os.path.ismount("/mnt/isodir"):
        return

    while True:
        try:
            isys.mount(device, "/mnt/isodir", fstype=fstype, options=options)
            break
        except SystemError, msg:
            log.error("couldn't mount ISO source directory: %s" % msg)
            ans = messageWindow(_("Couldn't Mount ISO Source"),
                          _("An error occurred mounting the source "
                            "device %s.  This may happen if your ISO "
                            "images are located on an advanced storage "
                            "device like LVM or RAID, or if there was a "
                            "problem mounting a partition.  Click exit "
                            "to abort the installation.")
                          % (device,), type="custom", custom_icon="error",
                          custom_buttons=[_("_Exit"), _("_Retry")])

            if ans == 0:
                sys.exit(0)
            else:
                continue

def mountImage(isodir, tree, messageWindow):
    if os.path.ismount(tree):
        raise SystemError, "trying to mount already-mounted iso image!"

    image = findFirstIsoImage(isodir, messageWindow)

    while True:
        try:
            isoImage = "%s/%s" % (isodir, image)
            isys.mount(isoImage, tree, fstype = 'iso9660', readOnly = True)
            break
        except:
            ans = messageWindow(_("Missing ISO 9660 Image"),
                                _("The installer has tried to mount the "
                                  "installation image, but cannot find it on "
                                  "the hard drive.\n\n"
                                  "Please copy this image to the "
                                  "drive and click Retry.  Click Exit "
                                  "to abort the installation."),
                                  type="custom",
                                  custom_icon="warning",
                                  custom_buttons=[_("_Exit"), _("_Retry")])
            if ans == 0:
                sys.exit(0)
            elif ans == 1:
                image = findFirstIsoImage(isodir, messageWindow)

# Find an attached CD/DVD drive with media in it that contains packages,
# and return that device name.
def scanForMedia(tree, storage):
    for dev in storage.devicetree.devices:
        if dev.type != "cdrom":
            continue

        storage.devicetree.updateDeviceFormat(dev)
        try:
            dev.format.mount(mountpoint=tree)
        except:
            continue

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

        return dev.name

    return None

def umountImage(tree):
    isys.umount(tree, removeDir=False)

def unmountCD(dev, messageWindow):
    if not dev:
        return

    while True:
        try:
            dev.format.unmount()
            break
        except Exception, e:
            log.error("exception in _unmountCD: %s" %(e,))
            messageWindow(_("Error"),
                          _("An error occurred unmounting the disc.  "
                            "Please make sure you're not accessing "
                            "%s from the shell on tty2 "
                            "and then click OK to retry.")
                          % (dev.path,))

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

        newStamp = f.readline().strip()

        try:
            descr = f.readline().strip()
        except:
            descr = None

        try:
            arch = f.readline().strip()
        except:
            arch = None

        f.close()

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

    return False