Skip to content

Commit

Permalink
fold up - mkfs and mount storage
Browse files Browse the repository at this point in the history
Signed-off-by: Serge Hallyn <[email protected]>
  • Loading branch information
hallyn committed Dec 22, 2023
1 parent 3e985be commit da53040
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
19 changes: 19 additions & 0 deletions pkg/mosconfig/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"syscall"

"github.com/apex/log"
ispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/project-machine/mos/pkg/trust"
"github.com/project-machine/mos/pkg/utils"

"machinerun.io/disko"
"machinerun.io/disko/partid"
Expand Down Expand Up @@ -186,6 +190,21 @@ func (i *StorageItem) Create(mos *Mos, allDisks disko.DiskSet, mysys disko.Syste
if err := mysys.CreatePartition(d, p); err != nil {
return errors.Wrapf(err, "Failed creating storage %#v", i)
}
dev := filepath.Join("/dev", pathForPartition(d.Name, p.Number))
cmd := []string{"mkfs.ext4", "-F", dev}
if err := utils.RunCommand(cmd...); err != nil {
return errors.Wrapf(err, "Failed creating fs on %#v", i)
}

// mount
dest := filepath.Join("/storage", i.Label)
if err := utils.EnsureDir(dest); err != nil {
return errors.Wrapf(err, "Failed creating mount dir %q", dest)
}
if err := syscall.Mount(dev, dest, "ext4", 0, ""); err != nil {
return errors.Wrapf(err, "Failed mounting %#v", i)
}
log.Infof("Created and mounted %#v onto %q", i, dest)
return nil
}
return errors.Errorf("Failed to find free space for %#v", i)
Expand Down
4 changes: 3 additions & 1 deletion pkg/mosconfig/mos.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (mos *Mos) Boot() error {
}

if err := mos.SetupStorage(m); err != nil {
return err
return errors.Wrapf(err, "Failed setting up storage")
}
// Now start the services
return mos.ActivateAll(m)
Expand All @@ -214,11 +214,13 @@ func (mos *Mos) SetupStorage(m *SysManifest) error {
}
}

// Create all needed storage partitions, and mount them
for _, n := range m.Storage {
if err := n.Create(mos, allDisks, sys); err != nil {
return errors.Wrapf(err, "Failed creating %#v", n)
}
}

return nil
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/mosconfig/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,16 @@ func pingRepo(port int) error {
url := fmt.Sprintf("127.0.0.1:%d", port)
return PingRepo(url)
}

func pathForPartition(diskPath string, ptnum uint) string {
base := filepath.Base(diskPath)
sep := ""
for _, pre := range []string{"loop", "nvme", "nbd"} {
if strings.HasPrefix(base, pre) {
sep = "p"
break
}
}

return diskPath + sep + fmt.Sprintf("%d", ptnum)
}

0 comments on commit da53040

Please sign in to comment.