summaryrefslogtreecommitdiffstats
path: root/import-layers/yocto-poky/scripts/lib/wic/utils/partitionedfs.py
diff options
context:
space:
mode:
Diffstat (limited to 'import-layers/yocto-poky/scripts/lib/wic/utils/partitionedfs.py')
-rw-r--r--import-layers/yocto-poky/scripts/lib/wic/utils/partitionedfs.py52
1 files changed, 35 insertions, 17 deletions
diff --git a/import-layers/yocto-poky/scripts/lib/wic/utils/partitionedfs.py b/import-layers/yocto-poky/scripts/lib/wic/utils/partitionedfs.py
index ad596d26f..cb03009fc 100644
--- a/import-layers/yocto-poky/scripts/lib/wic/utils/partitionedfs.py
+++ b/import-layers/yocto-poky/scripts/lib/wic/utils/partitionedfs.py
@@ -22,6 +22,7 @@ import os
from wic import msger
from wic.utils.errors import ImageError
from wic.utils.oe.misc import exec_cmd, exec_native_cmd
+from wic.filemap import sparse_copy
# Overhead of the MBR partitioning scheme (just one sector)
MBR_OVERHEAD = 1
@@ -32,7 +33,7 @@ GPT_OVERHEAD = 34
# Size of a sector in bytes
SECTOR_SIZE = 512
-class Image(object):
+class Image():
"""
Generic base object for an image.
@@ -42,6 +43,7 @@ class Image(object):
def __init__(self, native_sysroot=None):
self.disks = {}
self.partitions = []
+ self.partimages = []
# Size of a sector used in calculations
self.sector_size = SECTOR_SIZE
self._partitions_layed_out = False
@@ -66,15 +68,17 @@ class Image(object):
'offset': 0, # Offset of next partition (in sectors)
# Minimum required disk size to fit all partitions (in bytes)
'min_size': 0,
- 'ptable_format': "msdos"} # Partition table format
+ 'ptable_format': "msdos", # Partition table format
+ 'identifier': None} # Disk system identifier
- def add_disk(self, disk_name, disk_obj):
+ def add_disk(self, disk_name, disk_obj, identifier):
""" Add a disk object which have to be partitioned. More than one disk
can be added. In case of multiple disks, disk partitions have to be
added for each disk separately with 'add_partition()". """
self.__add_disk(disk_name)
self.disks[disk_name]['disk'] = disk_obj
+ self.disks[disk_name]['identifier'] = identifier
def __add_partition(self, part):
""" This is a helper function for 'add_partition()' which adds a
@@ -87,14 +91,14 @@ class Image(object):
def add_partition(self, size, disk_name, mountpoint, source_file=None, fstype=None,
label=None, fsopts=None, boot=False, align=None, no_table=False,
- part_type=None, uuid=None):
+ part_type=None, uuid=None, system_id=None):
""" Add the next partition. Prtitions have to be added in the
first-to-last order. """
ks_pnum = len(self.partitions)
# Converting kB to sectors for parted
- size = size * 1024 / self.sector_size
+ size = size * 1024 // self.sector_size
part = {'ks_pnum': ks_pnum, # Partition number in the KS file
'size': size, # In sectors
@@ -110,7 +114,8 @@ class Image(object):
'align': align, # Partition alignment
'no_table' : no_table, # Partition does not appear in partition table
'part_type' : part_type, # Partition type
- 'uuid': uuid} # Partition UUID
+ 'uuid': uuid, # Partition UUID
+ 'system_id': system_id} # Partition system id
self.__add_partition(part)
@@ -130,7 +135,7 @@ class Image(object):
for num in range(len(self.partitions)):
part = self.partitions[num]
- if not self.disks.has_key(part['disk_name']):
+ if part['disk_name'] not in self.disks:
raise ImageError("No disk %s for partition %s" \
% (part['disk_name'], part['mountpoint']))
@@ -171,12 +176,12 @@ class Image(object):
# gaps we could enlargea the previous partition?
# Calc how much the alignment is off.
- align_sectors = disk['offset'] % (part['align'] * 1024 / self.sector_size)
+ align_sectors = disk['offset'] % (part['align'] * 1024 // self.sector_size)
if align_sectors:
# If partition is not aligned as required, we need
# to move forward to the next alignment point
- align_sectors = (part['align'] * 1024 / self.sector_size) - align_sectors
+ align_sectors = (part['align'] * 1024 // self.sector_size) - align_sectors
msger.debug("Realignment for %s%s with %s sectors, original"
" offset %s, target alignment is %sK." %
@@ -234,7 +239,7 @@ class Image(object):
def __format_disks(self):
self.layout_partitions()
- for dev in self.disks.keys():
+ for dev in self.disks:
disk = self.disks[dev]
msger.debug("Initializing partition table for %s" % \
(disk['disk'].device))
@@ -242,6 +247,12 @@ class Image(object):
(disk['disk'].device, disk['ptable_format']),
self.native_sysroot)
+ if disk['identifier']:
+ msger.debug("Set disk identifier %x" % disk['identifier'])
+ with open(disk['disk'].device, 'r+b') as img:
+ img.seek(0x1B8)
+ img.write(disk['identifier'].to_bytes(4, 'little'))
+
msger.debug("Creating partitions")
for part in self.partitions:
@@ -296,7 +307,7 @@ class Image(object):
(part['num'], part['part_type'],
disk['disk'].device), self.native_sysroot)
- if part['uuid']:
+ if part['uuid'] and disk['ptable_format'] == "gpt":
msger.debug("partition %d: set UUID to %s" % \
(part['num'], part['uuid']))
exec_native_cmd("sgdisk --partition-guid=%d:%s %s" % \
@@ -310,6 +321,10 @@ class Image(object):
exec_native_cmd("parted -s %s set %d %s on" % \
(disk['disk'].device, part['num'], flag_name),
self.native_sysroot)
+ if part['system_id']:
+ exec_native_cmd("sfdisk --part-type %s %s %s" % \
+ (disk['disk'].device, part['num'], part['system_id']),
+ self.native_sysroot)
# Parted defaults to enabling the lba flag for fat16 partitions,
# which causes compatibility issues with some firmware (and really
@@ -330,6 +345,10 @@ class Image(object):
disk['disk'].cleanup()
except:
pass
+ # remove partition images
+ for image in self.partimages:
+ if os.path.isfile(image):
+ os.remove(image)
def assemble(self, image_file):
msger.debug("Installing partitions")
@@ -338,20 +357,19 @@ class Image(object):
source = part['source_file']
if source:
# install source_file contents into a partition
- cmd = "dd if=%s of=%s bs=%d seek=%d count=%d conv=notrunc" % \
- (source, image_file, self.sector_size,
- part['start'], part['size'])
- exec_cmd(cmd)
+ sparse_copy(source, image_file, part['start'] * self.sector_size)
msger.debug("Installed %s in partition %d, sectors %d-%d, "
"size %d sectors" % \
(source, part['num'], part['start'],
part['start'] + part['size'] - 1, part['size']))
- os.rename(source, image_file + '.p%d' % part['num'])
+ partimage = image_file + '.p%d' % part['num']
+ os.rename(source, partimage)
+ self.partimages.append(partimage)
def create(self):
- for dev in self.disks.keys():
+ for dev in self.disks:
disk = self.disks[dev]
disk['disk'].create()
OpenPOWER on IntegriCloud