Skip to content

Commit

Permalink
feat: skip filesystem for state and ephemeral partitions in the insta…
Browse files Browse the repository at this point in the history
…ller

Filesystem creation step is moved on the later stage: when Talos mounts
the partition for the first time.
Now it checks if the partition doesn't have any filesystem and formats
it right before mounting.

Additionally refactored mount options a bit:
- replaced separate options with a set of binary flags.
- implemented pre-mount and post-unmount hooks.

And fixed typos in couple of places and increased timeout for `apid ready`.

Signed-off-by: Artem Chernyshev <artem.0xD2@gmail.com>
  • Loading branch information
Unix4ever authored and talos-bot committed Feb 17, 2021
1 parent edbaa0b commit 02b3719
Show file tree
Hide file tree
Showing 20 changed files with 393 additions and 223 deletions.
47 changes: 44 additions & 3 deletions cmd/installer/pkg/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,51 @@ func (i *Installer) Install(seq runtime.Sequence) (err error) {
}

// Mount the partitions.
mountpoints := mount.NewMountPoints()

for _, label := range []string{constants.BootPartitionLabel, constants.EFIPartitionLabel} {
err = func() error {
var device string
// searching targets for the device to be used
OuterLoop:
for dev, targets := range i.manifest.Targets {
for _, target := range targets {
if target.Label == label {
device = dev

break OuterLoop
}
}
}

mountpoints, err := i.manifest.SystemMountpoints()
if err != nil {
return err
if device == "" {
return fmt.Errorf("failed to detect %s target device", label)
}

var bd *blockdevice.BlockDevice

bd, err = blockdevice.Open(device)
if err != nil {
return err
}

defer bd.Close() //nolint:errcheck

var mountpoint *mount.Point

mountpoint, err = mount.SystemMountPointForLabel(bd, label)
if err != nil {
return err
}

mountpoints.Set(label, mountpoint)

return nil
}()

if err != nil {
return err
}
}

if err = mount.Mount(mountpoints); err != nil {
Expand Down
27 changes: 10 additions & 17 deletions cmd/installer/pkg/install/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strings"
"time"

"github.com/dustin/go-humanize"
"github.com/talos-systems/go-blockdevice/blockdevice"
"github.com/talos-systems/go-blockdevice/blockdevice/partition/gpt"
"github.com/talos-systems/go-blockdevice/blockdevice/util"
Expand All @@ -22,6 +23,7 @@ import (
"github.com/talos-systems/talos/internal/app/machined/pkg/runtime"
"github.com/talos-systems/talos/internal/app/machined/pkg/runtime/v1alpha1/board"
"github.com/talos-systems/talos/internal/pkg/mount"
"github.com/talos-systems/talos/internal/pkg/partition"
"github.com/talos-systems/talos/pkg/machinery/constants"
)

Expand Down Expand Up @@ -131,16 +133,12 @@ func NewManifest(label string, sequence runtime.Sequence, bootPartitionFound boo

stateTarget := StateTarget(opts.Disk, &Target{
PreserveContents: bootPartitionFound,
ExtraPreserveSources: []PreserveSource{
{
Label: constants.LegacyBootPartitionLabel,
FileSystemType: FilesystemTypeVFAT,
FnmatchFilters: []string{"config.yaml"},
},
FormatOptions: &partition.FormatOptions{
FileSystemType: partition.FilesystemTypeNone,
},
})

ephemeralTarget := EphemeralTarget(opts.Disk, nil)
ephemeralTarget := EphemeralTarget(opts.Disk, NoFilesystem)

if opts.Force {
ephemeralTarget.Force = true
Expand Down Expand Up @@ -417,7 +415,7 @@ func (m *Manifest) preserveContents(device Device, targets []*Target) (err error

var (
sourcePart *gpt.Partition
fileSystemType FileSystemType
fileSystemType partition.FileSystemType
fnmatchFilters []string
)

Expand Down Expand Up @@ -466,11 +464,11 @@ func (m *Manifest) restoreContents(targets []*Target) error {
}

// SystemMountpoints returns list of system mountpoints for the manifest.
func (m *Manifest) SystemMountpoints() (*mount.Points, error) {
func (m *Manifest) SystemMountpoints(opts ...mount.Option) (*mount.Points, error) {
mountpoints := mount.NewMountPoints()

for dev := range m.Targets {
mp, err := mount.SystemMountPointsForDevice(dev)
mp, err := mount.SystemMountPointsForDevice(dev, opts...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -511,12 +509,7 @@ func (m *Manifest) zeroDevice(device Device) (err error) {
// nolint: dupl, gocyclo
func (t *Target) Partition(pt *gpt.GPT, pos int, bd *blockdevice.BlockDevice) (err error) {
if t.Skip {
var part *gpt.Partition

part, err = t.Locate(pt)
if err != nil {
return err
}
part := pt.Partitions().FindByName(t.Label)

if part != nil {
log.Printf("skipped %s (%s) size %d blocks", t.PartitionName, t.Label, part.Length())
Expand All @@ -525,7 +518,7 @@ func (t *Target) Partition(pt *gpt.GPT, pos int, bd *blockdevice.BlockDevice) (e
return nil
}

log.Printf("partitioning %s - %s\n", t.Device, t.Label)
log.Printf("partitioning %s - %s %q\n", t.Device, t.Label, humanize.Bytes(t.Size))

opts := []gpt.PartitionOption{
gpt.WithPartitionType(t.PartitionType),
Expand Down
35 changes: 18 additions & 17 deletions cmd/installer/pkg/install/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/talos-systems/talos/cmd/installer/pkg/install"
"github.com/talos-systems/talos/internal/app/machined/pkg/runtime"
"github.com/talos-systems/talos/internal/pkg/mount"
"github.com/talos-systems/talos/internal/pkg/partition"
"github.com/talos-systems/talos/pkg/machinery/constants"
"github.com/talos-systems/talos/pkg/makefs"
)
Expand All @@ -45,7 +46,7 @@ const (
)

const (
legacyBootSize = 512 * install.MiB
legacyBootSize = 512 * partition.MiB
legacyEphemeralSize = diskSize - legacyBootSize - gptReserved*lbaSize
)

Expand Down Expand Up @@ -112,47 +113,47 @@ func (suite *manifestSuite) verifyBlockdevice(manifest *install.Manifest, curren
suite.Assert().Len(table.Partitions().Items(), 6)

part := table.Partitions().Items()[0]
suite.Assert().Equal(install.EFISystemPartition, strings.ToUpper(part.Type.String()))
suite.Assert().Equal(partition.EFISystemPartition, strings.ToUpper(part.Type.String()))
suite.Assert().Equal(constants.EFIPartitionLabel, part.Name)
suite.Assert().EqualValues(0, part.Attributes)
suite.Assert().EqualValues(install.EFISize/lbaSize, part.Length())
suite.Assert().EqualValues(partition.EFISize/lbaSize, part.Length())

part = table.Partitions().Items()[1]
suite.Assert().Equal(install.BIOSBootPartition, strings.ToUpper(part.Type.String()))
suite.Assert().Equal(partition.BIOSBootPartition, strings.ToUpper(part.Type.String()))
suite.Assert().Equal(constants.BIOSGrubPartitionLabel, part.Name)
suite.Assert().EqualValues(4, part.Attributes)
suite.Assert().EqualValues(install.BIOSGrubSize/lbaSize, part.Length())
suite.Assert().EqualValues(partition.BIOSGrubSize/lbaSize, part.Length())

part = table.Partitions().Items()[2]
suite.Assert().Equal(install.LinuxFilesystemData, strings.ToUpper(part.Type.String()))
suite.Assert().Equal(partition.LinuxFilesystemData, strings.ToUpper(part.Type.String()))
suite.Assert().Equal(constants.BootPartitionLabel, part.Name)
suite.Assert().EqualValues(0, part.Attributes)
suite.Assert().EqualValues(install.BootSize/lbaSize, part.Length())
suite.Assert().EqualValues(partition.BootSize/lbaSize, part.Length())

part = table.Partitions().Items()[3]
suite.Assert().Equal(install.LinuxFilesystemData, strings.ToUpper(part.Type.String()))
suite.Assert().Equal(partition.LinuxFilesystemData, strings.ToUpper(part.Type.String()))
suite.Assert().Equal(constants.MetaPartitionLabel, part.Name)
suite.Assert().EqualValues(0, part.Attributes)
suite.Assert().EqualValues(install.MetaSize/lbaSize, part.Length())
suite.Assert().EqualValues(partition.MetaSize/lbaSize, part.Length())

part = table.Partitions().Items()[4]
suite.Assert().Equal(install.LinuxFilesystemData, strings.ToUpper(part.Type.String()))
suite.Assert().Equal(partition.LinuxFilesystemData, strings.ToUpper(part.Type.String()))
suite.Assert().Equal(constants.StatePartitionLabel, part.Name)
suite.Assert().EqualValues(0, part.Attributes)

if !upgradeFromLegacy {
suite.Assert().EqualValues(install.StateSize/lbaSize, part.Length())
suite.Assert().EqualValues(partition.StateSize/lbaSize, part.Length())
} else {
suite.Assert().EqualValues((diskSize-legacyEphemeralSize-install.EFISize-install.BIOSGrubSize-install.BootSize-install.MetaSize)/lbaSize-gptReserved, part.Length())
suite.Assert().EqualValues((diskSize-legacyEphemeralSize-partition.EFISize-partition.BIOSGrubSize-partition.BootSize-partition.MetaSize)/lbaSize-gptReserved, part.Length())
}

part = table.Partitions().Items()[5]
suite.Assert().Equal(install.LinuxFilesystemData, strings.ToUpper(part.Type.String()))
suite.Assert().Equal(partition.LinuxFilesystemData, strings.ToUpper(part.Type.String()))
suite.Assert().Equal(constants.EphemeralPartitionLabel, part.Name)
suite.Assert().EqualValues(0, part.Attributes)

if !upgradeFromLegacy {
suite.Assert().EqualValues((diskSize-install.EFISize-install.BIOSGrubSize-install.BootSize-install.MetaSize-install.StateSize)/lbaSize-gptReserved, part.Length())
suite.Assert().EqualValues((diskSize-partition.EFISize-partition.BIOSGrubSize-partition.BootSize-partition.MetaSize-partition.StateSize)/lbaSize-gptReserved, part.Length())
} else {
suite.Assert().EqualValues(legacyEphemeralSize/lbaSize, part.Length())
}
Expand Down Expand Up @@ -399,7 +400,7 @@ func (suite *manifestSuite) createTalosLegacyLayout() {
table, err := gpt.New(bd.Device())
suite.Require().NoError(err)

partBoot, err := table.Add(512*install.MiB,
partBoot, err := table.Add(512*partition.MiB,
gpt.WithLegacyBIOSBootableAttribute(true),
gpt.WithPartitionName(constants.LegacyBootPartitionLabel),
gpt.WithPartitionType("28732AC1-1FF8-D211-BA4B-00A0C93EC93B"),
Expand Down Expand Up @@ -437,8 +438,8 @@ func (suite *manifestSuite) createTalosLegacyLayout() {
}()

mountpoints := mount.NewMountPoints()
mountpoints.Set(constants.LegacyBootPartitionLabel, mount.NewMountPoint(partBootPath, filepath.Join(tempDir, "boot"), install.FilesystemTypeVFAT, 0, ""))
mountpoints.Set(constants.EphemeralPartitionLabel, mount.NewMountPoint(partEphemeralPath, filepath.Join(tempDir, "var"), install.FilesystemTypeXFS, 0, ""))
mountpoints.Set(constants.LegacyBootPartitionLabel, mount.NewMountPoint(partBootPath, filepath.Join(tempDir, "boot"), partition.FilesystemTypeVFAT, 0, ""))
mountpoints.Set(constants.EphemeralPartitionLabel, mount.NewMountPoint(partEphemeralPath, filepath.Join(tempDir, "var"), partition.FilesystemTypeXFS, 0, ""))

err = mount.Mount(mountpoints)
suite.Require().NoError(err)
Expand Down
Loading

0 comments on commit 02b3719

Please sign in to comment.