Skip to content

Commit

Permalink
feat(cli): support extend share dir (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackHole1 authored Apr 18, 2024
1 parent 5fccfe2 commit f6ab86e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
bindPID int
powerSaveMode bool
kernelDebug bool
extendShareDir string
)

func Parse() {
Expand All @@ -44,6 +45,7 @@ func Parse() {
flag.IntVar(&bindPID, "bind-pid", 0, "OVM will exit when the bound pid exited")
flag.BoolVar(&powerSaveMode, "power-save-mode", false, "Enable power save mode")
flag.BoolVar(&kernelDebug, "kernel-debug", false, "Enable kernel debug")
flag.StringVar(&extendShareDir, "extend-share-dir", "", "Extends share directory with the guest. e.g. --extend-share-dir=host-tmp:/tmp,host-var:/var")

flag.Parse()

Expand Down
19 changes: 19 additions & 0 deletions pkg/cli/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Context struct {
EventSocketPath string
PowerSaveMode bool
KernelDebug bool
ExtendShareDir map[string]string

Endpoint string
SSHPort int
Expand Down Expand Up @@ -112,6 +113,24 @@ func (c *Context) basic() error {
c.LockFile = lockPrefixPath + "/" + hash + "-" + name + ".pid"
}

c.ExtendShareDir = make(map[string]string)
if extendShareDir != "" {
for _, item := range strings.Split(extendShareDir, ",") {
parts := strings.Split(item, ":")
if len(parts) != 2 {
return fmt.Errorf("invalid extend share dir: %s", item)
}

if info, err := os.Stat(parts[1]); err != nil {
return fmt.Errorf("extend share dir %s not exists: %w", parts[1], err)
} else if !info.IsDir() {
return fmt.Errorf("extend share dir %s is not a directory", parts[1])
}

c.ExtendShareDir[parts[0]] = parts[1]
}
}

return nil
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/vfkit/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ var mounts = &_mounts{
},
}

func (m *_mounts) extend(tag, shareDir string) {
for _, fs := range mounts.list {
if fs.tag == tag || fs.shareDir == shareDir {
return
}
}

m.list = append(m.list, fs{tag: tag, shareDir: shareDir})
}

func (m *_mounts) toVFKit() (devices []config.VirtioDevice) {
for _, fs := range m.list {
d, _ := config.VirtioFsNew(fs.shareDir, fs.tag)
Expand Down
4 changes: 4 additions & 0 deletions pkg/vfkit/vfkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func Run(ctx context.Context, g *errgroup.Group, opt *cli.Context) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()

for tag, dir := range opt.ExtendShareDir {
mounts.extend(tag, dir)
}

log, err := logger.New(opt.LogPath, opt.Name+"-vfkit")
if err != nil {
return fmt.Errorf("create vfkit logger error: %v", err)
Expand Down

0 comments on commit f6ab86e

Please sign in to comment.