blob: c12383011196a90c4493d14c448b9baf29b7868b [file] [log] [blame]
Sandeep Patil59f04ee2018-05-30 13:46:55 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <errno.h>
18#include <fcntl.h>
19#include <getopt.h>
20#include <linux/dm-ioctl.h>
21#include <sys/ioctl.h>
22#include <sys/types.h>
23#include <unistd.h>
24
25#include <android-base/unique_fd.h>
26#include <dm.h>
27
28#include <functional>
29#include <iomanip>
30#include <ios>
31#include <iostream>
32#include <map>
33#include <string>
34#include <vector>
35
36using DeviceMapper = ::android::dm::DeviceMapper;
37using DmTarget = ::android::dm::DmTarget;
38
39static int Usage(void) {
40 std::cerr << "usage: dmctl <command> [command options]";
41 std::cerr << "commands:";
Sandeep Patil45d94ab2018-06-12 17:03:56 -070042 std::cerr << " create <dm-name> [dm-target> [-lo <filename>] <dm-target-args>]";
43 std::cerr, " delete <dm-name>";
Sandeep Patil59f04ee2018-05-30 13:46:55 -070044 std::cerr, " list";
45 std::cerr, " help";
46 return -EINVAL;
47}
48
49static int DmCreateCmdHandler(int argc, char** argv) {
Sandeep Patil45d94ab2018-06-12 17:03:56 -070050 if (argc < 1) {
51 std::cerr << "DmCreateCmdHandler: atleast 'name' MUST be provided for target device";
Sandeep Patil59f04ee2018-05-30 13:46:55 -070052 return -EINVAL;
53 }
54
Sandeep Patil45d94ab2018-06-12 17:03:56 -070055 std::string name = argv[0];
Sandeep Patil59f04ee2018-05-30 13:46:55 -070056 DeviceMapper& dm = DeviceMapper::Instance();
Sandeep Patil45d94ab2018-06-12 17:03:56 -070057 if (!dm.CreateDevice(name)) {
58 std::cerr << "DmCreateCmdHandler: Failed to create " << name << " device";
59 return -EIO;
Sandeep Patil59f04ee2018-05-30 13:46:55 -070060 }
61
Sandeep Patil45d94ab2018-06-12 17:03:56 -070062 // if we also have target specified
63 if (argc > 1) {
64 // fall through for now. This will eventually create a DmTarget() based on the target name
65 // passing it the table that is specified at the command line
Sandeep Patil59f04ee2018-05-30 13:46:55 -070066 }
67
Sandeep Patil45d94ab2018-06-12 17:03:56 -070068 return 0;
Sandeep Patil59f04ee2018-05-30 13:46:55 -070069}
70
71static int DmDeleteCmdHandler(int argc, char** argv) {
Sandeep Patil45d94ab2018-06-12 17:03:56 -070072 if (argc < 1) {
73 std::cerr << "DmCreateCmdHandler: atleast 'name' MUST be provided for target device";
74 return -EINVAL;
75 }
76
77 std::string name = argv[0];
78 DeviceMapper& dm = DeviceMapper::Instance();
79 if (!dm.DeleteDevice(name)) {
80 std::cerr << "DmCreateCmdHandler: Failed to create " << name << " device";
81 return -EIO;
Sandeep Patil59f04ee2018-05-30 13:46:55 -070082 }
83
84 return 0;
85}
86
87static int DmListCmdHandler(int /* argc */, char** /* argv */) {
88 std::cout << "Available Device Mapper Targets:" << std::endl;
89
90 DeviceMapper& dm = DeviceMapper::Instance();
91 std::vector<DmTarget> targets;
92 if (!dm.GetAvailableTargets(&targets)) {
Sandeep Patil45d94ab2018-06-12 17:03:56 -070093 std::cerr << "Failed to read available device mapper targets" << std::endl;
Sandeep Patil59f04ee2018-05-30 13:46:55 -070094 return -errno;
95 }
96
97 if (targets.empty()) {
98 std::cout << " <empty>" << std::endl;
99 return 0;
100 }
101
102 for (const auto& target : targets) {
103 std::cout << std::left << std::setw(20) << target.name() << " : " << target.version()
104 << std::endl;
105 }
106
107 return 0;
108}
109
110static int HelpCmdHandler(int /* argc */, char** /* argv */) {
111 Usage();
112 return 0;
113}
114
115static std::map<std::string, std::function<int(int, char**)>> cmdmap = {
116 {"create", DmCreateCmdHandler},
117 {"delete", DmDeleteCmdHandler},
118 {"list", DmListCmdHandler},
119 {"help", HelpCmdHandler},
120};
121
122int main(int argc, char** argv) {
123 android::base::InitLogging(argv, &android::base::StderrLogger);
124 if (argc < 2) {
125 return Usage();
126 }
127
128 for (const auto& cmd : cmdmap) {
129 if (cmd.first == argv[1]) {
130 return cmd.second(argc - 2, argv + 2);
131 }
132 }
133
134 return Usage();
135}