module Propellor.Property.Parted.Types where
import qualified Propellor.Property.Partition as Partition
import Utility.DataUnits
import Data.Char
import qualified Data.Semigroup as Sem
import Data.Monoid
import Prelude
class PartedVal a where
pval :: a -> String
data TableType = MSDOS | GPT | AIX | AMIGA | BSD | DVH | LOOP | MAC | PC98 | SUN
deriving (Show)
instance PartedVal TableType where
pval = map toLower . show
data PartTable = PartTable TableType Alignment [Partition]
deriving (Show)
instance Sem.Semigroup PartTable where
PartTable _l1 a1 ps1 <> PartTable l2 a2 ps2 =
PartTable l2 (max a1 a2) (ps1 ++ ps2)
instance Monoid PartTable where
mempty = PartTable MSDOS safeAlignment []
mappend = (Sem.<>)
data Partition = Partition
{ partType :: PartType
, partSize :: PartSize
, partFs :: Maybe Partition.Fs
, partMkFsOpts :: Partition.MkfsOpts
, partFlags :: [(PartFlag, Bool)]
, partName :: Maybe String
}
deriving (Show)
mkPartition :: Maybe Partition.Fs -> PartSize -> Partition
mkPartition fs sz = Partition
{ partType = Primary
, partSize = sz
, partFs = fs
, partMkFsOpts = []
, partFlags = []
, partName = Nothing
}
data PartType = Primary | Logical | Extended
deriving (Show)
instance PartedVal PartType where
pval Primary = "primary"
pval Logical = "logical"
pval Extended = "extended"
data PartSize
= MegaBytes Integer
| Bytes Integer
deriving (Show)
toPartSize :: ByteSize -> PartSize
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 Sem.Semigroup PartSize where
MegaBytes a <> MegaBytes b = MegaBytes (a + b)
Bytes a <> b = Bytes (a + fromPartSize b)
a <> Bytes b = Bytes (b + fromPartSize a)
instance Monoid PartSize where
mempty = MegaBytes 0
mappend = (Sem.<>)
reducePartSize :: PartSize -> PartSize -> PartSize
reducePartSize (MegaBytes a) (MegaBytes b) = MegaBytes (a - b)
reducePartSize (Bytes a) b = Bytes (a - fromPartSize b)
reducePartSize a (Bytes b) = Bytes (fromPartSize a - b)
newtype Alignment = Alignment ByteSize
deriving (Show, Eq, Ord)
safeAlignment :: Alignment
safeAlignment = Alignment (4*1024*1024)
fromAlignment :: Alignment -> ByteSize
fromAlignment (Alignment n) = n
data PartFlag = BootFlag | RootFlag | SwapFlag | HiddenFlag | RaidFlag | LvmFlag | LbaFlag | LegacyBootFlag | IrstFlag | EspFlag | PaloFlag | BiosGrubFlag
deriving (Show)
instance PartedVal PartFlag where
pval BootFlag = "boot"
pval RootFlag = "root"
pval SwapFlag = "swap"
pval HiddenFlag = "hidden"
pval RaidFlag = "raid"
pval LvmFlag = "lvm"
pval LbaFlag = "lba"
pval LegacyBootFlag = "legacy_boot"
pval IrstFlag = "irst"
pval EspFlag = "esp"
pval PaloFlag = "palo"
pval BiosGrubFlag = "bios_grub"
instance PartedVal Bool where
pval True = "on"
pval False = "off"
instance PartedVal Partition.Fs where
pval Partition.EXT2 = "ext2"
pval Partition.EXT3 = "ext3"
pval Partition.EXT4 = "ext4"
pval Partition.BTRFS = "btrfs"
pval Partition.REISERFS = "reiserfs"
pval Partition.XFS = "xfs"
pval Partition.FAT = "fat32"
pval Partition.VFAT = "fat32"
pval Partition.NTFS = "ntfs"
pval Partition.LinuxSwap = "linux-swap"