summaryrefslogtreecommitdiffstats
path: root/pyanaconda/isys/__init__.py
blob: 149936ebd21ae34e6a80c1b873673d4aa3c76b93 (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#
# isys.py - installer utility functions and glue for C module
#
# Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 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/>.
#
# Author(s): Matt Wilson <msw@redhat.com>
#            Erik Troan <ewt@redhat.com>
#            Jeremy Katz <katzj@redhat.com>
#

try:
    from pyanaconda import _isys
except ImportError:
    # We're running in some sort of testing mode, in which case we can fix
    # up PYTHONPATH and just do this basic import.
    import _isys

import string
import os
import os.path
import socket
import stat
import posix
import sys
from pyanaconda import iutil
import blivet.arch
import re
import struct
import dbus

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

NM_SERVICE = "org.freedesktop.NetworkManager"
NM_MANAGER_PATH = "/org/freedesktop/NetworkManager"
NM_SETTINGS_PATH = "/org/freedesktop/NetworkManager/Settings"
NM_MANAGER_IFACE = "org.freedesktop.NetworkManager"
NM_ACTIVE_CONNECTION_IFACE = "org.freedesktop.NetworkManager.Connection.Active"
NM_CONNECTION_IFACE = "org.freedesktop.NetworkManager.Settings.Connection"
NM_DEVICE_IFACE = "org.freedesktop.NetworkManager.Device"
NM_DEVICE_WIRED_IFACE = "org.freedesktop.NetworkManager.Device.Wired"
NM_IP4CONFIG_IFACE = "org.freedesktop.NetworkManager.IP4Config"
NM_IP6CONFIG_IFACE = "org.freedesktop.NetworkManager.IP6Config"
NM_ACCESS_POINT_IFACE = "org.freedesktop.NetworkManager.AccessPoint"

NM_STATE_UNKNOWN = 0
NM_STATE_ASLEEP = 10
NM_STATE_DISCONNECTED = 20
NM_STATE_DISCONNECTING = 30
NM_STATE_CONNECTING = 40
NM_STATE_CONNECTED_LOCAL = 50
NM_STATE_CONNECTED_SITE = 60
NM_STATE_CONNECTED_GLOBAL = 70
NM_DEVICE_STATE_ACTIVATED = 100
NM_DEVICE_TYPE_WIFI = 2
NM_DEVICE_TYPE_ETHERNET = 1

DBUS_PROPS_IFACE = "org.freedesktop.DBus.Properties"

if blivet.arch.getArch() in ("sparc", "ppc64"):
    MIN_RAM = 768 * 1024
    GUI_INSTALL_EXTRA_RAM = 512 * 1024
else:
    MIN_RAM = 512 * 1024
    GUI_INSTALL_EXTRA_RAM = 0

MIN_GUI_RAM = MIN_RAM + GUI_INSTALL_EXTRA_RAM
EARLY_SWAP_RAM = 896 * 1024

## Get the amount of free space available under a directory path.
# @param path The directory path to check.
# @return The amount of free space available, in 
def pathSpaceAvailable(path):
    return _isys.devSpaceFree(path)

def resetResolv():
    return _isys.resetresolv()

def modulesWithPaths():
    mods = []
    for modline in open("/proc/modules", "r"):
        modName = modline.split(" ", 1)[0]
        modInfo = iutil.execWithCapture("modinfo",
                ["-F", "filename", modName]).splitlines()
        modPaths = [ line.strip() for line in modInfo if line!="" ]
        mods.extend(modPaths)
    return mods

def isPseudoTTY (fd):
    return _isys.isPseudoTTY (fd)

## Flush filesystem buffers.
def sync ():
    return _isys.sync ()

## Determine if a file is an ISO image or not.
# @param file The full path to a file to check.
# @return True if ISO image, False otherwise.
def isIsoImage(file):
    return _isys.isisoimage(file)

# Return number of network devices
def getNetworkDeviceCount():
    bus = dbus.SystemBus()
    nm = bus.get_object(NM_SERVICE, NM_MANAGER_PATH)
    devlist = nm.get_dbus_method("GetDevices")()
    return len(devlist)

# Get a D-Bus interface for the specified device's (e.g., eth0) properties.
# If dev=None, return a hash of the form 'hash[dev] = props_iface' that
# contains all device properties for all interfaces that NetworkManager knows
# about.
def getDeviceProperties(dev=None):
    bus = dbus.SystemBus()
    nm = bus.get_object(NM_SERVICE, NM_MANAGER_PATH)
    devlist = nm.get_dbus_method("GetDevices")()
    all = {}

    for path in devlist:
        device = bus.get_object(NM_SERVICE, path)
        device_props_iface = dbus.Interface(device, DBUS_PROPS_IFACE)

        device_interface = str(device_props_iface.Get(NM_DEVICE_IFACE, "Interface"))

        if dev is None:
            all[device_interface] = device_props_iface
        elif device_interface == dev:
            return device_props_iface

    if dev is None:
        return all
    else:
        return None

def getMacAddress(dev):
    """Return MAC address of device. "" if not found"""
    if dev == '' or dev is None:
        return ""

    device_props_iface = getDeviceProperties(dev=dev)
    if device_props_iface is None:
        return ""

    device_macaddr = ""
    try:
        device_macaddr = device_props_iface.Get(NM_DEVICE_WIRED_IFACE, "HwAddress").upper()
    except dbus.exceptions.DBusException as e:
        log.debug("getMacAddress %s: %s" % (dev, e))
    return device_macaddr

# Determine if a network device is a wireless device.
def isWirelessDevice(dev_name):
    bus = dbus.SystemBus()
    nm = bus.get_object(NM_SERVICE, NM_MANAGER_PATH)
    devlist = nm.get_dbus_method("GetDevices")()

    for path in devlist:
        device = bus.get_object(NM_SERVICE, path)
        device_props_iface = dbus.Interface(device, DBUS_PROPS_IFACE)

        iface = device_props_iface.Get(NM_DEVICE_IFACE, "Interface")
        if iface == dev_name:
            device_type = device_props_iface.Get(NM_DEVICE_IFACE, "DeviceType")
            return device_type == NM_DEVICE_TYPE_WIFI

    return False


# Get IP addresses for a network device.
# Returns list of ipv4 or ipv6 addresses, depending
# on version parameter. ipv4 is default.
def getIPAddresses(dev, version=4):
    if dev == '' or dev is None:
       return None

    device_props_iface = getDeviceProperties(dev=dev)
    if device_props_iface is None:
        return None

    bus = dbus.SystemBus()

    addresses = []

    if version == 4:
        ip4_config_path = device_props_iface.Get(NM_DEVICE_IFACE, 'Ip4Config')
        if ip4_config_path != '/':
            ip4_config_obj = bus.get_object(NM_SERVICE, ip4_config_path)
            ip4_config_props = dbus.Interface(ip4_config_obj, DBUS_PROPS_IFACE)

            # addresses (3-element list:  ipaddr, netmask, gateway)
            addrs = ip4_config_props.Get(NM_IP4CONFIG_IFACE, "Addresses")
            for addr in addrs:
                try:
                    tmp = struct.pack('I', addr[0])
                    ipaddr = socket.inet_ntop(socket.AF_INET, tmp)
                    addresses.append(ipaddr)
                except ValueError as e:
                    log.debug("Exception caught trying to convert IP address %s: %s" %
                    (addr, e))
    elif version == 6:
        ip6_config_path = device_props_iface.Get(NM_DEVICE_IFACE, 'Ip6Config')
        if ip6_config_path != '/':
            ip6_config_obj = bus.get_object(NM_SERVICE, ip6_config_path)
            ip6_config_props = dbus.Interface(ip6_config_obj, DBUS_PROPS_IFACE)

            addrs = ip6_config_props.Get(NM_IP6CONFIG_IFACE, "Addresses")
            for addr in addrs:
                try:
                    addrstr = "".join(str(byte) for byte in addr[0])
                    ipaddr = socket.inet_ntop(socket.AF_INET6, addrstr)
                    # XXX - should we prefer Global or Site-Local types?
                    #       does NM prefer them?
                    addresses.append(ipaddr)
                except ValueError as e:
                    log.debug("Exception caught trying to convert IP address %s: %s" %
                    (addr, e))
    else:
        raise ValueError, "invalid IP version %d" % version

    return addresses

def prefix2netmask(prefix):
    return _isys.prefix2netmask(prefix)

def netmask2prefix (netmask):
    prefix = 0

    while prefix < 33:
        if (prefix2netmask(prefix) == netmask):
            return prefix

        prefix += 1

    return prefix

isPAE = None
def isPaeAvailable():
    global isPAE
    if isPAE is not None:
        return isPAE

    isPAE = False
    if not blivet.arch.isX86():
        return isPAE

    f = open("/proc/cpuinfo", "r")
    lines = f.readlines()
    f.close()

    for line in lines:
        if line.startswith("flags") and line.find("pae") != -1:
            isPAE = True
            break

    return isPAE

def getLinkStatus(dev):
    return _isys.getLinkStatus(dev)

def getAnacondaVersion():
    return _isys.getAnacondaVersion()

auditDaemon = _isys.auditdaemon

handleSegv = _isys.handleSegv

printObject = _isys.printObject
bind_textdomain_codeset = _isys.bind_textdomain_codeset
isVioConsole = _isys.isVioConsole
initLog = _isys.initLog
total_memory = _isys.total_memory