Skip to content
Merged
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
2 changes: 1 addition & 1 deletion internal/chroot/chrootbuild/chrootbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func (chrootBuilder *ChrootBuilder) BuildChrootEnv(targetOs string, targetDist s

log.Infof("Chroot environment build completed successfully")

if _, err = shell.ExecCmd("rm -rf "+chrootEnvPath, true, "", nil); err != nil {
if _, err = shell.ExecCmd("rm -rf "+chrootEnvPath, true, shell.HostPath, nil); err != nil {
log.Errorf("Failed to remove chroot environment build path: %v", err)
return fmt.Errorf("failed to remove chroot environment build path: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/chroot/chrootbuild/chrootbuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (t *testableChrootBuilder) BuildChrootEnv(targetOs, targetDist, targetArch

log.Infof("Chroot environment build completed successfully")

if _, err = shell.ExecCmd("rm -rf "+chrootEnvPath, true, "", nil); err != nil {
if _, err = shell.ExecCmd("rm -rf "+chrootEnvPath, true, shell.HostPath, nil); err != nil {
log.Errorf("Failed to remove chroot environment build path: %v", err)
return fmt.Errorf("failed to remove chroot environment build path: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/chroot/chrootenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (chrootEnv *ChrootEnv) MountChrootPath(hostFullPath, chrootPath, mountFlags
return fmt.Errorf("failed to get chroot host path for %s: %w", chrootPath, err)
}
if _, err := os.Stat(chrootHostPath); os.IsNotExist(err) {
if _, err = shell.ExecCmd("mkdir -p "+chrootHostPath, true, "", nil); err != nil {
if _, err = shell.ExecCmd("mkdir -p "+chrootHostPath, true, shell.HostPath, nil); err != nil {
return fmt.Errorf("failed to create directory %s: %w", chrootHostPath, err)
}
}
Expand Down Expand Up @@ -302,7 +302,7 @@ func (chrootEnv *ChrootEnv) createChrootRepo(targetOs, targetDist string) error
if err != nil {
return fmt.Errorf("failed to get chroot host path for local repo config: %w", err)
}
if _, err := shell.ExecCmd("rm -f "+chrootRepoCongfigPath+"/*", true, "", nil); err != nil {
if _, err := shell.ExecCmd("rm -f "+chrootRepoCongfigPath+"/*", true, shell.HostPath, nil); err != nil {
return fmt.Errorf("failed to remove existing local repo config files: %w", err)
}

Expand All @@ -325,7 +325,7 @@ func (chrootEnv *ChrootEnv) initChrootWorkspace() error {
chrootWorkspace := filepath.Join(chrootEnv.ChrootEnvRoot, "workspace")
chrootEnv.ChrootImageBuildDir = filepath.Join(chrootWorkspace, "imagebuild")
if _, err := os.Stat(chrootEnv.ChrootImageBuildDir); os.IsNotExist(err) {
if _, err = shell.ExecCmd("mkdir -p "+chrootEnv.ChrootImageBuildDir, true, "", nil); err != nil {
if _, err = shell.ExecCmd("mkdir -p "+chrootEnv.ChrootImageBuildDir, true, shell.HostPath, nil); err != nil {
return fmt.Errorf("failed to create directory %s: %w", chrootEnv.ChrootImageBuildDir, err)
}
}
Expand Down
10 changes: 5 additions & 5 deletions internal/chroot/deb/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (debInstaller *DebInstaller) cleanupOnError(chrootEnvPath, repoPath string,
return
}

if _, RemoveErr := shell.ExecCmd("rm -rf "+chrootEnvPath, true, "", nil); RemoveErr != nil {
if _, RemoveErr := shell.ExecCmd("rm -rf "+chrootEnvPath, true, shell.HostPath, nil); RemoveErr != nil {
log.Errorf("Failed to remove chroot environment build path: %v", RemoveErr)
*err = fmt.Errorf("operation failed: %w, cleanup errors: %v", *err, RemoveErr)
}
Expand All @@ -62,19 +62,19 @@ func (debInstaller *DebInstaller) UpdateLocalDebRepo(repoPath, targetArch string
metaDataPath := filepath.Join(repoPath,
fmt.Sprintf("dists/stable/main/binary-%s", targetArch), "Packages.gz")
if _, err := os.Stat(metaDataPath); err == nil {
if _, err = shell.ExecCmd("rm -f "+metaDataPath, false, "", nil); err != nil {
if _, err = shell.ExecCmd("rm -f "+metaDataPath, false, shell.HostPath, nil); err != nil {
return fmt.Errorf("failed to remove existing Packages.gz: %w", err)
}
}
metaDataDir := filepath.Dir(metaDataPath)
if _, err := os.Stat(metaDataDir); os.IsNotExist(err) {
if _, err = shell.ExecCmd("mkdir -p "+metaDataDir, false, "", nil); err != nil {
if _, err = shell.ExecCmd("mkdir -p "+metaDataDir, false, shell.HostPath, nil); err != nil {
return fmt.Errorf("failed to create metadata directory %s: %w", metaDataDir, err)
}
}

cmd := fmt.Sprintf("cd %s && sudo dpkg-scanpackages . /dev/null | gzip -9c > %s", repoPath, metaDataPath)
if _, err := shell.ExecCmd(cmd, false, "", nil); err != nil {
if _, err := shell.ExecCmd(cmd, false, shell.HostPath, nil); err != nil {
return fmt.Errorf("failed to create local debian cache repository: %w", err)
}

Expand Down Expand Up @@ -126,7 +126,7 @@ func (debInstaller *DebInstaller) InstallDebPkg(targetOsConfigDir, chrootEnvPath
"-- bookworm %s %s",
pkgListStr, chrootEnvPath, localRepoConfigPath)

if _, err = shell.ExecCmdWithStream(cmd, true, "", nil); err != nil {
if _, err = shell.ExecCmdWithStream(cmd, true, shell.HostPath, nil); err != nil {
log.Errorf("Failed to install debian packages in chroot environment: %v", err)
return fmt.Errorf("failed to install debian packages in chroot environment: %w", err)
}
Expand Down
8 changes: 4 additions & 4 deletions internal/chroot/rpm/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (rpmInstaller *RpmInstaller) cleanupOnError(chrootEnvPath string, err *erro
*err = fmt.Errorf("operation failed: %w, cleanup errors: %v", *err, cleanErr)
return
}
if _, RemoveErr := shell.ExecCmd("rm -rf "+chrootEnvPath, true, "", nil); RemoveErr != nil {
if _, RemoveErr := shell.ExecCmd("rm -rf "+chrootEnvPath, true, shell.HostPath, nil); RemoveErr != nil {
log.Errorf("failed to remove chroot environment build path: %v", RemoveErr)
*err = fmt.Errorf("operation failed: %w, cleanup errors: %v", *err, RemoveErr)
}
Expand All @@ -57,7 +57,7 @@ func (rpmInstaller *RpmInstaller) cleanupOnError(chrootEnvPath string, err *erro
func (rpmInstaller *RpmInstaller) InstallRpmPkg(targetOs, chrootEnvPath, chrootPkgCacheDir string, allPkgsList []string) (err error) {
chrootRpmDbPath := filepath.Join(chrootEnvPath, "var", "lib", "rpm")
if _, err := os.Stat(chrootRpmDbPath); os.IsNotExist(err) {
if _, err := shell.ExecCmd("mkdir -p "+chrootRpmDbPath, true, "", nil); err != nil {
if _, err := shell.ExecCmd("mkdir -p "+chrootRpmDbPath, true, shell.HostPath, nil); err != nil {
log.Errorf("Failed to create chroot RPM database directory: %v", err)
return fmt.Errorf("failed to create chroot environment directory: %w", err)
}
Expand Down Expand Up @@ -86,7 +86,7 @@ func (rpmInstaller *RpmInstaller) InstallRpmPkg(targetOs, chrootEnvPath, chrootP
cmdStr := fmt.Sprintf("rpm -i -v --nodeps --force --root %s --define '_dbpath /var/lib/rpm' %s",
chrootEnvPath, pkgPath)
var output string
output, err = shell.ExecCmd(cmdStr, true, "", nil)
output, err = shell.ExecCmd(cmdStr, true, shell.HostPath, nil)
if err != nil {
log.Errorf("Failed to install package %s: %v, output: %s", pkg, err, output)
return fmt.Errorf("failed to install package %s: %w, output: %s", pkg, err, output)
Expand All @@ -109,7 +109,7 @@ func (rpmInstaller *RpmInstaller) InstallRpmPkg(targetOs, chrootEnvPath, chrootP
// updateRpmDB updates the RPM database in the chroot environment
func (rpmInstaller *RpmInstaller) updateRpmDB(chrootEnvBuildPath, chrootPkgCacheDir string, rpmList []string) (err error) {
cmdStr := "rpm -E '%{_db_backend}'"
hostRpmDbBackend, err := shell.ExecCmd(cmdStr, false, "", nil)
hostRpmDbBackend, err := shell.ExecCmd(cmdStr, false, shell.HostPath, nil)
if err != nil {
log.Errorf("Failed to get host RPM DB backend: %v", err)
return fmt.Errorf("failed to get host RPM DB backend: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions internal/image/imageboot/imageboot.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ func installGrubWithEfiMode(installRoot, bootUUID, bootPrefix string, template *
}

chmodCmd := fmt.Sprintf("chmod -R 700 %s", filepath.Dir(grubFinalPath))
if _, err = shell.ExecCmd(chmodCmd, true, "", nil); err != nil {
if _, err = shell.ExecCmd(chmodCmd, true, shell.HostPath, nil); err != nil {
log.Errorf("Failed to set permissions for grub configuration directory: %v", err)
return fmt.Errorf("failed to set permissions for grub configuration directory: %w", err)
}

chmodCmd = fmt.Sprintf("chmod 400 %s", grubFinalPath)
if _, err = shell.ExecCmd(chmodCmd, true, "", nil); err != nil {
if _, err = shell.ExecCmd(chmodCmd, true, shell.HostPath, nil); err != nil {
log.Errorf("Failed to set permissions for grub configuration file: %v", err)
return fmt.Errorf("failed to set permissions for grub configuration file: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/image/imageconvert/imageconvert.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func convertImageFile(filePath, imageType string) (string, error) {
return outputFilePath, fmt.Errorf("unsupported image type: %s", imageType)
}

_, err := shell.ExecCmd(cmdStr, false, "", nil)
_, err := shell.ExecCmd(cmdStr, false, shell.HostPath, nil)
if err != nil {
log.Errorf("Failed to convert image file to %s: %v", imageType, err)
return outputFilePath, fmt.Errorf("failed to convert image file to %s: %w", imageType, err)
Expand Down
44 changes: 22 additions & 22 deletions internal/image/imagedisc/imagedisc.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func CreateRawFile(filePath string, fileSize string, sudo bool) error {
}
}
cmd := fmt.Sprintf("fallocate -l %s %s", fileSizeStr, filePath)
if _, err = shell.ExecCmd(cmd, sudo, "", nil); err != nil {
if _, err = shell.ExecCmd(cmd, sudo, shell.HostPath, nil); err != nil {
log.Errorf("Failed to create raw file %s: %v", filePath, err)
return fmt.Errorf("failed to create raw file %s: %w", filePath, err)
}
Expand All @@ -130,7 +130,7 @@ func GetDiskNameFromDiskPath(diskPath string) (string, error) {

func DiskGetHwSectorSize(diskName string) (int, error) {
cmd := fmt.Sprintf("cat /sys/block/%s/queue/hw_sector_size", diskName)
output, err := shell.ExecCmd(cmd, true, "", nil)
output, err := shell.ExecCmd(cmd, true, shell.HostPath, nil)
if err != nil {
log.Errorf("Failed to get hw sector size for disk %s: %v", diskName, err)
return 0, err
Expand All @@ -140,7 +140,7 @@ func DiskGetHwSectorSize(diskName string) (int, error) {

func DiskGetPhysicalBlockSize(diskName string) (int, error) {
cmd := fmt.Sprintf("cat /sys/block/%s/queue/physical_block_size", diskName)
output, err := shell.ExecCmd(cmd, true, "", nil)
output, err := shell.ExecCmd(cmd, true, shell.HostPath, nil)
if err != nil {
log.Errorf("Failed to get physical block size for disk %s: %v", diskName, err)
return 0, err
Expand All @@ -150,7 +150,7 @@ func DiskGetPhysicalBlockSize(diskName string) (int, error) {

func DiskGetDevInfo(diskPath string) (map[string]interface{}, error) {
cmd := fmt.Sprintf("lsblk %s --json --list --output NAME,PATH,PARTTYPE,FSTYPE,UUID,MOUNTPOINT,PARTUUID,PARTLABEL,TYPE", diskPath)
output, err := shell.ExecCmd(cmd, true, "", nil)
output, err := shell.ExecCmd(cmd, true, shell.HostPath, nil)
if err != nil {
log.Errorf("Failed to get device info for disk %s: %v", diskPath, err)
return nil, err
Expand All @@ -174,7 +174,7 @@ func DiskGetDevInfo(diskPath string) (map[string]interface{}, error) {

func DiskGetPartitionsInfo(diskPath string) ([]map[string]interface{}, error) {
cmd := fmt.Sprintf("lsblk %s --json --list --output NAME,PATH,PARTTYPE,FSTYPE,UUID,MOUNTPOINT,PARTUUID,PARTLABEL,TYPE", diskPath)
output, err := shell.ExecCmd(cmd, true, "", nil)
output, err := shell.ExecCmd(cmd, true, shell.HostPath, nil)
if err != nil {
log.Errorf("Failed to get partitions info for disk %s: %v", diskPath, err)
return nil, err
Expand All @@ -198,7 +198,7 @@ func DiskGetPartitionsInfo(diskPath string) ([]map[string]interface{}, error) {

func DiskGetInfo(diskPath string) (map[string]interface{}, error) {
cmd := fmt.Sprintf("fdisk -l %s", diskPath)
output, err := shell.ExecCmd(cmd, true, "", nil)
output, err := shell.ExecCmd(cmd, true, shell.HostPath, nil)
if err != nil {
log.Errorf("Failed to get disk info for disk %s: %v", diskPath, err)
return nil, err
Expand Down Expand Up @@ -272,7 +272,7 @@ func CheckDiskIOStats(diskPath string) (bool, error) {
return false, err
}
cmd := fmt.Sprintf("cat /proc/diskstats | grep %s*", diskName)
output, err := shell.ExecCmd(cmd, true, "", nil)
output, err := shell.ExecCmd(cmd, true, shell.HostPath, nil)
if err != nil {
log.Errorf("Failed to get io stats for disk %s: %v", diskPath, err)
return false, err
Expand Down Expand Up @@ -490,15 +490,15 @@ func diskPartitionCreate(
// Create the partition using sfdisk
cmdStr := fmt.Sprintf("echo '%s' | sudo sfdisk --no-reread --append %s",
sfdiskScript.String(), diskPath)
_, err = shell.ExecCmd(cmdStr, false, "", nil)
_, err = shell.ExecCmd(cmdStr, false, shell.HostPath, nil)
if err != nil {
log.Errorf("Failed to create partition %d on disk %s: %v", partitionNum, diskPath, err)
return "", fmt.Errorf("failed to create partition %d on disk %s: %w", partitionNum, diskPath, err)
}

// Refresh partition table using partx
cmdStr = fmt.Sprintf("partx -u %s", diskPath)
_, err = shell.ExecCmd(cmdStr, true, "", nil)
_, err = shell.ExecCmd(cmdStr, true, shell.HostPath, nil)
if err != nil {
log.Errorf("Failed to refresh partition table after creating partition %d: %v", partitionNum, err)
return "", fmt.Errorf("failed to refresh partition table after creating partition %d: %w", partitionNum, err)
Expand All @@ -514,7 +514,7 @@ func diskPartitionCreate(

if partitionInfo.FsType == "fat32" || partitionInfo.FsType == "fat16" || partitionInfo.FsType == "vfat" {
cmdStr = fmt.Sprintf("mkfs -t vfat %s", diskPartDev)
_, err := shell.ExecCmd(cmdStr, true, "", nil)
_, err := shell.ExecCmd(cmdStr, true, shell.HostPath, nil)
if err != nil {
log.Errorf("Failed to format partition %d with fs type %s: %v", partitionNum, partitionInfo.FsType, err)
return "", fmt.Errorf("failed to format partition %d with fs type %s: %w", partitionNum, partitionInfo.FsType, err)
Expand All @@ -534,20 +534,20 @@ func diskPartitionCreate(
} else {
cmdStr = fmt.Sprintf("mkfs -t %s %s", partitionInfo.FsType, diskPartDev)
}
_, err := shell.ExecCmd(cmdStr, true, "", nil)
_, err := shell.ExecCmd(cmdStr, true, shell.HostPath, nil)
if err != nil {
log.Errorf("Failed to format partition %d with fs type %s: %v", partitionNum, partitionInfo.FsType, err)
return "", fmt.Errorf("failed to format partition %d with fs type %s: %w", partitionNum, partitionInfo.FsType, err)
}
} else if partitionInfo.FsType == "linux-swap" {
cmdStr = fmt.Sprintf("mkswap %s", diskPartDev)
_, err := shell.ExecCmd(cmdStr, true, "", nil)
_, err := shell.ExecCmd(cmdStr, true, shell.HostPath, nil)
if err != nil {
log.Errorf("Failed to format partition %d with fs type %s: %v", partitionNum, partitionInfo.FsType, err)
return "", fmt.Errorf("failed to format partition %d with fs type %s: %w", partitionNum, partitionInfo.FsType, err)
}
cmdStr = fmt.Sprintf("swapon %s", diskPartDev)
_, err = shell.ExecCmd(cmdStr, true, "", nil)
_, err = shell.ExecCmd(cmdStr, true, shell.HostPath, nil)
if err != nil {
log.Errorf("Failed to enable swap on partition %d: %v", partitionNum, err)
return "", fmt.Errorf("failed to enable swap on partition %d: %w", partitionNum, err)
Expand All @@ -563,15 +563,15 @@ func diskPartitionDelete(diskPath string, partitionNum int) error {
return fmt.Errorf("invalid partition number: %d", partitionNum)
}
cmdStr := fmt.Sprintf("sfdisk --delete %s %d", diskPath, partitionNum)
_, err := shell.ExecCmd(cmdStr, true, "", nil)
_, err := shell.ExecCmd(cmdStr, true, shell.HostPath, nil)
if err != nil {
log.Errorf("Failed to delete partition %d: %v", partitionNum, err)
return fmt.Errorf("failed to delete partition %d: %w", partitionNum, err)
}

// Refresh partition table
cmdStr = fmt.Sprintf("partx -d --nr %d %s", partitionNum, diskPath)
_, err = shell.ExecCmd(cmdStr, true, "", nil)
_, err = shell.ExecCmd(cmdStr, true, shell.HostPath, nil)
if err != nil {
// Non-fatal if partition is already gone
log.Warnf("Could not remove partition %d from kernel table: %v", partitionNum, err)
Expand All @@ -597,7 +597,7 @@ func DiskPartitionsCreate(diskPath string, partitionsList []config.PartitionInfo

if partitionTableType == "gpt" {
cmdStr := fmt.Sprintf("echo 'label: gpt' | sudo sfdisk %s", diskPath)
_, err := shell.ExecCmd(cmdStr, false, "", nil)
_, err := shell.ExecCmd(cmdStr, false, shell.HostPath, nil)
if err != nil {
log.Errorf("Failed to create GPT partition table on disk %s: %v", diskPath, err)
return nil, fmt.Errorf("failed to create GPT partition table on disk %s: %w", diskPath, err)
Expand All @@ -622,7 +622,7 @@ func DiskPartitionsCreate(diskPath string, partitionsList []config.PartitionInfo
var partitionNum int
maxPrimaryPartitionsNum := 4
cmdStr := fmt.Sprintf("echo 'label: dos' | sudo sfdisk %s", diskPath)
_, err := shell.ExecCmd(cmdStr, false, "", nil)
_, err := shell.ExecCmd(cmdStr, false, shell.HostPath, nil)
if err != nil {
log.Errorf("Failed to create MBR partition table on disk %s: %v", diskPath, err)
return nil, fmt.Errorf("failed to create MBR partition table on disk %s: %w", diskPath, err)
Expand Down Expand Up @@ -679,7 +679,7 @@ func DiskPartitionsCreate(diskPath string, partitionsList []config.PartitionInfo

func GetPartitionLabel(diskPartDev string) (string, error) {
cmdStr := fmt.Sprintf("blkid %s -s PARTLABEL -o value", diskPartDev)
label, err := shell.ExecCmd(cmdStr, true, "", nil)
label, err := shell.ExecCmd(cmdStr, true, shell.HostPath, nil)
if err != nil {
log.Errorf("Failed to get partition label for %s: %v", diskPartDev, err)
return "", fmt.Errorf("failed to get partition label for %s: %w", diskPartDev, err)
Expand All @@ -689,13 +689,13 @@ func GetPartitionLabel(diskPartDev string) (string, error) {

func WipePartitions(diskPath string) error {
// Wipe filesystem signatures
_, err := shell.ExecCmd(fmt.Sprintf("wipefs -a -f %s", diskPath), true, "", nil)
_, err := shell.ExecCmd(fmt.Sprintf("wipefs -a -f %s", diskPath), true, shell.HostPath, nil)
if err != nil {
log.Errorf("Failed to wipe filesystem signatures on disk %s: %v", diskPath, err)
return fmt.Errorf("failed to wipe disk %s: %w", diskPath, err)
}

_, err = shell.ExecCmd("sync", true, "", nil)
_, err = shell.ExecCmd("sync", true, shell.HostPath, nil)
if err != nil {
log.Errorf("Failed to sync after wiping disk %s: %v", diskPath, err)
return fmt.Errorf("failed to sync after wiping disk %s: %w", diskPath, err)
Expand All @@ -705,7 +705,7 @@ func WipePartitions(diskPath string) error {

func GetUUID(diskPartitionPath string) (string, error) {
cmd := fmt.Sprintf("blkid %s -s UUID -o value", diskPartitionPath)
output, err := shell.ExecCmd(cmd, true, "", nil)
output, err := shell.ExecCmd(cmd, true, shell.HostPath, nil)
if err != nil {
log.Errorf("Failed to get UUID for %s: %v", diskPartitionPath, err)
return output, fmt.Errorf("failed to get partition UUID for %s: %w", diskPartitionPath, err)
Expand All @@ -715,7 +715,7 @@ func GetUUID(diskPartitionPath string) (string, error) {

func GetPartUUID(diskPartitionPath string) (string, error) {
cmd := fmt.Sprintf("blkid %s -s PARTUUID -o value", diskPartitionPath)
output, err := shell.ExecCmd(cmd, true, "", nil)
output, err := shell.ExecCmd(cmd, true, shell.HostPath, nil)
if err != nil {
log.Errorf("Failed to get PARTUUID for %s: %v", diskPartitionPath, err)
return output, fmt.Errorf("failed to get partition UUID for %s: %w", diskPartitionPath, err)
Expand Down
Loading
Loading