lorax/src/pylorax/sysutils.py

93 lines
2.2 KiB
Python
Raw Normal View History

#
2010-02-23 13:20:05 +00:00
# sysutils.py
2010-01-12 11:45:54 +00:00
#
# Copyright (C) 2009 Red Hat, Inc.
#
# 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/>.
#
# Red Hat Author(s): Martin Gracik <mgracik@redhat.com>
#
2010-10-12 16:23:29 +00:00
__all__ = ["joinpaths", "replace", "create_loop_dev", "remove_loop_dev",
"create_dm_dev", "remove_dm_dev"]
2010-02-23 13:20:05 +00:00
import sys
import os
import re
2010-10-12 16:23:29 +00:00
import fileinput
import subprocess
2010-10-12 16:23:29 +00:00
def joinpaths(*args, **kwargs):
path = os.path.sep.join(args)
2010-10-12 16:23:29 +00:00
if kwargs.get("follow_symlinks"):
return os.path.realpath(path)
else:
2010-10-12 16:23:29 +00:00
return path
2010-10-12 16:23:29 +00:00
def replace(fname, find, replace):
2010-02-23 13:20:05 +00:00
fin = fileinput.input(fname, inplace=1)
pattern = re.compile(find)
for line in fin:
2010-02-23 13:20:05 +00:00
line = pattern.sub(replace, line)
sys.stdout.write(line)
fin.close()
2010-10-12 16:23:29 +00:00
def create_loop_dev(fname):
cmd = ["losetup", "-f"]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
rc = p.wait()
if not rc == 0:
return None
2010-10-12 16:23:29 +00:00
loopdev = p.stdout.read().strip()
2010-10-12 16:23:29 +00:00
cmd = ["losetup", loopdev, fname]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
rc = p.wait()
if not rc == 0:
return None
2010-10-12 16:23:29 +00:00
return loopdev
2010-02-23 13:20:05 +00:00
2010-10-12 16:23:29 +00:00
def remove_loop_dev(dev):
cmd = ["losetup", "-d", dev]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
rc = p.wait()
2010-10-12 16:23:29 +00:00
def create_dm_dev(name, size, loopdev):
table = '0 {0} linear {1} 0'.format(size, loopdev)
2010-10-12 16:23:29 +00:00
cmd = ["dmsetup", "create", name, "--table", table]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
rc = p.wait()
if not rc == 0:
return None
2010-10-12 16:23:29 +00:00
return joinpaths("/dev/mapper", name)
2010-10-12 16:23:29 +00:00
def remove_dm_dev(dev):
cmd = ["dmsetup", "remove", dev]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
rc = p.wait()