Skip to content

Commit 05b57a1

Browse files
committed
pkg/store: add filenames constants
Signed-off-by: Akihiro Suda <[email protected]>
1 parent 68dc77e commit 05b57a1

File tree

9 files changed

+53
-30
lines changed

9 files changed

+53
-30
lines changed

cmd/limactl/start.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/AkihiroSuda/lima/pkg/limayaml"
1212
"github.com/AkihiroSuda/lima/pkg/start"
1313
"github.com/AkihiroSuda/lima/pkg/store"
14+
"github.com/AkihiroSuda/lima/pkg/store/filenames"
1415
"github.com/containerd/containerd/identifiers"
1516
"github.com/mattn/go-isatty"
1617
"github.com/norouter/norouter/cmd/norouter/editorcmd"
@@ -112,7 +113,7 @@ func loadOrCreateInstance(clicontext *cli.Context) (*store.Instance, error) {
112113
if err := os.MkdirAll(instDir, 0700); err != nil {
113114
return nil, err
114115
}
115-
if err := os.WriteFile(filepath.Join(instDir, store.YAMLFileName), yBytes, 0644); err != nil {
116+
if err := os.WriteFile(filepath.Join(instDir, filenames.LimaYAML), yBytes, 0644); err != nil {
116117
return nil, err
117118
}
118119
return store.Inspect(instName)

cmd/limactl/stop.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
hostagentapi "github.com/AkihiroSuda/lima/pkg/hostagent/api"
1212
"github.com/AkihiroSuda/lima/pkg/store"
13+
"github.com/AkihiroSuda/lima/pkg/store/filenames"
1314
"github.com/pkg/errors"
1415
"github.com/sirupsen/logrus"
1516
"github.com/urfave/cli/v2"
@@ -83,8 +84,8 @@ func waitForHostAgentTermination(ctx context.Context, inst *store.Instance) erro
8384
return false
8485
}
8586

86-
haStdoutPath := filepath.Join(inst.Dir, "ha.stdout.log")
87-
haStderrPath := filepath.Join(inst.Dir, "ha.stderr.log")
87+
haStdoutPath := filepath.Join(inst.Dir, filenames.HostAgentStdoutLog)
88+
haStderrPath := filepath.Join(inst.Dir, filenames.HostAgentStderrLog)
8889

8990
if err := hostagentapi.WatchEvents(ctx2, haStdoutPath, haStderrPath, onEvent); err != nil {
9091
return err

pkg/hostagent/hostagent.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/AkihiroSuda/lima/pkg/qemu"
2121
"github.com/AkihiroSuda/lima/pkg/sshutil"
2222
"github.com/AkihiroSuda/lima/pkg/store"
23+
"github.com/AkihiroSuda/lima/pkg/store/filenames"
2324
"github.com/AkihiroSuda/sshocker/pkg/ssh"
2425
"github.com/digitalocean/go-qemu/qmp"
2526
"github.com/digitalocean/go-qemu/qmp/raw"
@@ -139,7 +140,7 @@ func (a *HostAgent) Run(ctx context.Context) error {
139140
}
140141
defer logPipeRoutine(a.l, qStderr, "qemu[stderr]")
141142

142-
a.l.Infof("Starting QEMU (hint: to watch the boot progress, see %q)", filepath.Join(a.instDir, "serial.log"))
143+
a.l.Infof("Starting QEMU (hint: to watch the boot progress, see %q)", filepath.Join(a.instDir, filenames.SerialLog))
143144
a.l.Debugf("qCmd.Args: %v", qCmd.Args)
144145
if err := qCmd.Start(); err != nil {
145146
return err
@@ -193,7 +194,7 @@ func (a *HostAgent) Run(ctx context.Context) error {
193194

194195
func (a *HostAgent) shutdownQEMU(ctx context.Context, timeout time.Duration, qCmd *exec.Cmd, qWaitCh <-chan error) error {
195196
a.l.Info("Shutting down QEMU with ACPI")
196-
qmpSockPath := filepath.Join(a.instDir, "qmp.sock")
197+
qmpSockPath := filepath.Join(a.instDir, filenames.QMPSock)
197198
qmpClient, err := qmp.NewSocketMonitor("unix", qmpSockPath, 5*time.Second)
198199
if err != nil {
199200
a.l.WithError(err).Warnf("failed to open the QMP socket %q, forcibly killing QEMU", qmpSockPath)
@@ -227,7 +228,7 @@ func (a *HostAgent) killQEMU(ctx context.Context, timeout time.Duration, qCmd *e
227228
}
228229
qWaitErr := <-qWaitCh
229230
a.l.WithError(qWaitErr).Info("QEMU has exited, after killing forcibly")
230-
qemuPIDPath := filepath.Join(a.instDir, "qemu.pid")
231+
qemuPIDPath := filepath.Join(a.instDir, filenames.QemuPID)
231232
_ = os.RemoveAll(qemuPIDPath)
232233
return qWaitErr
233234
}
@@ -279,7 +280,7 @@ func (a *HostAgent) close() error {
279280
func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
280281
// TODO: use vSock (when QEMU for macOS gets support for vSock)
281282

282-
localUnix := filepath.Join(a.instDir, "ga.sock")
283+
localUnix := filepath.Join(a.instDir, filenames.GuestAgentSock)
283284
// guest should have same UID as the host (specified in cidata)
284285
remoteUnix := fmt.Sprintf("/run/user/%d/lima-guestagent.sock", os.Getuid())
285286

pkg/qemu/qemu.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/AkihiroSuda/lima/pkg/downloader"
1212
"github.com/AkihiroSuda/lima/pkg/limayaml"
13+
"github.com/AkihiroSuda/lima/pkg/store/filenames"
1314
"github.com/docker/go-units"
1415
"github.com/pkg/errors"
1516
"github.com/sirupsen/logrus"
@@ -22,13 +23,13 @@ type Config struct {
2223
}
2324

2425
func EnsureDisk(cfg Config) error {
25-
diffDisk := filepath.Join(cfg.InstanceDir, "diffdisk")
26+
diffDisk := filepath.Join(cfg.InstanceDir, filenames.DiffDisk)
2627
if _, err := os.Stat(diffDisk); err == nil || !errors.Is(err, os.ErrNotExist) {
2728
// disk is already ensured
2829
return err
2930
}
3031

31-
baseDisk := filepath.Join(cfg.InstanceDir, "basedisk")
32+
baseDisk := filepath.Join(cfg.InstanceDir, filenames.BaseDisk)
3233
if _, err := os.Stat(baseDisk); errors.Is(err, os.ErrNotExist) {
3334
var ensuredBaseDisk bool
3435
errs := make([]error, len(cfg.LimaYAML.Images))
@@ -120,10 +121,10 @@ func Cmdline(cfg Config) (string, []string, error) {
120121
args = append(args, "-boot", "order=c,splash-time=0,menu=on")
121122

122123
// Root disk
123-
args = append(args, "-drive", fmt.Sprintf("file=%s,if=virtio", filepath.Join(cfg.InstanceDir, "diffdisk")))
124+
args = append(args, "-drive", fmt.Sprintf("file=%s,if=virtio", filepath.Join(cfg.InstanceDir, filenames.DiffDisk)))
124125

125126
// cloud-init
126-
args = append(args, "-cdrom", filepath.Join(cfg.InstanceDir, "cidata.iso"))
127+
args = append(args, "-cdrom", filepath.Join(cfg.InstanceDir, filenames.CIDataISO))
127128

128129
// Network
129130
// CIDR is intentionally hardcoded to 192.168.5.0/24, as each of QEMU has its own independent slirp network.
@@ -155,11 +156,11 @@ func Cmdline(cfg Config) (string, []string, error) {
155156
args = append(args, "-parallel", "none")
156157

157158
// Serial
158-
serialSock := filepath.Join(cfg.InstanceDir, "serial.sock")
159+
serialSock := filepath.Join(cfg.InstanceDir, filenames.SerialSock)
159160
if err := os.RemoveAll(serialSock); err != nil {
160161
return "", nil, err
161162
}
162-
serialLog := filepath.Join(cfg.InstanceDir, "serial.log")
163+
serialLog := filepath.Join(cfg.InstanceDir, filenames.SerialLog)
163164
if err := os.RemoveAll(serialLog); err != nil {
164165
return "", nil, err
165166
}
@@ -170,7 +171,7 @@ func Cmdline(cfg Config) (string, []string, error) {
170171
// We also want to enable vsock and virtfs here, but QEMU does not support vsock and virtfs for macOS hosts
171172

172173
// QMP
173-
qmpSock := filepath.Join(cfg.InstanceDir, "qmp.sock")
174+
qmpSock := filepath.Join(cfg.InstanceDir, filenames.QMPSock)
174175
if err := os.RemoveAll(qmpSock); err != nil {
175176
return "", nil, err
176177
}
@@ -180,7 +181,7 @@ func Cmdline(cfg Config) (string, []string, error) {
180181

181182
// QEMU process
182183
args = append(args, "-name", "lima-"+cfg.Name)
183-
args = append(args, "-pidfile", filepath.Join(cfg.InstanceDir, "qemu.pid"))
184+
args = append(args, "-pidfile", filepath.Join(cfg.InstanceDir, filenames.QemuPID))
184185

185186
return exe, args, nil
186187
}

pkg/sshutil/sshutil.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"runtime"
77
"strings"
88

9+
"github.com/AkihiroSuda/lima/pkg/store/filenames"
910
"github.com/pkg/errors"
1011
"github.com/sirupsen/logrus"
1112
)
@@ -46,7 +47,7 @@ func DefaultPubKeys() []PubKey {
4647
}
4748

4849
func SSHArgs(instDir string) ([]string, error) {
49-
controlSock := filepath.Join(instDir, "ssh.sock")
50+
controlSock := filepath.Join(instDir, filenames.SSHSock)
5051
maxSockLen := 104
5152
if runtime.GOOS == "linux" {
5253
maxSockLen = 108

pkg/start/start.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ import (
1313
"github.com/AkihiroSuda/lima/pkg/limayaml"
1414
"github.com/AkihiroSuda/lima/pkg/qemu"
1515
"github.com/AkihiroSuda/lima/pkg/store"
16+
"github.com/AkihiroSuda/lima/pkg/store/filenames"
1617
"github.com/pkg/errors"
1718
"github.com/sirupsen/logrus"
1819
)
1920

2021
func ensureDisk(ctx context.Context, instName, instDir string, y *limayaml.LimaYAML) error {
21-
cidataISOPath := filepath.Join(instDir, "cidata.iso")
22+
cidataISOPath := filepath.Join(instDir, filenames.CIDataISO)
2223
if err := cidata.GenerateISO9660(cidataISOPath, instName, y); err != nil {
2324
return err
2425
}
@@ -35,7 +36,7 @@ func ensureDisk(ctx context.Context, instName, instDir string, y *limayaml.LimaY
3536
}
3637

3738
func Start(ctx context.Context, inst *store.Instance) error {
38-
haPIDPath := filepath.Join(inst.Dir, "ha.pid")
39+
haPIDPath := filepath.Join(inst.Dir, filenames.HostAgentPID)
3940
if _, err := os.Stat(haPIDPath); !errors.Is(err, os.ErrNotExist) {
4041
return errors.Errorf("instance %q seems running (hint: remove %q if the instance is not actually running)", inst.Name, haPIDPath)
4142
}
@@ -53,8 +54,8 @@ func Start(ctx context.Context, inst *store.Instance) error {
5354
if err != nil {
5455
return err
5556
}
56-
haStdoutPath := filepath.Join(inst.Dir, "ha.stdout.log")
57-
haStderrPath := filepath.Join(inst.Dir, "ha.stderr.log")
57+
haStdoutPath := filepath.Join(inst.Dir, filenames.HostAgentStdoutLog)
58+
haStderrPath := filepath.Join(inst.Dir, filenames.HostAgentStderrLog)
5859
if err := os.RemoveAll(haStdoutPath); err != nil {
5960
return err
6061
}

pkg/store/filenames/filenames.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Package filenames defines the names of the files that appear under an instance dir.
2+
//
3+
// See docs/internal.md .
4+
package filenames
5+
6+
const (
7+
LimaYAML = "lima.yaml"
8+
CIDataISO = "cidata.iso"
9+
BaseDisk = "basedisk"
10+
DiffDisk = "diffdisk"
11+
QemuPID = "qemu.pid"
12+
QMPSock = "qmp.sock"
13+
SerialLog = "serial.log"
14+
SerialSock = "serial.sock"
15+
SSHSock = "ssh.sock"
16+
GuestAgentSock = "ga.sock"
17+
HostAgentPID = "ha.pid"
18+
HostAgentStdoutLog = "ha.stdout.log"
19+
HostAgentStderrLog = "ha.stderr.log"
20+
)

pkg/store/instance.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99

1010
"github.com/AkihiroSuda/lima/pkg/limayaml"
11+
"github.com/AkihiroSuda/lima/pkg/store/filenames"
1112
)
1213

1314
type Status = string
@@ -34,7 +35,7 @@ func (inst *Instance) LoadYAML() (*limayaml.LimaYAML, error) {
3435
if inst.Dir == "" {
3536
return nil, errors.New("inst.Dir is empty")
3637
}
37-
yamlPath := filepath.Join(inst.Dir, YAMLFileName)
38+
yamlPath := filepath.Join(inst.Dir, filenames.LimaYAML)
3839
return LoadYAMLByFilePath(yamlPath)
3940
}
4041

@@ -50,7 +51,7 @@ func Inspect(instName string) (*Instance, error) {
5051
if err != nil {
5152
return nil, err
5253
}
53-
yamlPath := filepath.Join(instDir, YAMLFileName)
54+
yamlPath := filepath.Join(instDir, filenames.LimaYAML)
5455
y, err := LoadYAMLByFilePath(yamlPath)
5556
if err != nil {
5657
if errors.Is(err, os.ErrNotExist) {
@@ -63,13 +64,13 @@ func Inspect(instName string) (*Instance, error) {
6364
inst.Arch = y.Arch
6465
inst.SSHLocalPort = y.SSH.LocalPort
6566

66-
inst.HostAgentPID, err = readPIDFile(filepath.Join(instDir, "ha.pid"))
67+
inst.HostAgentPID, err = readPIDFile(filepath.Join(instDir, filenames.HostAgentPID))
6768
if err != nil {
6869
inst.Status = StatusBroken
6970
inst.Errors = append(inst.Errors, err)
7071
}
7172

72-
inst.QemuPID, err = readPIDFile(filepath.Join(instDir, "qemu.pid"))
73+
inst.QemuPID, err = readPIDFile(filepath.Join(instDir, filenames.QemuPID))
7374
if err != nil {
7475
inst.Status = StatusBroken
7576
inst.Errors = append(inst.Errors, err)

pkg/store/store.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@ import (
99
"github.com/containerd/containerd/identifiers"
1010
)
1111

12-
const (
13-
// DotLima is a directory that appears under the home directory.
14-
DotLima = ".lima"
15-
// YAMLFileName appears under an instance dir.
16-
YAMLFileName = "lima.yaml"
17-
)
12+
// DotLima is a directory that appears under the home directory.
13+
const DotLima = ".lima"
1814

1915
// LimaDir returns the abstract path of `~/.lima`.
2016
//

0 commit comments

Comments
 (0)