| 1 | |
| 2 | |
| 3 | |
| 4 | package driverutil |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "os" |
| 11 | "path/filepath" |
| 12 | |
| 13 | "github.com/docker/go-units" |
| 14 | "github.com/lima-vm/go-qcow2reader/image" |
| 15 | "github.com/sirupsen/logrus" |
| 16 | |
| 17 | "github.com/lima-vm/lima/v2/pkg/imgutil/nativeimgutil" |
| 18 | "github.com/lima-vm/lima/v2/pkg/imgutil/proxyimgutil" |
| 19 | "github.com/lima-vm/lima/v2/pkg/iso9660util" |
| 20 | "github.com/lima-vm/lima/v2/pkg/limatype/filenames" |
| 21 | "github.com/lima-vm/lima/v2/pkg/osutil" |
| 22 | ) |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | func MigrateDiskLayout(instDir string) error { |
| 28 | diskPath := filepath.Join(instDir, filenames.Disk) |
| 29 | if osutil.FileExists(diskPath) { |
| 30 | return nil |
| 31 | } |
| 32 | |
| 33 | diffDiskPath := filepath.Join(instDir, filenames.DiffDiskLegacy) |
| 34 | if osutil.FileExists(diffDiskPath) { |
| 35 | logrus.Infof("Creating symlink %#q -> %#q", filenames.Disk, filenames.DiffDiskLegacy) |
| 36 | if err := os.Symlink(filenames.DiffDiskLegacy, diskPath); err != nil { |
| 37 | return fmt.Errorf("failed to symlink %#q to %#q: %w", filenames.Disk, filenames.DiffDiskLegacy, err) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | baseDiskPath := filepath.Join(instDir, filenames.BaseDiskLegacy) |
| 42 | isoPath := filepath.Join(instDir, filenames.ISO) |
| 43 | if osutil.FileExists(baseDiskPath) && !osutil.FileExists(isoPath) { |
| 44 | isISO, err := iso9660util.IsISO9660(baseDiskPath) |
| 45 | if err != nil { |
| 46 | return err |
| 47 | } |
| 48 | if isISO { |
| 49 | logrus.Infof("Creating symlink %#q -> %#q", filenames.ISO, filenames.BaseDiskLegacy) |
| 50 | if err := os.Symlink(filenames.BaseDiskLegacy, isoPath); err != nil { |
| 51 | return fmt.Errorf("failed to symlink %#q to %#q: %w", filenames.ISO, filenames.BaseDiskLegacy, err) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | } |
| 56 | |
| 57 | return nil |
| 58 | } |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | func EnsureDisk(ctx context.Context, instDir, diskSize string, diskImageFormat image.Type) error { |
| 64 | diskPath := filepath.Join(instDir, filenames.Disk) |
| 65 | if _, err := os.Stat(diskPath); err == nil || !errors.Is(err, os.ErrNotExist) { |
| 66 | return err |
| 67 | } |
| 68 | |
| 69 | imagePath := filepath.Join(instDir, filenames.Image) |
| 70 | isISO, err := iso9660util.IsISO9660(imagePath) |
| 71 | if err != nil { |
| 72 | return err |
| 73 | } |
| 74 | |
| 75 | diskSizeInBytes, _ := units.RAMInBytes(diskSize) |
| 76 | diskUtil := proxyimgutil.NewDiskUtil(ctx) |
| 77 | |
| 78 | if isISO { |
| 79 | isoPath := filepath.Join(instDir, filenames.ISO) |
| 80 | if err := os.Rename(imagePath, isoPath); err != nil { |
| 81 | return err |
| 82 | } |
| 83 | f, err := os.Create(diskPath) |
| 84 | if err != nil { |
| 85 | _ = os.Rename(isoPath, imagePath) |
| 86 | return err |
| 87 | } |
| 88 | if err := f.Close(); err != nil { |
| 89 | os.Remove(diskPath) |
| 90 | _ = os.Rename(isoPath, imagePath) |
| 91 | return err |
| 92 | } |
| 93 | if err := diskUtil.Convert(ctx, diskImageFormat, diskPath, diskPath, &diskSizeInBytes, false); err != nil { |
| 94 | os.Remove(diskPath) |
| 95 | _ = os.Rename(isoPath, imagePath) |
| 96 | return fmt.Errorf("failed to create disk %#q: %w", diskPath, err) |
| 97 | } |
| 98 | } else { |
| 99 | format, err := nativeimgutil.DetectFormat(imagePath) |
| 100 | if err != nil { |
| 101 | return err |
| 102 | } |
| 103 | |
| 104 | |
| 105 | if format == string(diskImageFormat) { |
| 106 | if err = os.Rename(imagePath, diskPath); err != nil { |
| 107 | return fmt.Errorf("failed to rename %#q to %#q: %w", imagePath, diskPath, err) |
| 108 | } |
| 109 | if err := os.Chmod(diskPath, 0o644); err != nil { |
| 110 | return fmt.Errorf("failed to chmod %#q: %w", diskPath, err) |
| 111 | } |
| 112 | } else { |
| 113 | if err := diskUtil.Convert(ctx, diskImageFormat, imagePath, diskPath, &diskSizeInBytes, false); err != nil { |
| 114 | return fmt.Errorf("failed to convert %#q to %#q: %w", imagePath, diskPath, err) |
| 115 | } |
| 116 | os.Remove(imagePath) |
| 117 | } |
| 118 | } |
| 119 | return nil |
| 120 | } |