| 1 | |
| 2 | |
| 3 | |
| 4 | package cidata |
| 5 | |
| 6 | import ( |
| 7 | "bytes" |
| 8 | "embed" |
| 9 | "errors" |
| 10 | "fmt" |
| 11 | "io/fs" |
| 12 | "path" |
| 13 | "path/filepath" |
| 14 | "strings" |
| 15 | |
| 16 | "github.com/sethvargo/go-password/password" |
| 17 | |
| 18 | "github.com/lima-vm/lima/v2/pkg/identifiers" |
| 19 | "github.com/lima-vm/lima/v2/pkg/iso9660util" |
| 20 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 21 | "github.com/lima-vm/lima/v2/pkg/limatype/filenames" |
| 22 | "github.com/lima-vm/lima/v2/pkg/textutil" |
| 23 | ) |
| 24 | |
| 25 | |
| 26 | var templateFS embed.FS |
| 27 | |
| 28 | const templateFSRoot = "cidata.TEMPLATE.d" |
| 29 | |
| 30 | |
| 31 | var windowsTemplateFS embed.FS |
| 32 | |
| 33 | const windowsTemplateFSRoot = "wincidata.TEMPLATE.d" |
| 34 | |
| 35 | |
| 36 | |
| 37 | const windowsClientISOLabelPrefix = "CCCOMA_" |
| 38 | |
| 39 | type CACerts struct { |
| 40 | RemoveDefaults *bool |
| 41 | Trusted []Cert |
| 42 | } |
| 43 | |
| 44 | type Cert struct { |
| 45 | Lines []string |
| 46 | } |
| 47 | |
| 48 | type Containerd struct { |
| 49 | System bool |
| 50 | User bool |
| 51 | Archive string |
| 52 | } |
| 53 | type Network struct { |
| 54 | MACAddress string |
| 55 | Interface string |
| 56 | Metric uint32 |
| 57 | } |
| 58 | type Mount struct { |
| 59 | Tag string |
| 60 | MountPoint string |
| 61 | Type string |
| 62 | Options string |
| 63 | } |
| 64 | type BootCmds struct { |
| 65 | Lines []string |
| 66 | } |
| 67 | |
| 68 | type DataFile struct { |
| 69 | FileName string |
| 70 | Overwrite string |
| 71 | Owner string |
| 72 | Path string |
| 73 | Permissions string |
| 74 | } |
| 75 | |
| 76 | type YQProvision struct { |
| 77 | FileName string |
| 78 | Format string |
| 79 | Owner string |
| 80 | Path string |
| 81 | Permissions string |
| 82 | } |
| 83 | |
| 84 | type Disk struct { |
| 85 | Name string |
| 86 | Device string |
| 87 | Format bool |
| 88 | FSType string |
| 89 | FSArgs []string |
| 90 | } |
| 91 | type TemplateArgs struct { |
| 92 | Debug bool |
| 93 | OS limatype.OS |
| 94 | Arch limatype.Arch |
| 95 | Name string |
| 96 | Hostname string |
| 97 | IID string |
| 98 | User string |
| 99 | Comment string |
| 100 | Home string |
| 101 | Shell string |
| 102 | UID uint32 |
| 103 | PasswordlessSudo bool |
| 104 | SSHPubKeys []string |
| 105 | Mounts []Mount |
| 106 | MountType string |
| 107 | Disks []Disk |
| 108 | GuestInstallPrefix string |
| 109 | UpgradePackages bool |
| 110 | Containerd Containerd |
| 111 | Networks []Network |
| 112 | SlirpNICName string |
| 113 | SlirpGateway string |
| 114 | SlirpDNS string |
| 115 | SlirpIPAddress string |
| 116 | UDPDNSLocalPort int |
| 117 | TCPDNSLocalPort int |
| 118 | Env map[string]string |
| 119 | Param map[string]string |
| 120 | BootScripts bool |
| 121 | DataFiles []DataFile |
| 122 | YQProvisions []YQProvision |
| 123 | DNSAddresses []string |
| 124 | CACerts CACerts |
| 125 | HostHomeMountPoint string |
| 126 | BootCmds []BootCmds |
| 127 | RosettaEnabled bool |
| 128 | RosettaBinFmt bool |
| 129 | SkipDefaultDependencyResolution bool |
| 130 | VMType string |
| 131 | VSockPort int |
| 132 | VirtioPort string |
| 133 | Plain bool |
| 134 | TimeZone string |
| 135 | NoCloudInit bool |
| 136 | WindowsInitialPassword string |
| 137 | LegacyBIOS bool |
| 138 | IsWindowsServer bool |
| 139 | TPM bool |
| 140 | SuppressFirstLoginSetup bool |
| 141 | SuppressFirstLoginSetupPlist string |
| 142 | } |
| 143 | |
| 144 | func (t *TemplateArgs) generateWindowsInitialPassword() error { |
| 145 | const pwLen = 16 |
| 146 | |
| 147 | pw, err := password.Generate(pwLen, pwLen/4, 0, false, false) |
| 148 | if err != nil { |
| 149 | return fmt.Errorf("failed to generate password: %w", err) |
| 150 | } |
| 151 | |
| 152 | t.WindowsInitialPassword = pw |
| 153 | return nil |
| 154 | } |
| 155 | |
| 156 | |
| 157 | func (t *TemplateArgs) checkWindowsVersion(instDir string) error { |
| 158 | imagePath := filepath.Join(instDir, filenames.ISO) |
| 159 | label, err := iso9660util.Label(imagePath) |
| 160 | if err != nil { |
| 161 | return fmt.Errorf("failed to get ISO label: %w", err) |
| 162 | } |
| 163 | |
| 164 | t.IsWindowsServer = !strings.HasPrefix(label, windowsClientISOLabelPrefix) |
| 165 | |
| 166 | return nil |
| 167 | } |
| 168 | |
| 169 | func ValidateTemplateArgs(args *TemplateArgs) error { |
| 170 | if err := identifiers.Validate(args.Name); err != nil { |
| 171 | return err |
| 172 | } |
| 173 | |
| 174 | |
| 175 | if args.User == "root" { |
| 176 | return errors.New("field User must not be `root`") |
| 177 | } |
| 178 | if args.UID == 0 { |
| 179 | return errors.New("field UID must not be 0") |
| 180 | } |
| 181 | if args.Home == "" { |
| 182 | return errors.New("field Home must be set") |
| 183 | } |
| 184 | if args.Shell == "" { |
| 185 | return errors.New("field Shell must be set") |
| 186 | } |
| 187 | if len(args.SSHPubKeys) == 0 { |
| 188 | return errors.New("field SSHPubKeys must be set") |
| 189 | } |
| 190 | for i, m := range args.Mounts { |
| 191 | f := m.MountPoint |
| 192 | if !path.IsAbs(f) { |
| 193 | return fmt.Errorf("field mounts[%d] must be absolute, got %#q", i, f) |
| 194 | } |
| 195 | } |
| 196 | return nil |
| 197 | } |
| 198 | |
| 199 | func ExecuteTemplateCloudConfig(args *TemplateArgs) ([]byte, error) { |
| 200 | if err := ValidateTemplateArgs(args); err != nil { |
| 201 | return nil, err |
| 202 | } |
| 203 | |
| 204 | userData, err := templateFS.ReadFile(path.Join(templateFSRoot, "user-data")) |
| 205 | if err != nil { |
| 206 | return nil, err |
| 207 | } |
| 208 | |
| 209 | cloudConfigYaml := string(userData) |
| 210 | return textutil.ExecuteTemplate(cloudConfigYaml, args) |
| 211 | } |
| 212 | |
| 213 | func executeTemplateWalkDir(args *TemplateArgs, fsys fs.FS, root string) ([]iso9660util.Entry, error) { |
| 214 | if err := ValidateTemplateArgs(args); err != nil { |
| 215 | return nil, err |
| 216 | } |
| 217 | |
| 218 | fsys, err := fs.Sub(fsys, root) |
| 219 | if err != nil { |
| 220 | return nil, err |
| 221 | } |
| 222 | |
| 223 | var layout []iso9660util.Entry |
| 224 | walkFn := func(path string, d fs.DirEntry, walkErr error) error { |
| 225 | if walkErr != nil { |
| 226 | return walkErr |
| 227 | } |
| 228 | if d.IsDir() { |
| 229 | return nil |
| 230 | } |
| 231 | if !d.Type().IsRegular() { |
| 232 | return fmt.Errorf("got non-regular file %#q", path) |
| 233 | } |
| 234 | templateB, err := fs.ReadFile(fsys, path) |
| 235 | if err != nil { |
| 236 | return err |
| 237 | } |
| 238 | b, err := textutil.ExecuteTemplate(string(templateB), args) |
| 239 | if err != nil { |
| 240 | return err |
| 241 | } |
| 242 | layout = append(layout, iso9660util.Entry{ |
| 243 | Path: path, |
| 244 | Reader: bytes.NewReader(b), |
| 245 | }) |
| 246 | return nil |
| 247 | } |
| 248 | |
| 249 | if err := fs.WalkDir(fsys, ".", walkFn); err != nil { |
| 250 | return nil, err |
| 251 | } |
| 252 | |
| 253 | return layout, nil |
| 254 | } |
| 255 | |
| 256 | func ExecuteTemplateCIDataISO(args *TemplateArgs) ([]iso9660util.Entry, error) { |
| 257 | return executeTemplateWalkDir(args, templateFS, templateFSRoot) |
| 258 | } |
| 259 | |
| 260 | func ExecuteTemplateWindowsISO(args *TemplateArgs) ([]iso9660util.Entry, error) { |
| 261 | return executeTemplateWalkDir(args, windowsTemplateFS, windowsTemplateFSRoot) |
| 262 | } |