From 4e20a920baa6c9106179c3d8a1e8e66ffd50ce9c Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Wed, 20 Dec 2017 16:10:34 -0400 Subject: disk partition alignment Cheap flash drives need partitions aligned to 4 MiB in order to not be slow (and to avoid extra writes). And at least 1 MiB alignment is generally a good idea, and most people seem to think 4 MiB is for all drives. I noticed that Parted.partitioned does not do that; the first partition started at an offset of 1 MB, and subsequent partitions from where it ends. (The 1 MB offset came from the PartedVal PartSize instance, and note that it was not 1 MiB.) * Parted: Add an Alignment parameter. (API change) A good default to use is safeAlignment, which is 4MiB, well suited for inexpensive flash drives, and fine for other disks too. Previously, a very non-optimial 1MB (not 1MiB) alignment had been used. * DiskImage: Use safeAlignment. It didn't seem worth making the alignment configurable here. Alignment is implemented by offsetting the first partition's start position so it's aligned (making sure to leave room for the partition table). Each partition is then extended as needed so the next partition will start properly aligned. Note that parted rejects partition tables that don't fit in cylinder bounderies. Before, propellor let parted deal with the fine details of layout, so that was not a problem. Now it's possible to set some wacky Alignment not divisible by 512, or use Byte sizes for partitions and create a partition table that parted rejects. But, using safeAlignment and MegaBytes should always be safe. Also, this fixes a rounding bug in Parted.calcPartTable. It was rounding up to the nearest MegaByte when allocating remaining disk space, so returned partition table that was actually larger than the disk size. This commit was sponsored by an anonymous bitcoiner. --- src/Propellor/Property/DiskImage.hs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/Propellor/Property/DiskImage.hs') diff --git a/src/Propellor/Property/DiskImage.hs b/src/Propellor/Property/DiskImage.hs index 6564192f..79865db4 100644 --- a/src/Propellor/Property/DiskImage.hs +++ b/src/Propellor/Property/DiskImage.hs @@ -265,7 +265,7 @@ imageBuiltFrom img chrootdir tabletype final partspec = mkimg rmimg imageFinalized final dest mnts mntopts devs parttable rmimg = undoRevertableProperty (buildDiskImage img) `before` undoRevertableProperty (imageExists' dest dummyparttable) - dummyparttable = PartTable tabletype [] + dummyparttable = PartTable tabletype safeAlignment [] partitionsPopulated :: FilePath -> [Maybe MountPoint] -> [MountOpts] -> [LoopDev] -> Property DebianLike partitionsPopulated chrootdir mnts mntopts devs = property' desc $ \w -> @@ -300,7 +300,7 @@ fitChrootSize :: TableType -> [PartSpec ()] -> [PartSize] -> ([Maybe MountPoint] fitChrootSize tt l basesizes = (mounts, mountopts, parttable) where (mounts, mountopts, sizers, _) = unzip4 l - parttable = PartTable tt (zipWith id sizers basesizes) + parttable = PartTable tt safeAlignment (zipWith id sizers basesizes) -- | Generates a map of the sizes of the contents of -- every directory in a filesystem tree. @@ -388,7 +388,7 @@ imageExists' dest@(RawDiskImage img) parttable = (setup cleanup) `describe` type Finalization = (RawDiskImage -> FilePath -> [LoopDev] -> Property Linux) imageFinalized :: Finalization -> RawDiskImage -> [Maybe MountPoint] -> [MountOpts] -> [LoopDev] -> PartTable -> Property Linux -imageFinalized final img mnts mntopts devs (PartTable _ parts) = +imageFinalized final img mnts mntopts devs (PartTable _ _ parts) = property' "disk image finalized" $ \w -> withTmpDir "mnt" $ \top -> go w top `finally` liftIO (unmountall top) -- cgit v1.2.3