Skip to content

Commit df1c2d3

Browse files
author
Xun Chen
committed
Extend tcmu dev_config to support optional device ID
Signed-off-by: Xun Chen <xunchen@hust.edu.cn>
1 parent aa11595 commit df1c2d3

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

pkg/snapshot/storage.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,31 @@ func IsErofsFilesystem(path string) bool {
275275
return binary.LittleEndian.Uint32(byte4) == 0xe0f5e1e2
276276
}
277277

278+
// IsExperimentalEnabled checks whether the experimental.enable field is true in the config file.
279+
func IsExperimentalEnabled(configPath string) (bool, error) {
280+
// Read the configuration file
281+
data, err := os.ReadFile(configPath)
282+
if err != nil {
283+
return false, fmt.Errorf("failed to read config file %s: %w", configPath, err)
284+
}
285+
286+
// Define a struct to match the JSON structure
287+
type Config struct {
288+
Experimental struct {
289+
Enable bool `json:"enable"`
290+
} `json:"experimental"`
291+
}
292+
293+
// Parse the JSON data
294+
var config Config
295+
if err := json.Unmarshal(data, &config); err != nil {
296+
return false, fmt.Errorf("failed to parse config file %s: %w", configPath, err)
297+
}
298+
299+
// Return the value of experimental.enable
300+
return config.Experimental.Enable, nil
301+
}
302+
278303
func AttachDevice(ctx context.Context, params *AttachDeviceParams) (devName string, e error) {
279304

280305
devName = ""
@@ -299,8 +324,19 @@ func AttachDevice(ctx context.Context, params *AttachDeviceParams) (devName stri
299324
}
300325
}()
301326

302-
if err = os.WriteFile(path.Join(targetPath, "control"), ([]byte)(fmt.Sprintf("dev_config=overlaybd/%s", configPath)), 0666); err != nil {
303-
return devName, fmt.Errorf("failed to write target dev_config for %s: dev_config=overlaybd/%s: %w", targetPath, configPath, err)
327+
enabled, err := IsExperimentalEnabled("/etc/overlaybd-snapshotter/config.json")
328+
if err != nil {
329+
return devName, fmt.Errorf("failed to check experimental flag: %w", err)
330+
}
331+
log.G(ctx).Infof("experimental enabled: %t", enabled)
332+
if enabled {
333+
if err = os.WriteFile(path.Join(targetPath, "control"), ([]byte)(fmt.Sprintf("dev_config=overlaybd/%s;%s", configPath, snID)), 0666); err != nil {
334+
return devName, fmt.Errorf("failed to write target dev_config for %s: dev_config=overlaybd/%s;%s: %w", targetPath, configPath, snID, err)
335+
}
336+
} else {
337+
if err = os.WriteFile(path.Join(targetPath, "control"), ([]byte)(fmt.Sprintf("dev_config=overlaybd/%s", configPath)), 0666); err != nil {
338+
return devName, fmt.Errorf("failed to write target dev_config for %s: dev_config=overlaybd/%s: %w", targetPath, configPath, err)
339+
}
304340
}
305341

306342
err = os.WriteFile(path.Join(targetPath, "control"), ([]byte)(fmt.Sprintf("max_data_area_mb=%d", obdMaxDataAreaMB)), 0666)

script/config.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@
66
"runtimeType": "containerd",
77
"logReportCaller": false,
88
"autoRemoveDev": false,
9-
"mirrorRegistry": []
9+
"mirrorRegistry": [],
10+
"experimental": {
11+
"enable": true
12+
}
1013
}

0 commit comments

Comments
 (0)