Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a command to list kernel tags and pull kernels built by little-vm-helper-images #184

Merged
merged 6 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ For an example script, see [scripts/example.sh](scripts/example.sh).
LVH can be used to:
* build root images for VMs
* build kernels
* download kernels
* boot VMs using above

### Root images
### Build root images

Build example images:
```bash
Expand Down Expand Up @@ -79,7 +80,7 @@ $ ls -sh1 _data/images/*.img
1.7G _data/images/k8s.img
```

### Kernels
### Build kernels

```bash
$ mkdir -p _data/kernels
Expand Down Expand Up @@ -129,6 +130,53 @@ git/
Currently, kernels are built using the `bzImage` for x86\_64 or `Image.gz` for
arm64, and `tar-pkg` targets (see [pkg/kernels/conf.go](pkg/kernels/conf.go)).

### Download kernels

List the available versions

```console
$ lvh kernels catalog
bpf-next
rhel8
4.9
4.19
[...]
6.3
6.6
```

Retrieve the tags for a given version:
```console
$ lvh kernels catalog 6.6
6.6-20240123.120815
6.6-20240123.175813
[...]
6.6-20240404.144247
6.6-20240408.100959
6.6-main
```

See `lvh kernels catalog --help` for more details.

Download the kernel and related artifacts (BTF, modules, etc.)

```console
$ lvh kernels pull 6.6-main
joestringer marked this conversation as resolved.
Show resolved Hide resolved
$ find 6.6-main/ -maxdepth 3
6.6-main/
6.6-main/boot
6.6-main/boot/vmlinuz-6.6.25
6.6-main/boot/btf-6.6.25
6.6-main/boot/System.map-6.6.25
6.6-main/boot/vmlinux-6.6.25
6.6-main/boot/config-6.6.25
6.6-main/lib
6.6-main/lib/modules
6.6-main/lib/modules/6.6.25
```

See `lvh kernels pull --help` for more details.

### Booting images

You can use the `run` subcommand to start images.
Expand Down
2 changes: 2 additions & 0 deletions cmd/lvh/kernels/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,7 @@ func addCommand() *cobra.Command {
addCmd.Flags().BoolVar(&addPrintConfig, "just-print-config", false, "do not actually add the kernel. Just print its config.")
addCmd.Flags().BoolVar(&addFetch, "fetch", false, "fetch URL")
addCmd.Flags().BoolVar(&backupConf, "backup-conf", false, "backup configuration")
addCmd.Flags().StringVar(&dirName, dirNameCommand, "", dirNameHelp)
addCmd.MarkFlagRequired(dirNameCommand)
return addCmd
}
2 changes: 2 additions & 0 deletions cmd/lvh/kernels/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func buildCommand() *cobra.Command {
}

cmd.Flags().StringVar(&arch, "arch", "", "target architecture to build the kernel, e.g. 'amd64' or 'arm64' (default to native architecture)")
cmd.Flags().StringVar(&dirName, dirNameCommand, "", dirNameHelp)
cmd.MarkFlagRequired(dirNameCommand)
joestringer marked this conversation as resolved.
Show resolved Hide resolved

return cmd
}
103 changes: 103 additions & 0 deletions cmd/lvh/kernels/catalog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package kernels

import (
"fmt"
"regexp"
"slices"
"strings"

"github.com/google/go-containerregistry/pkg/crane"
"github.com/spf13/cobra"
"golang.org/x/mod/semver"
)

const (
kernelImageRepository = "quay.io/lvh-images/kernel-images"

kernelTagRegex = `^(.+)-([0-9]+\.[0-9]+|main)$`

repoCommand = "repo"
repoHelp = "specify the OCI repository to pull the image from"
)

func catalogCommand() *cobra.Command {
var (
repo string
)

cmd := &cobra.Command{
Use: "catalog [version]",
Short: "list available tags for kernel images from cilium/little-vm-helper-images",
Long: `List the available tags for kernel images built from cilium/little-vm-helper-images

Examples:
# List all available versions
lvh kernels catalog

# List the tags available for version 6.6
lvh kernels catalog 6.6

# Retrieve the latest tags available for version bpf-next
lvh kernels catalog bpf-next | tail -n 2

# Retrieve the latest CI-generated images for version bpf-next
lvh kernels catalog bpf-next --repo quay.io/lvh-images/kernel-images-ci`,
RunE: func(cmd *cobra.Command, args []string) error {
re := regexp.MustCompile(kernelTagRegex)

rawTagList, err := crane.ListTags(repo)
if err != nil {
return err
}

tags := map[string][]string{}
for _, tag := range rawTagList {
match := re.FindStringSubmatch(tag)
if len(match) < 3 {
// discard most of the tags that don't match the regex
continue
}

if strings.Contains(match[1], "-latest") {
// remove some tags with "-latest" that are obsolete
continue
}

tags[match[1]] = append(tags[match[1]], match[0])
}

if len(args) == 0 {
versions := []string{}
for v := range tags {
// semver package needs the v Prefix
versions = append(versions, "v"+v)
}
semver.Sort(versions)
for _, v := range versions {
cmd.Println(strings.TrimPrefix(v, "v"))
}
return nil
}

if _, found := tags[args[0]]; !found {
keys := []string{}
for key := range tags {
keys = append(keys, key)
}
return fmt.Errorf("kernel version not found, try: %s", keys)
}

for key := range tags {
slices.Sort(tags[key])
}
for _, tag := range tags[args[0]] {
cmd.Println(tag)
}
return nil
},
}

cmd.Flags().StringVar(&repo, repoCommand, kernelImageRepository, repoHelp)

return cmd
}
4 changes: 4 additions & 0 deletions cmd/lvh/kernels/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func configureCommand() *cobra.Command {
}

cmd.Flags().String(archFlag, "", archHelp)
cmd.Flags().StringVar(&dirName, dirNameCommand, "", dirNameHelp)
cmd.MarkFlagRequired("dir")

return cmd
}
Expand Down Expand Up @@ -66,6 +68,8 @@ func rawConfigureCommand() *cobra.Command {
}

cmd.Flags().String(archFlag, "", archHelp)
cmd.Flags().StringVar(&dirName, dirNameCommand, "", dirNameHelp)
cmd.MarkFlagRequired(dirNameCommand)

return cmd
}
7 changes: 6 additions & 1 deletion cmd/lvh/kernels/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func fetchCommand() *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "fetch <kernel>",
Short: "fetch kernel",
Args: cobra.ExactArgs(1),
Expand All @@ -22,4 +22,9 @@ func fetchCommand() *cobra.Command {
return kernels.FetchKernel(context.Background(), log, dirName, kname)
},
}

cmd.Flags().StringVar(&dirName, dirNameCommand, "", dirNameHelp)
cmd.MarkFlagRequired(dirNameCommand)

return cmd
}
2 changes: 2 additions & 0 deletions cmd/lvh/kernels/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,7 @@ func initCommand() *cobra.Command {
strings.Join(kernels.GetConfigGroupNames(), ","),
),
)
cmd.Flags().StringVar(&dirName, dirNameCommand, "", dirNameHelp)
cmd.MarkFlagRequired(dirNameCommand)
return cmd
}
15 changes: 10 additions & 5 deletions cmd/lvh/kernels/kernels.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ import (

var dirName string

const (
dirNameHelp = "directory to place kernels"
dirNameCommand = "dir"
)

func KernelsCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "kernels",
Short: "build kernels",
Use: "kernels",
Aliases: []string{"kernel", "k"},
Short: "build and pull kernels",
}

cmd.PersistentFlags().StringVar(&dirName, "dir", "", "directory to to place kernels")
cmd.MarkPersistentFlagRequired("dir")

cmd.AddCommand(
initCommand(),
listCommand(),
Expand All @@ -27,6 +30,8 @@ func KernelsCommand() *cobra.Command {
rawConfigureCommand(),
buildCommand(),
fetchCommand(),
catalogCommand(),
pullCommand(),
)

return cmd
Expand Down
7 changes: 6 additions & 1 deletion cmd/lvh/kernels/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func listCommand() *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "list",
Short: "list available kernels (by reading config file in directory)",
Run: func(cmd *cobra.Command, _ []string) {
Expand All @@ -27,4 +27,9 @@ func listCommand() *cobra.Command {
}
},
}

cmd.Flags().StringVar(&dirName, dirNameCommand, "", dirNameHelp)
cmd.MarkFlagRequired(dirNameCommand)

return cmd
}
Loading
Loading