Skip to content

Commit

Permalink
feat(init): add support for installing to a device (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradbeam authored and andrewrynhard committed Nov 30, 2018
1 parent a9c91d3 commit 79c96cf
Show file tree
Hide file tree
Showing 14 changed files with 832 additions and 60 deletions.
13 changes: 11 additions & 2 deletions src/initramfs/cmd/init/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ func kmsg(prefix string) (*os.File, error) {
return out, nil
}

// nolint: gocyclo
func initram() error {
// Read the block devices and populate the mount point definitions.
if err := mount.Init(constants.NewRoot); err != nil {
// Read the special filesystems and populate the mount point definitions.
if err := mount.InitSpecial(constants.NewRoot); err != nil {
return err
}
// Setup logging to /dev/kmsg.
Expand All @@ -66,6 +67,14 @@ func initram() error {
if err != nil {
return err
}
// Perform rootfs/datafs installation if defined
if err := p.Install(data); err != nil {
return err
}
// Read the block devices and populate the mount point definitions.
if err := mount.InitBlock(constants.NewRoot); err != nil {
return err
}
log.Printf("preparing the node for the platform: %s", p.Name())
// Perform any tasks required by a particular platform.
if err := p.Prepare(data); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions src/initramfs/cmd/init/pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const (
// NewRoot is the path where the switchroot target is mounted.
NewRoot = "/root"

// BootPartitionLabel is the label of the partition to use for mounting at
// the boot path.
BootPartitionLabel = "ESP"

// DataPartitionLabel is the label of the partition to use for mounting at
// the data path.
DataPartitionLabel = "DATA"
Expand Down
11 changes: 10 additions & 1 deletion src/initramfs/cmd/init/pkg/fs/xfs/xfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@ import (
"os/exec"
)

// GrowFS expands an XFS filesystem to the maximum possible. The partition
// GrowFS expands a XFS filesystem to the maximum possible. The partition
// MUST be mounted, or this will fail.
func GrowFS(partname string) error {
return cmd("xfs_growfs", "-d", partname)
}

// MakeFS creates a XFS filesystem on the specified partition
func MakeFS(partname string, force bool) error {
if force {
return cmd("mkfs.xfs", "-f", partname)
}

return cmd("mkfs.xfs", partname)
}

func cmd(name string, arg ...string) error {
cmd := exec.Command(name, arg...)
cmd.Stdout = os.Stdout
Expand Down
96 changes: 52 additions & 44 deletions src/initramfs/cmd/init/pkg/mount/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"
"path"
"strings"
"sync"
"time"

"github.com/autonomy/talos/src/initramfs/cmd/init/pkg/constants"
Expand All @@ -26,8 +25,6 @@ var (
blockdevices map[string]*Point
}

once sync.Once

special = map[string]*Point{
"dev": {"devtmpfs", "/dev", "devtmpfs", unix.MS_NOSUID, "mode=0755"},
"proc": {"proc", "/proc", "proc", unix.MS_NOSUID | unix.MS_NOEXEC | unix.MS_NODEV, ""},
Expand All @@ -48,27 +45,32 @@ type Point struct {

// BlockDevice represents the metadata on a block device probed by libblkid.
type BlockDevice struct {
dev string
TYPE string
UUID string
LABEL string
dev string
TYPE string
UUID string
LABEL string
PARTLABEL string
PARTUUID string
}

// Init initializes the mount points.
func Init(s string) (err error) {
once.Do(func() {
instance = struct {
special map[string]*Point
blockdevices map[string]*Point
}{
special,
map[string]*Point{},
}
})

if err = mountSpecialDevices(); err != nil {
return
// init initializes the instance metadata
func init() {
instance = struct {
special map[string]*Point
blockdevices map[string]*Point
}{
special,
map[string]*Point{},
}
}

// InitSpecial initializes the special device mount points.
func InitSpecial(s string) (err error) {
return mountSpecialDevices()
}

// InitBlock initializes the block device mount points.
func InitBlock(s string) (err error) {
blockdevices, err := probe()
if err != nil {
return fmt.Errorf("error probing block devices: %v", err)
Expand Down Expand Up @@ -150,16 +152,12 @@ func Mount(s string) error {

// Unmount unmounts the ROOT and DATA block devices.
func Unmount() error {
mountpoint, ok := instance.blockdevices[constants.DataPartitionLabel]
if ok {
if err := unix.Unmount(mountpoint.target, 0); err != nil {
return fmt.Errorf("unmount mount point %s: %v", mountpoint.target, err)
}
}
mountpoint, ok = instance.blockdevices[constants.RootPartitionLabel]
if ok {
if err := unix.Unmount(mountpoint.target, 0); err != nil {
return fmt.Errorf("unmount mount point %s: %v", mountpoint.target, err)
for _, disk := range []string{constants.RootPartitionLabel, constants.DataPartitionLabel} {
mountpoint, ok := instance.blockdevices[disk]
if ok {
if err := unix.Unmount(mountpoint.target, 0); err != nil {
return fmt.Errorf("unmount mount point %s: %v", mountpoint.target, err)
}
}
}

Expand Down Expand Up @@ -210,7 +208,7 @@ func fixDataPartition(blockdevices []*BlockDevice) error {
// nolint: errcheck
defer bd.Close()

pt, err := bd.PartitionTable()
pt, err := bd.PartitionTable(false)
if err != nil {
return err
}
Expand All @@ -237,7 +235,7 @@ func fixDataPartition(blockdevices []*BlockDevice) error {

// Rereading the partition table requires that all partitions be unmounted
// or it will fail with EBUSY.
if err := bd.RereadPartitionTable(devname); err != nil {
if err := bd.RereadPartitionTable(); err != nil {
return err
}
}
Expand Down Expand Up @@ -289,11 +287,10 @@ func mountBlockDevices(blockdevices []*BlockDevice, s string) (err error) {
func probe() (b []*BlockDevice, err error) {
b = []*BlockDevice{}

if err := appendBlockDeviceWithLabel(&b, constants.RootPartitionLabel); err != nil {
return nil, err
}
if err := appendBlockDeviceWithLabel(&b, constants.DataPartitionLabel); err != nil {
return nil, err
for _, disk := range []string{constants.RootPartitionLabel, constants.DataPartitionLabel} {
if err := appendBlockDeviceWithLabel(&b, disk); err != nil {
return nil, err
}
}

return b, nil
Expand All @@ -309,7 +306,7 @@ func appendBlockDeviceWithLabel(b *[]*BlockDevice, value string) error {
return fmt.Errorf("no device with attribute \"LABEL=%s\" found", value)
}

blockDevice, err := probeDevice(devname)
blockDevice, err := ProbeDevice(devname)
if err != nil {
return fmt.Errorf("failed to probe block device %q: %v", devname, err)
}
Expand All @@ -319,7 +316,8 @@ func appendBlockDeviceWithLabel(b *[]*BlockDevice, value string) error {
return nil
}

func probeDevice(devname string) (*BlockDevice, error) {
// ProbeDevice looks up UUID/TYPE/LABEL/PARTLABEL/PARTUUID from a block device
func ProbeDevice(devname string) (*BlockDevice, error) {
pr, err := blkid.NewProbeFromFilename(devname)
defer blkid.FreeProbe(pr)
if err != nil {
Expand All @@ -338,12 +336,22 @@ func probeDevice(devname string) (*BlockDevice, error) {
if err != nil {
return nil, err
}
PARTLABEL, err := blkid.ProbeLookupValue(pr, "PARTLABEL", nil)
if err != nil {
return nil, err
}
PARTUUID, err := blkid.ProbeLookupValue(pr, "PARTUUID", nil)
if err != nil {
return nil, err
}

return &BlockDevice{
dev: devname,
UUID: UUID,
TYPE: TYPE,
LABEL: LABEL,
dev: devname,
UUID: UUID,
TYPE: TYPE,
LABEL: LABEL,
PARTLABEL: PARTLABEL,
PARTUUID: PARTUUID,
}, nil
}

Expand Down
Loading

0 comments on commit 79c96cf

Please sign in to comment.