create-scratch.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package main
  2. import (
  3. "os"
  4. "path/filepath"
  5. "github.com/Microsoft/hcsshim/internal/appargs"
  6. "github.com/Microsoft/hcsshim/internal/lcow"
  7. "github.com/Microsoft/hcsshim/internal/uvm"
  8. "github.com/Microsoft/hcsshim/osversion"
  9. gcsclient "github.com/Microsoft/opengcs/client"
  10. "github.com/pkg/errors"
  11. "github.com/urfave/cli"
  12. )
  13. var createScratchCommand = cli.Command{
  14. Name: "create-scratch",
  15. Usage: "creates a scratch vhdx at 'destpath' that is ext4 formatted",
  16. Description: "Creates a scratch vhdx at 'destpath' that is ext4 formatted",
  17. Flags: []cli.Flag{
  18. cli.StringFlag{
  19. Name: "destpath",
  20. Usage: "Required: describes the destination vhd path",
  21. },
  22. },
  23. Before: appargs.Validate(),
  24. Action: func(context *cli.Context) error {
  25. dest := context.String("destpath")
  26. if dest == "" {
  27. return errors.New("'destpath' is required")
  28. }
  29. // If we only have v1 lcow support do it the old way.
  30. if osversion.Get().Build < osversion.RS5 {
  31. cfg := gcsclient.Config{
  32. Options: gcsclient.Options{
  33. KirdPath: filepath.Join(os.Getenv("ProgramFiles"), "Linux Containers"),
  34. KernelFile: "kernel",
  35. InitrdFile: uvm.InitrdFile,
  36. },
  37. Name: "createscratch-uvm",
  38. UvmTimeoutSeconds: 5 * 60, // 5 Min
  39. }
  40. if err := cfg.StartUtilityVM(); err != nil {
  41. return errors.Wrapf(err, "failed to start '%s'", cfg.Name)
  42. }
  43. defer cfg.Uvm.Terminate()
  44. if err := cfg.CreateExt4Vhdx(dest, lcow.DefaultScratchSizeGB, ""); err != nil {
  45. return errors.Wrapf(err, "failed to create ext4vhdx for '%s'", cfg.Name)
  46. }
  47. } else {
  48. opts := uvm.NewDefaultOptionsLCOW("createscratch-uvm", context.GlobalString("owner"))
  49. convertUVM, err := uvm.CreateLCOW(opts)
  50. if err != nil {
  51. return errors.Wrapf(err, "failed to create '%s'", opts.ID)
  52. }
  53. defer convertUVM.Close()
  54. if err := convertUVM.Start(); err != nil {
  55. return errors.Wrapf(err, "failed to start '%s'", opts.ID)
  56. }
  57. if err := lcow.CreateScratch(convertUVM, dest, lcow.DefaultScratchSizeGB, "", ""); err != nil {
  58. return errors.Wrapf(err, "failed to create ext4vhdx for '%s'", opts.ID)
  59. }
  60. }
  61. return nil
  62. },
  63. }