From b72e2035453a4dc8fc55c79677c51a7537877186 Mon Sep 17 00:00:00 2001 From: Hongzhen Luo Date: Wed, 21 Aug 2024 14:19:53 +0800 Subject: [PATCH] Set the correct fsType for erofs when prefetching through the file list When prefetching using the file list, the default fsType is ext4, even if the file system on the virtual block device is actually erofs. This sets the correct fsType based on the file system type present on the virtual block device. Signed-off-by: Hongzhen Luo --- pkg/snapshot/storage.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pkg/snapshot/storage.go b/pkg/snapshot/storage.go index 7d5cc723..bd5736f8 100644 --- a/pkg/snapshot/storage.go +++ b/pkg/snapshot/storage.go @@ -19,6 +19,7 @@ package snapshot import ( "bufio" "context" + "encoding/binary" "encoding/json" "fmt" "io" @@ -211,6 +212,23 @@ func (o *snapshotter) unmountAndDetachBlockDevice(ctx context.Context, snID stri return nil } +// determine whether the block device represented +// by @path is eorfs filesystem +func IsErofsFilesystem(path string) bool { + f, err := os.Open(path) + if err != nil { + return false + } + defer f.Close() + + byte4 := make([]byte, 4) + _, err = f.ReadAt(byte4, 1024) + if err != nil { + return false + } + return binary.LittleEndian.Uint32(byte4) == 0xe0f5e1e2 +} + // attachAndMountBlockDevice // // TODO(fuweid): need to track the middle state if the process has been killed. @@ -363,6 +381,10 @@ func (o *snapshotter) attachAndMountBlockDevice(ctx context.Context, snID string options := strings.Split(fsType, ";") fstype := options[0] + if IsErofsFilesystem(device) { + fstype = "erofs" + } + if mkfs { args := []string{"-t", fstype} if len(options) > 2 {