Add "dmctl uuid" command

This makes looking up device UUIDs a bit easier, as rather than depending on
the device mapper's sysfs layout we can depend on dmctl.  There's some
associated libdm plumbing, but the UUID was already pretty much availiable.

Test: I just ran this by hand.
Signed-off-by: Palmer Dabbelt <palmerdabbelt@google.com>
Change-Id: I7028eec6ab04601308047b67057e51a0ff44c0a7
This commit is contained in:
Palmer Dabbelt 2020-08-12 12:49:44 -07:00 committed by Akilesh Kailash
parent fd19ef40e8
commit 7dd8fe8e70
3 changed files with 39 additions and 0 deletions

View File

@ -427,6 +427,20 @@ bool DeviceMapper::GetDmDevicePathByName(const std::string& name, std::string* p
return true;
}
// Accepts a device mapper device name (like system_a, vendor_b etc) and
// returns its UUID.
bool DeviceMapper::GetDmDeviceUUIDByName(const std::string& name, std::string* uuid) {
struct dm_ioctl io;
InitIo(&io, name);
if (ioctl(fd_, DM_DEV_STATUS, &io) < 0) {
PLOG(WARNING) << "DM_DEV_STATUS failed for " << name;
return false;
}
*uuid = std::string(io.uuid);
return true;
}
bool DeviceMapper::GetDeviceNumber(const std::string& name, dev_t* dev) {
struct dm_ioctl io;
InitIo(&io, name);

View File

@ -172,6 +172,13 @@ class DeviceMapper final {
// could race with ueventd.
bool GetDmDevicePathByName(const std::string& name, std::string* path);
// Returns the device mapper UUID for a given name. If the device does not
// exist, false is returned, and the path parameter is not set.
//
// WaitForFile() should not be used in conjunction with this call, since it
// could race with ueventd.
bool GetDmDeviceUUIDByName(const std::string& name, std::string* path);
// Returns a device's unique path as generated by ueventd. This will return
// true as long as the device has been created, even if ueventd has not
// processed it yet.

View File

@ -49,6 +49,7 @@ static int Usage(void) {
std::cerr << " delete <dm-name>" << std::endl;
std::cerr << " list <devices | targets> [-v]" << std::endl;
std::cerr << " getpath <dm-name>" << std::endl;
std::cerr << " getuuid <dm-name>" << std::endl;
std::cerr << " info <dm-name>" << std::endl;
std::cerr << " status <dm-name>" << std::endl;
std::cerr << " resume <dm-name>" << std::endl;
@ -391,6 +392,22 @@ static int GetPathCmdHandler(int argc, char** argv) {
return 0;
}
static int GetUUIDCmdHandler(int argc, char** argv) {
if (argc != 1) {
std::cerr << "Invalid arguments, see \'dmctl help\'" << std::endl;
return -EINVAL;
}
DeviceMapper& dm = DeviceMapper::Instance();
std::string uuid;
if (!dm.GetDmDeviceUUIDByName(argv[0], &uuid)) {
std::cerr << "Could not query uuid of device \"" << argv[0] << "\"." << std::endl;
return -EINVAL;
}
std::cout << uuid << std::endl;
return 0;
}
static int InfoCmdHandler(int argc, char** argv) {
if (argc != 1) {
std::cerr << "Invalid arguments, see \'dmctl help\'" << std::endl;
@ -504,6 +521,7 @@ static std::map<std::string, std::function<int(int, char**)>> cmdmap = {
{"list", DmListCmdHandler},
{"help", HelpCmdHandler},
{"getpath", GetPathCmdHandler},
{"getuuid", GetUUIDCmdHandler},
{"info", InfoCmdHandler},
{"table", TableCmdHandler},
{"status", StatusCmdHandler},