Skip to content

Commit

Permalink
New upstream version 0.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
obbardc committed Oct 21, 2022
1 parent 3448a68 commit a7949c5
Show file tree
Hide file tree
Showing 21 changed files with 858 additions and 302 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"
71 changes: 71 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name: Build and Test

on:
push:
branches-ignore:
- '*.tmp'
pull_request:

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v2
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3

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: [bullseye, bookworm]
backend: [qemu, uml]
include:
- os: arch
backend: qemu
name: Test ${{matrix.os}} with ${{matrix.backend}} backend
runs-on: ubuntu-latest
defaults:
run:
shell: bash
container:
image: ghcr.io/go-debos/test-containers/fakemachine-${{matrix.os}}:main
options: >-
--security-opt label=disable
--cap-add=SYS_PTRACE
--tmpfs /scratch:exec
env:
TMP: /scratch
steps:
- name: Checkout code
uses: actions/checkout@v3

- 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 the bors success status against
bors:
name: bors
if: success()
needs:
- golangci
- test
runs-on: ubuntu-latest
steps:
- name: Mark the job as a success
run: exit 0
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
linters:
enable:
- gofmt
- whitespace
30 changes: 0 additions & 30 deletions Jenkinsfile

This file was deleted.

23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# fakemchine - fake a machine

Creates a vm based on the currently running system.

## Synopsis

fakemachine [OPTIONS]

```
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
```
80 changes: 52 additions & 28 deletions backend.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,65 @@
// +build linux
// +build amd64
//go:build linux && amd64
// +build linux,amd64

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()
b, backendErr := newBackend(backendName, m)
if backendErr != nil {
err = fmt.Errorf("%v, %v", 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)
}

Expand All @@ -58,11 +82,11 @@ 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
Expand All @@ -76,7 +100,7 @@ type backend interface {
// 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 a7949c5

Please sign in to comment.