1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
|
package main
import (
"bufio"
"context"
"errors"
"fmt"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"slices"
"sort"
"strings"
"github.com/spf13/cobra"
"golang.org/x/sys/unix"
"gopkg.in/yaml.v2"
incus "github.com/lxc/incus/v6/client"
cli "github.com/lxc/incus/v6/internal/cmd"
"github.com/lxc/incus/v6/internal/linux"
"github.com/lxc/incus/v6/internal/version"
"github.com/lxc/incus/v6/shared/api"
"github.com/lxc/incus/v6/shared/archive"
"github.com/lxc/incus/v6/shared/osarch"
"github.com/lxc/incus/v6/shared/revert"
localtls "github.com/lxc/incus/v6/shared/tls"
"github.com/lxc/incus/v6/shared/units"
"github.com/lxc/incus/v6/shared/util"
)
type cmdMigrate struct {
global *cmdGlobal
flagRsyncArgs string
}
func (c *cmdMigrate) Command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = "incus-migrate"
cmd.Short = "Physical to instance migration tool"
cmd.Long = `Description:
Physical to instance migration tool
This tool lets you turn any Linux filesystem (including your current one)
into an instance on a remote host.
It will setup a clean mount tree made of the root filesystem and any
additional mount you list, then transfer this through the migration
API to create a new instance from it.
The same set of options as ` + "`incus launch`" + ` are also supported.
`
cmd.RunE = c.Run
cmd.Flags().StringVar(&c.flagRsyncArgs, "rsync-args", "", "Extra arguments to pass to rsync"+"``")
return cmd
}
type cmdMigrateData struct {
SourcePath string
SourceFormat string
Mounts []string
InstanceArgs api.InstancesPost
Project string
}
func (c *cmdMigrateData) Render() string {
data := struct {
Name string `yaml:"Name"`
Project string `yaml:"Project"`
Type api.InstanceType `yaml:"Type"`
Source string `yaml:"Source"`
SourceFormat string `yaml:"Source format,omitempty"`
Mounts []string `yaml:"Mounts,omitempty"`
Profiles []string `yaml:"Profiles,omitempty"`
StoragePool string `yaml:"Storage pool,omitempty"`
StorageSize string `yaml:"Storage pool size,omitempty"`
Network string `yaml:"Network name,omitempty"`
Config map[string]string `yaml:"Config,omitempty"`
}{
c.InstanceArgs.Name,
c.Project,
c.InstanceArgs.Type,
c.SourcePath,
c.SourceFormat,
c.Mounts,
c.InstanceArgs.Profiles,
"",
"",
"",
c.InstanceArgs.Config,
}
disk, ok := c.InstanceArgs.Devices["root"]
if ok {
data.StoragePool = disk["pool"]
size, ok := disk["size"]
if ok {
data.StorageSize = size
}
}
network, ok := c.InstanceArgs.Devices["eth0"]
if ok {
data.Network = network["parent"]
}
out, err := yaml.Marshal(&data)
if err != nil {
return ""
}
return string(out)
}
func (c *cmdMigrate) askServer() (incus.InstanceServer, string, error) {
// Detect local server.
local, err := c.connectLocal()
if err == nil {
useLocal, err := c.global.asker.AskBool("The local Incus server is the target [default=yes]: ", "yes")
if err != nil {
return nil, "", err
}
if useLocal {
return local, "", nil
}
}
// Server address
serverURL, err := c.global.asker.AskString("Please provide Incus server URL: ", "", nil)
if err != nil {
return nil, "", err
}
serverURL, err = parseURL(serverURL)
if err != nil {
return nil, "", err
}
args := incus.ConnectionArgs{
UserAgent: fmt.Sprintf("LXC-MIGRATE %s", version.Version),
}
// Attempt to connect
server, err := incus.ConnectIncus(serverURL, &args)
if err != nil {
// Failed to connect using the system CA, so retrieve the remote certificate.
certificate, err := localtls.GetRemoteCertificate(serverURL, args.UserAgent)
if err != nil {
return nil, "", fmt.Errorf("Failed to get remote certificate: %w", err)
}
digest := localtls.CertFingerprint(certificate)
fmt.Println("Certificate fingerprint:", digest)
fmt.Print("ok (y/n)? ")
buf := bufio.NewReader(os.Stdin)
line, _, err := buf.ReadLine()
if err != nil {
return nil, "", err
}
if len(line) < 1 || line[0] != 'y' && line[0] != 'Y' {
return nil, "", fmt.Errorf("Server certificate rejected by user")
}
args.InsecureSkipVerify = true
server, err = incus.ConnectIncus(serverURL, &args)
if err != nil {
return nil, "", fmt.Errorf("Failed to connect to server: %w", err)
}
}
apiServer, _, err := server.GetServer()
if err != nil {
return nil, "", fmt.Errorf("Failed to get server: %w", err)
}
fmt.Println("")
type AuthMethod int
const (
authMethodTLSCertificate AuthMethod = iota
authMethodTLSTemporaryCertificate
authMethodTLSCertificateToken
)
// TLS is always available
var availableAuthMethods []AuthMethod
var authMethod AuthMethod
i := 1
if slices.Contains(apiServer.AuthMethods, api.AuthenticationMethodTLS) {
fmt.Printf("%d) Use a certificate token\n", i)
availableAuthMethods = append(availableAuthMethods, authMethodTLSCertificateToken)
i++
fmt.Printf("%d) Use an existing TLS authentication certificate\n", i)
availableAuthMethods = append(availableAuthMethods, authMethodTLSCertificate)
i++
fmt.Printf("%d) Generate a temporary TLS authentication certificate\n", i)
availableAuthMethods = append(availableAuthMethods, authMethodTLSTemporaryCertificate)
}
if len(apiServer.AuthMethods) > 1 || slices.Contains(apiServer.AuthMethods, api.AuthenticationMethodTLS) {
authMethodInt, err := c.global.asker.AskInt("Please pick an authentication mechanism above: ", 1, int64(i), "", nil)
if err != nil {
return nil, "", err
}
authMethod = availableAuthMethods[authMethodInt-1]
}
var certPath string
var keyPath string
var token string
if authMethod == authMethodTLSCertificate {
certPath, err = c.global.asker.AskString("Please provide the certificate path: ", "", func(path string) error {
if !util.PathExists(path) {
return errors.New("File does not exist")
}
return nil
})
if err != nil {
return nil, "", err
}
keyPath, err = c.global.asker.AskString("Please provide the keyfile path: ", "", func(path string) error {
if !util.PathExists(path) {
return errors.New("File does not exist")
}
return nil
})
if err != nil {
return nil, "", err
}
} else if authMethod == authMethodTLSCertificateToken {
token, err = c.global.asker.AskString("Please provide the certificate token: ", "", func(token string) error {
_, err := localtls.CertificateTokenDecode(token)
if err != nil {
return err
}
return nil
})
if err != nil {
return nil, "", err
}
}
var authType string
switch authMethod {
case authMethodTLSCertificate, authMethodTLSTemporaryCertificate, authMethodTLSCertificateToken:
authType = api.AuthenticationMethodTLS
}
return c.connectTarget(serverURL, certPath, keyPath, authType, token)
}
func (c *cmdMigrate) RunInteractive(server incus.InstanceServer) (cmdMigrateData, error) {
var err error
config := cmdMigrateData{}
config.InstanceArgs = api.InstancesPost{
Source: api.InstanceSource{
Type: "migration",
Mode: "push",
},
}
config.InstanceArgs.Config = map[string]string{}
config.InstanceArgs.Devices = map[string]map[string]string{}
// Provide instance type
instanceType, err := c.global.asker.AskInt("Would you like to create a container (1) or virtual-machine (2)?: ", 1, 2, "1", nil)
if err != nil {
return cmdMigrateData{}, err
}
if instanceType == 1 {
config.InstanceArgs.Type = api.InstanceTypeContainer
} else if instanceType == 2 {
config.InstanceArgs.Type = api.InstanceTypeVM
}
// Project
projectNames, err := server.GetProjectNames()
if err != nil {
return cmdMigrateData{}, err
}
if len(projectNames) > 1 {
project, err := c.global.asker.AskChoice("Project to create the instance in [default=default]: ", projectNames, api.ProjectDefaultName)
if err != nil {
return cmdMigrateData{}, err
}
config.Project = project
server = server.UseProject(config.Project)
} else {
config.Project = api.ProjectDefaultName
}
// Instance name
instanceNames, err := server.GetInstanceNames(api.InstanceTypeAny)
if err != nil {
return cmdMigrateData{}, err
}
for {
instanceName, err := c.global.asker.AskString("Name of the new instance: ", "", nil)
if err != nil {
return cmdMigrateData{}, err
}
if slices.Contains(instanceNames, instanceName) {
fmt.Printf("Instance %q already exists\n", instanceName)
continue
}
config.InstanceArgs.Name = instanceName
break
}
var question string
// Provide source path
if config.InstanceArgs.Type == api.InstanceTypeVM {
question = "Please provide the path to a disk, partition, or qcow2/raw/vmdk image file: "
} else {
question = "Please provide the path to a root filesystem: "
}
config.SourcePath, err = c.global.asker.AskString(question, "", func(s string) error {
if !util.PathExists(s) {
return errors.New("Path does not exist")
}
_, err := os.Stat(s)
if err != nil {
return err
}
// When migrating a VM, report the detected source format
if config.InstanceArgs.Type == api.InstanceTypeVM {
if linux.IsBlockdevPath(s) {
config.SourceFormat = "Block device"
} else if _, ext, _, _ := archive.DetectCompression(s); ext == ".qcow2" {
config.SourceFormat = "qcow2"
} else if _, ext, _, _ := archive.DetectCompression(s); ext == ".vmdk" {
config.SourceFormat = "vmdk"
} else {
// If the input isn't a block device or qcow2/vmdk image, assume it's raw.
// Positively identifying a raw image depends on parsing MBR/GPT partition tables.
config.SourceFormat = "raw"
}
}
return nil
})
if err != nil {
return cmdMigrateData{}, err
}
if config.InstanceArgs.Type == api.InstanceTypeVM {
architectureName, _ := osarch.ArchitectureGetLocal()
if slices.Contains([]string{"x86_64", "aarch64"}, architectureName) {
hasUEFI, err := c.global.asker.AskBool("Does the VM support UEFI booting? [default=yes]: ", "yes")
if err != nil {
return cmdMigrateData{}, err
}
if hasUEFI {
hasSecureBoot, err := c.global.asker.AskBool("Does the VM support UEFI Secure Boot? [default=yes]: ", "yes")
if err != nil {
return cmdMigrateData{}, err
}
if !hasSecureBoot {
config.InstanceArgs.Config["security.secureboot"] = "false"
}
} else {
config.InstanceArgs.Config["security.csm"] = "true"
config.InstanceArgs.Config["security.secureboot"] = "false"
}
}
}
var mounts []string
// Additional mounts for containers
if config.InstanceArgs.Type == api.InstanceTypeContainer {
addMounts, err := c.global.asker.AskBool("Do you want to add additional filesystem mounts? [default=no]: ", "no")
if err != nil {
return cmdMigrateData{}, err
}
if addMounts {
for {
path, err := c.global.asker.AskString("Please provide a path the filesystem mount path [empty value to continue]: ", "", func(s string) error {
if s != "" {
if util.PathExists(s) {
return nil
}
return errors.New("Path does not exist")
}
return nil
})
if err != nil {
return cmdMigrateData{}, err
}
if path == "" {
break
}
mounts = append(mounts, path)
}
config.Mounts = append(config.Mounts, mounts...)
}
}
for {
fmt.Println("\nInstance to be created:")
scanner := bufio.NewScanner(strings.NewReader(config.Render()))
for scanner.Scan() {
fmt.Printf(" %s\n", scanner.Text())
}
fmt.Print(`
Additional overrides can be applied at this stage:
1) Begin the migration with the above configuration
2) Override profile list
3) Set additional configuration options
4) Change instance storage pool or volume size
5) Change instance network
`)
choice, err := c.global.asker.AskInt("Please pick one of the options above [default=1]: ", 1, 5, "1", nil)
if err != nil {
return cmdMigrateData{}, err
}
switch choice {
case 1:
return config, nil
case 2:
err = c.askProfiles(server, &config)
case 3:
err = c.askConfig(&config)
case 4:
err = c.askStorage(server, &config)
case 5:
err = c.askNetwork(server, &config)
}
if err != nil {
fmt.Println(err)
}
}
}
func (c *cmdMigrate) Run(cmd *cobra.Command, args []string) error {
// Quick checks.
if os.Geteuid() != 0 {
return fmt.Errorf("This tool must be run as root")
}
_, err := exec.LookPath("rsync")
if err != nil {
return err
}
// Server
server, clientFingerprint, err := c.askServer()
if err != nil {
return err
}
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt)
ctx, cancel := context.WithCancel(context.Background())
go func() {
<-sigChan
if clientFingerprint != "" {
_ = server.DeleteCertificate(clientFingerprint)
}
cancel()
// The following nolint directive ignores the "deep-exit" rule of the revive linter.
// We should be exiting cleanly by passing the above context into each invoked method and checking for
// cancellation. Unfortunately our client methods do not accept a context argument.
os.Exit(1) //nolint:revive
}()
if clientFingerprint != "" {
defer func() { _ = server.DeleteCertificate(clientFingerprint) }()
}
config, err := c.RunInteractive(server)
if err != nil {
return err
}
if config.Project != "" {
server = server.UseProject(config.Project)
}
config.Mounts = append(config.Mounts, config.SourcePath)
// Get and sort the mounts
sort.Strings(config.Mounts)
// Create the mount namespace and ensure we're not moved around
runtime.LockOSThread()
defer runtime.UnlockOSThread()
// Unshare a new mntns so our mounts don't leak
err = unix.Unshare(unix.CLONE_NEWNS)
if err != nil {
return fmt.Errorf("Failed to unshare mount namespace: %w", err)
}
// Prevent mount propagation back to initial namespace
err = unix.Mount("", "/", "", unix.MS_REC|unix.MS_PRIVATE, "")
if err != nil {
return fmt.Errorf("Failed to disable mount propagation: %w", err)
}
// Create the temporary directory to be used for the mounts
path, err := os.MkdirTemp("", "incus-migrate_mount_")
if err != nil {
return err
}
// Automatically clean-up the temporary path on exit
defer func(path string) {
// Unmount the path if it's a mountpoint.
_ = unix.Unmount(path, unix.MNT_DETACH)
_ = unix.Unmount(filepath.Join(path, "root.img"), unix.MNT_DETACH)
// Cleanup VM image files.
_ = os.Remove(filepath.Join(path, "converted-raw-image.img"))
_ = os.Remove(filepath.Join(path, "root.img"))
// Remove the directory itself.
_ = os.Remove(path)
}(path)
var fullPath string
if config.InstanceArgs.Type == api.InstanceTypeContainer {
// Create the rootfs directory
fullPath = fmt.Sprintf("%s/rootfs", path)
err = os.Mkdir(fullPath, 0o755)
if err != nil {
return err
}
// Setup the source (mounts)
err = setupSource(fullPath, config.Mounts)
if err != nil {
return fmt.Errorf("Failed to setup the source: %w", err)
}
} else {
_, ext, convCmd, _ := archive.DetectCompression(config.SourcePath)
if ext == ".qcow2" || ext == ".vmdk" {
destImg := filepath.Join(path, "converted-raw-image.img")
cmd := []string{
"nice", "-n19", // Run with low priority to reduce CPU impact on other processes.
}
cmd = append(cmd, convCmd...)
cmd = append(cmd, "-p", "-t", "writeback")
// Check for Direct I/O support.
from, err := os.OpenFile(config.SourcePath, unix.O_DIRECT|unix.O_RDONLY, 0)
if err == nil {
cmd = append(cmd, "-T", "none")
_ = from.Close()
}
to, err := os.OpenFile(destImg, unix.O_DIRECT|unix.O_RDONLY, 0)
if err == nil {
cmd = append(cmd, "-t", "none")
_ = to.Close()
}
cmd = append(cmd, config.SourcePath, destImg)
fmt.Printf("Converting image %q to raw format before importing\n", config.SourcePath)
c := exec.Command(cmd[0], cmd[1:]...)
err = c.Run()
if err != nil {
return fmt.Errorf("Failed to convert image %q for importing: %w", config.SourcePath, err)
}
config.SourcePath = destImg
}
fullPath = path
target := filepath.Join(path, "root.img")
err := os.WriteFile(target, nil, 0o644)
if err != nil {
return fmt.Errorf("Failed to create %q: %w", target, err)
}
// Mount the path
err = unix.Mount(config.SourcePath, target, "none", unix.MS_BIND, "")
if err != nil {
return fmt.Errorf("Failed to mount %s: %w", config.SourcePath, err)
}
// Make it read-only
err = unix.Mount("", target, "none", unix.MS_BIND|unix.MS_RDONLY|unix.MS_REMOUNT, "")
if err != nil {
return fmt.Errorf("Failed to make %s read-only: %w", config.SourcePath, err)
}
}
// System architecture
architectureName, err := osarch.ArchitectureGetLocal()
if err != nil {
return err
}
config.InstanceArgs.Architecture = architectureName
revert := revert.New()
defer revert.Fail()
// Create the instance
op, err := server.CreateInstance(config.InstanceArgs)
if err != nil {
return err
}
revert.Add(func() {
_, _ = server.DeleteInstance(config.InstanceArgs.Name)
})
progress := cli.ProgressRenderer{Format: "Transferring instance: %s"}
_, err = op.AddHandler(progress.UpdateOp)
if err != nil {
progress.Done("")
return err
}
err = transferRootfs(ctx, server, op, fullPath, c.flagRsyncArgs, config.InstanceArgs.Type)
if err != nil {
return err
}
progress.Done(fmt.Sprintf("Instance %s successfully created", config.InstanceArgs.Name))
revert.Success()
return nil
}
func (c *cmdMigrate) askProfiles(server incus.InstanceServer, config *cmdMigrateData) error {
profileNames, err := server.GetProfileNames()
if err != nil {
return err
}
profiles, err := c.global.asker.AskString("Which profiles do you want to apply to the instance? (space separated) [default=default, \"-\" for none]: ", "default", func(s string) error {
// This indicates that no profiles should be applied.
if s == "-" {
return nil
}
profiles := strings.Split(s, " ")
for _, profile := range profiles {
if !slices.Contains(profileNames, profile) {
return fmt.Errorf("Unknown profile %q", profile)
}
}
return nil
})
if err != nil {
return err
}
if profiles != "-" {
config.InstanceArgs.Profiles = strings.Split(profiles, " ")
}
return nil
}
func (c *cmdMigrate) askConfig(config *cmdMigrateData) error {
configs, err := c.global.asker.AskString("Please specify config keys and values (key=value ...): ", "", func(s string) error {
if s == "" {
return nil
}
for _, entry := range strings.Split(s, " ") {
if !strings.Contains(entry, "=") {
return fmt.Errorf("Bad key=value configuration: %v", entry)
}
}
return nil
})
if err != nil {
return err
}
for _, entry := range strings.Split(configs, " ") {
key, value, _ := strings.Cut(entry, "=")
config.InstanceArgs.Config[key] = value
}
return nil
}
func (c *cmdMigrate) askStorage(server incus.InstanceServer, config *cmdMigrateData) error {
storagePools, err := server.GetStoragePoolNames()
if err != nil {
return err
}
if len(storagePools) == 0 {
return fmt.Errorf("No storage pools available")
}
storagePool, err := c.global.asker.AskChoice("Please provide the storage pool to use: ", storagePools, "")
if err != nil {
return err
}
config.InstanceArgs.Devices["root"] = map[string]string{
"type": "disk",
"pool": storagePool,
"path": "/",
}
changeStorageSize, err := c.global.asker.AskBool("Do you want to change the storage size? [default=no]: ", "no")
if err != nil {
return err
}
if changeStorageSize {
size, err := c.global.asker.AskString("Please specify the storage size: ", "", func(s string) error {
_, err := units.ParseByteSizeString(s)
return err
})
if err != nil {
return err
}
config.InstanceArgs.Devices["root"]["size"] = size
}
return nil
}
func (c *cmdMigrate) askNetwork(server incus.InstanceServer, config *cmdMigrateData) error {
networks, err := server.GetNetworkNames()
if err != nil {
return err
}
network, err := c.global.asker.AskChoice("Please specify the network to use for the instance: ", networks, "")
if err != nil {
return err
}
config.InstanceArgs.Devices["eth0"] = map[string]string{
"type": "nic",
"nictype": "bridged",
"parent": network,
"name": "eth0",
}
return nil
}
|