Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add real size to backing image #313

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type BackingImage struct {
UUID string `json:"uuid"`
Size int64 `json:"size"`
VirtualSize int64 `json:"virtualSize"`
RealSize int64 `json:"realSize"`
ExpectedChecksum string `json:"expectedChecksum"`

Status BackingImageStatus `json:"status"`
Expand All @@ -33,6 +34,7 @@ func RPCToBackingImage(obj *rpc.BackingImageResponse) *BackingImage {
UUID: obj.Spec.Uuid,
Size: obj.Spec.Size,
VirtualSize: obj.Spec.VirtualSize,
RealSize: obj.Spec.RealSize,
ExpectedChecksum: obj.Spec.Checksum,

Status: BackingImageStatus{
Expand Down Expand Up @@ -114,6 +116,7 @@ type FileInfo struct {
UUID string `json:"uuid"`
Size int64 `json:"size"`
VirtualSize int64 `json:"virtualSize"`
RealSize int64 `json:"realSize"`
State string `json:"state"`
Progress int `json:"progress"`
ProcessedSize int64 `json:"processedSize"`
Expand Down
1 change: 1 addition & 0 deletions pkg/manager/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ func backingImageResponse(fInfo *api.FileInfo) *rpc.BackingImageResponse {
Uuid: fInfo.UUID,
Size: fInfo.Size,
VirtualSize: fInfo.VirtualSize,
RealSize: fInfo.RealSize,
Checksum: fInfo.ExpectedChecksum,
},
Status: &rpc.BackingImageStatus{
Expand Down
1 change: 1 addition & 0 deletions pkg/sync/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,7 @@ func getAndWaitFileState(cli *client.SyncClient, curPath, desireState string, wa
UUID: fInfo.UUID,
Size: fInfo.Size,
VirtualSize: fInfo.VirtualSize,
RealSize: fInfo.RealSize,
ExpectedChecksum: fInfo.ExpectedChecksum,
CurrentChecksum: fInfo.CurrentChecksum,
ModificationTime: fInfo.ModificationTime,
Expand Down
20 changes: 19 additions & 1 deletion pkg/sync/sync_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type SyncingFile struct {
diskUUID string
size int64
virtualSize int64
realSize int64
state types.State
progress int
processedSize int64
Expand Down Expand Up @@ -258,6 +259,7 @@ func (sf *SyncingFile) checkAndReuseFile() (err error) {
sf.modificationTime = info.ModTime().UTC().String()
sf.updateSyncReadyNoLock()
sf.updateVirtualSizeNoLock(sf.filePath)
sf.updateRealSizeNoLock(sf.filePath)
sf.writeConfigNoLock()
sf.lock.Unlock()

Expand Down Expand Up @@ -359,6 +361,7 @@ func (sf *SyncingFile) getNoLock() api.FileInfo {
UUID: sf.uuid,
Size: sf.size,
VirtualSize: sf.virtualSize,
RealSize: sf.realSize,
State: string(sf.state),
Progress: sf.progress,
ProcessedSize: sf.processedSize,
Expand Down Expand Up @@ -917,6 +920,7 @@ func (sf *SyncingFile) finishProcessing(err error) (finalErr error) {
sf.currentChecksum = config.CurrentChecksum
sf.updateSyncReadyNoLock()
sf.updateVirtualSizeNoLock(sf.tmpFilePath)
sf.updateRealSizeNoLock(sf.tmpFilePath)
sf.writeConfigNoLock()

// Renaming won't change the file modification time.
Expand Down Expand Up @@ -966,6 +970,7 @@ func (sf *SyncingFile) postProcessSyncFile() {
}
sf.updateSyncReadyNoLock()
sf.updateVirtualSizeNoLock(sf.tmpFilePath)
sf.updateRealSizeNoLock(sf.tmpFilePath)
sf.writeConfigNoLock()

// Renaming won't change the file modification time.
Expand All @@ -992,14 +997,26 @@ func (sf *SyncingFile) updateVirtualSizeNoLock(filePath string) {
// with sf.tmpFilePath, sometimes with sf.filePath :-/
imgInfo, err := util.GetQemuImgInfo(filePath)
if err != nil {
sf.log.Warnf("SyncingFile: failed to get backing image virtual size: %v", err)
sf.log.WithError(err).Warnf("SyncingFile: failed to get backing image virtual size")
}
// This will be zero when there is an error, which allows components
// further up the stack to know that the virtual size somehow isn't
// available yet.
sf.virtualSize = imgInfo.VirtualSize
}

func (sf *SyncingFile) updateRealSizeNoLock(filePath string) {
realSize, err := util.GetFileRealSize(filePath)
if err != nil {
sf.log.WithError(err).Warnf("SyncingFile: failed to get backing image virtual size")
}

// This will be zero when there is an error, which allows components
// further up the stack to know that the real size somehow isn't
derekbit marked this conversation as resolved.
Show resolved Hide resolved
// available yet.
sf.realSize = realSize
}

func (sf *SyncingFile) handleFailureNoLock(err error) {
if err == nil {
return
Expand Down Expand Up @@ -1029,6 +1046,7 @@ func (sf *SyncingFile) writeConfigNoLock() {
UUID: sf.uuid,
Size: sf.size,
VirtualSize: sf.virtualSize,
RealSize: sf.realSize,
ExpectedChecksum: sf.expectedChecksum,
CurrentChecksum: sf.currentChecksum,
ModificationTime: sf.modificationTime,
Expand Down
3 changes: 2 additions & 1 deletion pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const (
DiskPathInContainer = "/data/"
DataSourceDirectoryName = "/tmp/"

DefaultSectorSize = 512
DefaultSectorSize = 512
DefaultLinuxBlcokSize = 512

DefaultManagerPort = 8000
DefaultDataSourceServerPort = 8000
Expand Down
16 changes: 16 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"os/exec"
"path/filepath"
"regexp"
"syscall"
"time"

"github.com/pkg/errors"
Expand All @@ -23,6 +24,8 @@ import (
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"

"github.com/longhorn/backing-image-manager/pkg/types"
)

const (
Expand Down Expand Up @@ -151,6 +154,7 @@ type SyncingFileConfig struct {
UUID string `json:"uuid"`
Size int64 `json:"size"`
VirtualSize int64 `json:"virtualSize"`
RealSize int64 `json:"realSize"`
ExpectedChecksum string `json:"expectedChecksum"`
CurrentChecksum string `json:"currentChecksum"`
ModificationTime string `json:"modificationTime"`
Expand Down Expand Up @@ -313,6 +317,18 @@ func ConvertFromQcow2ToRaw(sourcePath, targetPath string) error {
return nil
}

func GetFileRealSize(filePath string) (int64, error) {
var stat syscall.Stat_t
err := syscall.Stat(filePath, &stat)
if err != nil {
return 0, err
}
fmt.Printf("stat.Blksize: %v\n", stat.Blksize)

// 512 is defined in the Linux kernel and remains consistent across all distributions.
return stat.Blocks * types.DefaultLinuxBlcokSize, nil
}

func FileModificationTime(filePath string) string {
fi, err := os.Stat(filePath)
if err != nil {
Expand Down