| 1 | |
| 2 | |
| 3 | |
| 4 | package macos |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "os" |
| 11 | "path/filepath" |
| 12 | "time" |
| 13 | |
| 14 | "github.com/sirupsen/logrus" |
| 15 | |
| 16 | "github.com/lima-vm/lima/v2/pkg/apfs" |
| 17 | "github.com/lima-vm/lima/v2/pkg/imgutil/nativeimgutil/asifutil" |
| 18 | "github.com/lima-vm/lima/v2/pkg/limatype/filenames" |
| 19 | "github.com/lima-vm/lima/v2/pkg/osutil" |
| 20 | ) |
| 21 | |
| 22 | |
| 23 | |
| 24 | func attachImageWithRetry(ctx context.Context, disk string, retry int) (*asifutil.AttachedDisk, error) { |
| 25 | var ( |
| 26 | attached *asifutil.AttachedDisk |
| 27 | err error |
| 28 | ) |
| 29 | for range retry { |
| 30 | attached, err = asifutil.DiskutilImageAttachNoMount(ctx, disk) |
| 31 | if !errors.Is(err, asifutil.ErrResourceTemporarilyUnavailable) { |
| 32 | break |
| 33 | } |
| 34 | time.Sleep(3 * time.Second) |
| 35 | } |
| 36 | return attached, err |
| 37 | } |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | func Patch(ctx context.Context, disk string) error { |
| 44 | if err := patchWriteGuestFiles(ctx, disk); err != nil { |
| 45 | return err |
| 46 | } |
| 47 | return patchFixOwnership(ctx, disk) |
| 48 | } |
| 49 | |
| 50 | |
| 51 | |
| 52 | func patchWriteGuestFiles(ctx context.Context, disk string) error { |
| 53 | attached, err := attachImageWithRetry(ctx, disk, 3) |
| 54 | if err != nil { |
| 55 | return fmt.Errorf("failed to attach disk: %w", err) |
| 56 | } |
| 57 | if attached == nil || attached.Data == "" { |
| 58 | return errors.New("failed to find data slice in attached disk") |
| 59 | } |
| 60 | dataDevPath := "/dev/" + attached.Data |
| 61 | defer func() { |
| 62 | |
| 63 | if err := asifutil.DetachASIF(dataDevPath); err != nil { |
| 64 | logrus.WithError(err).Warnf("failed to detach %#q (%#q)", dataDevPath, disk) |
| 65 | } |
| 66 | }() |
| 67 | |
| 68 | instDir := filepath.Dir(disk) |
| 69 | mnt := filepath.Join(instDir, filenames.MntDir) |
| 70 | if err := os.MkdirAll(mnt, 0o755); err != nil { |
| 71 | return fmt.Errorf("failed to create mount point %#q: %w", mnt, err) |
| 72 | } |
| 73 | defer func() { |
| 74 | if err := os.Remove(mnt); err != nil { |
| 75 | logrus.WithError(err).Warnf("failed to remove mount point %#q", mnt) |
| 76 | } |
| 77 | }() |
| 78 | return writeGuestFiles(ctx, dataDevPath, mnt) |
| 79 | } |
| 80 | |
| 81 | |
| 82 | |
| 83 | func patchFixOwnership(ctx context.Context, disk string) error { |
| 84 | attached, err := attachImageWithRetry(ctx, disk, 3) |
| 85 | if err != nil { |
| 86 | return fmt.Errorf("failed to attach disk for ownership fix: %w", err) |
| 87 | } |
| 88 | if attached == nil || attached.Data == "" { |
| 89 | return errors.New("failed to find data slice in attached disk") |
| 90 | } |
| 91 | dataDevPath := "/dev/" + attached.Data |
| 92 | defer func() { |
| 93 | if err := asifutil.DetachASIF(dataDevPath); err != nil { |
| 94 | logrus.WithError(err).Warnf("failed to detach %#q (%#q)", dataDevPath, disk) |
| 95 | } |
| 96 | }() |
| 97 | |
| 98 | if attached.Container == "" { |
| 99 | return errors.New("diskutil did not report an APFS container device") |
| 100 | } |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | containerDev := "/dev/r" + attached.Container |
| 107 | if err = apfs.Chown(containerDev, apfs.VolRoleData, 0, 0, |
| 108 | "private/var/db/.AppleSetupDone", |
| 109 | "Library/User Template/.skipbuddy", |
| 110 | "usr/local/sbin", |
| 111 | "usr/local/sbin/lima-macos-init.sh", |
| 112 | "Library/LaunchDaemons/io.lima-vm.lima-macos-init.plist", |
| 113 | ); err != nil { |
| 114 | return fmt.Errorf("failed to fix file ownership on disk: %w", err) |
| 115 | } |
| 116 | |
| 117 | return nil |
| 118 | } |
| 119 | |
| 120 | |
| 121 | |
| 122 | func writeGuestFiles(ctx context.Context, dataSliceDevice, mnt string) error { |
| 123 | |
| 124 | if err := osutil.Mount(ctx, "apfs", dataSliceDevice, mnt, []string{"noowners"}); err != nil { |
| 125 | return err |
| 126 | } |
| 127 | defer func() { |
| 128 | if err := osutil.Umount(ctx, mnt); err != nil { |
| 129 | logrus.WithError(err).Warnf("failed to unmount %#q", mnt) |
| 130 | } |
| 131 | }() |
| 132 | |
| 133 | filesToTouch := []string{ |
| 134 | filepath.Join(mnt, "private/var/db/.AppleSetupDone"), |
| 135 | filepath.Join(mnt, "Library/User Template/.skipbuddy"), |
| 136 | } |
| 137 | for _, file := range filesToTouch { |
| 138 | if err := osutil.Touch(file); err != nil { |
| 139 | return fmt.Errorf("failed to touch %#q: %w", file, err) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | const initSh = `#!/bin/sh |
| 144 | set -eux |
| 145 | date |
| 146 | if [ ! -e /Volumes/cidata ]; then |
| 147 | echo "/Volumes/cidata is not mounted" >&2 |
| 148 | exit 1 |
| 149 | fi |
| 150 | exec /Volumes/cidata/lima-guestagent fake-cloud-init |
| 151 | ` |
| 152 | if err := os.MkdirAll(filepath.Join(mnt, "usr/local/sbin"), 0o755); err != nil { |
| 153 | return err |
| 154 | } |
| 155 | if err := os.WriteFile(filepath.Join(mnt, "usr/local/sbin/lima-macos-init.sh"), []byte(initSh), 0o755); err != nil { |
| 156 | return err |
| 157 | } |
| 158 | |
| 159 | const plist = `<?xml version="1.0" encoding="UTF-8"?> |
| 160 | <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 161 | <plist version="1.0"> |
| 162 | <dict> |
| 163 | <key>Label</key> |
| 164 | <string>io.lima-vm.lima-macos-init</string> |
| 165 | <key>ProgramArguments</key> |
| 166 | <array> |
| 167 | <string>/usr/local/sbin/lima-macos-init.sh</string> |
| 168 | </array> |
| 169 | <key>RunAtLoad</key> |
| 170 | <true/> |
| 171 | <key>StandardOutPath</key> |
| 172 | <string>/dev/tty.virtio</string> |
| 173 | <key>StandardErrorPath</key> |
| 174 | <string>/dev/tty.virtio</string> |
| 175 | </dict> |
| 176 | </plist> |
| 177 | ` |
| 178 | if err := os.WriteFile(filepath.Join(mnt, "Library/LaunchDaemons/io.lima-vm.lima-macos-init.plist"), []byte(plist), 0o755); err != nil { |
| 179 | return err |
| 180 | } |
| 181 | return nil |
| 182 | } |