summaryrefslogtreecommitdiffstats
path: root/scripts/lib/wic/utils/fs_related.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/lib/wic/utils/fs_related.py')
-rw-r--r--scripts/lib/wic/utils/fs_related.py111
1 files changed, 111 insertions, 0 deletions
diff --git a/scripts/lib/wic/utils/fs_related.py b/scripts/lib/wic/utils/fs_related.py
new file mode 100644
index 0000000000..ea9f85c60f
--- /dev/null
+++ b/scripts/lib/wic/utils/fs_related.py
@@ -0,0 +1,111 @@
1#!/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 sys
22import errno
23import stat
24import random
25import string
26import time
27import uuid
28
29from wic import msger
30from wic.utils import runner
31from wic.utils.errors import *
32from wic.utils.oe.misc import *
33
34def find_binary_path(binary):
35 if os.environ.has_key("PATH"):
36 paths = os.environ["PATH"].split(":")
37 else:
38 paths = []
39 if os.environ.has_key("HOME"):
40 paths += [os.environ["HOME"] + "/bin"]
41 paths += ["/usr/local/sbin", "/usr/local/bin", "/usr/sbin", "/usr/bin", "/sbin", "/bin"]
42
43 for path in paths:
44 bin_path = "%s/%s" % (path, binary)
45 if os.path.exists(bin_path):
46 return bin_path
47
48 print "External command '%s' not found, exiting." % binary
49 print " (Please install '%s' on your host system)" % binary
50 sys.exit(1)
51
52def makedirs(dirname):
53 """A version of os.makedirs() that doesn't throw an
54 exception if the leaf directory already exists.
55 """
56 try:
57 os.makedirs(dirname)
58 except OSError, err:
59 if err.errno != errno.EEXIST:
60 raise
61
62class Disk:
63 """
64 Generic base object for a disk.
65 """
66 def __init__(self, size, device = None):
67 self._device = device
68 self._size = size
69
70 def create(self):
71 pass
72
73 def cleanup(self):
74 pass
75
76 def get_device(self):
77 return self._device
78 def set_device(self, path):
79 self._device = path
80 device = property(get_device, set_device)
81
82 def get_size(self):
83 return self._size
84 size = property(get_size)
85
86
87class DiskImage(Disk):
88 """
89 A Disk backed by a file.
90 """
91 def __init__(self, image_file, size):
92 Disk.__init__(self, size)
93 self.image_file = image_file
94
95 def exists(self):
96 return os.path.exists(self.image_file)
97
98 def create(self):
99 if self.device is not None:
100 return
101
102 blocks = self.size / 1024
103 if self.size - blocks * 1024:
104 blocks += 1
105
106 # create disk image
107 dd_cmd = "dd if=/dev/zero of=%s bs=1024 seek=%d count=1" % \
108 (self.image_file, blocks)
109 exec_cmd(dd_cmd)
110
111 self.device = self.image_file