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

Podman volumes #11699

Closed
wants to merge 4 commits into from
Closed
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
4 changes: 4 additions & 0 deletions cmd/podman/machine/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ func init() {
flags.StringVar(&initOpts.ImagePath, ImagePathFlagName, cfg.Engine.MachineImage, "Path to qcow image")
_ = initCmd.RegisterFlagCompletionFunc(ImagePathFlagName, completion.AutocompleteDefault)

VolumeFlagName := "volume"
flags.StringArrayVarP(&initOpts.Volumes, VolumeFlagName, "v", []string{}, "Volumes to mount, source:target")
_ = initCmd.RegisterFlagCompletionFunc(VolumeFlagName, completion.AutocompleteDefault)

IgnitionPathFlagName := "ignition-path"
flags.StringVar(&initOpts.IgnitionPath, IgnitionPathFlagName, "", "Path to ignition file")
_ = initCmd.RegisterFlagCompletionFunc(IgnitionPathFlagName, completion.AutocompleteDefault)
Expand Down
4 changes: 4 additions & 0 deletions docs/source/markdown/podman-machine-init.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ Defaults to `testing`.

Memory (in MB).

#### **--volume**, **-v**=*source:target*

Mounts a volume from `source` to `target`.
Copy link
Member

@rhatdan rhatdan Sep 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a much richer explanation.
Something like:

The source volume will become available to local podman commands.  If you mounted your home 
directory into the podman machine. Then `podman run  -v /home/dwalsh:/home/dwalsh ...` commands
will be able to mount content from these machine directories into containers.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to comment on PR #11454 instead, where this commit was taken from


#### **--now**

Start the virtual machine immediately after it has been initialized.
Expand Down
1 change: 1 addition & 0 deletions pkg/machine/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type InitOptions struct {
DiskSize uint64
IgnitionPath string
ImagePath string
Volumes []string
IsDefault bool
Memory uint64
Name string
Expand Down
9 changes: 9 additions & 0 deletions pkg/machine/qemu/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ type MachineVM struct {
CPUs uint64
// The command line representation of the qemu command
CmdLine []string
// Mounts is the list of remote filesystems to mount
Mounts []Mount
// IdentityPath is the fq path to the ssh priv key
IdentityPath string
// IgnitionFilePath is the fq path to the .ign file
Expand All @@ -27,6 +29,13 @@ type MachineVM struct {
RemoteUsername string
}

type Mount struct {
Type string
Tag string
Source string
Target string
}

type Monitor struct {
// Address portion of the qmp monitor (/tmp/tmp.sock)
Address string
Expand Down
48 changes: 47 additions & 1 deletion pkg/machine/qemu/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,21 @@ func (v *MachineVM) Init(opts machine.InitOptions) error {
// Add arch specific options including image location
v.CmdLine = append(v.CmdLine, v.addArchOptions()...)

mounts := []Mount{}
for i, volume := range opts.Volumes {
tag := fmt.Sprintf("vol%d", i)
paths := strings.SplitN(volume, ":", 2)
source := paths[0]
target := source
if len(paths) > 1 {
target = paths[1]
}
addVirtfsOptions := []string{"-virtfs", fmt.Sprintf("local,path=%s,mount_tag=%s,security_model=mapped-xattr", source, tag)}
v.CmdLine = append(v.CmdLine, addVirtfsOptions...)
mounts = append(mounts, Mount{Type: "9p", Tag: tag, Source: source, Target: target})
}
v.Mounts = mounts

// Add location of bootable image
v.CmdLine = append(v.CmdLine, "-drive", "if=virtio,file="+v.ImagePath)
// This kind of stinks but no other way around this r/n
Expand Down Expand Up @@ -325,7 +340,28 @@ func (v *MachineVM) Start(name string, _ machine.StartOptions) error {
return err
}
_, err = bufio.NewReader(conn).ReadString('\n')
return err
if err != nil {
return err
}

if len(v.Mounts) > 0 {
for !v.isRunning() || !v.isListening() {
time.Sleep(100 * time.Millisecond)
}
}
for _, mount := range v.Mounts {
fmt.Printf("Mounting volume... %s:%s\n", mount.Source, mount.Target)
// create mountpoint directory if it doesn't exist
err = v.SSH(name, machine.SSHOptions{Args: []string{"-q", "--", "sudo", "mkdir", "-p", mount.Target}})
if err != nil {
return err
}
err = v.SSH(name, machine.SSHOptions{Args: []string{"-q", "--", "sudo", "mount", "-t", mount.Type, "-o", "trans=virtio", mount.Tag, mount.Target, "-o", "version=9p2000.L,msize=131072"}})
if err != nil {
return err
}
}
return nil
}

// Stop uses the qmp monitor to call a system_powerdown
Expand Down Expand Up @@ -482,6 +518,16 @@ func (v *MachineVM) isRunning() bool {
return true
}

func (v *MachineVM) isListening() bool {
// Check if we can dial it
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", "localhost", v.Port), 10*time.Millisecond)
if err != nil {
return false
}
conn.Close()
return true
}

// SSH opens an interactive SSH session to the vm specified.
// Added ssh function to VM interface: pkg/machine/config/go : line 58
func (v *MachineVM) SSH(name string, opts machine.SSHOptions) error {
Expand Down