@@ -7,14 +7,17 @@ import (
77 "context"
88 "fmt"
99 "math"
10+ "os"
1011 "path/filepath"
1112 "runtime"
1213
1314 "bauklotze/pkg/machine/define"
1415 "bauklotze/pkg/machine/events"
16+ "bauklotze/pkg/machine/fs"
1517 "bauklotze/pkg/machine/shim"
1618 "bauklotze/pkg/machine/vmconfig"
1719
20+ "github.com/sirupsen/logrus"
1821 "github.com/urfave/cli/v3"
1922)
2023
@@ -85,10 +88,12 @@ func initMachine(ctx context.Context, cli *cli.Command) error {
8588 VMM : cli .String ("vmm" ),
8689 }
8790
91+ migrateData (opts )
92+
8893 // add a default mount point that store generated ignition scripts
8994 opts .Volumes = append (opts .Volumes , define .IgnMnt )
9095
91- vmcFile := filepath . Join ( vmconfig . Workspace , define . ConfigPrefixDir , fmt . Sprintf ( "%s.json" , opts .VMName ) )
96+ vmcFile := opts .GetVMConfigPath ( )
9297
9398 var reinit bool
9499 mc , err := vmconfig .LoadMachineFromPath (vmcFile )
@@ -117,3 +122,50 @@ func initMachine(ctx context.Context, cli *cli.Command) error {
117122
118123 return nil
119124}
125+
126+ func migrateData (opts * vmconfig.VMOpts ) {
127+ if err := os .RemoveAll (filepath .Join (opts .Workspace , "logs" )); err != nil {
128+ logrus .Warnf ("remove logs failed: %v" , err )
129+ }
130+
131+ if err := os .RemoveAll (filepath .Join (opts .Workspace , "pids" )); err != nil {
132+ logrus .Warnf ("remove pids failed: %v" , err )
133+ }
134+
135+ if err := os .RemoveAll (filepath .Join (opts .Workspace , "config" )); err != nil {
136+ logrus .Warnf ("remove pids failed: %v" , err )
137+ }
138+
139+ if err := os .RemoveAll (filepath .Join (opts .Workspace , "socks" )); err != nil {
140+ logrus .Warnf ("remove pids failed: %v" , err )
141+ }
142+
143+ f := filepath .Join (opts .Workspace , "data" , "libkrun" , "default-arm64-data.raw" )
144+ if runtime .GOARCH == "amd64" {
145+ f = filepath .Join (opts .Workspace , "data" , "vfkit" , "default-amd64-data.raw" )
146+ }
147+ oldDataDiskFile := fs .NewFile (f )
148+
149+ f = filepath .Join (opts .Workspace , opts .VMName , define .DataPrefixDir , "data.img" )
150+ newDataDiskFile := fs .NewFile (f )
151+
152+ if ! oldDataDiskFile .IsExist () {
153+ logrus .Info ("old data disk does not exist, no need to migrate" )
154+ return
155+ }
156+
157+ logrus .Infof ("Move old data disk %q file to %q" , oldDataDiskFile .GetPath (), newDataDiskFile .GetPath ())
158+ if err := os .MkdirAll (filepath .Dir (newDataDiskFile .GetPath ()), 0755 ); err != nil {
159+ logrus .Warnf ("mkdir failed: %v" , err )
160+ return
161+ }
162+
163+ if err := os .Rename (oldDataDiskFile .GetPath (), newDataDiskFile .GetPath ()); err != nil {
164+ logrus .Warnf ("move old data disk file failed: %v" , err )
165+ return
166+ }
167+
168+ if err := os .RemoveAll (filepath .Join (opts .Workspace , "data" )); err != nil {
169+ logrus .Warnf ("remove data failed: %v" , err )
170+ }
171+ }
0 commit comments