github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/pkg/blkid/blkid.go (about)

     1  //go:build (linux && ignore) || cgo
     2  // +build linux,ignore cgo
     3  
     4  /*
     5  Copyright 2020 The OpenEBS Authors
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11      http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package blkid
    21  
    22  /*
    23   #cgo LDFLAGS: -lblkid
    24  #include "blkid/blkid.h"
    25  #include "string.h"
    26  #include "stdlib.h"
    27  */
    28  import "C"
    29  import (
    30  	"unsafe"
    31  )
    32  
    33  const (
    34  	fsTypeIdentifier             = "TYPE"
    35  	labelIdentifier              = "LABEL"
    36  	partitionTableUUIDIdentifier = "PTUUID"
    37  	partitionEntryUUIDIdentifier = "PARTUUID"
    38  )
    39  
    40  type DeviceIdentifier struct {
    41  	DevPath string
    42  }
    43  
    44  // GetOnDiskFileSystem returns the filesystem present on the disk by reading from the disk
    45  // using libblkid
    46  func (di *DeviceIdentifier) GetOnDiskFileSystem() string {
    47  	return di.GetTagValue(fsTypeIdentifier)
    48  }
    49  
    50  // GetOnDiskLabel returns the label present on the disk by reading from the disk
    51  // using libblkid
    52  func (di *DeviceIdentifier) GetOnDiskLabel() string {
    53  	return di.GetTagValue(labelIdentifier)
    54  }
    55  
    56  // GetPartitionTableUUID returns the partition table UUID present on the disk by reading from the disk
    57  // using libblkid
    58  func (di *DeviceIdentifier) GetPartitionTableUUID() string {
    59  	return di.GetTagValue(partitionTableUUIDIdentifier)
    60  }
    61  
    62  // GetPartitionEntryUUID returns the UUID of the partition, by reading from the disk using libblkid
    63  func (di *DeviceIdentifier) GetPartitionEntryUUID() string {
    64  	return di.GetTagValue(partitionEntryUUIDIdentifier)
    65  }
    66  
    67  func (di *DeviceIdentifier) GetTagValue(tag string) string {
    68  	var blkidType *C.char
    69  	blkidType = C.CString(tag)
    70  	defer C.free(unsafe.Pointer(blkidType))
    71  
    72  	var device *C.char
    73  	device = C.CString(di.DevPath)
    74  	defer C.free(unsafe.Pointer(device))
    75  
    76  	var tagValue *C.char
    77  	tagValue = C.blkid_get_tag_value(nil, blkidType, device)
    78  	defer C.free(unsafe.Pointer(tagValue))
    79  
    80  	return C.GoString(tagValue)
    81  }