| 1 | package guestos |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | |
| 12 | "github.com/google/uuid" |
| 13 | "github.com/sylvinus/agent-vm/internal/cage" |
| 14 | "github.com/sylvinus/agent-vm/internal/paths" |
| 15 | ) |
| 16 | |
| 17 | type windowsStorageSnapshot struct { |
| 18 | Profile WindowsProfile `json:"profile"` |
| 19 | Instance string `json:"instance"` |
| 20 | Disk windowsStorageDisk `json:"disk"` |
| 21 | Excluded []string `json:"excluded,omitempty"` |
| 22 | } |
| 23 | |
| 24 | type windowsStorageRuntime struct { |
| 25 | profile string |
| 26 | call func(context.Context, uuid.UUID, windowsHostStorageRequest) (windowsHostStorageState, error) |
| 27 | } |
| 28 | |
| 29 | func WindowsStorageRuntime(profile string) cage.StorageRuntime { |
| 30 | return &windowsStorageRuntime{profile: profile, call: windowsHostStorage} |
| 31 | } |
| 32 | |
| 33 | |
| 34 | |
| 35 | func RecoverWindowsStorage(ctx context.Context, profile string) error { |
| 36 | return cage.RecoverStorage(ctx, profile, WindowsStorageRuntime(profile)) |
| 37 | } |
| 38 | |
| 39 | func (*windowsStorageRuntime) Name() string { return "windows-hyperv" } |
| 40 | |
| 41 | func (runtime *windowsStorageRuntime) Snapshot(ctx context.Context, source string) (cage.StorageRuntimeSnapshot, error) { |
| 42 | var snapshot cage.StorageRuntimeSnapshot |
| 43 | binding, err := LoadWindowsProfile(runtime.profile) |
| 44 | if err != nil { |
| 45 | return snapshot, err |
| 46 | } |
| 47 | if binding == nil { |
| 48 | return snapshot, errors.New("Windows 客机尚未绑定") |
| 49 | } |
| 50 | state, err := runtime.call(ctx, binding.VMID, windowsHostStorageRequest{Action: "status"}) |
| 51 | if err != nil { |
| 52 | return snapshot, fmt.Errorf("请先在设置的环境页选择“同步环境组件”,完成 Windows 系统授权后再迁移:%w", err) |
| 53 | } |
| 54 | if state.Pending != "" { |
| 55 | return snapshot, errors.New("这台 Windows 客机仍有待恢复的存储迁移,请打开发起迁移的原工作台") |
| 56 | } |
| 57 | instance, err := paths.Resolve(binding.InstanceDirectory) |
| 58 | if err != nil { |
| 59 | return snapshot, err |
| 60 | } |
| 61 | runtimeState := windowsStorageSnapshot{Profile: *binding, Instance: instance, Disk: state.Disk} |
| 62 | if !cage.StorageContains(source, instance) { |
| 63 | snapshot.Backups = append(snapshot.Backups, instance) |
| 64 | } |
| 65 | if !cage.StorageContains(source, state.Disk.Path) && !cage.StorageContains(instance, state.Disk.Path) { |
| 66 | snapshot.Backups = append(snapshot.Backups, state.Disk.Path) |
| 67 | } |
| 68 | if state.Layout != nil { |
| 69 | |
| 70 | |
| 71 | for _, directory := range []string{filepath.Join(state.Layout.Configuration, "Virtual Machines"), filepath.Join(state.Layout.Snapshots, "Snapshots")} { |
| 72 | physical, resolveErr := cage.StoragePhysicalPath(directory) |
| 73 | if resolveErr != nil { |
| 74 | return snapshot, resolveErr |
| 75 | } |
| 76 | if cage.StorageContains(source, physical) && !cage.StorageContains(physical, state.Disk.Path) { |
| 77 | snapshot.Excluded = append(snapshot.Excluded, physical) |
| 78 | } else if cage.StorageContains(instance, physical) && !cage.StorageContains(physical, state.Disk.Path) { |
| 79 | runtimeState.Excluded = append(runtimeState.Excluded, physical) |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | snapshot.State, err = json.Marshal(runtimeState) |
| 84 | return snapshot, err |
| 85 | } |
| 86 | |
| 87 | func windowsMoveSnapshot(move cage.StorageTransition) (windowsStorageSnapshot, error) { |
| 88 | var snapshot windowsStorageSnapshot |
| 89 | if json.Unmarshal(move.State, &snapshot) != nil || snapshot.Profile.validate() != nil || |
| 90 | !filepath.IsAbs(snapshot.Instance) || !filepath.IsAbs(snapshot.Disk.Path) || snapshot.Disk.Volume == 0 || snapshot.Disk.FileID == 0 { |
| 91 | return snapshot, errors.New("Windows 存储迁移的恢复信息无效") |
| 92 | } |
| 93 | return snapshot, nil |
| 94 | } |
| 95 | |
| 96 | func (runtime *windowsStorageRuntime) action(ctx context.Context, move cage.StorageTransition, action, disk string) error { |
| 97 | snapshot, err := windowsMoveSnapshot(move) |
| 98 | if err != nil { |
| 99 | return err |
| 100 | } |
| 101 | state, err := runtime.call(ctx, snapshot.Profile.VMID, windowsHostStorageRequest{Action: action, ID: move.ID, |
| 102 | SourceVolume: snapshot.Disk.Volume, SourceFileID: snapshot.Disk.FileID, Disk: disk}) |
| 103 | if err != nil { |
| 104 | return err |
| 105 | } |
| 106 | if action == "begin" || action == "attach" { |
| 107 | if state.Pending != move.ID { |
| 108 | return errors.New("Windows 管理助手没有保留当前迁移事务") |
| 109 | } |
| 110 | } else if state.Pending != "" || state.Outcome != action { |
| 111 | return errors.New("Windows 管理助手尚未完成迁移恢复") |
| 112 | } |
| 113 | return nil |
| 114 | } |
| 115 | |
| 116 | func (runtime *windowsStorageRuntime) Begin(ctx context.Context, move cage.StorageTransition) error { |
| 117 | return runtime.action(ctx, move, "begin", "") |
| 118 | } |
| 119 | |
| 120 | func windowsMovedPath(value, source, target string) string { |
| 121 | if !cage.StorageContains(source, value) { |
| 122 | return value |
| 123 | } |
| 124 | rel, err := filepath.Rel(source, value) |
| 125 | if err != nil { |
| 126 | return value |
| 127 | } |
| 128 | return filepath.Join(target, rel) |
| 129 | } |
| 130 | |
| 131 | func (runtime *windowsStorageRuntime) movedProfile(move cage.StorageTransition, snapshot windowsStorageSnapshot) (WindowsProfile, string) { |
| 132 | logical := filepath.Join(runtime.profile, "state") |
| 133 | instance := filepath.Join(move.Destination, "windows-"+strings.ReplaceAll(snapshot.Profile.VMID.String(), "-", "")) |
| 134 | if cage.StorageContains(move.Source, snapshot.Instance) { |
| 135 | instance = windowsMovedPath(snapshot.Instance, move.Source, move.Destination) |
| 136 | } |
| 137 | next := snapshot.Profile |
| 138 | next.InstanceDirectory = instance |
| 139 | remap := func(value string) string { |
| 140 | value = windowsMovedPath(value, snapshot.Profile.InstanceDirectory, instance) |
| 141 | value = windowsMovedPath(value, snapshot.Instance, instance) |
| 142 | value = windowsMovedPath(value, move.Source, move.Destination) |
| 143 | return windowsMovedPath(value, logical, move.Destination) |
| 144 | } |
| 145 | next.RelayExecutable = remap(next.RelayExecutable) |
| 146 | next.PairingFile = remap(next.PairingFile) |
| 147 | next.NetworkPairingFile = remap(next.NetworkPairingFile) |
| 148 | disk := windowsMovedPath(snapshot.Disk.Path, snapshot.Instance, instance) |
| 149 | disk = windowsMovedPath(disk, move.Source, move.Destination) |
| 150 | if disk == snapshot.Disk.Path { |
| 151 | disk = filepath.Join(instance, filepath.Base(snapshot.Disk.Path)) |
| 152 | } |
| 153 | return next, disk |
| 154 | } |
| 155 | |
| 156 | func (runtime *windowsStorageRuntime) Switch(ctx context.Context, move cage.StorageTransition) error { |
| 157 | snapshot, err := windowsMoveSnapshot(move) |
| 158 | if err != nil { |
| 159 | return err |
| 160 | } |
| 161 | next, disk := runtime.movedProfile(move, snapshot) |
| 162 | physicalInstance := next.InstanceDirectory |
| 163 | if !cage.StorageContains(move.Source, snapshot.Instance) { |
| 164 | if err = cage.CopyStorage(ctx, snapshot.Instance, physicalInstance, snapshot.Excluded, nil); err != nil { |
| 165 | return err |
| 166 | } |
| 167 | } |
| 168 | if !cage.StorageContains(move.Source, snapshot.Disk.Path) && !cage.StorageContains(snapshot.Instance, snapshot.Disk.Path) { |
| 169 | if err = os.MkdirAll(physicalInstance, 0700); err != nil { |
| 170 | return err |
| 171 | } |
| 172 | if err = cage.CopyStorageFile(ctx, snapshot.Disk.Path, disk); err != nil { |
| 173 | return err |
| 174 | } |
| 175 | } |
| 176 | if err = prepareWindowsStorageDisk(ctx, snapshot.Disk.Path, disk); err != nil { |
| 177 | return err |
| 178 | } |
| 179 | for _, path := range []string{next.PairingFile, next.NetworkPairingFile} { |
| 180 | if path != "" && cage.StorageContains(move.Destination, path) { |
| 181 | if err = protectWindowsProfile(path); err != nil { |
| 182 | return err |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | if err = runtime.action(ctx, move, "attach", disk); err != nil { |
| 187 | return err |
| 188 | } |
| 189 | return runtime.writeProfile(snapshot.Profile, next, next) |
| 190 | } |
| 191 | |
| 192 | func (runtime *windowsStorageRuntime) Recover(ctx context.Context, move cage.StorageTransition, committed bool) error { |
| 193 | snapshot, err := windowsMoveSnapshot(move) |
| 194 | if err != nil { |
| 195 | return err |
| 196 | } |
| 197 | next, _ := runtime.movedProfile(move, snapshot) |
| 198 | chosen, action := snapshot.Profile, "rollback" |
| 199 | if committed { |
| 200 | chosen, action = next, "commit" |
| 201 | } |
| 202 | if err = runtime.action(ctx, move, action, ""); err != nil { |
| 203 | return err |
| 204 | } |
| 205 | return runtime.writeProfile(snapshot.Profile, next, chosen) |
| 206 | } |
| 207 | |
| 208 | func (runtime *windowsStorageRuntime) writeProfile(before, after, chosen WindowsProfile) error { |
| 209 | current, err := LoadWindowsProfile(runtime.profile) |
| 210 | if err != nil { |
| 211 | return err |
| 212 | } |
| 213 | if current == nil || *current != before && *current != after { |
| 214 | return errors.New("Windows 客机配对在迁移期间发生变化,请保留迁移记录并核对原配置") |
| 215 | } |
| 216 | if *current == chosen { |
| 217 | return nil |
| 218 | } |
| 219 | data, err := json.Marshal(chosen) |
| 220 | if err != nil { |
| 221 | return err |
| 222 | } |
| 223 | return replaceWindowsStorageProfile(WindowsProfilePath(runtime.profile), append(data, '\n')) |
| 224 | } |