Revision 3ccb2618 image_creator/disk.py

b/image_creator/disk.py
1 1
#!/usr/bin/env python
2 2

  
3
import losetup
3
from image_creator.util import get_command
4
from clint.textui import progress
5

  
4 6
import stat
5 7
import os
6 8
import tempfile
......
9 11
import sys
10 12
import guestfs
11 13

  
12
import pbs
13
from pbs import dd
14
from clint.textui import progress
15

  
16 14

  
17 15
class DiskError(Exception):
18 16
    pass
19 17

  
20

  
21
def find_sbin_command(command, exception):
22
    search_paths = ['/usr/local/sbin', '/usr/sbin', '/sbin']
23
    for fullpath in map(lambda x: "%s/%s" % (x, command), search_paths):
24
        if os.path.exists(fullpath) and os.access(fullpath, os.X_OK):
25
            return pbs.Command(fullpath)
26
        continue
27
    raise exception
28

  
29

  
30
try:
31
    from pbs import dmsetup
32
except pbs.CommandNotFound as e:
33
    dmsetup = find_sbin_command('dmsetup', e)
34

  
35
try:
36
    from pbs import blockdev
37
except pbs.CommandNotFound as e:
38
    blockdev = find_sbin_command('blockdev', e)
18
dd = get_command('dd')
19
dmsetup = get_command('dmsetup')
20
losetup = get_command('losetup')
21
blockdev = get_command('blockdev')
39 22

  
40 23

  
41 24
class Disk(object):
......
57 40
        self._cleanup_jobs.append((job, args))
58 41

  
59 42
    def _losetup(self, fname):
60
        loop = losetup.find_unused_loop_device()
61
        loop.mount(fname)
62
        self._add_cleanup(loop.unmount)
63
        return loop.device
43
        loop = losetup('-f', '--show', fname)
44
        loop = loop.strip() # remove the new-line char
45
        self._add_cleanup(losetup, '-d', loop)
46
        return loop
64 47

  
65 48
    def _dir_to_disk(self):
66 49
        raise NotImplementedError

Also available in: Unified diff