Skip to content

Commit

Permalink
Add output to GCE/Azure (#146)
Browse files Browse the repository at this point in the history
* Add output to GCE/Azure

Signed-off-by: Itxaka <[email protected]>
  • Loading branch information
Itxaka authored Jan 2, 2025
1 parent 062dedf commit 95281d6
Show file tree
Hide file tree
Showing 10 changed files with 415 additions and 67 deletions.
2 changes: 0 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ COPY ./image-assets/arm /arm
COPY ./image-assets/prepare_arm_images.sh /prepare_arm_images.sh

# RAW images helpers
COPY ./image-assets/gce.sh /gce.sh
COPY ./image-assets/azure.sh /azure.sh
COPY ./image-assets/netboot.sh /netboot.sh

COPY ./image-assets/defaults.yaml /defaults.yaml
Expand Down
5 changes: 3 additions & 2 deletions e2e/bootable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"path/filepath"
"strconv"

"github.com/google/uuid"
"github.com/gofrs/uuid"
process "github.com/mudler/go-processmanager"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -116,7 +116,8 @@ func defaultVMOptsNoDrives(stateDir string) []types.MachineOption {

var sshPort, spicePort int

vmName := uuid.New().String()
uid, _ := uuid.NewV4()
vmName := uid.String()

emulateTPM(stateDir)

Expand Down
75 changes: 75 additions & 0 deletions e2e/disks_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package e2e_test

import (
"archive/tar"
"bytes"
"compress/gzip"
"encoding/binary"
"encoding/hex"
"fmt"
"github.com/kairos-io/AuroraBoot/pkg/constants"
"github.com/kairos-io/AuroraBoot/pkg/ops"
"os"
"path/filepath"

Expand Down Expand Up @@ -83,7 +90,29 @@ var _ = Describe("Disk image generation", Label("raw-disks"), Serial, func() {
Expect(out).ToNot(ContainSubstring("dump-source"), out)
Expect(err).ToNot(HaveOccurred(), out)
_, err = os.Stat(filepath.Join(tempDir, "kairos-rockylinux-9-core-amd64-generic-v3.2.1.raw.gce.tar.gz"))
Expect(err).ToNot(HaveOccurred(), out)
// Open the file and check that there is a disk.raw file inside and check that its rounded to a GB
file, err := os.Open(filepath.Join(tempDir, "kairos-rockylinux-9-core-amd64-generic-v3.2.1.raw.gce.tar.gz"))
Expect(err).ToNot(HaveOccurred(), out)
defer file.Close()
// Create a gzip reader
gzr, err := gzip.NewReader(file)
Expect(err).ToNot(HaveOccurred(), out)
defer gzr.Close()

tr := tar.NewReader(gzr)
found := false
for {
hdr, err := tr.Next()
if err != nil {
break
}
if hdr.Name == "disk.raw" {
found = true
Expect(hdr.Size).To(BeNumerically(">", 1<<30), out)
}
}
Expect(found).To(BeTrue(), out)
Expect(err).ToNot(HaveOccurred(), out)
})
It("generates a vhd image", func() {
Expand All @@ -108,6 +137,28 @@ var _ = Describe("Disk image generation", Label("raw-disks"), Serial, func() {
Expect(err).ToNot(HaveOccurred(), out)
_, err = os.Stat(filepath.Join(tempDir, "kairos-rockylinux-9-core-amd64-generic-v3.2.1.raw.vhd"))
Expect(err).ToNot(HaveOccurred(), out)
// Check a few fields in the header to confirm its a VHD
f, _ := os.Open(filepath.Join(tempDir, "kairos-rockylinux-9-core-amd64-generic-v3.2.1.raw.vhd"))
defer f.Close()
info, _ := f.Stat()
// Should be divisible by 1024*1024
Expect(info.Size() % constants.MB).To(BeNumerically("==", 0))
// Dump the header from the file into our VHDHeader
buff := make([]byte, 512)
_, _ = f.ReadAt(buff, info.Size()-512)
_ = f.Close()

header := ops.VHDHeader{}
err = binary.Read(bytes.NewBuffer(buff[:]), binary.BigEndian, &header)
Expect(err).ToNot(HaveOccurred())
// Just check the fields that we know the value of, that should indicate that the header is valid
Expect(hex.EncodeToString(header.DiskType[:])).To(Equal("00000002"))
Expect(hex.EncodeToString(header.Features[:])).To(Equal("00000002"))
Expect(hex.EncodeToString(header.DataOffset[:])).To(Equal("ffffffffffffffff"))
Expect(hex.EncodeToString(header.CreatorApplication[:])).To(Equal("656c656d"))
Expect(hex.EncodeToString(header.CreatorHostOS[:])).To(Equal("73757365"))
Expect(hex.EncodeToString(header.DiskType[:])).To(Equal("00000002"))
fmt.Println(out)
})
})
})
Expand Down Expand Up @@ -204,6 +255,30 @@ stages:
Expect(err).ToNot(HaveOccurred(), out)
_, err = os.Stat(filepath.Join(tempDir, "kairos-opensuse-tumbleweed-core-amd64-generic-v3.2.1.raw.gce.tar.gz"))
Expect(err).ToNot(HaveOccurred(), out)
Expect(err).ToNot(HaveOccurred(), out)
// Open the file and check that there is a disk.raw file inside and check that its rounded to a GB
file, err := os.Open(filepath.Join(tempDir, "kairos-opensuse-tumbleweed-core-amd64-generic-v3.2.1.raw.gce.tar.gz"))
Expect(err).ToNot(HaveOccurred(), out)
defer file.Close()
// Create a gzip reader
gzr, err := gzip.NewReader(file)
Expect(err).ToNot(HaveOccurred(), out)
defer gzr.Close()

tr := tar.NewReader(gzr)
found := false
for {
hdr, err := tr.Next()
if err != nil {
break
}
if hdr.Name == "disk.raw" {
found = true
Expect(hdr.Size).To(BeNumerically(">", 1<<30), out)
}
}
Expect(found).To(BeTrue(), out)
Expect(err).ToNot(HaveOccurred(), out)
})
It("generates a vhd image", func() {
image := "quay.io/kairos/opensuse:tumbleweed-core-amd64-generic-v3.2.1"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ require (
github.com/foxboron/sbctl v0.0.0-20240526163235-64e649b31c8e
github.com/gofrs/uuid v4.4.0+incompatible
github.com/google/go-containerregistry v0.20.2
github.com/google/uuid v1.6.0
github.com/hashicorp/go-multierror v1.1.1
github.com/kairos-io/go-ukify v0.2.5
github.com/kairos-io/kairos-agent/v2 v2.16.2-0.20241223225924-d8d75ebd3741
Expand Down Expand Up @@ -104,6 +103,7 @@ require (
github.com/google/go-tspi v0.3.0 // indirect
github.com/google/pprof v0.0.0-20241210010833-40e02aabc2ad // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gookit/color v1.5.4 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
Expand Down
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1261,8 +1261,6 @@ golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20200331195152-e8c3332aa8e5/go.mod h1:4M0jN8W1tt0AVLNr8HDosyJCDCDuyL9N9+3m7wDWgKw=
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f h1:XdNn9LlyWAhLVp6P/i8QYBW+hlyhrhei9uErw2B5GJo=
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f/go.mod h1:D5SMRVC3C2/4+F/DB1wZsLRnSNimn2Sp/NPsCrsv8ak=
golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67 h1:1UoZQm6f0P/ZO0w1Ri+f+ifG/gXhegadRdwBIXEFWDo=
golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67/go.mod h1:qj5a5QZpwLU2NLQudwIN5koi3beDhSAlJwa67PuM98c=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
Expand Down Expand Up @@ -1351,10 +1349,6 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/net v0.31.0 h1:68CPQngjLL0r2AlUKiSxtQFKvzRVbnzLwMUn5SzcLHo=
golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM=
golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI=
golang.org/x/net v0.32.0/go.mod h1:CwU0IoeOlnQQWJ6ioyFrfRuomB8GKF6KbYXZVyeXNfs=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down Expand Up @@ -1594,8 +1588,6 @@ golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.27.0 h1:qEKojBykQkQ4EynWy4S8Weg69NumxKdn40Fce3uc/8o=
golang.org/x/tools v0.27.0/go.mod h1:sUi0ZgbwW9ZPAq26Ekut+weQPR5eIM6GQLQ1Yjm1H0Q=
golang.org/x/tools v0.28.0 h1:WuB6qZ4RPCQo5aP3WdKZS7i595EdWqWR8vqJTlwTVK8=
golang.org/x/tools v0.28.0/go.mod h1:dcIOrVd3mfQKTgrDVQHqCPMWy6lnhfhtX3hLXYVLfRw=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
24 changes: 0 additions & 24 deletions image-assets/azure.sh

This file was deleted.

26 changes: 0 additions & 26 deletions image-assets/gce.sh

This file was deleted.

3 changes: 3 additions & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ const IsoOutput UkiOutput = "iso"
const ContainerOutput UkiOutput = "container"
const DefaultOutput UkiOutput = "uki"

const MB = int64(1024 * 1024)
const GB = 1024 * MB

func OutPutTypes() []string {
return []string{string(IsoOutput), string(ContainerOutput), string(DefaultOutput)}
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/ops/disks.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,11 @@ func ConvertRawDiskToVHD(src string) func(ctx context.Context) error {
}

internal.Log.Logger.Info().Msgf("Generating raw disk from '%s'", glob[0])
out, err := utils.SH(fmt.Sprintf("/azure.sh %s", glob[0]))
internal.Log.Logger.Printf("Output '%s'", out)
output, err := Raw2Azure(glob[0])
if err != nil {
internal.Log.Logger.Error().Msgf("Generating raw disk from '%s' failed with error '%s'", glob[0], err.Error())
} else {
internal.Log.Logger.Info().Msgf("Generated VHD disk '%s'", output)
}
return err
}
Expand All @@ -261,10 +262,11 @@ func ConvertRawDiskToGCE(src string) func(ctx context.Context) error {
}

internal.Log.Logger.Info().Msgf("Generating raw disk '%s'", glob[0])
out, err := utils.SH(fmt.Sprintf("/gce.sh %s", glob[0]))
internal.Log.Logger.Printf("Output '%s'", out)
output, err := Raw2Gce(glob[0])
if err != nil {
internal.Log.Logger.Error().Msgf("Generating raw disk from '%s' failed with error '%s'", src, err.Error())
} else {
internal.Log.Logger.Info().Msgf("Generated GCE disk '%s'", output)
}
return err
}
Expand Down
Loading

0 comments on commit 95281d6

Please sign in to comment.