Skip to content

Commit

Permalink
Remove FstabTruncate option
Browse files Browse the repository at this point in the history
  • Loading branch information
upils committed Oct 25, 2023
1 parent 96f2ac5 commit 381615b
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 83 deletions.
5 changes: 1 addition & 4 deletions internal/imagedefinition/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ The following specification defines what is supported in the YAML:
# ubuntu-image will support creating many different types of
# artifacts, including the actual images, manifest files,
# changelogs, and a list of files in the rootfs.
# Set a custom fstab. The existing one (if any) will be truncated.
fstab: (optional)
-
# the value of LABEL= for the fstab entry
Expand All @@ -256,10 +257,6 @@ The following specification defines what is supported in the YAML:
dump: <bool> (optional)
# the order to fsck the filesystem
fsck-order: <int>
# Define what to do with the existing (if any) fstab file.
# Default to "false", which will keep existing entries and append new
# ones (if any)
fstab_truncate: <bool> (optional)
artifacts:
# Used to specify that ubuntu-image should create a .img file.
img: (optional)
Expand Down
3 changes: 1 addition & 2 deletions internal/imagedefinition/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ package imagedefinition
import "errors"

var (
ErrKeepEnabledNil = errors.New("KeepEnabled is nil. Thi value cannot be properly used.")
ErrFstabTruncateNil = errors.New("FstabTruncate is nil. Thi value cannot be properly used.")
ErrKeepEnabledNil = errors.New("KeepEnabled is nil. Thi value cannot be properly used.")
)
1 change: 0 additions & 1 deletion internal/imagedefinition/image_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ type Customization struct {
ExtraPackages []*Package `yaml:"extra-packages" json:"ExtraPackages,omitempty" extra_step_prebuilt_rootfs:"install_extra_packages"`
ExtraSnaps []*Snap `yaml:"extra-snaps" json:"ExtraSnaps,omitempty" extra_step_prebuilt_rootfs:"install_extra_snaps"`
Fstab []*Fstab `yaml:"fstab" json:"Fstab,omitempty"`
FstabTruncate *bool `yaml:"fstab_truncate" json:"FstabTruncate,omitempty" default:"false"`
Manual *Manual `yaml:"manual" json:"Manual,omitempty"`
}

Expand Down
29 changes: 1 addition & 28 deletions internal/statemachine/classic_states.go
Original file line number Diff line number Diff line change
Expand Up @@ -1018,27 +1018,7 @@ func (stateMachine *StateMachine) customizeFstab() error {

fstabPath := filepath.Join(stateMachine.tempDirs.chroot, "etc", "fstab")

if classicStateMachine.ImageDef.Customization.FstabTruncate == nil {
return imagedefinition.ErrFstabTruncateNil
}

// open /etc/fstab for writing
flags := os.O_CREATE | os.O_RDWR
if *classicStateMachine.ImageDef.Customization.FstabTruncate {
flags = flags | os.O_TRUNC
} else {
flags = flags | os.O_APPEND
}

// check if fstab is empty
info, err := os.Stat(fstabPath) // an error means the file do not exist, which is fine
emptyFstab := true

if err == nil && info.Size() != 0 {
emptyFstab = false
}

fstabIO, err := osOpenFile(fstabPath, flags, 0644)
fstabIO, err := osOpenFile(fstabPath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
if err != nil {
return fmt.Errorf("Error opening fstab: %s", err.Error())
}
Expand All @@ -1063,13 +1043,6 @@ func (stateMachine *StateMachine) customizeFstab() error {
fstabEntries = append(fstabEntries, fstabEntry)
}

if !*classicStateMachine.ImageDef.Customization.FstabTruncate && !emptyFstab {
_, err = fstabIO.Write([]byte("\n"))
if err != nil {
return err
}
}

_, err = fstabIO.Write([]byte(strings.Join(fstabEntries, "\n") + "\n"))

return err
Expand Down
49 changes: 5 additions & 44 deletions internal/statemachine/classic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3740,12 +3740,11 @@ func TestCustomizeFstab(t *testing.T) {
testCases := []struct {
name string
fstab []*imagedefinition.Fstab
fstabTruncate bool
expectedFstab string
existingFstab string
}{
{
name: "one_entry to an empty fstab",
name: "one entry to an empty fstab",
fstab: []*imagedefinition.Fstab{
{
Label: "writable",
Expand All @@ -3760,7 +3759,7 @@ func TestCustomizeFstab(t *testing.T) {
`,
},
{
name: "one_entry to a non-empty fstab",
name: "one entry to a non-empty fstab",
fstab: []*imagedefinition.Fstab{
{
Label: "writable",
Expand All @@ -3771,30 +3770,12 @@ func TestCustomizeFstab(t *testing.T) {
FsckOrder: 1,
},
},
expectedFstab: `LABEL=xxx / ext4 discard,errors=remount-ro 0 1
LABEL=writable / ext4 defaults 1 1
`,
existingFstab: `LABEL=xxx / ext4 discard,errors=remount-ro 0 1`,
},
{
name: "one_entry to a non-empty fstab to be truncated",
fstab: []*imagedefinition.Fstab{
{
Label: "writable",
Mountpoint: "/",
FSType: "ext4",
MountOptions: "defaults",
Dump: true,
FsckOrder: 1,
},
},
fstabTruncate: true,
expectedFstab: `LABEL=writable / ext4 defaults 1 1
`,
existingFstab: `LABEL=xxx /test ext4 discard,errors=remount-ro 0 1`,
existingFstab: `LABEL=xxx / ext4 discard,errors=remount-ro 0 1`,
},
{
name: "two_entries",
name: "two entries",
fstab: []*imagedefinition.Fstab{
{
Label: "writable",
Expand All @@ -3815,19 +3796,6 @@ LABEL=writable / ext4 defaults 1 1
},
expectedFstab: `LABEL=writable / ext4 defaults 0 1
LABEL=system-boot /boot/firmware vfat defaults 0 1
`,
},
{
name: "defaults_assumed",
fstab: []*imagedefinition.Fstab{
{
Label: "writable",
Mountpoint: "/",
FSType: "ext4",
FsckOrder: 1,
},
},
expectedFstab: `LABEL=writable / ext4 defaults 0 1
`,
},
}
Expand All @@ -3846,8 +3814,7 @@ LABEL=system-boot /boot/firmware vfat defaults 0 1
Series: getHostSuite(),
Rootfs: &imagedefinition.Rootfs{},
Customization: &imagedefinition.Customization{
Fstab: tc.fstab,
FstabTruncate: helper.BoolPtr(tc.fstabTruncate),
Fstab: tc.fstab,
},
}

Expand Down Expand Up @@ -3911,7 +3878,6 @@ func TestStateMachine_customizeFstab_fail(t *testing.T) {
FsckOrder: 1,
},
},
FstabTruncate: helper.BoolPtr(true),
},
}

Expand All @@ -3921,11 +3887,6 @@ func TestStateMachine_customizeFstab_fail(t *testing.T) {
}()
err := stateMachine.customizeFstab()
asserter.AssertErrContains(err, "Error opening fstab")
osOpenFile = os.OpenFile

stateMachine.ImageDef.Customization.FstabTruncate = nil
err = stateMachine.customizeFstab()
asserter.AssertErrContains(err, imagedefinition.ErrFstabTruncateNil.Error())
})
}

Expand Down
4 changes: 0 additions & 4 deletions internal/statemachine/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -901,10 +901,6 @@ func checkCustomizationSteps(searchStruct interface{}, tag string) (extraStates
for i := 0; i < elem.NumField(); i++ {
field := elem.Field(i)

if field.Kind() == reflect.Bool {
continue
}

if !field.IsNil() {
tags := elem.Type().Field(i).Tag
tagValue, hasTag := tags.Lookup(tag)
Expand Down

0 comments on commit 381615b

Please sign in to comment.