Skip to content

Commit

Permalink
New upstream version 0.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
obbardc committed Feb 8, 2023
1 parent 0f0ff8b commit 127c9f2
Show file tree
Hide file tree
Showing 13 changed files with 246 additions and 33 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ on:
push:
branches-ignore:
- '*.tmp'
# Build at 04:00am every Monday
schedule:
- cron: "0 4 * * 1"
pull_request:
workflow_dispatch:

jobs:
golangci:
Expand All @@ -16,6 +20,23 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v3

man-page:
name: Check if man page has been regenerated
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name:
run: |
sudo apt-get update
sudo apt-get install -y pandoc
# Don't check the diff of the final manpage, instead check the
# intermediate markdownfile instead as it is a lot less likely to
# drastically change with different versions of pandoc etc.
cd doc/man/ && ./create_manpage.sh
git checkout *.1
git diff --exit-code
test:
strategy:
fail-fast: false
Expand Down Expand Up @@ -64,6 +85,7 @@ jobs:
if: success()
needs:
- golangci
- man-page
- test
runs-on: ubuntu-latest
steps:
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
fakemachine
/fakemachine
.*swp
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# fakemchine - fake a machine
# fakemachine - fake a machine

Creates a vm based on the currently running system.
Creates a virtual machine based on the currently running system.

## Synopsis

fakemachine [OPTIONS]

```
fakemachine [options] <command to run inside machine>
fakemachine [--help]
```

Application Options:
```
-b, --backend=[auto|kvm|uml|qemu] Virtualisation backend to use (default: auto)
-v, --volume= volume to mount
-i, --image= image to add
Expand Down
6 changes: 6 additions & 0 deletions backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ func newBackend(name string, m *Machine) (backend, error) {
if name == "auto" {
for _, backend := range backends {
backendName := backend.Name()

/* The qemu backend is slow, don't allow users to auto-select it */
if backendName == "qemu" {
continue
}

b, backendErr := newBackend(backendName, m)
if backendErr != nil {
err = fmt.Errorf("%v, %v", err, backendErr)
Expand Down
2 changes: 1 addition & 1 deletion backend_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func (b qemuBackend) StartQemu(kvm bool) (bool, error) {

for _, point := range m.mounts {
qemuargs = append(qemuargs, "-virtfs",
fmt.Sprintf("local,mount_tag=%s,path=%s,security_model=none",
fmt.Sprintf("local,mount_tag=%s,path=%s,security_model=none,multidevs=remap",
point.label, point.hostDirectory))
}

Expand Down
6 changes: 5 additions & 1 deletion cmd/fakemachine/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Options struct {
CPUs int `short:"c" long:"cpus" description:"Number of CPUs for the fakemachine"`
ScratchSize string `short:"s" long:"scratchsize" description:"On-disk scratch space size (with a unit suffix, e.g. 4G); if unset, memory backed scratch space is used"`
ShowBoot bool `long:"show-boot" description:"Show boot/console messages from the fakemachine"`
Quiet bool `short:"q" long:"quiet" description:"Don't show logs from fakemachine or the backend; only print the command's stdout/stderr"`
}

var options Options
Expand Down Expand Up @@ -77,7 +78,9 @@ func SetupImages(m *fakemachine.Machine, options Options) {
os.Exit(1)
}

fmt.Printf("Exposing %s as %s\n", parts[0], l)
if !options.Quiet {
fmt.Printf("Exposing %s as %s\n", parts[0], l)
}
}
}

Expand Down Expand Up @@ -153,6 +156,7 @@ func main() {
}

m.SetShowBoot(options.ShowBoot)
m.SetQuiet(options.Quiet)
SetupVolumes(m, options)
SetupImages(m, options)
SetupEnviron(m, options)
Expand Down
26 changes: 26 additions & 0 deletions doc/man/create_manpage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash
# Create a manpage from the README.md

# Add header
echo '''% fakemachine(1)
# NAME
fakemachine - fake a machine
''' > fakemachine.md

# Add README.md
tail -n +2 ../../README.md >> fakemachine.md

# Some tweaks to the markdown
# Uppercase titles
sed -i 's/^\(##.*\)$/\U\1/' fakemachine.md

# Remove double #
sed -i 's/^\##/#/' fakemachine.md

# Create the manpage
pandoc -s -t man fakemachine.md -o fakemachine.1

# Resulting manpage can be browsed with groff:
#groff -man -Tascii fakemachine.1
50 changes: 50 additions & 0 deletions doc/man/fakemachine.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
.\" Automatically generated by Pandoc 2.17.1.1
.\"
.\" Define V font for inline verbatim, using C font in formats
.\" that render this, and otherwise B font.
.ie "\f[CB]x\f[]"x" \{\
. ftr V B
. ftr VI BI
. ftr VB B
. ftr VBI BI
.\}
.el \{\
. ftr V CR
. ftr VI CI
. ftr VB CB
. ftr VBI CBI
.\}
.TH "fakemachine" "1" "" "" ""
.hy
.SH NAME
.PP
fakemachine - fake a machine
.PP
Creates a virtual machine based on the currently running system.
.SH SYNOPSIS
.IP
.nf
\f[C]
fakemachine [options] <command to run inside machine>
fakemachine [--help]
\f[R]
.fi
.PP
Application Options:
.IP
.nf
\f[C]
-b, --backend=[auto|kvm|uml|qemu] Virtualisation backend to use (default: auto)
-v, --volume= volume to mount
-i, --image= image to add
-e, --environ-var= Environment variables (use -e VARIABLE:VALUE syntax)
-m, --memory= Amount of memory for the fakemachine in megabytes
-c, --cpus= Number of CPUs for the fakemachine
-s, --scratchsize= On-disk scratch space size (with a unit suffix, e.g. 4G); if unset,
memory backed scratch space is used
--show-boot Show boot/console messages from the fakemachine

Help Options:
-h, --help Show this help message
\f[R]
.fi
31 changes: 31 additions & 0 deletions doc/man/fakemachine.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
% fakemachine(1)

# NAME

fakemachine - fake a machine


Creates a virtual machine based on the currently running system.

# SYNOPSIS

```
fakemachine [options] <command to run inside machine>
fakemachine [--help]
```

Application Options:
```
-b, --backend=[auto|kvm|uml|qemu] Virtualisation backend to use (default: auto)
-v, --volume= volume to mount
-i, --image= image to add
-e, --environ-var= Environment variables (use -e VARIABLE:VALUE syntax)
-m, --memory= Amount of memory for the fakemachine in megabytes
-c, --cpus= Number of CPUs for the fakemachine
-s, --scratchsize= On-disk scratch space size (with a unit suffix, e.g. 4G); if unset,
memory backed scratch space is used
--show-boot Show boot/console messages from the fakemachine
Help Options:
-h, --help Show this help message
```
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ require (
github.com/docker/go-units v0.5.0
github.com/jessevdk/go-flags v1.5.0
github.com/klauspost/compress v1.15.3
github.com/stretchr/testify v1.8.0
github.com/stretchr/testify v1.8.1
github.com/surma/gocpio v1.1.0
github.com/ulikunitz/xz v0.5.10
github.com/ulikunitz/xz v0.5.11
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad
)
8 changes: 5 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/surma/gocpio v1.1.0 h1:RUWT+VqJ8GSodSv7Oh5xjIxy7r24CV1YvothHFfPxcQ=
github.com/surma/gocpio v1.1.0/go.mod h1:zaLNaN+EDnfSnNdWPJJf9OZxWF817w5dt8JNzF9LCVI=
github.com/ulikunitz/xz v0.5.10 h1:t92gobL9l3HE202wg3rlk19F6X+JOxl9BBrCCMYEYd8=
github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/ulikunitz/xz v0.5.11 h1:kpFauv27b6ynzBNT/Xy+1k+fK4WswhN/6PN5WhFAGw8=
github.com/ulikunitz/xz v0.5.11/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
46 changes: 41 additions & 5 deletions 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 @@ -162,6 +163,7 @@ type Machine struct {
memory int
numcpus int
showBoot bool
quiet bool
Environ []string

scratchsize int64
Expand Down Expand Up @@ -272,8 +274,7 @@ if [ $? != 0 ]; then
exit
fi
echo Running '%[2]s' using '%[1]s' backend
%[2]s
%[1]s
echo $? > /run/fakemachine/result
`

Expand All @@ -294,7 +295,7 @@ Environment=HOME=/root IN_FAKE_MACHINE=yes %[2]s
WorkingDirectory=-/scratch
ExecStart=/wrapper
ExecStopPost=/bin/sync
ExecStopPost=/bin/systemctl poweroff -ff
ExecStopPost=/bin/systemctl poweroff -q -ff
Type=idle
TTYPath=%[1]s
StandardInput=tty-force
Expand Down Expand Up @@ -446,6 +447,13 @@ func (m *Machine) SetShowBoot(showBoot bool) {
m.showBoot = showBoot
}

// SetQuiet sets whether fakemachine should print additional information (e.g.
// the command to be ran) or just print the stdout/stderr of the command to be
// ran.
func (m *Machine) SetQuiet(quiet bool) {
m.quiet = quiet
}

// SetScratch sets the size and location of on-disk scratch space to allocate
// (sparsely) for /scratch. If not set /scratch will be backed by memory. If
// Path is "" then the working directory is used as a default storage location
Expand Down Expand Up @@ -578,6 +586,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 Expand Up @@ -662,6 +689,11 @@ func (m *Machine) startup(command string, extracontent [][2]string) (int, error)
return -1, err
}

/* Ensure systemd-resolved is available */
if _, err := os.Stat("/lib/systemd/systemd-resolved"); err != nil {
return -1, err
}

/* Amd64 dynamic linker */
err = w.CopyFile("/lib64/ld-linux-x86-64.so.2")
if err != nil {
Expand Down Expand Up @@ -765,7 +797,7 @@ func (m *Machine) startup(command string, extracontent [][2]string) (int, error)
}

err = w.WriteFile("/wrapper",
fmt.Sprintf(commandWrapper, backend.Name(), command), 0755)
fmt.Sprintf(commandWrapper, command), 0755)
if err != nil {
return -1, err
}
Expand Down Expand Up @@ -795,6 +827,10 @@ func (m *Machine) startup(command string, extracontent [][2]string) (int, error)
w.Close()
f.Close()

if !m.quiet {
fmt.Printf("Running %s using %s backend\n", command, backend.Name())
}

success, err := backend.Start()
if !success || err != nil {
return -1, fmt.Errorf("error starting %s backend: %v", backend.Name(), err)
Expand Down
Loading

0 comments on commit 127c9f2

Please sign in to comment.