Skip to content

Repository files navigation

go-kargs: parse and manipulate kernel command line arguments

Latest release Go Reference Test Coverage OpenSSF Scorecard

Additional project checks

Quality

Lint Release Go Report Card

Security

CodeQL Vulnerability Check


Read, set, delete, then write back out kernel command line arguments.

package main

import (
	"fmt"

	kargs "github.com/synackd/go-kargs"
)

func main() {
	// Parse kernel command line arguments
	kargsIn := `nomodeset root=live:https://172.16.0.254/boot-images/compute/base/test console=tty0,115200 console=ttyS0,115200 printk.devkmsg=ratelimit printk.time=1`
	k := kargs.NewKargs([]byte(kargsIn))
	fmt.Println(k)

	// Get all values for an argument
	valConsole, isSetConsole := k.GetKarg("console")
	if isSetConsole {
		fmt.Printf("console: %v\n", valConsole)
	} else {
		fmt.Println("console not set")
	}

	// Works even with a single value
	valRoot, isSetRoot := k.GetKarg("root")
	if isSetRoot {
		fmt.Printf("root: %v\n", valRoot)
	} else {
		fmt.Println("root not set")
	}

	// Get all arguments for a module
	fmt.Println("args for printk " + k.FlagsForModule("printk"))

	// Override multiple args
	if err := k.SetKarg("console", "ttyS1,155200n8"); err != nil {
		fmt.Println("params with new console settings: " + k.String())
	}
}

Output:

nomodeset root=live:https://172.16.0.254/boot-images/compute/base/test console=tty0,115200 console=ttyS0,115200 printk.devkmsg=ratelimit printk.time=1
console: [tty0,115200 ttyS0,115200]
root: [live:https://172.16.0.254/boot-images/compute/base/test]
args for printk devkmsg=ratelimit time=1

Installation

go get github.com/synackd/go-kargs

Import as kargs "github.com/synackd/go-kargs"

Development

This library targets Go 1.17 and later. A Makefile provides the local checks that mirror the CI workflows (run make help for the full list):

make test        # run unit tests
make race        # run unit tests with the race detector
make coverage    # run tests and print a coverage summary
make vet         # run go vet
make lint        # run golangci-lint
make govulncheck # run govulncheck
make check       # run all of the above

Documentation

See https://pkg.go.dev/github.com/synackd/go-kargs

About

Read and write kernel command line arguments in Go

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages