跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

disk.go

120 行 · 3.9 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package driverutil
5
6import (
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// MigrateDiskLayout creates symlinks from the current filenames (disk, iso) to
25// the legacy filenames (diffdisk, basedisk) used by older Lima versions.
26// The original files are left in place so older Lima versions can still use them.
27func MigrateDiskLayout(instDir string) error {
28 diskPath := filepath.Join(instDir, filenames.Disk)
29 if osutil.FileExists(diskPath) {
30 return nil // already migrated or new instance
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 // Non-ISO basedisk is a legacy qcow2 backing file; leave it for QEMU to resolve.
55 }
56
57 return nil
58}
59
60// EnsureDisk creates the VM disk from the downloaded image.
61// For ISO images, it renames the image to "iso" and creates an empty disk.
62// For non-ISO images, it converts the image to "disk" and removes the original.
63func 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 // Resize handled by prepareDisk() after CreateDisk()
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}