Skip to content

Commit

Permalink
Merge pull request #242 from diskfs/superblock-test-byte-count
Browse files Browse the repository at this point in the history
get more accurate read of ext4 superblock in tests
  • Loading branch information
deitch committed Jul 22, 2024
2 parents ec697b0 + 71cf74b commit 40221ec
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 27 deletions.
36 changes: 9 additions & 27 deletions filesystem/ext4/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"bytes"
"encoding/binary"
"fmt"
"math"
"os"
"os/exec"
"regexp"
Expand All @@ -25,6 +24,7 @@ const (
testRootDirFile = "testdata/dist/root_directory.bin"
testSuperblockFile = "testdata/dist/superblock.bin"
testFilesystemStats = "testdata/dist/stats.txt"
testKBWrittenFile = "testdata/dist/lifetime_kb.txt"
)

// TestMain sets up the test environment and runs the tests
Expand Down Expand Up @@ -500,22 +500,6 @@ var testSuperblockFuncs = map[string]testSuperblockFunc{
sb.reservedBlocksDefaultGID = uint16(gid)
return nil
},
"Lifetime writes": func(sb *superblock, value string) error {
parts := strings.Split(value, " ")
if len(parts) < 2 {
return fmt.Errorf("invalid lifetime writes string %s", value)
}
writes, err := strconv.ParseUint(parts[0], 10, 64)
if err != nil {
return fmt.Errorf("Lifetime writes: %w", err)
}
// if this is in MB, we need to convert to KB
if parts[1] == "MB" {
writes *= uint64(KB)
}
sb.totalKBWritten = writes
return nil
},
"Filesystem flags": func(sb *superblock, value string) error {
flags := strings.Split(value, " ")
for _, flag := range flags {
Expand Down Expand Up @@ -655,6 +639,7 @@ func testGetValidSuperblockAndGDTs() (sb *superblock, gd []groupDescriptor, supe
}
}
}

// these have been fixed. If they ever change, we will need to modify here.
sb.errorFirstTime = time.Unix(0, 0).UTC()
sb.errorLastTime = time.Unix(0, 0).UTC()
Expand All @@ -665,18 +650,15 @@ func testGetValidSuperblockAndGDTs() (sb *superblock, gd []groupDescriptor, supe
sb.journalSuperblockUUID = &juuid
sb.clusterSize = 1

// this is a bit strange, but necessary. The totalKB written given by all tools round, just enough to make our calculations off
// so we will adjust the value of sb to match expected if it is within 1%; good enough
parsed, err := superblockFromBytes(superblockBytes)
// lifetime writes in KB is done separately, because debug -R "stats" and dumpe2fs only
// round it out
KBWritten, err := os.ReadFile(testKBWrittenFile)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("Failed to parse superblock bytes: %w", err)
return nil, nil, nil, nil, fmt.Errorf("failed to read %s: %w", testKBWrittenFile, err)
}

sbKBWritten := float64(sb.totalKBWritten)
parsedKBWritten := float64(parsed.totalKBWritten)
KBdiff := math.Abs(parsedKBWritten - sbKBWritten)
if KBdiff/sbKBWritten < 0.01 {
sb.totalKBWritten = parsed.totalKBWritten
sb.totalKBWritten, err = strconv.ParseUint(strings.TrimSpace(string(KBWritten)), 10, 64)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("failed to parse KB written: %w", err)
}

return sb, descs, superblockBytes, gdtBytes[:64*len(descs)], nil
Expand Down
1 change: 1 addition & 0 deletions filesystem/ext4/testdata/buildimg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ dd if=ext4.img of=gdt.bin bs=1024 count=1 skip=2
dd if=ext4.img of=superblock.bin bs=1024 count=1 skip=1
dd if=superblock.bin bs=1 skip=208 count=16 2>/dev/null | hexdump -e '16/1 "%02x" "\n"' > journaluuid.txt
dd if=superblock.bin bs=1 skip=$((0x10c)) count=$((15 * 4)) | hexdump -e '15/4 "0x%08x, " "\n"' > journalinodex.txt
dd if=superblock.bin count=2 skip=376 bs=1 2>/dev/null| hexdump -e '1/2 "%u"' > lifetime_kb.txt
EOF

0 comments on commit 40221ec

Please sign in to comment.