Skip to content

Commit 4983fb0

Browse files
authored
Merge branch 'lima-vm:master' into master
2 parents de1cb71 + df7ed15 commit 4983fb0

File tree

24 files changed

+110
-86
lines changed

24 files changed

+110
-86
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939
- name: Verify generated files
4040
run: make install-tools generate check-generated
4141
- name: Run golangci-lint
42-
uses: golangci/golangci-lint-action@dec74fa03096ff515422f71d18d41307cacde373 # v7.0.0
42+
uses: golangci/golangci-lint-action@a5307c8f68bcd21ff73283f8629eb09caa60d7a7 # v7.0.0
4343
with:
4444
version: v2.0.1
4545
args: --verbose
@@ -50,7 +50,7 @@ jobs:
5050
sudo apt-get update
5151
sudo apt-get install -y shellcheck
5252
- name: Run file and directory name linter
53-
uses: ls-lint/action@1887e6c0e7f2dfa81a2d67591f0eb7782720026f # v2.2.3
53+
uses: ls-lint/action@acc185de566968dd9b3ffc1b79dfc86e22306442 # v2.3.0
5454
- name: Run shellcheck
5555
run: find . -name '*.sh' | xargs shellcheck
5656
- name: Install shfmt

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ linters:
1717
- godot
1818
- govet
1919
- ineffassign
20+
- intrange
2021
- misspell
2122
- nakedret
2223
- noctx

cmd/limactl/list.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ func fieldNames() []string {
2222
names := []string{}
2323
var data store.FormatData
2424
t := reflect.TypeOf(data)
25-
for i := 0; i < t.NumField(); i++ {
25+
for i := range t.NumField() {
2626
f := t.Field(i)
2727
if f.Anonymous {
28-
for j := 0; j < f.Type.NumField(); j++ {
28+
for j := range f.Type.NumField() {
2929
names = append(names, f.Type.Field(j).Name)
3030
}
3131
} else {

cmd/limactl/main.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func newApp() *cobra.Command {
7878
rootCmd.PersistentFlags().Bool("debug", false, "debug mode")
7979
// TODO: "survey" does not support using cygwin terminal on windows yet
8080
rootCmd.PersistentFlags().Bool("tty", isatty.IsTerminal(os.Stdout.Fd()), "Enable TUI interactions such as opening an editor. Defaults to true when stdout is a terminal. Set to false for automation.")
81+
rootCmd.PersistentFlags().BoolP("yes", "y", false, "Alias of --tty=false")
8182
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, _ []string) error {
8283
l, _ := cmd.Flags().GetString("log-level")
8384
if l != "" {
@@ -139,6 +140,22 @@ func newApp() *cobra.Command {
139140
if nfs {
140141
return errors.New("must not run on NFS dir")
141142
}
143+
144+
if cmd.Flags().Changed("yes") && cmd.Flags().Changed("tty") {
145+
return errors.New("cannot use both --tty and --yes flags at the same time")
146+
}
147+
148+
if cmd.Flags().Changed("yes") {
149+
// Sets the value of the yesValue flag by using the yes flag.
150+
yesValue, _ := cmd.Flags().GetBool("yes")
151+
if yesValue {
152+
// Sets to the default value false
153+
err := cmd.Flags().Set("tty", "false")
154+
if err != nil {
155+
return err
156+
}
157+
}
158+
}
142159
return nil
143160
}
144161
rootCmd.AddGroup(&cobra.Group{ID: "basic", Title: "Basic Commands:"})

cmd/limactl/prune.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package main
55

66
import (
7+
"maps"
78
"os"
89

910
"github.com/lima-vm/lima/pkg/downloader"
@@ -73,9 +74,7 @@ func knownLocations() (map[string]limayaml.File, error) {
7374
if err != nil {
7475
return nil, err
7576
}
76-
for k, v := range locationsFromLimaYAML(instance.Config) {
77-
locations[k] = v
78-
}
77+
maps.Copy(locations, locationsFromLimaYAML(instance.Config))
7978
}
8079

8180
// Collect locations from templates
@@ -92,9 +91,7 @@ func knownLocations() (map[string]limayaml.File, error) {
9291
if err != nil {
9392
return nil, err
9493
}
95-
for k, v := range locationsFromLimaYAML(y) {
96-
locations[k] = v
97-
}
94+
maps.Copy(locations, locationsFromLimaYAML(y))
9895
}
9996
return locations, nil
10097
}

pkg/cidata/cidata.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"errors"
99
"fmt"
1010
"io"
11+
"maps"
1112
"net"
1213
"net/url"
1314
"os"
@@ -50,9 +51,7 @@ func setupEnv(instConfigEnv map[string]string, propagateProxyEnv bool, slirpGate
5051
return env, err
5152
}
5253
// env.* settings from lima.yaml override system settings without giving a warning
53-
for name, value := range instConfigEnv {
54-
env[name] = value
55-
}
54+
maps.Copy(env, instConfigEnv)
5655
// Current process environment setting override both system settings and env.*
5756
lowerVars := []string{"ftp_proxy", "http_proxy", "https_proxy", "no_proxy"}
5857
upperVars := make([]string, len(lowerVars))

pkg/downloader/downloader_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func TestDownloadRemote(t *testing.T) {
9191
t.Run("parallel", func(t *testing.T) {
9292
cacheDir := filepath.Join(t.TempDir(), "cache")
9393
results := make(chan downloadResult, parallelDownloads)
94-
for i := 0; i < parallelDownloads; i++ {
94+
for range parallelDownloads {
9595
go func() {
9696
// Parallel download is supported only for different instances with unique localPath.
9797
localPath := filepath.Join(t.TempDir(), t.Name())
@@ -131,7 +131,7 @@ func TestDownloadRemote(t *testing.T) {
131131
t.Run("parallel", func(t *testing.T) {
132132
cacheDir := filepath.Join(t.TempDir(), "cache")
133133
results := make(chan downloadResult, parallelDownloads)
134-
for i := 0; i < parallelDownloads; i++ {
134+
for range parallelDownloads {
135135
go func() {
136136
r, err := Download(context.Background(), "", dummyRemoteFileURL,
137137
WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
@@ -177,7 +177,7 @@ func TestDownloadRemote(t *testing.T) {
177177

178178
func countResults(t *testing.T, results chan downloadResult) (downloaded, cached int) {
179179
t.Helper()
180-
for i := 0; i < parallelDownloads; i++ {
180+
for range parallelDownloads {
181181
result := <-results
182182
if result.err != nil {
183183
t.Errorf("Download failed: %s", result.err)

pkg/guestagent/procnettcp/procnettcp.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func ParseWithEndian(r io.Reader, kind Kind, isBE bool) ([]Entry, error) {
6565
fields := strings.Fields(line)
6666
switch i {
6767
case 0:
68-
for j := 0; j < len(fields); j++ {
68+
for j := range fields {
6969
fieldNames[fields[j]] = j
7070
}
7171
if _, ok := fieldNames["local_address"]; !ok {
@@ -135,18 +135,18 @@ func ParseAddressWithEndian(s string, isBE bool) (net.IP, uint16, error) {
135135
}
136136

137137
ipBytes := make([]byte, len(split[0])/2) // 4 bytes (8 chars) or 16 bytes (32 chars)
138-
for i := 0; i < len(split[0])/8; i++ {
138+
for i := range len(split[0]) / 8 {
139139
quartet := split[0][8*i : 8*(i+1)]
140140
quartetB, err := hex.DecodeString(quartet) // surprisingly little endian, per 4 bytes, on little endian hosts
141141
if err != nil {
142142
return nil, 0, fmt.Errorf("unparsable address %q: unparsable quartet %q: %w", s, quartet, err)
143143
}
144144
if isBE {
145-
for j := 0; j < len(quartetB); j++ {
145+
for j := range quartetB {
146146
ipBytes[4*i+j] = quartetB[j]
147147
}
148148
} else {
149-
for j := 0; j < len(quartetB); j++ {
149+
for j := range quartetB {
150150
ipBytes[4*i+len(quartetB)-1-j] = quartetB[j]
151151
}
152152
}

pkg/hostagent/requirements.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (a *HostAgent) waitForRequirements(label string, requirements []requirement
2323

2424
for i, req := range requirements {
2525
retryLoop:
26-
for j := 0; j < retries; j++ {
26+
for j := range retries {
2727
logrus.Infof("Waiting for the %s requirement %d of %d: %q", label, i+1, len(requirements), req.description)
2828
err := a.waitForRequirement(req)
2929
if err == nil {

pkg/limatmpl/embed_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ func RunEmbedTest(t *testing.T, tc embedTestCase) {
437437
assert.NilError(t, err, tc.description)
438438
}
439439
tmpl := &limatmpl.Template{
440-
Bytes: []byte(fmt.Sprintf("base: base0.yaml\n%s", tc.template)),
440+
Bytes: fmt.Appendf(nil, "base: base0.yaml\n%s", tc.template),
441441
Locator: "tmpl.yaml",
442442
}
443443
// Don't include `base` property if base0 is a script

pkg/limatmpl/locator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ images:
191191
- location: %q
192192
arch: %q
193193
`
194-
tmpl.Bytes = []byte(fmt.Sprintf(template, imageArch, locator, imageArch))
194+
tmpl.Bytes = fmt.Appendf(nil, template, imageArch, locator, imageArch)
195195
tmpl.Name = InstNameFromImageURL(locator, imageArch)
196196
return true
197197
}

pkg/limayaml/defaults.go

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
_ "embed"
1010
"errors"
1111
"fmt"
12+
"maps"
1213
"net"
1314
"os"
1415
"os/user"
@@ -470,15 +471,9 @@ func FillDefault(y, d, o *LimaYAML, filePath string, warn bool) {
470471

471472
hosts := make(map[string]string)
472473
// Values can be either names or IP addresses. Name values are canonicalized in the hostResolver.
473-
for k, v := range d.HostResolver.Hosts {
474-
hosts[k] = v
475-
}
476-
for k, v := range y.HostResolver.Hosts {
477-
hosts[k] = v
478-
}
479-
for k, v := range o.HostResolver.Hosts {
480-
hosts[k] = v
481-
}
474+
maps.Copy(hosts, d.HostResolver.Hosts)
475+
maps.Copy(hosts, y.HostResolver.Hosts)
476+
maps.Copy(hosts, o.HostResolver.Hosts)
482477
y.HostResolver.Hosts = hosts
483478

484479
y.Provision = slices.Concat(o.Provision, y.Provision, d.Provision)
@@ -852,27 +847,15 @@ func FillDefault(y, d, o *LimaYAML, filePath string, warn bool) {
852847
}
853848

854849
env := make(map[string]string)
855-
for k, v := range d.Env {
856-
env[k] = v
857-
}
858-
for k, v := range y.Env {
859-
env[k] = v
860-
}
861-
for k, v := range o.Env {
862-
env[k] = v
863-
}
850+
maps.Copy(env, d.Env)
851+
maps.Copy(env, y.Env)
852+
maps.Copy(env, o.Env)
864853
y.Env = env
865854

866855
param := make(map[string]string)
867-
for k, v := range d.Param {
868-
param[k] = v
869-
}
870-
for k, v := range y.Param {
871-
param[k] = v
872-
}
873-
for k, v := range o.Param {
874-
param[k] = v
875-
}
856+
maps.Copy(param, d.Param)
857+
maps.Copy(param, y.Param)
858+
maps.Copy(param, o.Param)
876859
y.Param = param
877860

878861
if y.CACertificates.RemoveDefaults == nil {

pkg/limayaml/validate.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"path/filepath"
1313
"regexp"
1414
"runtime"
15+
"slices"
1516
"strconv"
1617
"strings"
1718
"unicode"
@@ -198,10 +199,8 @@ func Validate(y *LimaYAML, warn bool) error {
198199
return fmt.Errorf("field `mountType` must be %q or %q or %q, or %q, got %q", REVSSHFS, NINEP, VIRTIOFS, WSLMount, *y.MountType)
199200
}
200201

201-
for _, f := range y.MountTypesUnsupported {
202-
if f == *y.MountType {
203-
return fmt.Errorf("field `mountType` must not be one of %v (`mountTypesUnsupported`), got %q", y.MountTypesUnsupported, *y.MountType)
204-
}
202+
if slices.Contains(y.MountTypesUnsupported, *y.MountType) {
203+
return fmt.Errorf("field `mountType` must not be one of %v (`mountTypesUnsupported`), got %q", y.MountTypesUnsupported, *y.MountType)
205204
}
206205

207206
if warn && runtime.GOOS != "linux" {
@@ -341,7 +340,7 @@ func Validate(y *LimaYAML, warn bool) error {
341340
return err
342341
}
343342
}
344-
for j := 0; j < 2; j++ {
343+
for j := range 2 {
345344
if err := validatePort(fmt.Sprintf("%s.guestPortRange[%d]", field, j), rule.GuestPortRange[j]); err != nil {
346345
return err
347346
}

pkg/lockutil/lockutil_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestWithDirLock(t *testing.T) {
2121
log := filepath.Join(dir, "log")
2222

2323
errc := make(chan error, 10)
24-
for i := 0; i < parallel; i++ {
24+
for i := range parallel {
2525
go func() {
2626
err := WithDirLock(dir, func() error {
2727
if _, err := os.Stat(log); err == nil {
@@ -43,7 +43,7 @@ func TestWithDirLock(t *testing.T) {
4343
}()
4444
}
4545

46-
for i := 0; i < parallel; i++ {
46+
for range parallel {
4747
err := <-errc
4848
if err != nil {
4949
t.Error(err)

pkg/nativeimgutil/nativeimgutil.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ import (
2323
"github.com/sirupsen/logrus"
2424
)
2525

26+
// Disk image size must be aigned to sector size. Qemu block layer is rounding
27+
// up the size to 512 bytes. Apple virtualization framework reject disks not
28+
// aligned to 512 bytes.
29+
const sectorSize = 512
30+
31+
// RoundUp rounds size up to sectorSize.
32+
func RoundUp(size int) int {
33+
sectors := (size + sectorSize - 1) / sectorSize
34+
return sectors * sectorSize
35+
}
36+
2637
// CreateRawDataDisk creates an empty raw data disk.
2738
func CreateRawDataDisk(dir string, size int) error {
2839
dataDisk := filepath.Join(dir, filenames.DataDisk)
@@ -34,13 +45,15 @@ func CreateRawDataDisk(dir string, size int) error {
3445
return err
3546
}
3647
defer f.Close()
37-
return f.Truncate(int64(size))
48+
roundedSize := RoundUp(size)
49+
return f.Truncate(int64(roundedSize))
3850
}
3951

4052
// ResizeRawDataDisk resizes a raw data disk.
4153
func ResizeRawDataDisk(dir string, size int) error {
4254
dataDisk := filepath.Join(dir, filenames.DataDisk)
43-
return os.Truncate(dataDisk, int64(size))
55+
roundedSize := RoundUp(size)
56+
return os.Truncate(dataDisk, int64(roundedSize))
4457
}
4558

4659
// ConvertToRaw converts a source disk into a raw disk.

pkg/nativeimgutil/nativeimgutil_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,24 @@ import (
1313
"gotest.tools/v3/assert"
1414
)
1515

16+
func TestRoundUp(t *testing.T) {
17+
tests := []struct {
18+
Size int
19+
Rounded int
20+
}{
21+
{0, 0},
22+
{1, 512},
23+
{511, 512},
24+
{512, 512},
25+
{123456789, 123457024},
26+
}
27+
for _, tc := range tests {
28+
if RoundUp(tc.Size) != tc.Rounded {
29+
t.Errorf("expected %d, got %d", tc.Rounded, tc.Size)
30+
}
31+
}
32+
}
33+
1634
func createImg(name, format, size string) error {
1735
return exec.Command("qemu-img", "create", name, "-f", format, size).Run()
1836
}

pkg/networks/validate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (c *Config) Validate() error {
2121
paths := reflect.ValueOf(&c.Paths).Elem()
2222
pathsMap := make(map[string]string, paths.NumField())
2323
var socketVMNetNotFound bool
24-
for i := 0; i < paths.NumField(); i++ {
24+
for i := range paths.NumField() {
2525
// extract YAML name from struct tag; strip options like "omitempty"
2626
name := paths.Type().Field(i).Tag.Get("yaml")
2727
if i := strings.IndexRune(name, ','); i > -1 {

0 commit comments

Comments
 (0)