1 |
charliebrady |
1.1 |
# swap.py |
2 |
|
|
# Python module for managing swap devices. |
3 |
|
|
# |
4 |
|
|
# Copyright (C) 2009 Red Hat, Inc. |
5 |
|
|
# |
6 |
|
|
# This copyrighted material is made available to anyone wishing to use, |
7 |
|
|
# modify, copy, or redistribute it subject to the terms and conditions of |
8 |
|
|
# the GNU General Public License v.2, or (at your option) any later version. |
9 |
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT |
10 |
|
|
# ANY WARRANTY expressed or implied, including the implied warranties of |
11 |
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
12 |
|
|
# Public License for more details. You should have received a copy of the |
13 |
|
|
# GNU General Public License along with this program; if not, write to the |
14 |
|
|
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
15 |
|
|
# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the |
16 |
|
|
# source code or documentation are not subject to the GNU General Public |
17 |
|
|
# License and may only be used or replicated with the express permission of |
18 |
|
|
# Red Hat, Inc. |
19 |
|
|
# |
20 |
|
|
# Red Hat Author(s): Dave Lehman <dlehman@redhat.com> |
21 |
|
|
# |
22 |
|
|
|
23 |
|
|
import resource |
24 |
|
|
|
25 |
|
|
import iutil |
26 |
|
|
import os |
27 |
|
|
|
28 |
|
|
from ..errors import * |
29 |
|
|
from . import dm |
30 |
|
|
|
31 |
|
|
import gettext |
32 |
|
|
_ = lambda x: gettext.ldgettext("anaconda", x) |
33 |
|
|
|
34 |
|
|
|
35 |
|
|
def mkswap(device, label='', progress=None): |
36 |
|
|
# We use -f to force since mkswap tends to refuse creation on lvs with |
37 |
|
|
# a message about erasing bootbits sectors on whole disks. Bah. |
38 |
|
|
argv = ["-f"] |
39 |
|
|
if label: |
40 |
|
|
argv.extend(["-L", label]) |
41 |
|
|
argv.append(device) |
42 |
|
|
|
43 |
|
|
rc = iutil.execWithPulseProgress("mkswap", argv, |
44 |
|
|
stderr = "/dev/tty5", |
45 |
|
|
stdout = "/dev/tty5", |
46 |
|
|
progress=progress) |
47 |
|
|
|
48 |
|
|
if rc: |
49 |
|
|
raise SwapError("mkswap failed for '%s'" % device) |
50 |
|
|
|
51 |
|
|
def swapon(device, priority=None): |
52 |
|
|
pagesize = resource.getpagesize() |
53 |
|
|
buf = None |
54 |
|
|
sig = None |
55 |
|
|
|
56 |
|
|
if pagesize > 2048: |
57 |
|
|
num = pagesize |
58 |
|
|
else: |
59 |
|
|
num = 2048 |
60 |
|
|
|
61 |
|
|
try: |
62 |
|
|
fd = os.open(device, os.O_RDONLY) |
63 |
|
|
buf = os.read(fd, num) |
64 |
|
|
except OSError: |
65 |
|
|
pass |
66 |
|
|
finally: |
67 |
|
|
try: |
68 |
|
|
os.close(fd) |
69 |
|
|
except (OSError, UnboundLocalError): |
70 |
|
|
pass |
71 |
|
|
|
72 |
|
|
if buf is not None and len(buf) == pagesize: |
73 |
|
|
sig = buf[pagesize - 10:] |
74 |
|
|
if sig == 'SWAP-SPACE': |
75 |
|
|
raise OldSwapError |
76 |
|
|
if sig == 'S1SUSPEND\x00' or sig == 'S2SUSPEND\x00': |
77 |
|
|
raise SuspendError |
78 |
|
|
|
79 |
|
|
if sig != 'SWAPSPACE2': |
80 |
|
|
raise UnknownSwapError |
81 |
|
|
|
82 |
|
|
argv = [] |
83 |
|
|
if isinstance(priority, int) and 0 <= priority <= 32767: |
84 |
|
|
argv.extend(["-p", "%d" % priority]) |
85 |
|
|
argv.append(device) |
86 |
|
|
|
87 |
|
|
rc = iutil.execWithRedirect("swapon", |
88 |
|
|
argv, |
89 |
|
|
stderr = "/dev/tty5", |
90 |
|
|
stdout = "/dev/tty5") |
91 |
|
|
|
92 |
|
|
if rc: |
93 |
|
|
raise SwapError("swapon failed for '%s'" % device) |
94 |
|
|
|
95 |
|
|
def swapoff(device): |
96 |
|
|
rc = iutil.execWithRedirect("swapoff", [device], |
97 |
|
|
stderr = "/dev/tty5", |
98 |
|
|
stdout = "/dev/tty5") |
99 |
|
|
|
100 |
|
|
if rc: |
101 |
|
|
raise SwapError("swapoff failed for '%s'" % device) |
102 |
|
|
|
103 |
|
|
def swapstatus(device): |
104 |
|
|
alt_dev = None |
105 |
|
|
if device.startswith("/dev/mapper/"): |
106 |
|
|
# get the real device node for device-mapper devices since the ones |
107 |
|
|
# with meaningful names are just symlinks |
108 |
|
|
try: |
109 |
|
|
alt_dev = "/dev/%s" % dm.dm_node_from_name(device.split("/")[-1]) |
110 |
|
|
except DMError: |
111 |
|
|
alt_dev = None |
112 |
|
|
|
113 |
|
|
lines = open("/proc/swaps").readlines() |
114 |
|
|
status = False |
115 |
|
|
for line in lines: |
116 |
|
|
if not line.strip(): |
117 |
|
|
continue |
118 |
|
|
|
119 |
|
|
swap_dev = line.split()[0] |
120 |
|
|
if swap_dev in [device, alt_dev]: |
121 |
|
|
status = True |
122 |
|
|
break |
123 |
|
|
|
124 |
|
|
return status |
125 |
|
|
|