summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2019-06-19 09:57:35 -0400
committerAndrew Cady <d@jerkface.net>2019-06-19 09:57:35 -0400
commitba7d51a5f5487f466f0b3da68aa731fab35a4774 (patch)
tree61e3b3ea2fea2fad76fc2850ad04145b92cccc5c
parent44fb9d1ece95a1fe59e98f3ffc46dcbc57e2be07 (diff)
remove created files/dirs on exception in initial disk image creation
-rw-r--r--fsmgr.hs52
1 files changed, 29 insertions, 23 deletions
diff --git a/fsmgr.hs b/fsmgr.hs
index fa114ec..0dc1790 100644
--- a/fsmgr.hs
+++ b/fsmgr.hs
@@ -33,37 +33,18 @@ noParent (EmptyImageOfBytes _) = True
33noParent (ParentImageConfigFile _) = False 33noParent (ParentImageConfigFile _) = False
34 34
35buildRoot :: DiskImageConfig -> FilePath -> Action () 35buildRoot :: DiskImageConfig -> FilePath -> Action ()
36buildRoot DiskImageConfig{..} finalOut = do 36buildRoot config@DiskImageConfig{..} finalOut = do
37 let out = finalOut <.> "tmp" 37 let out = finalOut <.> "tmp"
38 mountpoint = finalOut <.> "mnt" 38 mountpoint = finalOut <.> "mnt"
39 cmd_ "sh -c" ["! mountpoint -q \"$0\" || umount \"$0\" ", mountpoint] 39 cmd_ "sh -c" ["! mountpoint -q \"$0\" || umount \"$0\" ", mountpoint]
40 case initialImage of
41 ParentImageConfigFile f -> do
42 let parent = "_build" </> f -<.> "btrfs"
43 need [parent]
44 cmd_ "cp --reflink" [parent, out]
45 cmd_ "btrfstune -f -S0" [out]
46 cmd_ "mkdir -p" [mountpoint]
47 cmd_ "mount -t btrfs" [out] mountpoint
48 EmptyImageOfBytes n -> do
49 cmd_ "truncate -s" [show n] [out]
50 cmd_ "mkfs.btrfs" [out]
51 cmd_ "mkdir -p" [mountpoint]
52 cmd_ "mount -t btrfs" [out] mountpoint
53
54 -- create new default subvolume, and then remount with it
55 createDefaultSubvolume mountpoint
56 cmd_ "umount" [mountpoint]
57 cmd_ "mount -t btrfs" [out, mountpoint]
58 40
59 cmd_ (Cwd mountpoint) "mkdir -p var/cache/apt"
60 cmd_ (Cwd mountpoint) "mkdir -p var/cache/debconf"
61 cmd_ (Cwd mountpoint) "btrfs subvolume create var/cache/apt/archives"
62 -- TODO: catch errors and umount, rmdir mountpoint
63 let (abortion :: IO ()) = do 41 let (abortion :: IO ()) = do
64 cmd_ "umount" [mountpoint] 42 cmd_ "umount" [mountpoint]
65 cmd_ "rmdir" [mountpoint] 43 cmd_ "rmdir" [mountpoint]
66 cmd_ "rm -f" [out] 44 cmd_ "rm -f" [out]
45
46 handle' abortion $ buildInitialImage config mountpoint out
47
67 handle' abortion $ do 48 handle' abortion $ do
68 {- 1. debconf -} 49 {- 1. debconf -}
69 forM_ debconfConfig $ 50 forM_ debconfConfig $
@@ -97,6 +78,31 @@ buildRoot DiskImageConfig{..} finalOut = do
97 cmd_ "btrfstune -S1" [out] 78 cmd_ "btrfstune -S1" [out]
98 cmd_ "mv" [out, finalOut] 79 cmd_ "mv" [out, finalOut]
99 80
81buildInitialImage :: DiskImageConfig -> FilePath -> FilePath -> Action ()
82buildInitialImage DiskImageConfig{..} mountpoint out = do
83 case initialImage of
84 ParentImageConfigFile f -> do
85 let parent = "_build" </> f -<.> "btrfs"
86 need [parent]
87 cmd_ "cp --reflink" [parent, out]
88 cmd_ "btrfstune -f -S0" [out]
89 cmd_ "mkdir -p" [mountpoint]
90 cmd_ "mount -t btrfs" [out] mountpoint
91 EmptyImageOfBytes n -> do
92 cmd_ "truncate -s" [show n] [out]
93 cmd_ "mkfs.btrfs" [out]
94 cmd_ "mkdir -p" [mountpoint]
95 cmd_ "mount -t btrfs" [out] mountpoint
96
97 -- create new default subvolume, and then remount with it
98 createDefaultSubvolume mountpoint
99 cmd_ "umount" [mountpoint]
100 cmd_ "mount -t btrfs" [out, mountpoint]
101
102 cmd_ (Cwd mountpoint) "mkdir -p var/cache/apt"
103 cmd_ (Cwd mountpoint) "mkdir -p var/cache/debconf"
104 cmd_ (Cwd mountpoint) "btrfs subvolume create var/cache/apt/archives"
105
100ignoreErrors :: Action () -> Action () 106ignoreErrors :: Action () -> Action ()
101ignoreErrors = flip actionCatch (\(SomeException _) -> return ()) 107ignoreErrors = flip actionCatch (\(SomeException _) -> return ())
102 108