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.rsync import Rsync
44 from image_creator.util import get_command
45 from image_creator.util import FatalError
47 findfs = get_command('findfs')
48 truncate = get_command('truncate')
49 dd = get_command('dd')
50 dmsetup = get_command('dmsetup')
51 losetup = get_command('losetup')
52 mount = get_command('mount')
53 umount = get_command('umount')
72 def __init__(self, out, meta):
76 self.out.output('Searching for root device ...', False)
77 root = self._get_root_partition()
79 if root.startswith("UUID=") or root.startswith("LABEL="):
80 root = findfs(root).stdout.strip()
82 if not re.match('/dev/[hsv]d[a-z][1-9]*$', root):
83 raise FatalError("Don't know how to handle root device: %s" % root)
87 disk_file = re.split('[0-9]', root)[0]
88 device = parted.Device(disk_file)
89 self.disk = parted.Disk(device)
91 def _read_fstable(self, f):
93 if not os.path.isfile(f):
94 raise FatalError("Unable to open: `%s'. File is missing." % f)
96 FileSystemTableEntry = namedtuple('FileSystemTableEntry',
97 'dev mpoint fs opts freq passno')
98 with open(f) as table:
99 for line in iter(table):
100 entry = line.split('#')[0].strip().split()
103 yield FileSystemTableEntry(*entry)
105 def _get_root_partition(self):
106 for entry in self._read_fstable('/etc/fstab'):
107 if entry.mpoint == '/':
110 raise FatalError("Unable to find root device in /etc/fstab")
112 def _is_mpoint(self, path):
113 for entry in self._read_fstable('/proc/mounts'):
114 if entry.mpoint == path:
118 def _get_mount_options(self, device):
119 for entry in self._read_fstable('/proc/mounts'):
120 if not entry.dev.startswith('/'):
123 if os.path.realpath(entry.dev) == os.path.realpath(device):
128 def _create_partition_table(self, image):
130 if self.disk.type != 'msdos':
131 raise FatalError('Only msdos partition tables are supported')
133 # Copy the MBR and the space between the MBR and the first partition.
134 # In Grub version 1 Stage 1.5 is located there.
135 first_sector = self.disk.getPrimaryPartitions()[0].geometry.start
137 dd('if=%s' % self.disk.device.path, 'of=%s' % image,
138 'bs=%d' % self.disk.device.sectorSize,
139 'count=%d' % first_sector, 'conv=notrunc')
141 # Create the Extended boot records (EBRs) in the image
142 extended = self.disk.getExtendedPartition()
146 # Extended boot records precede the logical partitions they describe
147 logical = self.disk.getLogicalPartitions()
148 start = extended.geometry.start
149 for i in range(len(logical)):
150 end = logical[i].geometry.start - 1
151 dd('if=%s' % self.disk.device.path, 'of=%s' % image,
152 'count=%d' % (end - start + 1), 'conv=notrunc',
153 'seek=%d' % start, 'skip=%d' % start)
154 start = logical[i].geometry.end + 1
156 def _get_partitions(self, disk):
157 Partition = namedtuple('Partition', 'num start end type fs')
160 for p in disk.partitions:
162 start = p.geometry.start
165 fs = p.fileSystem.type if p.fileSystem is not None else ''
166 partitions.append(Partition(num, start, end, ptype, fs))
170 def _shrink_partitions(self, image):
172 new_end = self.disk.device.getLength()
174 image_dev = parted.Device(image)
175 image_disk = parted.Disk(image_dev)
177 is_extended = lambda p: p.type == parted.PARTITION_EXTENDED
178 is_logical = lambda p: p.type == parted.PARTITION_LOGICAL
180 partitions = self._get_partitions(self.disk)
182 last = partitions[-1]
183 if last.fs == 'linux-swap(v1)':
185 size = (last.end - last.start + 1) * self.disk.device.sectorSize
186 self.meta['SWAP'] = "%d:%s" % (last.num, (size + MB - 1) // MB)
188 image_disk.deletePartition(
189 image_disk.getPartitionBySector(last.start))
192 if is_logical(last) and last.num == 5:
193 extended = image_disk.getExtendedPartition()
194 image_disk.deletePartition(extended)
196 partitions.remove(filter(is_extended, partitions)[0])
198 partitions.remove(last)
199 last = partitions[-1]
201 # Leave 2048 blocks at the end
202 new_end = last.end + 2048
204 mount_options = self._get_mount_options(
205 self.disk.getPartitionBySector(last.start).path)
206 if mount_options is not None:
207 stat = os.statvfs(mount_options.mpoint)
208 # Shrink the last partition. The new size should be the size of the
210 blcks = stat.f_blocks - stat.f_bavail
211 new_size = (blcks * stat.f_frsize) // self.disk.device.sectorSize
213 # Add 10% just to be on the safe side
214 part_end = last.start + (new_size * 11) // 10
216 part_end = ((part_end + 2047) // 2048) * 2048
218 image_disk.setPartitionGeometry(
219 image_disk.getPartitionBySector(last.start),
220 parted.Constraint(device=image_disk.device),
221 start=last.start, end=last.end)
224 # Parted may have changed this for better alignment
225 part_end = image_disk.getPartitionBySector(last.start).geometry.end
226 last = last._replace(end=part_end)
227 partitions[-1] = last
229 # Leave 2048 blocks at the end.
230 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 _to_exclude(self):
283 local_filesystems = MKFS_OPTS.keys() + ['rootfs']
284 for entry in self._read_fstable('/proc/mounts'):
285 if entry.fs in local_filesystems:
288 mpoint = entry.mpoint
289 if mpoint in excluded:
292 descendants = filter(lambda p: p.startswith(mpoint + '/'),
295 for d in descendants:
297 excluded.append(mpoint)
302 found_ancestor = False
303 while dirname != '/':
304 (dirname, basename) = os.path.split(dirname)
305 if dirname in excluded:
306 found_ancestor = True
309 if not found_ancestor:
310 excluded.append(mpoint)
312 return map(lambda d: d + "/*", excluded)
314 def _create_filesystems(self, image):
316 partitions = self._get_partitions(parted.Disk(parted.Device(image)))
318 for p in self.disk.partitions:
319 filesystems[p.number] = self._get_mount_options(p.path)
321 unmounted = filter(lambda p: filesystems[p.num] is None, partitions)
322 mounted = filter(lambda p: filesystems[p.num] is not None, partitions)
324 # For partitions that are not mounted right now, we can simply dd them
327 dd('if=%s' % self.disk.device.path, 'of=%s' % image,
328 'count=%d' % (p.end - p.start + 1), 'conv=notrunc',
329 'seek=%d' % p.start, 'skip=%d' % p.start)
331 loop = str(losetup('-f', '--show', image)).strip()
336 mapped[i] = self._map_partition(loop, i, p.start, p.end)
338 # Create the file systems
339 for i, dev in mapped.iteritems():
340 fs = filesystems[i].fs
341 self.out.output('Creating %s filesystem on partition %d ... ' %
343 get_command('mkfs.%s' % fs)(*(MKFS_OPTS[fs] + [dev]))
344 self.out.success('done')
346 target = tempfile.mkdtemp()
348 absmpoints = self._mount(target,
349 [(mapped[i], filesystems[i].mpoint) for i in mapped.keys()]
351 exclude = self._to_exclude() + [image]
352 rsync = Rsync('/', target, exclude)
353 msg = "Copying host files into the image"
354 rsync.archive().run(self.out, msg)
357 self._umount_all(target)
360 for dev in mapped.values():
361 self._unmap_partition(dev)
364 def create_image(self):
366 image = '/mnt/%s.diskdump' % uuid.uuid4().hex
368 disk_size = self.disk.device.getLength() * self.disk.device.sectorSize
370 # Create sparse file to host the image
371 truncate("-s", "%d" % disk_size, image)
373 self._create_partition_table(image)
374 end_sector = self._shrink_partitions(image)
376 # Check if the available space is enough to host the image
377 dirname = os.path.dirname(image)
378 size = (end_sector + 1) * self.disk.device.sectorSize
379 self.out.output("Examining available space in %s ..." % dirname, False)
380 stat = os.statvfs(dirname)
381 available = stat.f_bavail * stat.f_frsize
382 if available <= size:
383 raise FatalError('Not enough space in %s to host the image' %
385 self.out.success("sufficient")
387 self._create_filesystems(image)
391 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :