Skip to content

Commit

Permalink
Replace debootstrap with mmdebstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
upils committed Jan 30, 2024
1 parent 2c907b2 commit 0039c92
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 23 deletions.
11 changes: 6 additions & 5 deletions internal/statemachine/classic_states.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,16 @@ func (stateMachine *StateMachine) createChroot() error {
return fmt.Errorf("Failed to create chroot directory %s : %s", stateMachine.tempDirs.chroot, err.Error())
}

debootstrapCmd := generateDebootstrapCmd(classicStateMachine.ImageDef,
mmdebstrapCmd := generateMmdebstrapCmd(classicStateMachine.ImageDef,
stateMachine.tempDirs.chroot,
classicStateMachine.commonFlags.Debug,
)

debootstrapOutput := helper.SetCommandOutput(debootstrapCmd, classicStateMachine.commonFlags.Debug)
debootstrapOutput := helper.SetCommandOutput(mmdebstrapCmd, classicStateMachine.commonFlags.Debug)

if err := debootstrapCmd.Run(); err != nil {
return fmt.Errorf("Error running debootstrap command \"%s\". Error is \"%s\". Output is: \n%s",
debootstrapCmd.String(), err.Error(), debootstrapOutput.String())
if err := mmdebstrapCmd.Run(); err != nil {
return fmt.Errorf("Error running mmdebstrap command \"%s\". Error is \"%s\". Output is: \n%s",
mmdebstrapCmd.String(), err.Error(), debootstrapOutput.String())
}

err := stateMachine.fixHostname()
Expand Down
11 changes: 7 additions & 4 deletions internal/statemachine/classic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3624,6 +3624,7 @@ func TestCreateChroot(t *testing.T) {
Series: getHostSuite(),
Rootfs: &imagedefinition.Rootfs{
Pocket: "proposed",
Mirror: "http://archive.ubuntu.com/ubuntu/",
},
}

Expand Down Expand Up @@ -3700,7 +3701,9 @@ func TestFailedCreateChroot(t *testing.T) {
stateMachine.ImageDef = imagedefinition.ImageDefinition{
Architecture: getHostArch(),
Series: getHostSuite(),
Rootfs: &imagedefinition.Rootfs{},
Rootfs: &imagedefinition.Rootfs{
Mirror: "http://archive.ubuntu.com/ubuntu/",
},
}

err := stateMachine.makeTemporaryDirectories()
Expand All @@ -3722,7 +3725,7 @@ func TestFailedCreateChroot(t *testing.T) {
execCommand = exec.Command
})
err = stateMachine.createChroot()
asserter.AssertErrContains(err, "Error running debootstrap command")
asserter.AssertErrContains(err, "Error running mmdebstrap command")
execCommand = exec.Command

// Check if failure of open hostname file is detected
Expand All @@ -3731,7 +3734,7 @@ func TestFailedCreateChroot(t *testing.T) {
err = stateMachine.makeTemporaryDirectories()
asserter.AssertErrNil(err, true)

// Prepare a fallthrough debootstrap
// Prepare a fallthrough mmdebstrap
testCaseName = "TestFailedCreateChrootNoHostname"
execCommand = fakeExecCommand
t.Cleanup(func() {
Expand All @@ -3755,7 +3758,7 @@ func TestFailedCreateChroot(t *testing.T) {
err = stateMachine.makeTemporaryDirectories()
asserter.AssertErrNil(err, true)

// Prepare a fallthrough debootstrap
// Prepare a fallthrough mmdebstrap
testCaseName = "TestFailedCreateChrootSkip"
osTruncate = mockTruncate
t.Cleanup(func() {
Expand Down
22 changes: 15 additions & 7 deletions internal/statemachine/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,32 +605,40 @@ func cloneGitRepo(imageDefinition imagedefinition.ImageDefinition, workDir strin
return err
}

// generateDebootstrapCmd generates the debootstrap command used to create a chroot
// generateMmdebstrapCmd generates the mmdebstrap command used to create a chroot
// environment that will eventually become the rootfs of the resulting image
func generateDebootstrapCmd(imageDefinition imagedefinition.ImageDefinition, targetDir string) *exec.Cmd {
debootstrapCmd := execCommand("debootstrap",
func generateMmdebstrapCmd(imageDefinition imagedefinition.ImageDefinition, targetDir string, debug bool) *exec.Cmd {
mmdebstrapCmd := execCommand("mmdebstrap",
"--arch", imageDefinition.Architecture,
"--variant=minbase",
"--mode=sudo",
"--include=apt",
"--format=dir",
"--skip=cleanup/reproducible", // Prevents mmdebstrap from deleting /etc/machine-id
)

if debug {
mmdebstrapCmd.Args = append(mmdebstrapCmd.Args, "--verbose")
}

if imageDefinition.Customization != nil && len(imageDefinition.Customization.ExtraPPAs) > 0 {
// ca-certificates is needed to use PPAs
debootstrapCmd.Args = append(debootstrapCmd.Args, "--include=ca-certificates")
mmdebstrapCmd.Args = append(mmdebstrapCmd.Args, "--include=ca-certificates")
}

if len(imageDefinition.Rootfs.Components) > 0 {
components := strings.Join(imageDefinition.Rootfs.Components, ",")
debootstrapCmd.Args = append(debootstrapCmd.Args, "--components="+components)
mmdebstrapCmd.Args = append(mmdebstrapCmd.Args, "--components="+components)
}

// add the SUITE TARGET and MIRROR arguments
debootstrapCmd.Args = append(debootstrapCmd.Args, []string{
mmdebstrapCmd.Args = append(mmdebstrapCmd.Args, []string{
imageDefinition.Series,
targetDir,
imageDefinition.Rootfs.Mirror,
}...)

return debootstrapCmd
return mmdebstrapCmd
}

// generateAptCmd generates the apt command used to create a chroot
Expand Down
56 changes: 56 additions & 0 deletions internal/statemachine/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1519,3 +1519,59 @@ func Test_getMountCmd_fail(t *testing.T) {
asserter.Errorf("gotUmountCmds should be nil but is %s", gotUmountCmds)
}
}

func Test_generateMmdebstrapCmd(t *testing.T) {
type args struct {
imageDefinition imagedefinition.ImageDefinition
targetDir string
debug bool
}
tests := []struct {
name string
args args
want string
}{
{
name: "simple case",
args: args{
imageDefinition: imagedefinition.ImageDefinition{
Architecture: "amd64",
Series: "jammy",
Rootfs: &imagedefinition.Rootfs{
Mirror: "http://archive.ubuntu.com/ubuntu/",
},
},
},
want: "/usr/bin/mmdebstrap --arch amd64 --variant=minbase --mode=sudo --include=apt --format=dir --skip=cleanup/reproducible jammy http://archive.ubuntu.com/ubuntu/",
},
{
name: "complex case",
args: args{
imageDefinition: imagedefinition.ImageDefinition{
Architecture: "amd64",
Series: "jammy",
Rootfs: &imagedefinition.Rootfs{
Mirror: "http://archive.ubuntu.com/ubuntu/",
Components: []string{"main,restricted"},
},
Customization: &imagedefinition.Customization{
ExtraPPAs: []*imagedefinition.PPA{
{
PPAName: "test",
},
},
},
},
debug: true,
},
want: "/usr/bin/mmdebstrap --arch amd64 --variant=minbase --mode=sudo --include=apt --format=dir --skip=cleanup/reproducible --verbose --include=ca-certificates --components=main,restricted jammy http://archive.ubuntu.com/ubuntu/",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
asserter := helper.Asserter{T: t}
got := generateMmdebstrapCmd(tt.args.imageDefinition, tt.args.targetDir, tt.args.debug)
asserter.AssertEqual(tt.want, got.String())
})
}
}
13 changes: 8 additions & 5 deletions internal/statemachine/pack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,21 +270,24 @@ func TestPackStateMachine_SuccessfulRun(t *testing.T) {
)
asserter.AssertErrNil(err, true)

debootstrapCmd := execCommand("debootstrap",
mmdebstrapCmd := execCommand("mmdebstrap",
"--arch", "amd64",
"--variant=minbase",
"--mode=sudo",
"--include=apt",
"--format=dir",
"--include=grub2-common",
"jammy",
stateMachine.Opts.RootfsDir,
"http://archive.ubuntu.com/ubuntu/",
)

debootstrapOutput := helper.SetCommandOutput(debootstrapCmd, true)
mmdebstrapOutput := helper.SetCommandOutput(mmdebstrapCmd, true)

err = debootstrapCmd.Run()
err = mmdebstrapCmd.Run()
if err != nil {
t.Errorf("Error running debootstrap command \"%s\". Error is \"%s\". Output is: \n%s",
debootstrapCmd.String(), err.Error(), debootstrapOutput.String())
t.Errorf("Error running mmdebstrap command \"%s\". Error is \"%s\". Output is: \n%s",
mmdebstrapCmd.String(), err.Error(), mmdebstrapOutput.String())
}
asserter.AssertErrNil(err, true)

Expand Down
1 change: 1 addition & 0 deletions internal/statemachine/tests_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var basicImageDef = imagedefinition.ImageDefinition{
Series: getHostSuite(),
Rootfs: &imagedefinition.Rootfs{
Archive: "ubuntu",
Mirror: "http://archive.ubuntu.com/ubuntu/",
},
Customization: &imagedefinition.Customization{},
}
Expand Down
3 changes: 1 addition & 2 deletions snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ environment:
PATH: $SNAP/usr/bin:$SNAP/bin:$SNAP/usr/sbin:$SNAP/sbin:$PATH
GCONV_PATH: /snap/core22/current/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/gconv
PYTHONPATH: $SNAP/usr/lib/python3/dist-packages:$PYTHONPATH
DEBOOTSTRAP_DIR: $SNAP/usr/share/debootstrap
PERL5LIB: $SNAP/usr/share/perl5:$SNAP/usr/share/perl:$PERL5LIB

apps:
Expand All @@ -39,7 +38,7 @@ parts:
- fdisk
- gdisk
- fakeroot
- debootstrap
- mmdebstrap
- gpg
- germinate
- mtools
Expand Down

0 comments on commit 0039c92

Please sign in to comment.