@@ -10,8 +10,10 @@ import (
10
10
"io/fs"
11
11
"os"
12
12
"text/tabwriter"
13
+ "path/filepath"
13
14
14
15
"github.com/docker/go-units"
16
+ "github.com/lima-vm/go-qcow2reader"
15
17
"github.com/lima-vm/lima/pkg/nativeimgutil"
16
18
"github.com/lima-vm/lima/pkg/qemu"
17
19
"github.com/lima-vm/lima/pkg/store"
@@ -44,6 +46,7 @@ func newDiskCommand() *cobra.Command {
44
46
newDiskDeleteCommand (),
45
47
newDiskUnlockCommand (),
46
48
newDiskResizeCommand (),
49
+ newDiskAddCommand (),
47
50
)
48
51
return diskCommand
49
52
}
@@ -418,3 +421,64 @@ func diskResizeAction(cmd *cobra.Command, args []string) error {
418
421
func diskBashComplete (cmd * cobra.Command , _ []string , _ string ) ([]string , cobra.ShellCompDirective ) {
419
422
return bashCompleteDiskNames (cmd )
420
423
}
424
+
425
+ // func newDiskRegisterCommand()
426
+ func newDiskAddCommand () * cobra.Command {
427
+ diskAddCommand := & cobra.Command {
428
+ Use : "add existing DISK to Lima" ,
429
+ Example : `
430
+ Add a disk:
431
+ $ limactl disk add DISK` ,
432
+ Short : "Add existing to Lima" ,
433
+ Args : WrapArgsError (cobra .ExactArgs (1 )),
434
+ RunE : diskAddAction ,
435
+ ValidArgsFunction : diskBashComplete ,
436
+ }
437
+
438
+ diskAddCommand .Flags ().String ("filename" , "" , "Path to disk image" )
439
+ _ = diskAddCommand .MarkFlagRequired ("filename" )
440
+ return diskAddCommand
441
+ }
442
+
443
+ func diskAddAction (cmd * cobra.Command , args []string ) error {
444
+ fName , err := cmd .Flags ().GetString ("filename" )
445
+ if err != nil {
446
+ return err
447
+ }
448
+
449
+ f , err := os .Open (fName )
450
+ defer f .Close ()
451
+ if err != nil {
452
+ return err
453
+ }
454
+
455
+ img , err := qcow2reader .Open (f )
456
+ if err != nil {
457
+ return err
458
+ }
459
+
460
+ diskName := args [0 ]
461
+ // diskName := filepath.Base(fName)
462
+ // diskName = strings.TrimSuffix(diskName, filepath.Ext(diskName))
463
+ diskSize := img .Size ()
464
+ format := img .Type ()
465
+
466
+ switch format {
467
+ case "qcow2" , "raw" :
468
+ default :
469
+ return fmt .Errorf (`disk format %q not supported, use "qcow2" or "raw" instead` , format )
470
+ }
471
+
472
+ diskDir , err := store .DiskDir (diskName )
473
+ if err := os .MkdirAll (diskDir , 0755 ); err != nil {
474
+ return err
475
+ }
476
+
477
+ if err := os .Symlink (fName , filepath .Join (diskDir , "datadisk" )); err != nil {
478
+ return err
479
+ }
480
+
481
+ logrus .Infof ("Add %s with size %s to Lima" , diskName , units .BytesSize (float64 (diskSize )))
482
+
483
+ return nil
484
+ }
0 commit comments