Skip to content

Commit

Permalink
Support Squashfs Layer (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
hardikdr authored Aug 21, 2024
1 parent c3353a6 commit 6ae9430
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*.dylib
bin
testbin/*
dev

# Test binary, build with `go test -c`
*.test
Expand Down
7 changes: 5 additions & 2 deletions cmd/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func Command(storeFactory common.StoreFactory) *cobra.Command {
var (
tagName string
rootFSPath string
squashFSPath string
initRAMFSPath string
kernelPath string
commandLine string
Expand All @@ -27,12 +28,13 @@ func Command(storeFactory common.StoreFactory) *cobra.Command {
Short: "Build an image and store it to the local store with an optional tag.",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
return Run(ctx, storeFactory, tagName, rootFSPath, initRAMFSPath, kernelPath, commandLine)
return Run(ctx, storeFactory, tagName, rootFSPath, squashFSPath, initRAMFSPath, kernelPath, commandLine)
},
}

cmd.Flags().StringVar(&tagName, "tag", "", "Optional tag of image.")
cmd.Flags().StringVar(&rootFSPath, "rootfs-file", "", "Path pointing to a root fs file.")
cmd.Flags().StringVar(&squashFSPath, "squashfs-file", "", "Path pointing to a squash fs file.")
cmd.Flags().StringVar(&initRAMFSPath, "initramfs-file", "", "Path pointing to an initram fs file.")
cmd.Flags().StringVar(&kernelPath, "kernel-file", "", "Path pointing to a kernel file (usually ending with 'vmlinuz').")
cmd.Flags().StringVar(&commandLine, "command-line", "", "Command line arguments to supply to the kernel.")
Expand All @@ -43,7 +45,7 @@ func Command(storeFactory common.StoreFactory) *cobra.Command {
func Run(
ctx context.Context,
storeFactory common.StoreFactory,
ref, rootFSPath, initRAMFSPath, kernelPath, commandLine string,
ref, rootFSPath, squashFSPath, initRAMFSPath, kernelPath, commandLine string,
) error {
s, err := storeFactory()
if err != nil {
Expand All @@ -58,6 +60,7 @@ func Run(
FileLayer(rootFSPath, imageutil.WithMediaType(ironcoreimage.RootFSLayerMediaType)).
FileLayer(initRAMFSPath, imageutil.WithMediaType(ironcoreimage.InitRAMFSLayerMediaType)).
FileLayer(kernelPath, imageutil.WithMediaType(ironcoreimage.KernelLayerMediaType)).
FileLayer(squashFSPath, imageutil.WithMediaType(ironcoreimage.SquashFSLayerMediaType)).
Complete()
if err != nil {
return fmt.Errorf("error building image: %w", err)
Expand Down
6 changes: 6 additions & 0 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
RootFSLayerMediaType = "application/vnd.ironcore.image.rootfs.v1alpha1.rootfs"
InitRAMFSLayerMediaType = "application/vnd.ironcore.image.initramfs.v1alpha1.initramfs"
KernelLayerMediaType = "application/vnd.ironcore.image.vmlinuz.v1alpha1.vmlinuz"
SquashFSLayerMediaType = "application/vnd.ironcore.image.squashfs.v1alpha1.squashfs"
)

type Config struct {
Expand Down Expand Up @@ -49,6 +50,7 @@ func SetupContext(ctx context.Context) context.Context {
ctx = remotes.WithMediaTypeKeyPrefix(ctx, RootFSLayerMediaType, "layer-")
ctx = remotes.WithMediaTypeKeyPrefix(ctx, InitRAMFSLayerMediaType, "layer-")
ctx = remotes.WithMediaTypeKeyPrefix(ctx, KernelLayerMediaType, "layer-")
ctx = remotes.WithMediaTypeKeyPrefix(ctx, SquashFSLayerMediaType, "layer-")
return ctx
}

Expand All @@ -75,6 +77,8 @@ func ResolveImage(ctx context.Context, ociImg image.Image) (*Image, error) {
img.Kernel = layer
case RootFSLayerMediaType:
img.RootFS = layer
case SquashFSLayerMediaType:
img.SquashFS = layer
default:
return nil, fmt.Errorf("unknown layer type %q", layer.Descriptor().MediaType)
}
Expand Down Expand Up @@ -102,6 +106,8 @@ type Image struct {
Config Config
// RootFS is the layer containing the root file system.
RootFS image.Layer
// SquashFS is the layer containing the root file system.
SquashFS image.Layer
// InitRAMFs is the layer containing the initramfs / initrd.
InitRAMFs image.Layer
// Kernel is the layer containing the kernel.
Expand Down
13 changes: 8 additions & 5 deletions image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ var _ = Describe("Image", func() {
var (
ctx context.Context

config Config
kernelData, initramfsData, rootfsData []byte
config Config
kernelData, initramfsData, rootfsData, squashfsData []byte

configLayer, kernelLayer, initramfsLayer, rootfsLayer image.Layer
img image.Image
configLayer, kernelLayer, initramfsLayer, rootfsLayer, squashfsLayer image.Layer
img image.Image
)

BeforeEach(func() {
Expand All @@ -33,16 +33,18 @@ var _ = Describe("Image", func() {
kernelData = []byte("kernel")
initramfsData = []byte("initramfs")
rootfsData = []byte("rootfs")
squashfsData = []byte("squashfs")

c, err := imageutil.JSONValueLayer(config, imageutil.WithMediaType(ConfigMediaType))
Expect(err).NotTo(HaveOccurred())
configLayer = c
kernelLayer = imageutil.BytesLayer(kernelData, imageutil.WithMediaType(KernelLayerMediaType))
initramfsLayer = imageutil.BytesLayer(initramfsData, imageutil.WithMediaType(InitRAMFSLayerMediaType))
rootfsLayer = imageutil.BytesLayer(rootfsData, imageutil.WithMediaType(RootFSLayerMediaType))
squashfsLayer = imageutil.BytesLayer(squashfsData, imageutil.WithMediaType(SquashFSLayerMediaType))

i, err := imageutil.NewBuilder(configLayer).
Layers(kernelLayer, initramfsLayer, rootfsLayer).
Layers(kernelLayer, initramfsLayer, rootfsLayer, squashfsLayer).
Complete()
Expect(err).NotTo(HaveOccurred())
img = i
Expand All @@ -61,6 +63,7 @@ var _ = Describe("Image", func() {
Expect(imageutil.ReadLayerContent(ctx, res.Kernel)).To(Equal(kernelData))
Expect(imageutil.ReadLayerContent(ctx, res.RootFS)).To(Equal(rootfsData))
Expect(imageutil.ReadLayerContent(ctx, res.InitRAMFs)).To(Equal(initramfsData))
Expect(imageutil.ReadLayerContent(ctx, res.SquashFS)).To(Equal(squashfsData))
})

It("should error if the image contains invalid layers", func() {
Expand Down

0 comments on commit 6ae9430

Please sign in to comment.