Skip to content

Commit

Permalink
exec/util: add blkid API to query block devices with FSYTPE=UDF
Browse files Browse the repository at this point in the history
This change adds an initial building block to support Azure gen2
VMs by fetching block devices with the filesystem set to UDF.
We need to be a little defensive here by considering a case where
more than one block device might have the same filesystem.
  • Loading branch information
sohankunkerkar committed Jul 1, 2021
1 parent 6544b42 commit ad7af2d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
38 changes: 38 additions & 0 deletions internal/exec/util/blkid.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <blkid/blkid.h>
#include "blkid.h"
#include <stdlib.h>

// blkid_free_probe is safe to call with NULL pointers
static inline void _free_probe(blkid_probe *pr) { if (pr) blkid_free_probe(*pr); }
Expand Down Expand Up @@ -248,3 +249,40 @@ static result_t extract_part_info(blkid_partition part, struct partition_info *i

return RESULT_OK;
}

// blkid_get_block_devices_with_udf fetches the block devices with FSTYPE set to udf.
result_t blkid_get_block_devices_with_udf(struct block_device_list *device){
blkid_dev_iterate iter;
blkid_dev dev;
blkid_cache cache = NULL;
int err, count = 0;
const char *ctmp = NULL;

if ((err = blkid_get_cache(&cache,"/dev/null") != 0))
return RESULT_FAIL_TO_RETRIEVE;

if ((err = blkid_probe_all(cache) != 0))
return RESULT_FAIL_TO_RETRIEVE;

iter = blkid_dev_iterate_begin(cache);

blkid_dev_set_search(iter, "TYPE", "udf");

while (blkid_dev_next(iter, &dev) == 0) {
dev = blkid_verify(cache, dev);
if (!dev)
continue;
ctmp = blkid_dev_devname(dev);
err = checked_copy(device->path[count], ctmp, MAX_BLOCK_DEVICE_PATH_LEN);
if (err)
return err;
count++;
if (count > MAX_BLOCK_DEVICES)
return RESULT_MAX_BLOCK_DEVICES;
}
blkid_dev_iterate_end(iter);
if (count == 0)
return RESULT_NO_BLOCK_DEVICE_FOUND;
device->length = count;
return RESULT_OK;
}
17 changes: 17 additions & 0 deletions internal/exec/util/blkid.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,20 @@ func filesystemLookup(device string, allowAmbivalent bool, fieldName string) (st
}
return string(buf[:bytes.IndexByte(buf[:], 0)]), nil
}

// GetUdfBlockDevices returns a slice of the block devices with filesystem set to udf
func GetUdfBlockDevices() ([]string, error) {
var dev C.struct_block_device_list
res := C.blkid_get_block_devices_with_udf(&dev)

if res == C.RESULT_LOOKUP_FAILED || res == C.RESULT_OVERFLOW || res == C.RESULT_FAIL_TO_RETRIEVE || res == C.RESULT_MAX_BLOCK_DEVICES || res == C.RESULT_NO_BLOCK_DEVICE_FOUND {
return nil, fmt.Errorf("failed to retrieve block devices with filesystem set to udf")
}

length := int(dev.length)
blkDeviceList := make([]string, length)
for i := 0; i < length; i++ {
blkDeviceList[i] = C.GoString(&dev.path[i][0])
}
return blkDeviceList, nil
}
14 changes: 13 additions & 1 deletion internal/exec/util/blkid.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ typedef enum {
RESULT_NO_PARTITION_TABLE,
RESULT_BAD_INDEX,
RESULT_GET_PARTLIST_FAILED,
RESULT_FAIL_TO_RETRIEVE,
RESULT_DISK_HAS_NO_TYPE,
RESULT_DISK_NOT_GPT,
RESULT_BAD_PARAMS,
RESULT_NO_BLOCK_DEVICE_FOUND,
RESULT_OVERFLOW,
RESULT_MAX_BLOCK_DEVICES,
RESULT_NO_TOPO,
RESULT_NO_SECTOR_SIZE,
RESULT_BAD_SECTOR_SIZE,
Expand All @@ -48,6 +51,14 @@ struct partition_info {
int number;
};

#define MAX_BLOCK_DEVICES 10
#define MAX_BLOCK_DEVICE_PATH_LEN 50

struct block_device_list {
char path[MAX_BLOCK_DEVICES][MAX_BLOCK_DEVICE_PATH_LEN];
int length;
};

result_t blkid_lookup(const char *device, bool allow_ambivalent, const char *field_name, char buf[], size_t buf_len);

result_t blkid_get_num_partitions(const char *device, int *ret);
Expand All @@ -57,5 +68,6 @@ result_t blkid_get_logical_sector_size(const char *device, int *ret_sector_size)
// WARNING part_num may not be what you expect. see the .c file's comment for why
result_t blkid_get_partition(const char *device, int part_num, struct partition_info *info);

#endif // _BLKID_H_
result_t blkid_get_block_devices_with_udf(struct block_device_list *device);

#endif // _BLKID_H_
1 change: 1 addition & 0 deletions internal/providers/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func FetchFromOvfDevice(f *resource.Fetcher, ovfFsTypes []string) (types.Config,

logger := f.Logger
logger.Debug("waiting for config DVD...")

waitForCdrom(logger, devicePath)

fsType, err := checkOvfFsType(logger, devicePath, ovfFsTypes)
Expand Down

0 comments on commit ad7af2d

Please sign in to comment.