1 # Copyright 2012 GRNET S.A. All rights reserved.
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
7 # 1. Redistributions of source code must retain the above
8 # copyright notice, this list of conditions and the following
11 # 2. Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following
13 # disclaimer in the documentation and/or other materials
14 # provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
29 # The views and conclusions contained in the software and
30 # documentation are those of the authors and should not be
31 # interpreted as representing official policies, either expressed
32 # or implied, of GRNET S.A.
39 from collections import namedtuple
43 from image_creator.util import get_command
44 from image_creator.util import FatalError
46 findfs = get_command('findfs')
47 truncate = get_command('truncate')
48 dd = get_command('dd')
49 dmsetup = get_command('dmsetup')
50 losetup = get_command('losetup')
51 mount = get_command('mount')
52 umount = get_command('umount')
71 def __init__(self, out, meta):
75 self.out.output('Searching for root device ...', False)
76 root = self._get_root_partition()
78 if root.startswith("UUID=") or root.startswith("LABEL="):
79 root = findfs(root).stdout.strip()
81 if not re.match('/dev/[hsv]d[a-z][1-9]*$', root):
82 raise FatalError("Don't know how to handle root device: %s" % root)
86 disk_file = re.split('[0-9]', root)[0]
87 device = parted.Device(disk_file)
88 self.disk = parted.Disk(device)
90 def _read_fstable(self, f):
92 if not os.path.isfile(f):
93 raise FatalError("Unable to open: `%s'. File is missing." % f)
95 FileSystemEntry = namedtuple('FileSystemEntry',
96 'dev mpoint fs opts freq passno')
97 with open(f) as table:
98 for line in iter(table):
99 entry = line.split('#')[0].strip().split()
102 yield FileSystemEntry(*entry)
104 def _get_root_partition(self):
105 for entry in self._read_fstable('/etc/fstab'):
106 if entry.mpoint == '/':
109 raise FatalError("Unable to find root device in /etc/fstab")
111 def _is_mpoint(self, path):
112 for entry in self._read_fstable('/proc/mounts'):
113 if entry.mpoint == path:
117 def _get_mount_options(self, device):
118 for entry in self._read_fstable('/proc/mounts'):
119 if not entry.dev.startswith('/'):
122 if os.path.realpath(entry.dev) == os.path.realpath(device):
127 def _create_partition_table(self, image):
129 if self.disk.type != 'msdos':
130 raise FatalError('Only msdos partition tables are supported')
132 # Copy the MBR and the space between the MBR and the first partition.
133 # In Grub version 1 Stage 1.5 is located there.
134 first_sector = self.disk.getPrimaryPartitions()[0].geometry.start
136 dd('if=%s' % self.disk.device.path, 'of=%s' % image,
137 'bs=%d' % self.disk.device.sectorSize,
138 'count=%d' % first_sector, 'conv=notrunc')
140 # Create the Extended boot records (EBRs) in the image
141 extended = self.disk.getExtendedPartition()
145 # Extended boot records precede the logical partitions they describe
146 logical = self.disk.getLogicalPartitions()
147 start = extended.geometry.start
148 for i in range(len(logical)):
149 end = logical[i].geometry.start - 1
150 dd('if=%s' % self.disk.device.path, 'of=%s' % image,
151 'count=%d' % (end - start + 1), 'conv=notrunc',
152 'seek=%d' % start, 'skip=%d' % start)
153 start = logical[i].geometry.end + 1
155 def _get_partitions(self, disk):
156 Partition = namedtuple('Partition', 'num start end type fs')
159 for p in disk.partitions:
161 start = p.geometry.start
164 fs = p.fileSystem.type if p.fileSystem is not None else ''
165 partitions.append(Partition(num, start, end, ptype, fs))
169 def _shrink_partitions(self, image):
171 new_end = self.disk.device.getLength()
173 image_dev = parted.Device(image)
174 image_disk = parted.Disk(image_dev)
176 is_extended = lambda p: p.type == parted.PARTITION_EXTENDED
177 is_logical = lambda p: p.type == parted.PARTITION_LOGICAL
179 partitions = self._get_partitions(self.disk)
181 last = partitions[-1]
182 if last.fs == 'linux-swap(v1)':
184 size = (last.end - last.start + 1) * self.disk.device.sectorSize
185 self.meta['SWAP'] = "%d:%s" % (last.num, (size + MB - 1) // MB)
187 image_disk.deletePartition(
188 image_disk.getPartitionBySector(last.start))
191 if is_logical(last) and last.num == 5:
192 extended = image_disk.getExtendedPartition()
193 image_disk.deletePartition(extended)
195 partitions.remove(filter(is_extended, partitions)[0])
197 partitions.remove(last)
198 last = partitions[-1]
200 # Leave 2048 blocks at the end
201 new_end = last.end + 2048
203 mount_options = self._get_mount_options(
204 self.disk.getPartitionBySector(last.start).path)
205 if mount_options is not None:
206 stat = os.statvfs(mount_options.mpoint)
207 # Shrink the last partition. The new size should be the size of the
209 blcks = stat.f_blocks - stat.f_bavail
210 new_size = (blcks * stat.f_frsize) // self.disk.device.sectorSize
212 # Add 10% just to be on the safe side
213 part_end = last.start + (new_size * 11) // 10
215 part_end = ((part_end + 2047) // 2048) * 2048
217 image_disk.setPartitionGeometry(
218 image_disk.getPartitionBySector(last.start),
219 parted.Constraint(device=image_disk.device),
220 start=last.start, end=last.end)
223 # Parted may have changed this for better alignment
224 part_end = image_disk.getPartitionBySector(last.start).geometry.end
225 last = last._replace(end=part_end)
226 partitions[-1] = last
228 # Leave 2048 blocks at the end.
229 new_end = new_size + 2048
232 if last.type == parted.PARTITION_LOGICAL:
233 # Fix the extended partition
234 extended = disk.getExtendedPartition()
236 image_disk.setPartitionGeometry(extended,
237 parted.Constraint(device=img_dev),
238 ext.geometry.start, end=last.end)
243 def _map_partition(self, dev, num, start, end):
244 name = os.path.basename(dev)
245 tablefd, table = tempfile.mkstemp()
247 size = end - start + 1
248 os.write(tablefd, "0 %d linear %s %d" % (size, dev, start))
249 dmsetup('create', "%sp%d" % (name, num), table)
253 return "/dev/mapper/%sp%d" % (name, num)
255 def _unmap_partition(self, dev):
256 if not os.path.exists(dev):
259 dmsetup('remove', dev.split('/dev/mapper/')[1])
262 def _mount(self, target, devs):
264 devs.sort(key=lambda d: d[1])
265 for dev, mpoint in devs:
266 absmpoint = os.path.abspath(target + mpoint)
267 if not os.path.exists(absmpoint):
268 os.makedirs(absmpoint)
269 mount(dev, absmpoint)
271 def _umount_all(self, target):
273 for entry in self._read_fstable('/proc/mounts'):
274 if entry.mpoint.startswith(os.path.abspath(target)):
275 mpoints.append(entry.mpoint)
278 for mpoint in reversed(mpoints):
281 def _create_filesystems(self, image):
283 partitions = self._get_partitions(parted.Disk(parted.Device(image)))
285 for p in self.disk.partitions:
286 filesystems[p.number] = self._get_mount_options(p.path)
288 unmounted = filter(lambda p: filesystems[p.num] is None, partitions)
289 mounted = filter(lambda p: filesystems[p.num] is not None, partitions)
291 # For partitions that are not mounted right now, we can simply dd them
294 dd('if=%s' % self.disk.device.path, 'of=%s' % image,
295 'count=%d' % (p.end - p.start + 1), 'conv=notrunc',
296 'seek=%d' % p.start, 'skip=%d' % p.start)
298 loop = str(losetup('-f', '--show', image)).strip()
303 mapped[i] = self._map_partition(loop, i, p.start, p.end)
305 # Create the file systems
306 for i, dev in mapped.iteritems():
307 fs = filesystems[i].fs
308 self.out.output('Creating %s filesystem on partition %d ... ' %
310 get_command('mkfs.%s' % fs)(*(MKFS_OPTS[fs] + [dev]))
311 self.out.success('done')
313 target = tempfile.mkdtemp()
315 absmpoints = self._mount(target,
316 [(mapped[i], filesystems[i].mpoint) for i in mapped.keys()]
320 self._umount_all(target)
323 for dev in mapped.values():
324 self._unmap_partition(dev)
327 def create_image(self):
329 image = '/mnt/%s.diskdump' % uuid.uuid4().hex
331 disk_size = self.disk.device.getLength() * self.disk.device.sectorSize
333 # Create sparse file to host the image
334 truncate("-s", "%d" % disk_size, image)
336 self._create_partition_table(image)
337 end_sector = self._shrink_partitions(image)
339 # Check if the available space is enough to host the image
340 dirname = os.path.dirname(image)
341 size = (end_sector + 1) * self.disk.device.sectorSize
342 self.out.output("Examining available space in %s ..." % dirname, False)
343 stat = os.statvfs(dirname)
344 available = stat.f_bavail * stat.f_frsize
345 if available <= size:
346 raise FatalError('Not enough space in %s to host the image' %
348 self.out.success("sufficient")
350 self._create_filesystems(image)
354 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :