-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Raw image modification implementation (#18)
- Loading branch information
Showing
8 changed files
with
225 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package build | ||
|
||
import ( | ||
_ "embed" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
) | ||
|
||
const ( | ||
copyExec = "/bin/cp" | ||
modifyScriptName = "modify-raw-image.sh" | ||
) | ||
|
||
//go:embed scripts/modify-raw-image.sh | ||
var modifyRawImageScript string | ||
|
||
func (b *Builder) buildRawImage() error { | ||
cmd := b.createRawImageCopyCommand() | ||
err := cmd.Run() | ||
if err != nil { | ||
return fmt.Errorf("copying the base image %s to the output image location %s: %w", | ||
b.imageConfig.Image.BaseImage, b.generateOutputImageFilename(), err) | ||
} | ||
|
||
err = b.writeModifyScript() | ||
if err != nil { | ||
return fmt.Errorf("writing the image modification script: %w", err) | ||
} | ||
|
||
cmd = b.createModifyCommand() | ||
err = cmd.Run() | ||
if err != nil { | ||
return fmt.Errorf("running the image modification script: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (b *Builder) createRawImageCopyCommand() *exec.Cmd { | ||
baseImagePath := b.generateBaseImageFilename() | ||
outputImagePath := b.generateOutputImageFilename() | ||
|
||
cmd := exec.Command(copyExec, baseImagePath, outputImagePath) | ||
return cmd | ||
} | ||
|
||
func (b *Builder) writeModifyScript() error { | ||
imageToModify := b.generateOutputImageFilename() | ||
contents := fmt.Sprintf(modifyRawImageScript, imageToModify, b.combustionDir) | ||
|
||
scriptPath := filepath.Join(b.buildConfig.BuildDir, modifyScriptName) | ||
err := os.WriteFile(scriptPath, []byte(contents), os.ModePerm) | ||
if err != nil { | ||
return fmt.Errorf("writing file %s: %w", scriptPath, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (b *Builder) createModifyCommand() *exec.Cmd { | ||
scriptPath := filepath.Join(b.buildConfig.BuildDir, modifyScriptName) | ||
cmd := exec.Command(scriptPath) | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package build | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/suse-edge/edge-image-builder/pkg/config" | ||
) | ||
|
||
func TestCreateRawImageCopyCommand(t *testing.T) { | ||
// Setup | ||
imageConfig := config.ImageConfig{ | ||
Image: config.Image{ | ||
BaseImage: "base-image", | ||
OutputImageName: "build-image", | ||
}, | ||
} | ||
buildConfig := config.BuildConfig{ | ||
ImageConfigDir: "config-dir", | ||
} | ||
builder := New(&imageConfig, &buildConfig) | ||
|
||
// Test | ||
cmd := builder.createRawImageCopyCommand() | ||
|
||
// Verify | ||
require.NotNil(t, cmd) | ||
|
||
assert.Equal(t, copyExec, cmd.Path) | ||
expectedArgs := []string { | ||
copyExec, | ||
builder.generateBaseImageFilename(), | ||
builder.generateOutputImageFilename(), | ||
} | ||
assert.Equal(t, expectedArgs, cmd.Args) | ||
} | ||
|
||
func TestWriteModifyScript(t *testing.T) { | ||
// Setup | ||
tmpDir, err := os.MkdirTemp("", "eib-") | ||
require.NoError(t, err) | ||
defer os.RemoveAll(tmpDir) | ||
|
||
imageConfig := config.ImageConfig{ | ||
Image: config.Image{ | ||
OutputImageName: "output-image", | ||
}, | ||
} | ||
buildConfig := config.BuildConfig{ | ||
ImageConfigDir: "config-dir", | ||
BuildDir: tmpDir, | ||
} | ||
builder := New(&imageConfig, &buildConfig) | ||
builder.combustionDir = "combustion-dir" | ||
|
||
// Test | ||
err = builder.writeModifyScript() | ||
|
||
// Verify | ||
require.NoError(t, err) | ||
|
||
expectedFilename := filepath.Join(tmpDir, modifyScriptName) | ||
foundBytes, err := os.ReadFile(expectedFilename) | ||
require.NoError(t, err) | ||
|
||
foundContents := string(foundBytes) | ||
assert.Contains(t, foundContents, "guestfish --rw -a config-dir/output-image") | ||
assert.Contains(t, foundContents, "copy-in combustion-dir") | ||
} | ||
|
||
func TestCreateModifyCommand(t *testing.T) { | ||
// Setup | ||
buildConfig := config.BuildConfig{ | ||
BuildDir: "build-dir", | ||
} | ||
builder := New(nil, &buildConfig) | ||
|
||
// Test | ||
cmd := builder.createModifyCommand() | ||
|
||
// Verify | ||
require.NotNil(t, cmd) | ||
|
||
expectedPath := filepath.Join("build-dir", modifyScriptName) | ||
assert.Equal(t, expectedPath, cmd.Path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#!/bin/bash | ||
set -euo pipefail | ||
|
||
# Substitution Fields | ||
# 1. Full path to the image to modify | ||
# 2. Full path to the combustion directory | ||
|
||
# Guestfish Commands Explanation | ||
# | ||
# sh "btrfs property set / ro false" | ||
# - Enables write access to the read only filesystem | ||
# | ||
# copy-in _s / | ||
# - Copies the combustion directory into the root of the image | ||
# - _s should be populated with the full path to the built combustion directory | ||
# | ||
# sh "btrfs filesystem label / INSTALL" | ||
# - As of Oct 25, 2023, combustion only checks volumes of certain names for the | ||
# /combustion directory. The SLE Micro raw image sets the root partition name to | ||
# "ROOT", which isn't one of the checked volume names. This line changes the | ||
# label to "INSTALL" (the same as the ISO installer uses) so it's picked up | ||
# when combustion runs. | ||
# | ||
# sh "btrfs property set / ro true" | ||
# - Resets the filesystem to read only | ||
|
||
guestfish --rw -a %s -i <<'EOF' | ||
sh "btrfs property set / ro false" | ||
copy-in %s / | ||
sh "btrfs filesystem label / INSTALL" | ||
sh "btrfs property set / ro true" | ||
EOF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters