1 | /* $Id: VBoxVolInfo.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Apps - VBoxVolInfo, Volume information tool.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 |
|
---|
30 | /*********************************************************************************************************************************
|
---|
31 | * Header Files *
|
---|
32 | *********************************************************************************************************************************/
|
---|
33 | #include <dirent.h>
|
---|
34 | extern "C"
|
---|
35 | {
|
---|
36 | #define private privatekw
|
---|
37 | #include <libdevmapper.h>
|
---|
38 | }
|
---|
39 | #include <stdio.h>
|
---|
40 | #include <sys/types.h>
|
---|
41 | #include <sys/stat.h>
|
---|
42 | #include <unistd.h>
|
---|
43 |
|
---|
44 |
|
---|
45 | /*********************************************************************************************************************************
|
---|
46 | * Function Prototypes *
|
---|
47 | *********************************************************************************************************************************/
|
---|
48 | void print_dev_name(dev_t devid);
|
---|
49 |
|
---|
50 | /*
|
---|
51 | * Extracts logical volume dependencies via devmapper API and print them out.
|
---|
52 | */
|
---|
53 | int main(int argc, char **argv)
|
---|
54 | {
|
---|
55 | struct dm_task *dmtask;
|
---|
56 | struct dm_info dminfo;
|
---|
57 |
|
---|
58 | if (argc != 2)
|
---|
59 | {
|
---|
60 | fprintf(stderr, "USAGE: %s <volume_name>\n", argv[0]);
|
---|
61 | return 1;
|
---|
62 | }
|
---|
63 |
|
---|
64 | dmtask = dm_task_create(DM_DEVICE_DEPS);
|
---|
65 | if (!dmtask)
|
---|
66 | return 2;
|
---|
67 |
|
---|
68 | if (dm_task_set_name(dmtask, argv[1]))
|
---|
69 | if (dm_task_run(dmtask))
|
---|
70 | if (dm_task_get_info(dmtask, &dminfo))
|
---|
71 | {
|
---|
72 | struct dm_deps *dmdeps = dm_task_get_deps(dmtask);
|
---|
73 | if (dmdeps)
|
---|
74 | {
|
---|
75 | unsigned i;
|
---|
76 | for (i = 0; i < dmdeps->count; ++i)
|
---|
77 | print_dev_name(dmdeps->device[i]);
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | dm_task_destroy(dmtask);
|
---|
82 | return 0;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /*
|
---|
86 | * Looks up device name by id using /dev directory. Prints it to stdout.
|
---|
87 | */
|
---|
88 | void print_dev_name(dev_t devid)
|
---|
89 | {
|
---|
90 | char path[PATH_MAX];
|
---|
91 | struct dirent *de;
|
---|
92 | DIR *dir = opendir("/dev");
|
---|
93 |
|
---|
94 | while ((de = readdir(dir)) != NULL)
|
---|
95 | {
|
---|
96 | struct stat st;
|
---|
97 | snprintf(path, sizeof(path), "/dev/%s", de->d_name);
|
---|
98 | if (!stat(path, &st))
|
---|
99 | if (S_ISBLK(st.st_mode))
|
---|
100 | if (devid == st.st_rdev)
|
---|
101 | {
|
---|
102 | puts(de->d_name);
|
---|
103 | break;
|
---|
104 | }
|
---|
105 | }
|
---|
106 | closedir(dir);
|
---|
107 | }
|
---|