github.com/google/cadvisor@v0.49.1/devicemapper/dmsetup_client.go (about)

     1  // Copyright 2016 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package devicemapper
    16  
    17  import (
    18  	"os/exec"
    19  	"strconv"
    20  	"strings"
    21  
    22  	"k8s.io/klog/v2"
    23  )
    24  
    25  // DmsetupClient is a low-level client for interacting with device mapper via
    26  // the `dmsetup` utility, which is provided by the `device-mapper` package.
    27  type DmsetupClient interface {
    28  	// Table runs `dmsetup table` on the given device name and returns the
    29  	// output or an error.
    30  	Table(deviceName string) ([]byte, error)
    31  	// Message runs `dmsetup message` on the given device, passing the given
    32  	// message to the given sector, and returns the output or an error.
    33  	Message(deviceName string, sector int, message string) ([]byte, error)
    34  	// Status runs `dmsetup status` on the given device and returns the output
    35  	// or an error.
    36  	Status(deviceName string) ([]byte, error)
    37  }
    38  
    39  // NewDmSetupClient returns a new DmsetupClient.
    40  func NewDmsetupClient() DmsetupClient {
    41  	return &defaultDmsetupClient{}
    42  }
    43  
    44  // defaultDmsetupClient is a functional DmsetupClient
    45  type defaultDmsetupClient struct{}
    46  
    47  var _ DmsetupClient = &defaultDmsetupClient{}
    48  
    49  func (c *defaultDmsetupClient) Table(deviceName string) ([]byte, error) {
    50  	return c.dmsetup("table", deviceName)
    51  }
    52  
    53  func (c *defaultDmsetupClient) Message(deviceName string, sector int, message string) ([]byte, error) {
    54  	return c.dmsetup("message", deviceName, strconv.Itoa(sector), message)
    55  }
    56  
    57  func (c *defaultDmsetupClient) Status(deviceName string) ([]byte, error) {
    58  	return c.dmsetup("status", deviceName)
    59  }
    60  
    61  func (*defaultDmsetupClient) dmsetup(args ...string) ([]byte, error) {
    62  	klog.V(5).Infof("running dmsetup %v", strings.Join(args, " "))
    63  	return exec.Command("dmsetup", args...).Output()
    64  }