Skip to content

Commit

Permalink
Merge tag 'debian/0.0.9-1' into droidian
Browse files Browse the repository at this point in the history
golang-github-go-debos-fakemachine Debian release 0.0.9-1
  • Loading branch information
g7 committed May 4, 2024
2 parents 05f83ee + c0a2d16 commit 25ddd51
Show file tree
Hide file tree
Showing 32 changed files with 1,431 additions and 426 deletions.
10 changes: 10 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
- package-ecosystem: "gomod"
directory: "/"
schedule:
interval: "daily"
98 changes: 98 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
name: Build and Test

on:
push:
branches-ignore:
- '*.tmp'
# Build at 04:00am every Monday
schedule:
- cron: "0 4 * * 1"
pull_request:
workflow_dispatch:

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v5
- uses: actions/checkout@v4
- 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@v4
- 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
matrix:
# Currently nested virtualisation (hence kvm) is not supported on GitHub
# actions; but the qemu backend is enough to test Fakemachine
# functionality without hardware acceleration since the majority of code
# is shared between the qemu and kvm backends.
# See https://github.com/actions/runner-images/issues/183
#
# For Arch Linux uml is not yet supported, so only test under qemu there.
os: [bookworm, trixie]
backend: [qemu, uml, kvm]
include:
- os: arch
backend: "qemu"
- os: arch
backend: "kvm"
name: Test ${{matrix.os}} with ${{matrix.backend}} backend
runs-on: ${{ matrix.backend == 'kvm' && 'kvm' || 'ubuntu-latest' }}
defaults:
run:
shell: bash
container:
image: ghcr.io/go-debos/test-containers/${{matrix.os}}:main
options: >-
--security-opt label=disable
--cap-add=SYS_PTRACE
--tmpfs /scratch:exec
${{ matrix.backend == 'kvm' && '--device /dev/kvm' || '' }}
env:
TMP: /scratch
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Test build
run: go build -o fakemachine cmd/fakemachine/main.go

- name: Run unit tests (${{matrix.backend}} backend)
run: go test -v ./... --backend=${{matrix.backend}} | tee test.out

- name: Ensure no tests were skipped
run: "! grep -q SKIP test.out"

# Job to key success status against
allgreen:
name: allgreen
if: always()
needs:
- golangci
- man-page
- test
runs-on: ubuntu-latest
steps:
- name: Decide whether the needed jobs succeeded or failed
uses: re-actors/alls-green@release/v1
with:
jobs: ${{ toJSON(needs) }}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
fakemachine
/fakemachine
.*swp
6 changes: 6 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
linters:
enable:
- errorlint
- gofmt
- stylecheck
- whitespace
30 changes: 0 additions & 30 deletions Jenkinsfile

This file was deleted.

26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 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
```
97 changes: 66 additions & 31 deletions backend.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,85 @@
//go:build linux
// +build linux
// +build amd64 and arm64 and arm

package fakemachine

import(
import (
"fmt"
)

// A list of backends which are implemented
// List of backends in order of their priority in the "auto" algorithm
func implementedBackends(m *Machine) []backend {
return []backend{
newKvmBackend(m),
newUmlBackend(m),
newQemuBackend(m),
}
}

/* A list of backends which are implemented - sorted in order in which the
* "auto" backend chooses them.
*/
func BackendNames() []string {
return []string{"auto", "kvm", "uml"}
names := []string{"auto"}

for _, backend := range implementedBackends(nil) {
names = append(names, backend.Name())
}

return names
}

/* The "auto" backend loops through each backend, starting with the lowest order.
* The backend is created and checked if the creation was successful (i.e. it is
* supported on this machine). If so, that backend is used for the fakemachine. If
* unsuccessful, the next backend is created until no more backends remain then
* an error is thrown explaining why each backend was unsuccessful.
*/
func newBackend(name string, m *Machine) (backend, error) {
backends := implementedBackends(m)
var b backend

switch name {
case "auto":
// select kvm first
b, kvm_err := newBackend("kvm", m)
if kvm_err == nil {
var err 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 {
/* Append the error to any existing backend creation error(s).
* Since we cannot join errors together in golang <1.20, instead
* join the error messages strings and return that as a new error.
*/
if err != nil {
err = fmt.Errorf("%v, %v", err.Error(), backendErr.Error())
} else {
err = backendErr
}
continue
}
return b, nil
}
return nil, err
}

// falling back to uml
b, uml_err := newBackend("uml", m)
if uml_err == nil {
return b, nil
// find backend by name
for _, backend := range backends {
if backend.Name() == name {
b = backend
}

// no backend supported
return nil, fmt.Errorf("%v, %v", kvm_err, uml_err)
case "kvm":
b = newKvmBackend(m)
case "uml":
b = newUmlBackend(m)
default:
}
if b == nil {
return nil, fmt.Errorf("%s backend does not exist", name)
}

// check backend is supported
if supported, err := b.Supported(); !supported {
return nil, fmt.Errorf("%s backend not supported: %v", name, err)
return nil, fmt.Errorf("%s backend not supported: %w", name, err)
}

return b, nil
Expand All @@ -58,25 +96,22 @@ type backend interface {
// Get kernel release version
KernelRelease() (string, error)

// The path to the kernel and modules
KernelPath() (kernelPath string, moddir string, err error)
// The path to the kernel
KernelPath() (kernelPath string, err error)

// A list of modules to include in the initrd
InitrdModules() []string
// The path to the modules
ModulePath() (moddir string, err error)

// A list of udev rules
UdevRules() []string

// The match expression used for the networkd configuration
NetworkdMatch() string

// The tty used for the job output
JobOutputTTY() string

// The parameters used to mount a specific volume into the machine
MountParameters(mount mountPoint) (fstype string, options []string)

// A list of modules which should be probed in the initscript
// A list of modules to be added to initrd and probed in the initscript
InitModules() []string

// A list of additional volumes which should mounted in the initscript
Expand Down
Loading

0 comments on commit 25ddd51

Please sign in to comment.