blob: 2e74461a4021b20681507e3820e5a90754e7752d [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#!/usr/bin/env python -tt
2#
3# Copyright (c) 2007, Red Hat, Inc.
4# Copyright (c) 2009, 2010, 2011 Intel, Inc.
5#
6# This program is free software; you can redistribute it and/or modify it
7# under the terms of the GNU General Public License as published by the Free
8# Software Foundation; version 2 of the License
9#
10# This program is distributed in the hope that it will be useful, but
11# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13# for more details.
14#
15# You should have received a copy of the GNU General Public License along
16# with this program; if not, write to the Free Software Foundation, Inc., 59
17# Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19from __future__ import with_statement
20import os
21import errno
22
23from wic.utils.oe.misc import exec_cmd
24
25def makedirs(dirname):
26 """A version of os.makedirs() that doesn't throw an
27 exception if the leaf directory already exists.
28 """
29 try:
30 os.makedirs(dirname)
31 except OSError, err:
32 if err.errno != errno.EEXIST:
33 raise
34
35class Disk:
36 """
37 Generic base object for a disk.
38 """
39 def __init__(self, size, device=None):
40 self._device = device
41 self._size = size
42
43 def create(self):
44 pass
45
46 def cleanup(self):
47 pass
48
49 def get_device(self):
50 return self._device
51 def set_device(self, path):
52 self._device = path
53 device = property(get_device, set_device)
54
55 def get_size(self):
56 return self._size
57 size = property(get_size)
58
59
60class DiskImage(Disk):
61 """
62 A Disk backed by a file.
63 """
64 def __init__(self, image_file, size):
65 Disk.__init__(self, size)
66 self.image_file = image_file
67
68 def exists(self):
69 return os.path.exists(self.image_file)
70
71 def create(self):
72 if self.device is not None:
73 return
74
75 blocks = self.size / 1024
76 if self.size - blocks * 1024:
77 blocks += 1
78
79 # create disk image
80 dd_cmd = "dd if=/dev/zero of=%s bs=1024 seek=%d count=1" % \
81 (self.image_file, blocks)
82 exec_cmd(dd_cmd)
83
84 self.device = self.image_file