Skip to content

Commit 7769a64

Browse files
author
Kathryn Baldauf
authored
Merge pull request #1757 from katiewasnothere/kabaldau/scsi_ensure_filesystem
SCSI ensure filesystem
2 parents e5af8fb + af8c444 commit 7769a64

13 files changed

Lines changed: 676 additions & 237 deletions

File tree

ext4/tar2ext4/tar2ext4.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,19 +215,19 @@ func Convert(r io.Reader, w io.ReadWriteSeeker, options ...Option) error {
215215
// More details can be found here https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout
216216
//
217217
// Our goal is to skip the Group 0 padding, read and return the ext4 SuperBlock
218-
func ReadExt4SuperBlock(vhdPath string) (*format.SuperBlock, error) {
219-
vhd, err := os.OpenFile(vhdPath, os.O_RDONLY, 0)
218+
func ReadExt4SuperBlock(devicePath string) (*format.SuperBlock, error) {
219+
dev, err := os.OpenFile(devicePath, os.O_RDONLY, 0)
220220
if err != nil {
221221
return nil, err
222222
}
223-
defer vhd.Close()
223+
defer dev.Close()
224224

225225
// Skip padding at the start
226-
if _, err := vhd.Seek(1024, io.SeekStart); err != nil {
226+
if _, err := dev.Seek(1024, io.SeekStart); err != nil {
227227
return nil, err
228228
}
229229
var sb format.SuperBlock
230-
if err := binary.Read(vhd, binary.LittleEndian, &sb); err != nil {
230+
if err := binary.Read(dev, binary.LittleEndian, &sb); err != nil {
231231
return nil, err
232232
}
233233
// Make sure the magic bytes are correct.
@@ -237,6 +237,15 @@ func ReadExt4SuperBlock(vhdPath string) (*format.SuperBlock, error) {
237237
return &sb, nil
238238
}
239239

240+
// IsDeviceExt4 is will read the device's superblock and determine if it is
241+
// and ext4 superblock.
242+
func IsDeviceExt4(devicePath string) bool {
243+
// ReadExt4SuperBlock will check the superblock magic number for us,
244+
// so we know if no error is returned, this is an ext4 device.
245+
_, err := ReadExt4SuperBlock(devicePath)
246+
return err == nil
247+
}
248+
240249
// ConvertAndComputeRootDigest writes a compact ext4 file system image that contains the files in the
241250
// input tar stream, computes the resulting file image's cryptographic hashes (merkle tree) and returns
242251
// merkle tree root digest. Convert is called with minimal options: ConvertWhiteout and MaximumDiskSize

internal/guest/runtime/hcsv2/uvm.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -980,9 +980,14 @@ func modifyMappedVirtualDisk(
980980
return errors.Wrapf(err, "mounting scsi device controller %d lun %d onto %s denied by policy", mvd.Controller, mvd.Lun, mvd.MountPath)
981981
}
982982
}
983-
983+
config := &scsi.Config{
984+
Encrypted: mvd.Encrypted,
985+
VerityInfo: mvd.VerityInfo,
986+
EnsureFilesystem: mvd.EnsureFilesystem,
987+
Filesystem: mvd.Filesystem,
988+
}
984989
return scsi.Mount(mountCtx, mvd.Controller, mvd.Lun, mvd.Partition, mvd.MountPath,
985-
mvd.ReadOnly, mvd.Encrypted, mvd.Options, mvd.VerityInfo)
990+
mvd.ReadOnly, mvd.Options, config)
986991
}
987992
return nil
988993
case guestrequest.RequestTypeRemove:
@@ -992,9 +997,14 @@ func modifyMappedVirtualDisk(
992997
return fmt.Errorf("unmounting scsi device at %s denied by policy: %w", mvd.MountPath, err)
993998
}
994999
}
995-
1000+
config := &scsi.Config{
1001+
Encrypted: mvd.Encrypted,
1002+
VerityInfo: mvd.VerityInfo,
1003+
EnsureFilesystem: mvd.EnsureFilesystem,
1004+
Filesystem: mvd.Filesystem,
1005+
}
9961006
if err := scsi.Unmount(ctx, mvd.Controller, mvd.Lun, mvd.Partition,
997-
mvd.MountPath, mvd.Encrypted, mvd.VerityInfo); err != nil {
1007+
mvd.MountPath, config); err != nil {
9981008
return err
9991009
}
10001010
}

internal/guest/storage/crypt/crypt.go

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ var (
2121
_cryptsetupOpen = cryptsetupOpen
2222
_generateKeyFile = generateKeyFile
2323
_osMkdirTemp = os.MkdirTemp
24-
_mkfsXfs = mkfsXfs
2524
_osRemoveAll = os.RemoveAll
2625
_zeroFirstBlock = zeroFirstBlock
2726
)
@@ -88,17 +87,6 @@ func cryptsetupClose(deviceName string) error {
8887
return cryptsetupCommand(closeArgs)
8988
}
9089

91-
// invoke mkfs.xfs for the given device.
92-
func mkfsXfs(devicePath string) error {
93-
args := []string{"-f", devicePath}
94-
cmd := exec.Command("mkfs.xfs", args...)
95-
output, err := cmd.CombinedOutput()
96-
if err != nil {
97-
return errors.Wrapf(err, "failed to execute mkfs.ext4: %s", string(output))
98-
}
99-
return nil
100-
}
101-
10290
// EncryptDevice creates a dm-crypt target for a container scratch vhd.
10391
//
10492
// In order to mount a block device as an encrypted device:
@@ -121,12 +109,11 @@ func mkfsXfs(devicePath string) error {
121109
// /dev/mapper/`cryptDeviceTemplate`. This can be mounted directly, but it
122110
// doesn't have any format yet.
123111
//
124-
// 4. Format the unencrypted block device as xfs.
112+
// 4. Prepare the unecrypted block device to be later formatted as xfs
125113
// 4.1. Zero the first block. It appears that mkfs.xfs reads this before formatting.
126-
// 4.2. Format the device as xfs.
127114

128115
func EncryptDevice(ctx context.Context, source string, dmCryptName string) (path string, err error) {
129-
// Create temporary directory to store the keyfile and EXT4 image
116+
// Create temporary directory to store the keyfile and xfs image
130117
tempDir, err := _osMkdirTemp("", "dm-crypt")
131118
if err != nil {
132119
return "", errors.Wrapf(err, "failed to create temporary folder: %s", source)
@@ -172,11 +159,6 @@ func EncryptDevice(ctx context.Context, source string, dmCryptName string) (path
172159
return "", fmt.Errorf("failed to zero first block: %w", err)
173160
}
174161

175-
// 4.2. Format it as xfs
176-
if err = _mkfsXfs(deviceNamePath); err != nil {
177-
return "", fmt.Errorf("mkfs.xfs failed to format %s: %w", deviceNamePath, err)
178-
}
179-
180162
return deviceNamePath, nil
181163
}
182164

internal/guest/storage/crypt/crypt_test.go

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -125,46 +125,6 @@ func Test_Encrypt_Cryptsetup_Open_Error(t *testing.T) {
125125
}
126126
}
127127

128-
func Test_Encrypt_Mkfs_Error(t *testing.T) {
129-
clearCryptTestDependencies()
130-
131-
// Test what happens when mkfs fails to format the unencrypted device.
132-
// Verify that the arguments passed to it are the right ones.
133-
_generateKeyFile = func(path string, size int64) error {
134-
return nil
135-
}
136-
_osRemoveAll = func(path string) error {
137-
return nil
138-
}
139-
_cryptsetupFormat = func(source string, keyFilePath string) error {
140-
return nil
141-
}
142-
_cryptsetupOpen = func(source string, deviceName string, keyFilePath string) error {
143-
return nil
144-
}
145-
_cryptsetupClose = func(deviceName string) error {
146-
return nil
147-
}
148-
_zeroFirstBlock = func(_ string, _ int) error {
149-
return nil
150-
}
151-
152-
source := "/dev/sda"
153-
formatTarget := "/dev/mapper/dm-crypt-name"
154-
155-
expectedErr := errors.New("expected error message")
156-
_mkfsXfs = func(arg string) error {
157-
if arg != formatTarget {
158-
t.Fatalf("expected args: '%v' got: '%v'", formatTarget, arg)
159-
}
160-
return expectedErr
161-
}
162-
163-
if _, err := EncryptDevice(context.Background(), source, "dm-crypt-name"); errors.Unwrap(err) != expectedErr {
164-
t.Fatalf("expected err: '%v' got: '%v'", expectedErr, err)
165-
}
166-
}
167-
168128
func Test_Encrypt_Success(t *testing.T) {
169129
clearCryptTestDependencies()
170130

@@ -184,9 +144,6 @@ func Test_Encrypt_Success(t *testing.T) {
184144
_zeroFirstBlock = func(_ string, _ int) error {
185145
return nil
186146
}
187-
_mkfsXfs = func(arg string) error {
188-
return nil
189-
}
190147

191148
source := "/dev/sda"
192149
dmCryptName := "dm-crypt-name"

internal/guest/storage/ext4/format.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func mkfsExt4Command(args []string) error {
1818
}
1919
return nil
2020
}
21-
func FormatExt4(ctx context.Context, source string) error {
21+
func Format(ctx context.Context, source string) error {
2222
// Format source as ext4
2323
if err := mkfsExt4Command([]string{source}); err != nil {
2424
return fmt.Errorf("mkfs.ext4 failed to format %s: %w", source, err)

0 commit comments

Comments
 (0)