-
Notifications
You must be signed in to change notification settings - Fork 0
/
0005-Add-partitioned-disk-creator.patch
224 lines (216 loc) · 7.9 KB
/
0005-Add-partitioned-disk-creator.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
From 0eb497d06493826b89fddb9ae5aa28be3b2c5165 Mon Sep 17 00:00:00 2001
From: Lubomir Rintel <[email protected]>
Date: Fri, 6 Jun 2014 18:03:04 +0200
Subject: [PATCH 5/8] Add partitioned disk creator
---
imgcreate/creator.py | 163 +++++++++++++++++++++++++++++++++++++++++++++++++++
imgcreate/fs.py | 9 ++-
2 files changed, 170 insertions(+), 2 deletions(-)
diff --git a/imgcreate/creator.py b/imgcreate/creator.py
index 42faf6f..e7bc95d 100644
--- a/imgcreate/creator.py
+++ b/imgcreate/creator.py
@@ -24,6 +24,7 @@ import tempfile
import shutil
import logging
import subprocess
+import parted
import selinux
import yum
@@ -945,3 +946,165 @@ class LoopImageCreator(ImageCreator):
def _stage_final_image(self):
self._resparse()
shutil.move(self._image, self._outdir + "/" + self.name + ".img")
+
+class PartitionedImageCreator(ImageCreator):
+ """Installs a system into a loopback-mountable filesystem image.
+
+ LoopImageCreator is a straightforward ImageCreator subclass; the system
+ is installed into an ext3 filesystem on a sparse file which can be
+ subsequently loopback-mounted.
+
+ """
+
+ def __init__(self, ks, name, fslabel=None, releasever=None, tmpdir="/tmp",
+ useplugins=False, cacheonly=False, docleanup=True):
+ """Initialize a LoopImageCreator instance.
+
+ This method takes the same arguments as ImageCreator.__init__() with
+ the addition of:
+
+ fslabel -- A string used as a label for any filesystems created.
+
+ """
+ ImageCreator.__init__(self, ks, name, releasever=releasever, tmpdir=tmpdir,
+ useplugins=useplugins, cacheonly=cacheonly, docleanup=docleanup)
+
+ self.__fslabel = None
+ self.fslabel = fslabel
+
+ self.__minsize_KB = 0
+ self.__blocksize = 4096
+
+ self.__imgdir = None
+
+ self.__image_size_mb = 0
+ for p in self.ks.handler.partition.partitions:
+ if not p.size:
+ raise CreatorError("All partitions should have sizes")
+ self.__image_size_mb += p.size
+
+ self.__partitions = None
+
+ #
+ # Properties
+ #
+ def __get_image(self):
+ if self.__imgdir is None:
+ raise CreatorError("_image is not valid before calling mount()")
+ return self.__imgdir + "/disk.img"
+ _image = property(__get_image)
+ """The location of the image file.
+
+ This is the path to the filesystem image. Subclasses may use this path
+ in order to package the image in _stage_final_image().
+
+ Note, this directory does not exist before ImageCreator.mount() is called.
+
+ Note also, this is a read-only attribute.
+
+ """
+
+ def _base_on(self, base_on):
+ shutil.copyfile(base_on, self._image)
+
+ #
+ # Actual implementation
+ #
+ def _mount_instroot(self, base_on = None):
+ self.__imgdir = self._mkdtemp()
+
+ # Create a disk image with empty MBR partition table
+ subprocess.call (('dd', 'if=/dev/zero', 'of=%s' % (self._image),
+ 'bs=1024k', 'seek=%s' % (self.__image_size_mb), 'count=0'))
+ device = parted.Device(path=self._image)
+ disk = parted.freshDisk(device, 'msdos')
+
+ partitions = []
+ offset_mb = 0
+ for p in self.ks.handler.partition.partitions:
+
+ offset = offset_mb * 1024L * 1024L
+ size = p.size * 1024L * 1024L
+
+ start_sector = offset / device.sectorSize
+ end_sector = (offset + size) / device.sectorSize - 1
+
+ # Move beginning of the first partition so that it does not
+ # overlap the partition table
+ if start_sector == 0:
+ start_sector = 63
+ size -= 63 * device.sectorSize
+ offset = start_sector * device.sectorSize
+
+ geometry = parted.Geometry (device=device, start=start_sector, end=end_sector)
+ fstype = p.fstype
+ if fstype == 'vfat':
+ fstype = 'fat32'
+ fs = parted.FileSystem (fstype, geometry=geometry)
+ partition = parted.Partition (disk=disk, fs=fs, type=parted.PARTITION_NORMAL, geometry=geometry)
+ geometry = parted.Geometry(device=device, start=start_sector, end=end_sector)
+ constraint = parted.Constraint(exactGeom=geometry)
+ disk.addPartition(partition=partition, constraint=constraint)
+
+ partitions.append({'p': p, 'size': size, 'offset': offset})
+ offset_mb += p.size
+
+ disk.commit()
+
+ # Partitions should be mounted from the shortest paths to the longest
+ self.__partitions = sorted(partitions, key=lambda k: len(k['p'].mountpoint))
+
+ for par in self.__partitions:
+ loop = LoopbackDisk(self._image, par['size'], par['offset'])
+ loop.create()
+ fslabel = par['p'].label
+ if not fslabel:
+ fslabel = "_" + self.fslabel
+ if (par['p'].fstype == 'fat16' or par['p'].fstype == 'fat32' or
+ par['p'].fstype == 'vfat' or par['p'].fstype == 'msdos'):
+ instloop = FatDiskMount(loop,
+ self._instroot + par['p'].mountpoint,
+ 'vfat',
+ self.__blocksize,
+ fslabel,
+ self.tmpdir)
+ else:
+ instloop = ExtDiskMount(loop,
+ self._instroot + par['p'].mountpoint,
+ par['p'].fstype,
+ self.__blocksize,
+ fslabel,
+ self.tmpdir)
+ instloop.mount()
+ par['disk'] = instloop
+
+ def _get_fstab(self):
+ """Return the desired contents of /etc/fstab.
+
+ This is the hook where subclasses may specify the contents of
+ /etc/fstab by returning a string containing the desired contents.
+
+ A sensible default implementation is provided.
+
+ """
+ s = ""
+ i = 1
+ for par in self.__partitions:
+ opts = par['p'].fsopts
+ if not opts:
+ opts = 'defaults'
+ s += "UUID=" + par['disk'].uuid() + " "
+ s += par['p'].mountpoint + " "
+ s += par['p'].fstype + " "
+ s += opts + " "
+ s += "1 %d\n" % i
+ i = i + 1
+ s += self._get_fstab_special()
+ return s
+
+ def _unmount_instroot(self):
+ if not self.__partitions is None:
+ # Unmount in reverse order
+ self.__partitions.reverse()
+ for par in self.__partitions:
+ par['disk'].cleanup()
diff --git a/imgcreate/fs.py b/imgcreate/fs.py
index 98cc9e1..441f793 100644
--- a/imgcreate/fs.py
+++ b/imgcreate/fs.py
@@ -448,13 +448,18 @@ class DiskMount(Mount):
self.mounted = True
+ def uuid(self):
+ args = ("/sbin/blkid", "-s", "UUID", "-o", "value", self.disk.device)
+ rc = subprocess.Popen(args, stdout=subprocess.PIPE).communicate()[0].rstrip()
+ return rc
+
class ExtDiskMount(DiskMount):
"""A DiskMount object that is able to format/resize ext[23] filesystems."""
def __init__(self, disk, mountdir, fstype, blocksize, fslabel,
rmmountdir=True, tmpdir="/tmp"):
DiskMount.__init__(self, disk, mountdir, fstype, rmmountdir)
self.blocksize = blocksize
- self.fslabel = "_" + fslabel
+ self.fslabel = fslabel
self.tmpdir = tmpdir
def __format_filesystem(self):
@@ -547,7 +552,7 @@ class FatDiskMount(DiskMount):
rmmountdir=True, tmpdir="/tmp"):
DiskMount.__init__(self, disk, mountdir, fstype, rmmountdir)
self.blocksize = blocksize
- self.fslabel = "_" + fslabel
+ self.fslabel = fslabel
self.tmpdir = tmpdir
def mount(self):
--
1.9.3