Blame internal/disk/disk.go

Packit Service 509fd4
// Disk package contains abstract data-types to define disk-related entities.
Packit Service 509fd4
//
Packit Service 509fd4
// PartitionTable, Partition and Filesystem types are currently defined.
Packit Service 509fd4
// All of them can be 1:1 converted to osbuild.QEMUAssemblerOptions.
Packit Service 509fd4
package disk
Packit Service 509fd4
Packit Service 509fd4
import (
Packit Service 509fd4
	"sort"
Packit Service 509fd4
Packit Service 15f37d
	osbuild "github.com/osbuild/osbuild-composer/internal/osbuild1"
Packit Service 509fd4
)
Packit Service 509fd4
Packit Service 509fd4
type PartitionTable struct {
Packit Service 509fd4
	// Size of the disk.
Packit Service 509fd4
	Size uint64
Packit Service 509fd4
	UUID string
Packit Service 509fd4
	// Partition table type, e.g. dos, gpt.
Packit Service 509fd4
	Type       string
Packit Service 509fd4
	Partitions []Partition
Packit Service 509fd4
}
Packit Service 509fd4
Packit Service 509fd4
type Partition struct {
Packit Service 509fd4
	Start    uint64
Packit Service 509fd4
	Size     uint64
Packit Service 509fd4
	Type     string
Packit Service 509fd4
	Bootable bool
Packit Service 509fd4
	// ID of the partition, dos doesn't use traditional UUIDs, therefore this
Packit Service 509fd4
	// is just a string.
Packit Service 509fd4
	UUID string
Packit Service 509fd4
	// If nil, the partition is raw; It doesn't contain a filesystem.
Packit Service 509fd4
	Filesystem *Filesystem
Packit Service 509fd4
}
Packit Service 509fd4
Packit Service 509fd4
type Filesystem struct {
Packit Service 509fd4
	Type string
Packit Service 509fd4
	// ID of the filesystem, vfat doesn't use traditional UUIDs, therefore this
Packit Service 509fd4
	// is just a string.
Packit Service 509fd4
	UUID       string
Packit Service 509fd4
	Label      string
Packit Service 509fd4
	Mountpoint string
Packit Service 509fd4
	// The fourth field of fstab(5); fs_mntops
Packit Service 509fd4
	FSTabOptions string
Packit Service 509fd4
	// The fifth field of fstab(5); fs_freq
Packit Service 509fd4
	FSTabFreq uint64
Packit Service 509fd4
	// The sixth field of fstab(5); fs_passno
Packit Service 509fd4
	FSTabPassNo uint64
Packit Service 509fd4
}
Packit Service 509fd4
Packit Service 509fd4
// Converts PartitionTable to osbuild.QEMUAssemblerOptions that encode
Packit Service 509fd4
// the same partition table.
Packit Service 509fd4
func (pt PartitionTable) QEMUAssemblerOptions() osbuild.QEMUAssemblerOptions {
Packit Service 509fd4
	var partitions []osbuild.QEMUPartition
Packit Service 509fd4
	for _, p := range pt.Partitions {
Packit Service 509fd4
		partitions = append(partitions, p.QEMUPartition())
Packit Service 509fd4
	}
Packit Service 509fd4
Packit Service 509fd4
	return osbuild.QEMUAssemblerOptions{
Packit Service 509fd4
		Size:       pt.Size,
Packit Service 509fd4
		PTUUID:     pt.UUID,
Packit Service 509fd4
		PTType:     pt.Type,
Packit Service 509fd4
		Partitions: partitions,
Packit Service 509fd4
	}
Packit Service 509fd4
}
Packit Service 509fd4
Packit Service 509fd4
// Generates org.osbuild.fstab stage options from this partition table.
Packit Service 509fd4
func (pt PartitionTable) FSTabStageOptions() *osbuild.FSTabStageOptions {
Packit Service 509fd4
	var options osbuild.FSTabStageOptions
Packit Service 509fd4
	for _, p := range pt.Partitions {
Packit Service 509fd4
		fs := p.Filesystem
Packit Service 509fd4
		if fs == nil {
Packit Service 509fd4
			continue
Packit Service 509fd4
		}
Packit Service 509fd4
Packit Service 509fd4
		options.AddFilesystem(fs.UUID, fs.Type, fs.Mountpoint, fs.FSTabOptions, fs.FSTabFreq, fs.FSTabPassNo)
Packit Service 509fd4
	}
Packit Service 509fd4
Packit Service 509fd4
	// sort the entries by PassNo to maintain backward compatibility
Packit Service 509fd4
	sort.Slice(options.FileSystems, func(i, j int) bool {
Packit Service 509fd4
		return options.FileSystems[i].PassNo < options.FileSystems[j].PassNo
Packit Service 509fd4
	})
Packit Service 509fd4
Packit Service 509fd4
	return &options
Packit Service 509fd4
}
Packit Service 509fd4
Packit Service 509fd4
// Returns the root partition (the partition whose filesystem has / as
Packit Service 509fd4
// a mountpoint) of the partition table. Nil is returned if there's no such
Packit Service 509fd4
// partition.
Packit Service 509fd4
func (pt PartitionTable) RootPartition() *Partition {
Packit Service 509fd4
	for _, p := range pt.Partitions {
Packit Service 509fd4
		if p.Filesystem == nil {
Packit Service 509fd4
			continue
Packit Service 509fd4
		}
Packit Service 509fd4
Packit Service 509fd4
		if p.Filesystem.Mountpoint == "/" {
Packit Service 509fd4
			return &p
Packit Service 509fd4
		}
Packit Service 509fd4
	}
Packit Service 509fd4
Packit Service 509fd4
	return nil
Packit Service 509fd4
}
Packit Service 509fd4
Packit Service 509fd4
// Converts Partition to osbuild.QEMUPartition that encodes the same partition.
Packit Service 509fd4
func (p Partition) QEMUPartition() osbuild.QEMUPartition {
Packit Service 509fd4
	var fs *osbuild.QEMUFilesystem
Packit Service 509fd4
	if p.Filesystem != nil {
Packit Service 509fd4
		f := p.Filesystem.QEMUFilesystem()
Packit Service 509fd4
		fs = &f
Packit Service 509fd4
	}
Packit Service 509fd4
	return osbuild.QEMUPartition{
Packit Service 509fd4
		Start:      p.Start,
Packit Service 509fd4
		Size:       p.Size,
Packit Service 509fd4
		Type:       p.Type,
Packit Service 509fd4
		Bootable:   p.Bootable,
Packit Service 509fd4
		UUID:       p.UUID,
Packit Service 509fd4
		Filesystem: fs,
Packit Service 509fd4
	}
Packit Service 509fd4
}
Packit Service 509fd4
Packit Service 509fd4
// Converts Filesystem to osbuild.QEMUFilesystem that encodes the same fs.
Packit Service 509fd4
func (fs Filesystem) QEMUFilesystem() osbuild.QEMUFilesystem {
Packit Service 509fd4
	return osbuild.QEMUFilesystem{
Packit Service 509fd4
		Type:       fs.Type,
Packit Service 509fd4
		UUID:       fs.UUID,
Packit Service 509fd4
		Label:      fs.Label,
Packit Service 509fd4
		Mountpoint: fs.Mountpoint,
Packit Service 509fd4
	}
Packit Service 509fd4
}