Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better handle filesystem optional features. #66

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions toolkit/tools/imagegen/diskutils/diskutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,11 @@ import (
"github.com/microsoft/azurelinux/toolkit/tools/internal/logger"
"github.com/microsoft/azurelinux/toolkit/tools/internal/retry"
"github.com/microsoft/azurelinux/toolkit/tools/internal/shell"
"github.com/microsoft/azurelinux/toolkit/tools/internal/targetos"
"github.com/sirupsen/logrus"
)

var (
// When calling mkfs, the default options change depending on the host OS you are running on and typically match
// what the distro has decided is best for their OS. For example, for ext2/3/4, the defaults are stored in
// /etc/mke2fs.conf.
// However, when building Azure Linux images, the defaults should be as consistent as possible and should only contain
// features that are supported on Azure Linux.
DefaultMkfsOptions = map[string][]string{
"ext2": {"-b", "4096", "-O", "none,sparse_super,large_file,filetype,resize_inode,dir_index,ext_attr"},
"ext3": {"-b", "4096", "-O", "none,sparse_super,large_file,filetype,resize_inode,dir_index,ext_attr,has_journal"},
"ext4": {"-b", "4096", "-O", "none,sparse_super,large_file,filetype,resize_inode,dir_index,ext_attr,has_journal,extent,huge_file,flex_bg,metadata_csum,64bit,dir_nlink,extra_isize"},
}

partedVersionRegex = regexp.MustCompile(`^parted \(GNU parted\) (\d+)\.(\d+)`)

// The default partition name used when the version of `parted` is too old (<3.5).
Expand Down Expand Up @@ -433,7 +423,7 @@ func WaitForDevicesToSettle() error {
}

// CreatePartitions creates partitions on the specified disk according to the disk config
func CreatePartitions(diskDevPath string, disk configuration.Disk, rootEncryption configuration.RootEncryption,
func CreatePartitions(targetOs targetos.TargetOs, diskDevPath string, disk configuration.Disk, rootEncryption configuration.RootEncryption,
cwize1 marked this conversation as resolved.
Show resolved Hide resolved
diskKnownToBeEmpty bool,
) (partDevPathMap map[string]string, partIDToFsTypeMap map[string]string, encryptedRoot EncryptedRootDevice, err error) {
const timeoutInSeconds = "5"
Expand Down Expand Up @@ -492,7 +482,7 @@ func CreatePartitions(diskDevPath string, disk configuration.Disk, rootEncryptio
return partDevPathMap, partIDToFsTypeMap, encryptedRoot, err
}

partFsType, err := FormatSinglePartition(partDevPath, partition)
partFsType, err := formatSinglePartition(targetOs, partDevPath, partition)
if err != nil {
err = fmt.Errorf("failed to format partition:\n%w", err)
return partDevPathMap, partIDToFsTypeMap, encryptedRoot, err
Expand Down Expand Up @@ -785,8 +775,8 @@ func setGptPartitionType(partition configuration.Partition, timeoutInSeconds, di
return
}

// FormatSinglePartition formats the given partition to the type specified in the partition configuration
func FormatSinglePartition(partDevPath string, partition configuration.Partition,
// formatSinglePartition formats the given partition to the type specified in the partition configuration
func formatSinglePartition(targetOs targetos.TargetOs, partDevPath string, partition configuration.Partition,
) (fsType string, err error) {
const (
totalAttempts = 5
Expand All @@ -800,12 +790,16 @@ func FormatSinglePartition(partDevPath string, partition configuration.Partition
// To handle such cases, we can retry the command.
switch fsType {
case "fat32", "fat16", "vfat", "ext2", "ext3", "ext4", "xfs":
mkfsOptions := DefaultMkfsOptions[fsType]

if fsType == "fat32" || fsType == "fat16" {
fsType = "vfat"
}

mkfsOptions, err := getFileSystemOptions(targetOs, fsType)
if err != nil {
err = fmt.Errorf("failed to get mkfs args for filesystem type (%s):\n%w", fsType, err)
return fsType, err
}

mkfsArgs := []string{"-t", fsType}
mkfsArgs = append(mkfsArgs, mkfsOptions...)
mkfsArgs = append(mkfsArgs, partDevPath)
Expand All @@ -821,6 +815,7 @@ func FormatSinglePartition(partDevPath string, partition configuration.Partition
}, totalAttempts, retryDuration)
if err != nil {
err = fmt.Errorf("could not format partition with type %v after %v retries", fsType, totalAttempts)
return "", err
}
case "linux-swap":
err = retry.Run(func() error {
Expand All @@ -833,6 +828,7 @@ func FormatSinglePartition(partDevPath string, partition configuration.Partition
}, totalAttempts, retryDuration)
if err != nil {
err = fmt.Errorf("could not format partition with type %v after %v retries", fsType, totalAttempts)
return "", err
}

_, stderr, err := shell.Execute("swapon", partDevPath)
Expand Down
Loading
Loading