Revision 8c574358 image_creator/disk.py

b/image_creator/disk.py
13 13
from pbs import blockdev
14 14
from pbs import dd
15 15

  
16
class DiskError(Exception): pass
16

  
17
class DiskError(Exception):
18
    pass
19

  
17 20

  
18 21
class Disk(object):
19 22

  
......
38 41
        while len(self._devices):
39 42
            device = self._devices.pop()
40 43
            device.destroy()
41
            
44

  
42 45
        while len(self._cleanup_jobs):
43 46
            job, args = self._cleanup_jobs.pop()
44 47
            job(*args)
......
57 60
        size = blockdev('--getsize', sourcedev)
58 61
        cowfd, cow = tempfile.mkstemp()
59 62
        self._add_cleanup(os.unlink, cow)
60
        # Create 1G cow file
61
        dd('if=/dev/null', 'of=%s' % cow, 'bs=1k' ,'seek=%d' % (1024*1024))
63
        # Create 1G cow sparse file
64
        dd('if=/dev/null', 'of=%s' % cow, 'bs=1k', 'seek=%d' % (1024 * 1024))
62 65
        cowdev = self._losetup(cow)
63 66

  
64 67
        snapshot = uuid.uuid4().hex
......
79 82
        self._devices.remove(device)
80 83
        device.destroy()
81 84

  
85

  
82 86
class DiskDevice(object):
83 87

  
84
    def __init__(self, device, bootable = True):
88
    def __init__(self, device, bootable=True):
85 89
        self.device = device
86 90
        self.bootable = bootable
87 91

  
......
89 93

  
90 94
        self.g.set_trace(1)
91 95

  
92
        self.g.add_drive_opts(device, readonly = 0)
96
        self.g.add_drive_opts(device, readonly=0)
93 97
        self.g.launch()
94 98
        roots = self.g.inspect_os()
95 99
        if len(roots) == 0:
......
100 104
        self.root = roots[0]
101 105
        self.ostype = self.g.inspect_get_type(self.root)
102 106
        self.distro = self.g.inspect_get_distro(self.root)
103
    
107

  
104 108
    def destroy(self):
105 109
        self.g.umount_all()
106 110
        self.g.sync()
107 111
        # Close the guestfs handler
108 112
        self.g.close()
109 113
        del self.g
110
    
114

  
111 115
    def mount(self):
112 116
        mps = self.g.inspect_get_mountpoints(self.root)
117

  
113 118
        # Sort the keys to mount the fs in a correct order.
114 119
        # / should be mounted befor /boot, etc
115
        def compare (a, b):
116
            if len(a[0]) > len(b[0]): return 1
117
            elif len(a[0]) == len(b[0]): return 0
118
            else: return -1
120
        def compare(a, b):
121
            if len(a[0]) > len(b[0]):
122
                return 1
123
            elif len(a[0]) == len(b[0]):
124
                return 0
125
            else:
126
                return -1
119 127
        mps.sort(compare)
120 128
        for mp, dev in mps:
121 129
            try:
......
123 131
            except RuntimeError as msg:
124 132
                print "%s (ignored)" % msg
125 133

  
134
    def umount(self):
135
        self.g.umount_all()
136

  
137
    def shrink(self):
138
        dev = self.g.part_to_dev(self.root)
139
        parttype = self.g.part_get_parttype(dev)
140
        if parttype != 'msdos':
141
            raise DiskError("You have a %s partition table. "
142
                "Only msdos partitions are supported" % parttype)
143

  
144
        last_partition = self.g.part_list(dev)[-1]
145

  
146
        if last_partition['part_num'] > 4:
147
            raise DiskError("This disk contains logical partitions. "
148
                "Only primary partitions are supported.")
149

  
150
        part_dev = "%s%d" % (dev, last_partition['part_num'])
151
        fs_type = self.g.vfs_type(part_dev)
152
        if not re.match("ext[234]", fs_type):
153
            print "Warning, don't know how to resize %s partitions" % vfs_type
154
            return
155

  
156
        self.g.e2fsck_f(part_dev)
157
        self.g.resize2fs_M(part_dev)
158
        output = self.g.tune2fs_l(part_dev)
159
        block_size = int(filter(lambda x: x[0] == 'Block size', output)[0][1])
160
        block_cnt = int(filter(lambda x: x[0] == 'Block count', output)[0][1])
161

  
162
        sector_size = self.g.blockdev_getss(dev)
163

  
164
        start = last_partition['part_start'] / sector_size
165
        end = start + (block_size * block_cnt) / sector_size - 1
166

  
167

  
168

  
126 169
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :

Also available in: Unified diff