github.com/vmware/govmomi@v0.43.0/govc/host/storage/partition.go (about)

     1  /*
     2  Copyright (c) 2015 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package storage
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  	"io"
    24  	"text/tabwriter"
    25  
    26  	"github.com/vmware/govmomi/govc/cli"
    27  	"github.com/vmware/govmomi/govc/flags"
    28  	"github.com/vmware/govmomi/units"
    29  	"github.com/vmware/govmomi/vim25/mo"
    30  	"github.com/vmware/govmomi/vim25/types"
    31  )
    32  
    33  type partition struct {
    34  	*flags.HostSystemFlag
    35  	*flags.OutputFlag
    36  }
    37  
    38  func init() {
    39  	cli.Register("host.storage.partition", &partition{})
    40  }
    41  
    42  func (cmd *partition) Register(ctx context.Context, f *flag.FlagSet) {
    43  	cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx)
    44  	cmd.HostSystemFlag.Register(ctx, f)
    45  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    46  	cmd.OutputFlag.Register(ctx, f)
    47  }
    48  
    49  func (cmd *partition) Process(ctx context.Context) error {
    50  	if err := cmd.HostSystemFlag.Process(ctx); err != nil {
    51  		return err
    52  	}
    53  	if err := cmd.OutputFlag.Process(ctx); err != nil {
    54  		return err
    55  	}
    56  	return nil
    57  }
    58  
    59  func (cmd *partition) Usage() string {
    60  	return "DEVICE_PATH"
    61  }
    62  
    63  func (cmd *partition) Description() string {
    64  	return `Show partition table for device at DEVICE_PATH.`
    65  }
    66  
    67  func (cmd *partition) Run(ctx context.Context, f *flag.FlagSet) error {
    68  	if f.NArg() != 1 {
    69  		return fmt.Errorf("specify device path")
    70  	}
    71  
    72  	path := f.Args()[0]
    73  
    74  	host, err := cmd.HostSystem()
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	ss, err := host.ConfigManager().StorageSystem(ctx)
    80  	if err != nil {
    81  		return err
    82  	}
    83  
    84  	var hss mo.HostStorageSystem
    85  	err = ss.Properties(ctx, ss.Reference(), nil, &hss)
    86  	if err != nil {
    87  		return nil
    88  	}
    89  
    90  	info, err := ss.RetrieveDiskPartitionInfo(ctx, path)
    91  	if err != nil {
    92  		return err
    93  	}
    94  
    95  	return cmd.WriteResult(partitionInfo(*info))
    96  }
    97  
    98  type partitionInfo types.HostDiskPartitionInfo
    99  
   100  func (p partitionInfo) Write(w io.Writer) error {
   101  	tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
   102  
   103  	fmt.Fprintf(tw, "Table format: %s\n", p.Spec.PartitionFormat)
   104  	fmt.Fprintf(tw, "Number of sectors: %d\n", p.Spec.TotalSectors)
   105  	fmt.Fprintf(tw, "\n")
   106  
   107  	fmt.Fprintf(tw, "Number\t")
   108  	fmt.Fprintf(tw, "Start\t")
   109  	fmt.Fprintf(tw, "End\t")
   110  	fmt.Fprintf(tw, "Size\t")
   111  	fmt.Fprintf(tw, "Type\t")
   112  	fmt.Fprintf(tw, "\n")
   113  
   114  	for _, e := range p.Spec.Partition {
   115  		sectors := e.EndSector - e.StartSector
   116  
   117  		fmt.Fprintf(tw, "%d\t", e.Partition)
   118  		fmt.Fprintf(tw, "%d\t", e.StartSector)
   119  		fmt.Fprintf(tw, "%d\t", e.EndSector)
   120  		fmt.Fprintf(tw, "%s\t", units.ByteSize(sectors*512))
   121  		fmt.Fprintf(tw, "%s\t", e.Type)
   122  		fmt.Fprintf(tw, "\n")
   123  	}
   124  
   125  	return tw.Flush()
   126  }