Skip to content

Commit 71cf74b

Browse files
committed
get more accurate read of ext4 superblock in tests
Signed-off-by: Avi Deitcher <[email protected]>
1 parent ec697b0 commit 71cf74b

File tree

2 files changed

+10
-27
lines changed

2 files changed

+10
-27
lines changed

filesystem/ext4/common_test.go

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"bytes"
66
"encoding/binary"
77
"fmt"
8-
"math"
98
"os"
109
"os/exec"
1110
"regexp"
@@ -25,6 +24,7 @@ const (
2524
testRootDirFile = "testdata/dist/root_directory.bin"
2625
testSuperblockFile = "testdata/dist/superblock.bin"
2726
testFilesystemStats = "testdata/dist/stats.txt"
27+
testKBWrittenFile = "testdata/dist/lifetime_kb.txt"
2828
)
2929

3030
// TestMain sets up the test environment and runs the tests
@@ -500,22 +500,6 @@ var testSuperblockFuncs = map[string]testSuperblockFunc{
500500
sb.reservedBlocksDefaultGID = uint16(gid)
501501
return nil
502502
},
503-
"Lifetime writes": func(sb *superblock, value string) error {
504-
parts := strings.Split(value, " ")
505-
if len(parts) < 2 {
506-
return fmt.Errorf("invalid lifetime writes string %s", value)
507-
}
508-
writes, err := strconv.ParseUint(parts[0], 10, 64)
509-
if err != nil {
510-
return fmt.Errorf("Lifetime writes: %w", err)
511-
}
512-
// if this is in MB, we need to convert to KB
513-
if parts[1] == "MB" {
514-
writes *= uint64(KB)
515-
}
516-
sb.totalKBWritten = writes
517-
return nil
518-
},
519503
"Filesystem flags": func(sb *superblock, value string) error {
520504
flags := strings.Split(value, " ")
521505
for _, flag := range flags {
@@ -655,6 +639,7 @@ func testGetValidSuperblockAndGDTs() (sb *superblock, gd []groupDescriptor, supe
655639
}
656640
}
657641
}
642+
658643
// these have been fixed. If they ever change, we will need to modify here.
659644
sb.errorFirstTime = time.Unix(0, 0).UTC()
660645
sb.errorLastTime = time.Unix(0, 0).UTC()
@@ -665,18 +650,15 @@ func testGetValidSuperblockAndGDTs() (sb *superblock, gd []groupDescriptor, supe
665650
sb.journalSuperblockUUID = &juuid
666651
sb.clusterSize = 1
667652

668-
// this is a bit strange, but necessary. The totalKB written given by all tools round, just enough to make our calculations off
669-
// so we will adjust the value of sb to match expected if it is within 1%; good enough
670-
parsed, err := superblockFromBytes(superblockBytes)
653+
// lifetime writes in KB is done separately, because debug -R "stats" and dumpe2fs only
654+
// round it out
655+
KBWritten, err := os.ReadFile(testKBWrittenFile)
671656
if err != nil {
672-
return nil, nil, nil, nil, fmt.Errorf("Failed to parse superblock bytes: %w", err)
657+
return nil, nil, nil, nil, fmt.Errorf("failed to read %s: %w", testKBWrittenFile, err)
673658
}
674-
675-
sbKBWritten := float64(sb.totalKBWritten)
676-
parsedKBWritten := float64(parsed.totalKBWritten)
677-
KBdiff := math.Abs(parsedKBWritten - sbKBWritten)
678-
if KBdiff/sbKBWritten < 0.01 {
679-
sb.totalKBWritten = parsed.totalKBWritten
659+
sb.totalKBWritten, err = strconv.ParseUint(strings.TrimSpace(string(KBWritten)), 10, 64)
660+
if err != nil {
661+
return nil, nil, nil, nil, fmt.Errorf("failed to parse KB written: %w", err)
680662
}
681663

682664
return sb, descs, superblockBytes, gdtBytes[:64*len(descs)], nil

filesystem/ext4/testdata/buildimg.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ dd if=ext4.img of=gdt.bin bs=1024 count=1 skip=2
4141
dd if=ext4.img of=superblock.bin bs=1024 count=1 skip=1
4242
dd if=superblock.bin bs=1 skip=208 count=16 2>/dev/null | hexdump -e '16/1 "%02x" "\n"' > journaluuid.txt
4343
dd if=superblock.bin bs=1 skip=$((0x10c)) count=$((15 * 4)) | hexdump -e '15/4 "0x%08x, " "\n"' > journalinodex.txt
44+
dd if=superblock.bin count=2 skip=376 bs=1 2>/dev/null| hexdump -e '1/2 "%u"' > lifetime_kb.txt
4445
EOF

0 commit comments

Comments
 (0)