disk: move createFilesystem to disk.go

Move the `createFilesystem` method over the `disk.go` and make it
a public method of `PartitionTable`.
This commit is contained in:
Christian Kellner 2021-11-15 20:49:44 +00:00 committed by Tom Gundersen
parent 7e4097fb6f
commit 9746a87609
2 changed files with 49 additions and 44 deletions

View file

@ -1,11 +1,8 @@
package disk
import (
"encoding/hex"
"io"
"math/rand"
"github.com/google/uuid"
"github.com/osbuild/osbuild-composer/internal/blueprint"
)
@ -38,7 +35,7 @@ func CreatePartitionTable(
for _, m := range mountpoints {
sectors := table.BytesToSectors(m.MinSize)
if m.Mountpoint != "/" {
table.createFilesystem(m.Mountpoint, sectors)
table.CreateFilesystem(m.Mountpoint, sectors)
}
}
@ -62,43 +59,3 @@ func CreatePartitionTable(
return *table, nil
}
func (pt *PartitionTable) createFilesystem(mountpoint string, size uint64) {
filesystem := Filesystem{
Type: "xfs",
Mountpoint: mountpoint,
FSTabOptions: "defaults",
FSTabFreq: 0,
FSTabPassNo: 0,
}
partition := Partition{
Size: size,
Filesystem: &filesystem,
}
if pt.Type == "gpt" {
partition.Type = FilesystemDataGUID
}
pt.Partitions = append(pt.Partitions, partition)
}
func newRandomUUIDFromReader(r io.Reader) (uuid.UUID, error) {
var id uuid.UUID
_, err := io.ReadFull(r, id[:])
if err != nil {
return uuid.Nil, err
}
id[6] = (id[6] & 0x0f) | 0x40 // Version 4
id[8] = (id[8] & 0x3f) | 0x80 // Variant is 10
return id, nil
}
// NewRandomVolIDFromReader creates a random 32 bit hex string to use as a
// volume ID for FAT filesystems
func NewRandomVolIDFromReader(r io.Reader) (string, error) {
volid := make([]byte, 4)
_, err := r.Read(volid)
return hex.EncodeToString(volid), err
}

View file

@ -5,7 +5,9 @@
package disk
import (
"encoding/hex"
"errors"
"io"
"math/rand"
"sort"
@ -269,6 +271,29 @@ func (pt *PartitionTable) BootFilesystem() *Filesystem {
return pt.FindFilesystemForMountpoint("/boot")
}
// Create a new filesystem within the partition table at the given mountpoint
// with the given minimum size in sectors.
func (pt *PartitionTable) CreateFilesystem(mountpoint string, size uint64) {
filesystem := Filesystem{
Type: "xfs",
Mountpoint: mountpoint,
FSTabOptions: "defaults",
FSTabFreq: 0,
FSTabPassNo: 0,
}
partition := Partition{
Size: size,
Filesystem: &filesystem,
}
if pt.Type == "gpt" {
partition.Type = FilesystemDataGUID
}
pt.Partitions = append(pt.Partitions, partition)
}
// Generate all needed UUIDs for all the partiton and filesystems
//
// Will not overwrite existing UUIDs and only generate UUIDs for
@ -365,3 +390,26 @@ func (fs *Filesystem) QEMUFilesystem() osbuild.QEMUFilesystem {
Mountpoint: fs.Mountpoint,
}
}
// uuid generator helpers
// GeneratesnewRandomUUIDFromReader generates a new random UUID (version
// 4 using) via the given random number generator.
func newRandomUUIDFromReader(r io.Reader) (uuid.UUID, error) {
var id uuid.UUID
_, err := io.ReadFull(r, id[:])
if err != nil {
return uuid.Nil, err
}
id[6] = (id[6] & 0x0f) | 0x40 // Version 4
id[8] = (id[8] & 0x3f) | 0x80 // Variant is 10
return id, nil
}
// NewRandomVolIDFromReader creates a random 32 bit hex string to use as a
// volume ID for FAT filesystems
func NewRandomVolIDFromReader(r io.Reader) (string, error) {
volid := make([]byte, 4)
_, err := r.Read(volid)
return hex.EncodeToString(volid), err
}