summaryrefslogtreecommitdiff
path: root/src/Propellor/Property/Parted/Types.hs
diff options
context:
space:
mode:
authorJoey Hess2017-12-20 16:10:34 -0400
committerJoey Hess2017-12-20 17:10:40 -0400
commit4e20a920baa6c9106179c3d8a1e8e66ffd50ce9c (patch)
treee19903ae14197d0ebd7e676e7b5c070cf4499d50 /src/Propellor/Property/Parted/Types.hs
parentad3cc8fb46b051e45ed51126abec4fda79a4deb6 (diff)
disk partition alignment
Cheap flash drives need partitions aligned to 4 MiB in order to not be slow (and to avoid extra writes). <https://lwn.net/Articles/428584/> 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.
Diffstat (limited to 'src/Propellor/Property/Parted/Types.hs')
-rw-r--r--src/Propellor/Property/Parted/Types.hs53
1 files changed, 35 insertions, 18 deletions
diff --git a/src/Propellor/Property/Parted/Types.hs b/src/Propellor/Property/Parted/Types.hs
index e32df310..6b6b42e2 100644
--- a/src/Propellor/Property/Parted/Types.hs
+++ b/src/Propellor/Property/Parted/Types.hs
@@ -1,6 +1,5 @@
module Propellor.Property.Parted.Types where
-import Propellor.Base
import qualified Propellor.Property.Partition as Partition
import Utility.DataUnits
@@ -17,14 +16,16 @@ instance PartedVal TableType where
pval = map toLower . show
-- | A disk's partition table.
-data PartTable = PartTable TableType [Partition]
+data PartTable = PartTable TableType Alignment [Partition]
deriving (Show)
instance Monoid PartTable where
- -- | default TableType is MSDOS
- mempty = PartTable MSDOS []
+ -- | default TableType is MSDOS, with a `safeAlignment`.
+ mempty = PartTable MSDOS safeAlignment []
-- | uses the TableType of the second parameter
- mappend (PartTable _l1 ps1) (PartTable l2 ps2) = PartTable l2 (ps1 ++ ps2)
+ -- and the larger alignment,
+ mappend (PartTable _l1 a1 ps1) (PartTable l2 a2 ps2) =
+ PartTable l2 (max a1 a2) (ps1 ++ ps2)
-- | A partition on the disk.
data Partition = Partition
@@ -57,34 +58,50 @@ instance PartedVal PartType where
pval Logical = "logical"
pval Extended = "extended"
--- | All partition sizing is done in megabytes, so that parted can
--- automatically lay out the partitions.
---
--- Note that these are SI megabytes, not mebibytes.
-newtype PartSize = MegaBytes Integer
+-- | Size of a partition.
+data PartSize
+ -- Since disk sizes are typically given in MB, not MiB, this
+ -- uses SI MegaBytes (powers of 10).
+ = MegaBytes Integer
+ -- For more control, the partition size can be given in bytes.
+ -- Note that this will prevent any automatic alignment from
+ -- being done.
+ | Bytes Integer
deriving (Show)
-instance PartedVal PartSize where
- pval (MegaBytes n)
- | n > 0 = val n ++ "MB"
- -- parted can't make partitions smaller than 1MB;
- -- avoid failure in edge cases
- | otherwise = "1MB"
-
-- | Rounds up to the nearest MegaByte.
toPartSize :: ByteSize -> PartSize
-toPartSize b = MegaBytes $ ceiling (fromInteger b / 1000000 :: Double)
+toPartSize = toPartSize' ceiling
+
+toPartSize' :: (Double -> Integer) -> ByteSize -> PartSize
+toPartSize' rounder b = MegaBytes $ rounder (fromInteger b / 1000000 :: Double)
fromPartSize :: PartSize -> ByteSize
fromPartSize (MegaBytes b) = b * 1000000
+fromPartSize (Bytes n) = n
instance Monoid PartSize where
mempty = MegaBytes 0
mappend (MegaBytes a) (MegaBytes b) = MegaBytes (a + b)
+ mappend (Bytes a) b = Bytes (a + fromPartSize b)
+ mappend a (Bytes b) = Bytes (b + fromPartSize a)
reducePartSize :: PartSize -> PartSize -> PartSize
reducePartSize (MegaBytes a) (MegaBytes b) = MegaBytes (a - b)
+-- | Partitions need to be aligned for optimal efficiency.
+-- The alignment is a number of bytes.
+newtype Alignment = Alignment ByteSize
+ deriving (Show, Eq, Ord)
+
+-- | 4MiB alignment is optimal for inexpensive flash drives and
+-- is a good safe default for all drives.
+safeAlignment :: Alignment
+safeAlignment = Alignment (4*1024*1024)
+
+fromAlignment :: Alignment -> ByteSize
+fromAlignment (Alignment n) = n
+
-- | Flags that can be set on a partition.
data PartFlag = BootFlag | RootFlag | SwapFlag | HiddenFlag | RaidFlag | LvmFlag | LbaFlag | LegacyBootFlag | IrstFlag | EspFlag | PaloFlag
deriving (Show)