Commit a3990d97 authored by Nikos Skalkotos's avatar Nikos Skalkotos

Always use get_command() to import commands

If you are a regular user, some root commands like modprobe may not be
in your path.
parent 35841794
......@@ -25,9 +25,6 @@ import os
import re
import json
import tempfile
from sh import qemu_img
from sh import qemu_nbd
from sh import modprobe
class FatalError(Exception):
......@@ -35,8 +32,25 @@ class FatalError(Exception):
pass
def get_command(command):
"""Return a file system binary command"""
def find_sbin_command(command, exception):
search_paths = ['/usr/local/sbin', '/usr/sbin', '/sbin']
for fullpath in map(lambda x: "%s/%s" % (x, command), search_paths):
if os.path.exists(fullpath) and os.access(fullpath, os.X_OK):
return sh.Command(fullpath)
raise exception
try:
return sh.__getattr__(command)
except sh.CommandNotFound as e:
return find_sbin_command(command, e)
def image_info(image):
"""Returns information about an image file"""
qemu_img = get_command('qemu-img')
info = qemu_img('info', '--output', 'json', image)
return json.loads(str(info))
......@@ -44,6 +58,7 @@ def image_info(image):
def create_snapshot(source, target_dir):
"""Returns a qcow2 snapshot of an image file"""
qemu_img = get_command('qemu-img')
snapfd, snap = tempfile.mkstemp(prefix='snapshot-', dir=target_dir)
os.close(snapfd)
qemu_img('create', '-f', 'qcow2', '-o',
......@@ -51,21 +66,6 @@ def create_snapshot(source, target_dir):
return snap
def get_command(command):
"""Return a file system binary command"""
def find_sbin_command(command, exception):
search_paths = ['/usr/local/sbin', '/usr/sbin', '/sbin']
for fullpath in map(lambda x: "%s/%s" % (x, command), search_paths):
if os.path.exists(fullpath) and os.access(fullpath, os.X_OK):
return sh.Command(fullpath)
raise exception
try:
return sh.__getattr__(command)
except sh.CommandNotFound as e:
return find_sbin_command(command, e)
def get_kvm_binary():
"""Returns the path to the kvm binary and some extra arguments if needed"""
......@@ -131,6 +131,8 @@ class QemuNBD(object):
self.image = image
self.device = None
self.pattern = re.compile('^nbd\d+$')
self.modprobe = get_command('modprobe')
self.qemu_nbd = get_command('qemu-nbd')
def _list_devices(self):
"""Returns all the NBD block devices"""
......@@ -141,7 +143,7 @@ class QemuNBD(object):
devs = self._list_devices()
if len(devs) == 0: # Is nbd module loaded?
modprobe('nbd', 'max_part=16')
self.modprobe('nbd', 'max_part=16')
# Wait a second for /dev to be populated
time.sleep(1)
devs = self._list_devices()
......@@ -166,7 +168,7 @@ class QemuNBD(object):
args.append('-r')
args.append(self.image)
qemu_nbd(*args)
self.qemu_nbd(*args)
self.device = device
return device
......@@ -174,7 +176,7 @@ class QemuNBD(object):
"""Disconnect the image from the connected device"""
assert self.device is not None, "No device connected"
qemu_nbd('-d', self.device)
self.qemu_nbd('-d', self.device)
self.device = None
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment