Skip to content

Commit

Permalink
Merge #73
Browse files Browse the repository at this point in the history
73: Check mountpoints exist on the host early r=sjoerdsimons a=obbardc

If a mountpoint doesn't exist on the host, the backend
will fail to mount it and results in an ugly error
from the backend:

    $ fakemachine -v /null
    qemu-system-x86_64: -virtfs local,mount_tag=virtfs-5,path=/null,security_model=none: cannot initialize fsdev 'virtfs-5': failed to open '/null': No such file or directory
    fakemachine: error starting kvm backend: <nil>

Check if a mountpoint exists before attempting to start
the backend.

Signed-off-by: Christopher Obbard <[email protected]>

Co-authored-by: Christopher Obbard <[email protected]>
  • Loading branch information
bors[bot] and obbardc authored Feb 3, 2023
2 parents fd7e1f9 + 18886b2 commit b4b03b7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
22 changes: 21 additions & 1 deletion machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import (
"os/exec"
"path"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"text/template"

"github.com/go-debos/fakemachine/cpio"
writerhelper "github.com/go-debos/fakemachine/cpio"
)

func mergedUsrSystem() bool {
Expand Down Expand Up @@ -578,6 +579,25 @@ func (m *Machine) startup(command string, extracontent [][2]string) (int, error)

os.Setenv("PATH", os.Getenv("PATH")+":/sbin:/usr/sbin")

/* Sanity check mountpoints */
for _, v := range m.mounts {
/* Check the directory exists on the host */
stat, err := os.Stat(v.hostDirectory)
if err != nil || !stat.IsDir() {
return -1, fmt.Errorf("Couldn't mount %s inside machine: expected a directory", v.hostDirectory)
}

/* Check for whitespace in the machine directory */
if regexp.MustCompile(`\s`).MatchString(v.machineDirectory) {
return -1, fmt.Errorf("Couldn't mount %s inside machine: machine directory (%s) contains whitespace", v.hostDirectory, v.machineDirectory)
}

/* Check for whitespace in the label */
if regexp.MustCompile(`\s`).MatchString(v.label) {
return -1, fmt.Errorf("Couldn't mount %s inside machine: label (%s) contains whitespace", v.hostDirectory, v.label)
}
}

tmpdir, err := ioutil.TempDir("", "fakemachine-")
if err != nil {
return -1, err
Expand Down
32 changes: 32 additions & 0 deletions machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,35 @@ func TestImageLabel(t *testing.T) {
t.Fatalf("Test for images in the machine failed failed with %d", exitcode)
}
}

func TestVolumes(t *testing.T) {
t.Parallel()
if InMachine() {
t.Log("Running in the machine")
return
}

/* Try to mount a non-existent file into the machine */
m := CreateMachine(t)
m.AddVolume("random_directory_never_exists")

exitcode, err := m.RunInMachineWithArgs([]string{"-test.run TestVolumes"})
require.Equal(t, exitcode, -1)
require.Error(t, err)

/* Try to mount a device file into the machine */
m = CreateMachine(t)
m.AddVolume("/dev/zero")

exitcode, err = m.RunInMachineWithArgs([]string{"-test.run TestVolumes"})
require.Equal(t, exitcode, -1)
require.Error(t, err)

/* Try to mount a volume with whitespace into the machine */
m = CreateMachine(t)
m.AddVolumeAt("/dev", "/dev ices")

exitcode, err = m.RunInMachineWithArgs([]string{"-test.run TestVolumes"})
require.Equal(t, exitcode, -1)
require.Error(t, err)
}

0 comments on commit b4b03b7

Please sign in to comment.