@@ -9,12 +9,16 @@ import (
99 "fmt"
1010 "io/fs"
1111 "os"
12+ "path/filepath"
1213 "text/tabwriter"
1314
15+ contfs "github.com/containerd/continuity/fs"
1416 "github.com/docker/go-units"
17+ "github.com/lima-vm/go-qcow2reader"
1518 "github.com/lima-vm/lima/pkg/nativeimgutil"
1619 "github.com/lima-vm/lima/pkg/qemu"
1720 "github.com/lima-vm/lima/pkg/store"
21+ "github.com/lima-vm/lima/pkg/store/filenames"
1822 "github.com/sirupsen/logrus"
1923 "github.com/spf13/cobra"
2024)
@@ -44,6 +48,7 @@ func newDiskCommand() *cobra.Command {
4448 newDiskDeleteCommand (),
4549 newDiskUnlockCommand (),
4650 newDiskResizeCommand (),
51+ newDiskImportCommand (),
4752 )
4853 return diskCommand
4954}
@@ -418,3 +423,63 @@ func diskResizeAction(cmd *cobra.Command, args []string) error {
418423func diskBashComplete (cmd * cobra.Command , _ []string , _ string ) ([]string , cobra.ShellCompDirective ) {
419424 return bashCompleteDiskNames (cmd )
420425}
426+
427+ func newDiskImportCommand () * cobra.Command {
428+ diskImportCommand := & cobra.Command {
429+ Use : "import DISK FILE" ,
430+ Example : `
431+ Import a disk:
432+ $ limactl disk import DISK DISKPATH
433+ ` ,
434+ Short : "Import an existing disk to Lima" ,
435+ Args : WrapArgsError (cobra .ExactArgs (2 )),
436+ RunE : diskImportAction ,
437+ }
438+ return diskImportCommand
439+ }
440+
441+ func diskImportAction (_ * cobra.Command , args []string ) error {
442+ diskName := args [0 ]
443+ fName := args [1 ]
444+
445+ diskDir , err := store .DiskDir (diskName )
446+ if err != nil {
447+ return err
448+ }
449+
450+ if _ , err := os .Stat (diskDir ); ! errors .Is (err , fs .ErrNotExist ) {
451+ return fmt .Errorf ("disk %q already exists (%q)" , diskName , diskDir )
452+ }
453+
454+ f , err := os .Open (fName )
455+ if err != nil {
456+ return err
457+ }
458+ defer f .Close ()
459+
460+ img , err := qcow2reader .Open (f )
461+ if err != nil {
462+ return err
463+ }
464+
465+ diskSize := img .Size ()
466+ format := img .Type ()
467+
468+ switch format {
469+ case "qcow2" , "raw" :
470+ default :
471+ return fmt .Errorf (`disk format %q not supported, use "qcow2" or "raw" instead` , format )
472+ }
473+
474+ if err := os .MkdirAll (diskDir , 0o755 ); err != nil {
475+ return err
476+ }
477+
478+ if err := contfs .CopyFile (filepath .Join (diskDir , filenames .DataDisk ), fName ); err != nil {
479+ return nil
480+ }
481+
482+ logrus .Infof ("Imported %s with size %s" , diskName , units .BytesSize (float64 (diskSize )))
483+
484+ return nil
485+ }
0 commit comments