Skip to content

Commit 7720dce

Browse files
authored
refactor(workspace): re-struct the workspace (#87)
New workspace structure ``` ➜ workspace/ . ├── default │   ├── config │   │   └── config.json │   ├── data │   │   ├── data.img │   │   ├── default.img │   │   ├── sshkey │   │   └── sshkey.pub │   ├── logs │   │   └── ovm.log │   ├── pids │   │   ├── gvproxy.pid │   │   └── krunkit.pid │   └── socks │   ├── gvproxy.sock │   ├── gvproxy.sock-krun.sock │   ├── oo-ssh-agent-host.sock │   ├── ovm_restapi.socks │   └── podman-api.sock 8 directories, 14 files ```
1 parent da42589 commit 7720dce

File tree

9 files changed

+116
-26
lines changed

9 files changed

+116
-26
lines changed

cmd/init.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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+
}

cmd/main.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package main
55

66
import (
77
"context"
8+
"errors"
89
"os"
910
"path/filepath"
1011

@@ -51,8 +52,10 @@ func main() {
5152
},
5253
Before: func(ctx context.Context, command *cli.Command) (context.Context, error) {
5354
events.SetReportURL(command.String("report-url"))
55+
// a GLOB var that stores the workspace value, this var only need be initialized once
5456
vmconfig.Workspace = command.String("workspace")
55-
loggerSetup(command.String("log-out"), command.String("workspace"))
57+
logFile := filepath.Join(vmconfig.Workspace, command.String("name"), define.LogPrefixDir, define.LogFileName)
58+
loggerSetup(command.String("log-out"), logFile)
5659
return ctx, nil
5760
},
5861
}
@@ -76,7 +79,7 @@ const MaxSizeInMB = 5
7679
// loggerSetup outType: file, stdout
7780
// if outType is file, workspace is required
7881
// if outType is terminal, workspace is not required, all output will be sent to Terminal's stdout/stderr
79-
func loggerSetup(outType string, workspace string) {
82+
func loggerSetup(outType string, f string) {
8083
logrus.SetLevel(logrus.InfoLevel)
8184
logrus.SetFormatter(&logrus.TextFormatter{
8285
FullTimestamp: true,
@@ -88,19 +91,28 @@ func loggerSetup(outType string, workspace string) {
8891
logrus.SetOutput(os.Stderr)
8992

9093
if outType == define.LogOutFile {
91-
logFile := fs.NewFile(filepath.Join(workspace, define.LogPrefixDir, define.LogFileName))
94+
logFile := fs.NewFile(f)
9295
if logFile.IsExist() {
93-
logrus.Infof("Log file %q already exists, discarding the first 5 Mib", filepath.Join(workspace, define.LogPrefixDir, define.LogFileName))
96+
logrus.Infof("Log file %q already exists, discarding the first 5 Mib", logFile.GetPath())
9497
if err := logFile.DiscardBytesAtBegin(MaxSizeInMB); err != nil {
9598
logrus.Warnf("failed to discard log file: %q", err)
9699
}
97100
}
98101

99-
logrus.Infof("Save log to %q", filepath.Join(workspace, define.LogPrefixDir, define.LogFileName))
100-
if fd, err := os.OpenFile(filepath.Join(workspace, define.LogPrefixDir, define.LogFileName), os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.ModePerm); err == nil {
101-
os.Stdout = fd
102-
os.Stderr = fd
103-
logrus.SetOutput(fd)
102+
logrus.Infof("Save log to %q", logFile.GetPath())
103+
if err := logFile.MakeBaseDir(); err != nil {
104+
logrus.Warnf("failed to create log directory: %q", err)
105+
return
104106
}
107+
108+
fd, err := os.OpenFile(logFile.GetPath(), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
109+
if err != nil && !errors.Is(err, os.ErrNotExist) {
110+
logrus.Warnf("failed to open log file: %q", err)
111+
return
112+
}
113+
114+
os.Stdout = fd
115+
os.Stderr = fd
116+
logrus.SetOutput(fd)
105117
}
106118
}

cmd/start.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ import (
88
"fmt"
99
"os"
1010
"os/signal"
11-
"path/filepath"
1211
"syscall"
1312
"time"
1413

1514
"bauklotze/pkg/api/server"
16-
"bauklotze/pkg/machine/define"
1715
"bauklotze/pkg/machine/events"
1816
"bauklotze/pkg/machine/krunkit"
1917
"bauklotze/pkg/machine/shim"
@@ -39,11 +37,12 @@ var startCmd = cli.Command{
3937

4038
func start(parentCtx context.Context, cli *cli.Command) error {
4139
opts := &vmconfig.VMOpts{
42-
VMName: cli.String("name"),
43-
PPID: cli.Int("ppid"),
40+
Workspace: cli.String("workspace"),
41+
VMName: cli.String("name"),
42+
PPID: cli.Int("ppid"),
4443
}
4544

46-
vmcFile := filepath.Join(vmconfig.Workspace, define.ConfigPrefixDir, fmt.Sprintf("%s.json", opts.VMName))
45+
vmcFile := opts.GetVMConfigPath()
4746

4847
// We first check the status of the pid passed in via --ppid,
4948
// and if it is inactive, exit immediately without running any of the following code

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ require (
4545
github.com/yusufpapurcu/wmi v1.2.4 // indirect
4646
golang.org/x/net v0.36.0 // indirect
4747
golang.org/x/text v0.25.0 // indirect
48-
)
48+
)

pkg/machine/define/define.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const (
4545

4646
IgnMnt = "/tmp/initfs:/tmp/initfs"
4747
SSHAuthLocalSockName = "oo-ssh-agent-host.sock"
48+
VMConfigJson = "config.json"
4849

4950
LogOutFile = "file"
5051
LogOutTerminal = "terminal"

pkg/machine/machine_common.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"bauklotze/pkg/httpclient"
1515
"bauklotze/pkg/machine/define"
1616
"bauklotze/pkg/machine/events"
17+
"bauklotze/pkg/machine/fs"
1718
"bauklotze/pkg/machine/ignition"
1819
sshService "bauklotze/pkg/machine/ssh/service"
1920
"bauklotze/pkg/machine/vmconfig"
@@ -110,7 +111,7 @@ func InitializeVM(opts *vmconfig.VMOpts) (*vmconfig.MachineConfig, error) {
110111
}
111112

112113
logrus.Infof("create data disk image %q with sizeInGb %d", mc.DataDisk.Path, define.DataDiskSizeInGB)
113-
if err := CreateAndResizeDisk(mc.DataDisk.Path, define.DataDiskSizeInGB); err != nil {
114+
if err := CreateAndResizeDisk(mc.DataDisk.Path, define.DataDiskSizeInGB, false); err != nil {
114115
return nil, fmt.Errorf("initialize vm failed: %w", err)
115116
}
116117

@@ -207,7 +208,20 @@ func VirtIOFsToVFKitVirtIODevice(mounts []volumes.Mount) ([]vfConfig.VirtioDevic
207208
}
208209

209210
// CreateAndResizeDisk create a disk file with sizeInGB, and truncate it to sizeInGB.
210-
func CreateAndResizeDisk(f string, sizeInGB int64) error {
211+
func CreateAndResizeDisk(f string, sizeInGB int64, force bool) error {
212+
disk := fs.NewFile(f)
213+
if force {
214+
if err := disk.Delete(); err != nil {
215+
return fmt.Errorf("failed to delete %q: %w", f, err)
216+
}
217+
}
218+
219+
if disk.IsExist() {
220+
logrus.Infof("data disk %q already exists, skip re-create data disk", f)
221+
return nil
222+
}
223+
224+
logrus.Infof("creating data disk %q and resize to %d GB", f, sizeInGB)
211225
file, err := os.OpenFile(f, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
212226
if err != nil {
213227
return fmt.Errorf("failed to create disk: %q, %w", f, err)

pkg/machine/shim/host.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func Update(mc *vmconfig.MachineConfig, opts *vmconfig.VMOpts) (*vmconfig.Machin
5353

5454
if mc.DataDisk.Version != opts.DataVersion {
5555
logrus.Infof("Data image version is not match, try to update data image")
56-
if err := machine.CreateAndResizeDisk(mc.DataDisk.Path, define.DataDiskSizeInGB); err != nil {
56+
if err := machine.CreateAndResizeDisk(mc.DataDisk.Path, define.DataDiskSizeInGB, true); err != nil {
5757
return nil, fmt.Errorf("update data image failed: %w", err)
5858
}
5959
mc.DataDisk.Version = opts.DataVersion

pkg/machine/vmconfig/config.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33

44
package vmconfig
55

6+
import (
7+
"path/filepath"
8+
9+
"bauklotze/pkg/machine/define"
10+
)
11+
612
type ResourceConfig struct {
713
// CPUs to be assigned to the VM
814
CPUs int64 `json:"cpus,omitempty"`
@@ -26,3 +32,7 @@ type VMOpts struct {
2632
ReportURL string
2733
VMM string
2834
}
35+
36+
func (opts *VMOpts) GetVMConfigPath() string {
37+
return filepath.Join(opts.Workspace, opts.VMName, define.ConfigPrefixDir, define.VMConfigJson)
38+
}

pkg/machine/vmconfig/vmconfig.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ type VMState struct {
2828
PodmanReady bool
2929
}
3030

31-
var Workspace string
31+
var (
32+
Workspace string
33+
)
3234

3335
type VMProvider interface { //nolint:interfacebloat
3436
InitializeVM(opts *VMOpts) (*MachineConfig, error)
@@ -178,13 +180,13 @@ func NewMachineConfig(opts *VMOpts) *MachineConfig {
178180
mc.VMType = opts.VMM
179181
mc.VMName = opts.VMName
180182

181-
mc.Dirs.ConfigDir = filepath.Join(Workspace, define.ConfigPrefixDir)
182-
mc.Dirs.DataDir = filepath.Join(Workspace, define.DataPrefixDir)
183-
mc.Dirs.LogsDir = filepath.Join(Workspace, define.LogPrefixDir)
184-
mc.Dirs.SocksDir = filepath.Join(Workspace, define.SocksPrefixDir)
185-
mc.Dirs.PidsDir = filepath.Join(Workspace, define.PidsPrefixDir)
183+
mc.Dirs.ConfigDir = filepath.Join(Workspace, opts.VMName, define.ConfigPrefixDir)
184+
mc.Dirs.DataDir = filepath.Join(Workspace, opts.VMName, define.DataPrefixDir)
185+
mc.Dirs.LogsDir = filepath.Join(Workspace, opts.VMName, define.LogPrefixDir)
186+
mc.Dirs.SocksDir = filepath.Join(Workspace, opts.VMName, define.SocksPrefixDir)
187+
mc.Dirs.PidsDir = filepath.Join(Workspace, opts.VMName, define.PidsPrefixDir)
186188

187-
mc.ConfigFile = filepath.Join(mc.Dirs.ConfigDir, fmt.Sprintf("%s.json", opts.VMName))
189+
mc.ConfigFile = filepath.Join(mc.Dirs.ConfigDir, define.VMConfigJson)
188190
mc.Resources = ResourceConfig{
189191
CPUs: opts.CPUs,
190192
DataDiskSizeGB: define.DataDiskSizeInGB,

0 commit comments

Comments
 (0)