github.com/vmware/govmomi@v0.51.0/cli/host/storage/partition.go (about)

     1  // © Broadcom. All Rights Reserved.
     2  // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package storage
     6  
     7  import (
     8  	"context"
     9  	"flag"
    10  	"fmt"
    11  	"io"
    12  	"text/tabwriter"
    13  
    14  	"github.com/vmware/govmomi/cli"
    15  	"github.com/vmware/govmomi/cli/flags"
    16  	"github.com/vmware/govmomi/units"
    17  	"github.com/vmware/govmomi/vim25/mo"
    18  	"github.com/vmware/govmomi/vim25/types"
    19  )
    20  
    21  type partition struct {
    22  	*flags.HostSystemFlag
    23  	*flags.OutputFlag
    24  }
    25  
    26  func init() {
    27  	cli.Register("host.storage.partition", &partition{})
    28  }
    29  
    30  func (cmd *partition) Register(ctx context.Context, f *flag.FlagSet) {
    31  	cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx)
    32  	cmd.HostSystemFlag.Register(ctx, f)
    33  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    34  	cmd.OutputFlag.Register(ctx, f)
    35  }
    36  
    37  func (cmd *partition) Process(ctx context.Context) error {
    38  	if err := cmd.HostSystemFlag.Process(ctx); err != nil {
    39  		return err
    40  	}
    41  	if err := cmd.OutputFlag.Process(ctx); err != nil {
    42  		return err
    43  	}
    44  	return nil
    45  }
    46  
    47  func (cmd *partition) Usage() string {
    48  	return "DEVICE_PATH"
    49  }
    50  
    51  func (cmd *partition) Description() string {
    52  	return `Show partition table for device at DEVICE_PATH.`
    53  }
    54  
    55  func (cmd *partition) Run(ctx context.Context, f *flag.FlagSet) error {
    56  	if f.NArg() != 1 {
    57  		return fmt.Errorf("specify device path")
    58  	}
    59  
    60  	path := f.Args()[0]
    61  
    62  	host, err := cmd.HostSystem()
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	ss, err := host.ConfigManager().StorageSystem(ctx)
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	var hss mo.HostStorageSystem
    73  	err = ss.Properties(ctx, ss.Reference(), nil, &hss)
    74  	if err != nil {
    75  		return nil
    76  	}
    77  
    78  	info, err := ss.RetrieveDiskPartitionInfo(ctx, path)
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	return cmd.WriteResult(partitionInfo(*info))
    84  }
    85  
    86  type partitionInfo types.HostDiskPartitionInfo
    87  
    88  func (p partitionInfo) Write(w io.Writer) error {
    89  	tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
    90  
    91  	fmt.Fprintf(tw, "Table format: %s\n", p.Spec.PartitionFormat)
    92  	fmt.Fprintf(tw, "Number of sectors: %d\n", p.Spec.TotalSectors)
    93  	fmt.Fprintf(tw, "\n")
    94  
    95  	fmt.Fprintf(tw, "Number\t")
    96  	fmt.Fprintf(tw, "Start\t")
    97  	fmt.Fprintf(tw, "End\t")
    98  	fmt.Fprintf(tw, "Size\t")
    99  	fmt.Fprintf(tw, "Type\t")
   100  	fmt.Fprintf(tw, "\n")
   101  
   102  	for _, e := range p.Spec.Partition {
   103  		sectors := e.EndSector - e.StartSector
   104  
   105  		fmt.Fprintf(tw, "%d\t", e.Partition)
   106  		fmt.Fprintf(tw, "%d\t", e.StartSector)
   107  		fmt.Fprintf(tw, "%d\t", e.EndSector)
   108  		fmt.Fprintf(tw, "%s\t", units.ByteSize(sectors*512))
   109  		fmt.Fprintf(tw, "%s\t", e.Type)
   110  		fmt.Fprintf(tw, "\n")
   111  	}
   112  
   113  	return tw.Flush()
   114  }