Skip to content

Commit 9c55b9d

Browse files
authored
Use build-disk command (#1847)
* Use build-disk command This commit changes the way we build our test disks. It uses the new build-disk command instead of losetup and the install command. This means we can run build-disk without privileges and also build expandable images. Signed-off-by: Fredrik Lönnegren <[email protected]>
1 parent fd92278 commit 9c55b9d

File tree

14 files changed

+134
-53
lines changed

14 files changed

+134
-53
lines changed

.github/workflows/build_and_test.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ jobs:
128128
env:
129129
FLAVOR: ${{ inputs.flavor }}
130130
ARCH: ${{ inputs.arch }}
131-
COS_TIMEOUT: 800
131+
COS_TIMEOUT: 1600
132132
strategy:
133133
matrix:
134134
test: ${{ fromJson(needs.detect.outputs.tests) }}
@@ -162,14 +162,14 @@ jobs:
162162
- if: ${{ env.ARCH == 'x86_64' }}
163163
name: Prepare test (x86_64)
164164
run: |
165-
make QCOW2=/tmp/elemental-${{ env.FLAVOR }}.${{ env.ARCH}}.qcow2 ELMNTL_ACCEL=hvf ELMNTL_TARGETARCH=${{ env.ARCH }} ELMNTL_FIRMWARE=$(find /usr/local/Cellar/qemu -name edk2-${{ env.ARCH }}-code.fd -print -quit) prepare-test
165+
make DISK=/tmp/elemental-${{ env.FLAVOR }}.${{ env.ARCH}}.qcow2 ELMNTL_ACCEL=hvf ELMNTL_TARGETARCH=${{ env.ARCH }} ELMNTL_FIRMWARE=$(find /usr/local/Cellar/qemu -name edk2-${{ env.ARCH }}-code.fd -print -quit) prepare-test
166166
- if: ${{ env.ARCH == 'aarch64' }}
167167
name: Prepare test (aarch64)
168168
run: |
169-
make QCOW2=/tmp/elemental-${{ env.FLAVOR }}.${{ env.ARCH}}.qcow2 ELMNTL_ACCEL=none ELMNTL_MACHINETYPE=virt ELMNTL_TARGETARCH=${{ env.ARCH }} ELMNTL_FIRMWARE=/usr/share/AAVMF/AAVMF_CODE.fd prepare-test
169+
make DISK=/tmp/elemental-${{ env.FLAVOR }}.${{ env.ARCH}}.qcow2 ELMNTL_ACCEL=none ELMNTL_MACHINETYPE=virt ELMNTL_TARGETARCH=${{ env.ARCH }} ELMNTL_FIRMWARE=/usr/share/AAVMF/AAVMF_CODE.fd prepare-test
170170
- name: Run ${{ matrix.test }}
171171
run: |
172-
make QCOW2=/tmp/elemental-${{ env.FLAVOR }}.${{ env.ARCH}}.qcow2 ${{ matrix.test }}
172+
make DISK=/tmp/elemental-${{ env.FLAVOR }}.${{ env.ARCH}}.qcow2 ${{ matrix.test }}
173173
# TODO include other logs SUT collects on failure
174174
- name: Upload serial console for ${{ matrix.test }}
175175
uses: actions/upload-artifact@v3

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ isowork/
33
*.iso
44
*.iso.sha256
55
*.qcow2
6+
*.raw
7+
*.img
68
*.log
79
*.pid
810
iso-meta.json

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ RUN ARCH=$(uname -m); \
4949
mtools \
5050
xorriso \
5151
cosign \
52+
gptfdisk \
5253
lvm2
5354

5455
COPY --from=elemental-bin /usr/bin/elemental /usr/bin/elemental

Makefile

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Directory of Makefile
22
export ROOT_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
33

4-
QCOW2?=$(shell ls $(ROOT_DIR)/build/*.qcow2 2> /dev/null)
4+
DISK?=$(shell ls $(ROOT_DIR)/build/*.qcow2 2> /dev/null)
5+
DISKSIZE?=20G
56
ISO?=$(shell ls $(ROOT_DIR)/build/*.iso 2> /dev/null)
67
FLAVOR?=green
78
ARCH?=$(shell uname -m)
@@ -61,28 +62,24 @@ build-iso: build-os
6162
--local --platform $(PLATFORM) --squash-no-compression -o /build $(REPO):$(VERSION)
6263

6364
.PHONY: clean-iso
64-
clean-iso: build-example-os
65+
clean-iso: build-os
6566
$(DOCKER) run --rm -v $(ROOT_DIR)/build:/build --entrypoint /bin/bash $(REPO):$(VERSION) -c "rm -v /build/*.iso /build/*.iso.sha256 || true"
6667

6768
.PHONY: build-disk
6869
build-disk: build-os
6970
@echo Building ${ARCH} disk
7071
mkdir -p $(ROOT_DIR)/build
71-
qemu-img create -f raw build/elemental-$(FLAVOR).$(ARCH).img $(IMAGE_SIZE)
72-
- losetup -f --show build/elemental-$(FLAVOR).$(ARCH).img > .loop
73-
$(DOCKER) run --rm --privileged --device=$$(cat .loop):$$(cat .loop) -v /var/run/docker.sock:/var/run/docker.sock \
74-
--entrypoint=/bin/bash $(TOOLKIT_REPO):$(VERSION) -c "mount -t devtmpfs none /dev && \
75-
elemental --debug install --firmware efi --system.uri $(REPO):$(VERSION) --local --disable-boot-entry --platform $(PLATFORM) $$(cat .loop)"
76-
losetup -d $$(cat .loop)
77-
rm .loop
78-
qemu-img convert -O qcow2 build/elemental-$(FLAVOR).$(ARCH).img build/elemental-$(FLAVOR).$(ARCH).qcow2
79-
rm build/elemental-$(FLAVOR).$(ARCH).img
72+
$(DOCKER) run --rm -v /var/run/docker.sock:/var/run/docker.sock -v $(ROOT_DIR)/build:/build \
73+
--entrypoint /usr/bin/elemental \
74+
${TOOLKIT_REPO}:${VERSION} --debug build-disk --unprivileged --expandable -n elemental-$(FLAVOR).$(ARCH) --local \
75+
--squash-no-compression -o /build ${REPO}:${VERSION}
76+
dd if=$(ROOT_DIR)/build/elemental-$(FLAVOR).$(ARCH).raw of=$(ROOT_DIR)/build/elemental-$(FLAVOR).$(ARCH).img conv=notrunc
77+
qemu-img convert -O qcow2 $(ROOT_DIR)/build/elemental-$(FLAVOR).$(ARCH).img $(ROOT_DIR)/build/elemental-$(FLAVOR).$(ARCH).qcow2
78+
qemu-img resize $(ROOT_DIR)/build/elemental-$(FLAVOR).$(ARCH).qcow2 $(DISKSIZE)
8079

8180
.PHONY: clean-disk
8281
clean-disk:
83-
losetup -d $$(cat .loop)
84-
rm build/*.img
85-
rm .loop
82+
rm $(ROOT_DIR)/build/elemental-$(FLAVOR).$(ARCH).{raw,img,qcow2}
8683

8784
.PHONY: vet
8885
vet:

cmd/build-disk.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func NewBuildDisk(root *cobra.Command, addCheckRoot bool) *cobra.Command {
4848
},
4949
RunE: func(cmd *cobra.Command, args []string) (err error) {
5050
var cfg *v1.BuildConfig
51-
var spec *v1.Disk
51+
var spec *v1.DiskSpec
5252
var imgSource *v1.ImageSource
5353

5454
defer func() {
@@ -108,8 +108,8 @@ func NewBuildDisk(root *cobra.Command, addCheckRoot bool) *cobra.Command {
108108
c.Flags().Bool("expandable", false, "Creates an expandable image including only the recovery image")
109109
c.Flags().Bool("unprivileged", false, "Makes a build runnable within a non-privileged container, avoids mounting filesystems (experimental)")
110110
c.Flags().VarP(imgType, "type", "t", "Type of image to create")
111-
// TODO verify if cross-arch builds make any sense
112-
//addArchFlags(c)
111+
c.Flags().StringSliceP("cloud-init", "c", []string{}, "Cloud-init config files")
112+
addPlatformFlags(c)
113113
addLocalImageFlag(c)
114114
addSquashFsCompressionFlags(c)
115115
addCosignFlags(c)

cmd/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ func ReadBuildISO(b *v1.BuildConfig, flags *pflag.FlagSet) (*v1.LiveISO, error)
319319
return iso, err
320320
}
321321

322-
func ReadBuildDisk(b *v1.BuildConfig, flags *pflag.FlagSet) (*v1.Disk, error) {
322+
func ReadBuildDisk(b *v1.BuildConfig, flags *pflag.FlagSet) (*v1.DiskSpec, error) {
323323
disk := config.NewDisk(b)
324324
vp := viper.Sub("disk")
325325
if vp == nil {

make/Makefile.test

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ GINKGO_ARGS?=-v --fail-fast -r --timeout=3h
77

88
.PHONY: prepare-test
99
prepare-test:
10-
ifeq ("$(QCOW2)","")
11-
@echo "No qcow2 disk found, run 'make build-disk' first"
10+
ifeq ("$(DISK)","")
11+
@echo "No disk image found, run 'make build-disk' first"
1212
@exit 1
1313
endif
14-
@scripts/run_vm.sh start $(QCOW2)
15-
@echo "VM started from $(QCOW2)"
14+
@scripts/run_vm.sh start $(DISK)
15+
@echo "VM started from $(DISK)"
16+
$(GINKGO) run $(GINKGO_ARGS) ./tests/wait-active
1617

1718
.PHONY: prepare-installer-test
1819
prepare-installer-test:

pkg/action/build-disk.go

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"time"
2929

3030
"github.com/mudler/yip/pkg/schema"
31+
3132
"github.com/rancher/elemental-toolkit/pkg/constants"
3233
"github.com/rancher/elemental-toolkit/pkg/elemental"
3334
elementalError "github.com/rancher/elemental-toolkit/pkg/error"
@@ -49,12 +50,12 @@ const (
4950

5051
type BuildDiskAction struct {
5152
cfg *v1.BuildConfig
52-
spec *v1.Disk
53+
spec *v1.DiskSpec
5354
// holds the root path within the working directory of all partitions
5455
roots map[string]string
5556
}
5657

57-
func NewBuildDiskAction(cfg *v1.BuildConfig, spec *v1.Disk) *BuildDiskAction {
58+
func NewBuildDiskAction(cfg *v1.BuildConfig, spec *v1.DiskSpec) *BuildDiskAction {
5859
return &BuildDiskAction{cfg: cfg, spec: spec}
5960
}
6061

@@ -161,29 +162,23 @@ func (b *BuildDiskAction) BuildDiskRun() (err error) { //nolint:gocyclo
161162
return err
162163
}
163164

164-
err = grub.SetPersistentVariables(
165-
filepath.Join(b.roots[constants.OEMPartName], constants.GrubEnv),
166-
map[string]string{
167-
"next_entry": constants.RecoveryImgName,
168-
},
169-
)
170-
if err != nil {
171-
b.cfg.Logger.Errorf("failed setting firstboot menu entry: %s", err.Error())
172-
return err
165+
if b.spec.Expandable {
166+
err = grub.SetPersistentVariables(
167+
filepath.Join(b.roots[constants.OEMPartName], constants.GrubEnv),
168+
map[string]string{
169+
"next_entry": constants.RecoveryImgName,
170+
},
171+
)
172+
if err != nil {
173+
b.cfg.Logger.Errorf("failed setting firstboot menu entry: %s", err.Error())
174+
return err
175+
}
173176
}
174177

178+
grubVars := b.spec.GetGrubLabels()
175179
err = grub.SetPersistentVariables(
176180
filepath.Join(b.roots[constants.StatePartName], constants.GrubOEMEnv),
177-
map[string]string{
178-
"efi_label": b.spec.Partitions.EFI.FilesystemLabel,
179-
"oem_label": b.spec.Partitions.OEM.FilesystemLabel,
180-
"recovery_label": b.spec.Partitions.Recovery.FilesystemLabel,
181-
"state_label": b.spec.Partitions.State.FilesystemLabel,
182-
"persistent_label": b.spec.Partitions.Persistent.FilesystemLabel,
183-
"active_label": b.spec.Active.Label,
184-
"passive_label": b.spec.Passive.Label,
185-
"system_label": b.spec.Recovery.Label,
186-
},
181+
grubVars,
187182
)
188183
if err != nil {
189184
b.cfg.Logger.Errorf("failed setting grub environment variables: %s", err.Error())

pkg/action/build_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ var _ = Describe("Build Actions", func() {
219219
})
220220
})
221221
Describe("Build disk", Label("disk", "build"), func() {
222-
var disk *v1.Disk
222+
var disk *v1.DiskSpec
223223

224224
BeforeEach(func() {
225225
tmpDir, err := utils.TempDir(fs, "", "test")

pkg/config/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ func NewDiskElementalParitions(workdir string) v1.ElementalPartitions {
530530
return partitions
531531
}
532532

533-
func NewDisk(cfg *v1.BuildConfig) *v1.Disk {
533+
func NewDisk(cfg *v1.BuildConfig) *v1.DiskSpec {
534534
var workdir string
535535
var recoveryImg, activeImg, passiveImg v1.Image
536536

@@ -566,7 +566,7 @@ func NewDisk(cfg *v1.BuildConfig) *v1.Disk {
566566
)+mountSuffix,
567567
)
568568

569-
return &v1.Disk{
569+
return &v1.DiskSpec{
570570
Partitions: NewDiskElementalParitions(workdir),
571571
GrubConf: constants.GrubConf,
572572
Active: activeImg,

0 commit comments

Comments
 (0)