@@ -7,17 +7,15 @@ import (
7
7
"context"
8
8
"crypto/md5"
9
9
"encoding/hex"
10
- "errors"
11
10
"fmt"
12
11
"log/slog"
13
12
"os"
14
13
"os/exec"
15
14
"os/signal"
16
- "strconv"
17
- "strings"
18
15
"syscall"
19
16
20
17
"github.com/edgelesssys/contrast/internal/logger"
18
+ "github.com/edgelesssys/contrast/internal/mount"
21
19
"github.com/spf13/cobra"
22
20
)
23
21
@@ -109,7 +107,7 @@ func setupEncryptedMount(cmd *cobra.Command, _ []string) error {
109
107
return err
110
108
}
111
109
// The decrypted devices with <name> will always be mapped to /dev/mapper/<name> by default.
112
- if err := setupMount (ctx , logger , "/dev/mapper/" + luksVolume .mappingName , luksVolume .volumeMountPoint ); err != nil {
110
+ if err := mount . SetupMount (ctx , logger , "/dev/mapper/" + luksVolume .mappingName , luksVolume .volumeMountPoint ); err != nil {
113
111
return err
114
112
}
115
113
@@ -122,86 +120,6 @@ func setupEncryptedMount(cmd *cobra.Command, _ []string) error {
122
120
return nil
123
121
}
124
122
125
- // setupMount mounts the csi device to the state disk mount point.
126
- func setupMount (ctx context.Context , logger * slog.Logger , devPath , mountPath string ) error {
127
- blk , err := blkid (ctx , devPath )
128
- if errors .Is (err , errNotIdentified ) {
129
- logger .Info ("csi device not identified, assuming first start, formatting" , "device" , devPath )
130
- if err := mkfsExt4 (ctx , devPath ); err != nil {
131
- return err
132
- }
133
- } else if err != nil {
134
- return err
135
- } else if blk .Type != "ext4" {
136
- logger .Info ("csi device is not ext4, assuming first start, formatting" , "device" , devPath )
137
- if err := mkfsExt4 (ctx , devPath ); err != nil {
138
- return err
139
- }
140
- }
141
-
142
- if err := mount (ctx , devPath , mountPath ); err != nil {
143
- return err
144
- }
145
- logger .Info ("csi device mounted to state disk mount point" , "dev" , devPath , "mountPoint" , mountPath )
146
-
147
- return nil
148
- }
149
-
150
- type blk struct {
151
- DevName string
152
- UUID string
153
- BlockSize int
154
- Type string
155
- }
156
-
157
- var errNotIdentified = errors .New ("blkid did not identify the device" )
158
-
159
- func blkid (ctx context.Context , devName string ) (* blk , error ) {
160
- cmd := exec .CommandContext (ctx , "blkid" , "-o" , "export" , devName )
161
- out , err := cmd .CombinedOutput ()
162
- var exitErr * exec.ExitError
163
- if errors .As (err , & exitErr ) && exitErr .ExitCode () == 2 {
164
- // See man page, sec return code.
165
- return nil , errNotIdentified
166
- } else if err != nil {
167
- return nil , fmt .Errorf ("blkid: %w, output: %q" , err , out )
168
- }
169
- lines := strings .Split (string (out ), "\n " )
170
- b := & blk {}
171
- for _ , line := range lines {
172
- if line == "" {
173
- continue
174
- }
175
- key , value , ok := strings .Cut (line , "=" )
176
- if ! ok {
177
- return nil , fmt .Errorf ("parsing blkid output line %q: %w" , line , err )
178
- }
179
- switch key {
180
- case "DEVNAME" :
181
- b .DevName = value
182
- case "UUID" :
183
- b .UUID = value
184
- case "TYPE" :
185
- b .Type = value
186
- case "BLOCK_SIZE" :
187
- b .BlockSize , err = strconv .Atoi (value )
188
- if err != nil {
189
- return nil , fmt .Errorf ("parsing BLOCK_SIZE of blkid output %q: %w" , value , err )
190
- }
191
- }
192
- }
193
- return b , nil
194
- }
195
-
196
- func mkfsExt4 (ctx context.Context , devName string ) error {
197
- cmd := exec .CommandContext (ctx , "mkfs.ext4" , devName )
198
- out , err := cmd .CombinedOutput ()
199
- if err != nil {
200
- return fmt .Errorf ("mkfs.ext4: %w, output: %q" , err , out )
201
- }
202
- return nil
203
- }
204
-
205
123
// isLuks wraps the cryptsetup isLuks command and returns a bool reflecting if the device is formatted as LUKS.
206
124
func isLuks (ctx context.Context , logger * slog.Logger , devName string ) bool {
207
125
if _ , err := exec .CommandContext (ctx , "cryptsetup" , "isLuks" , "--debug" , devName ).CombinedOutput (); err != nil {
@@ -226,16 +144,3 @@ func openEncryptedDevice(ctx context.Context, luksVolume *luksVolume) error {
226
144
}
227
145
return nil
228
146
}
229
-
230
- func mount (ctx context.Context , devName , mountPoint string ) error {
231
- if err := os .MkdirAll (mountPoint , 0o755 ); err != nil {
232
- return fmt .Errorf ("mkdir: %w" , err )
233
- }
234
- cmd := exec .CommandContext (ctx , "mount" , devName , mountPoint )
235
-
236
- out , err := cmd .CombinedOutput ()
237
- if err != nil {
238
- return fmt .Errorf ("mount: %w, output: %q" , err , out )
239
- }
240
- return nil
241
- }
0 commit comments