|
| 1 | +/* |
| 2 | + * Copyright (C) 2026 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 "cuttlefish/host/commands/cvd/cli/commands/setup.h" |
| 18 | + |
| 19 | +#include <iostream> |
| 20 | +#include <memory> |
| 21 | +#include <string> |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +#include "absl/strings/str_join.h" |
| 25 | + |
| 26 | +#include "cuttlefish/common/libs/utils/subprocess.h" |
| 27 | +#include "cuttlefish/host/commands/cvd/cli/command_request.h" |
| 28 | +#include "cuttlefish/host/commands/cvd/cli/commands/command_handler.h" |
| 29 | +#include "cuttlefish/host/commands/cvd/cli/types.h" |
| 30 | +#include "cuttlefish/host/libs/vm_manager/host_configuration.h" |
| 31 | +#include "cuttlefish/result/result.h" |
| 32 | + |
| 33 | +namespace cuttlefish { |
| 34 | +namespace { |
| 35 | + |
| 36 | +constexpr char kSummaryHelpText[] = "Configure the host for Cuttlefish"; |
| 37 | + |
| 38 | +constexpr char kDetailedHelpText[] = |
| 39 | + R"(cvd setup - configure host for cuttlefish |
| 40 | +
|
| 41 | +Checks host configuration (kernel version, group memberships) and automatically applies fixes (e.g., adding user to kvm and cvdnetwork groups). Some fixes may require sudo permissions. |
| 42 | +
|
| 43 | +Usage: |
| 44 | + cvd setup |
| 45 | +)"; |
| 46 | + |
| 47 | +using vm_manager::HostConfigurationAction; |
| 48 | +using vm_manager::ValidateHostConfiguration; |
| 49 | + |
| 50 | +class CvdSetupHandler : public CvdCommandHandler { |
| 51 | + public: |
| 52 | + CvdSetupHandler() = default; |
| 53 | + |
| 54 | + Result<void> Handle(const CommandRequest& request) override { |
| 55 | + CF_EXPECT(CanHandle(request)); |
| 56 | + |
| 57 | + std::vector<HostConfigurationAction> actions = |
| 58 | + CF_EXPECT(ValidateHostConfiguration()); |
| 59 | + if (actions.empty()) { |
| 60 | + std::cout << "Host configuration is already valid. No setup required." |
| 61 | + << std::endl; |
| 62 | + return {}; |
| 63 | + } |
| 64 | + |
| 65 | + std::cout << "Applying host configuration fixes..." << std::endl; |
| 66 | + for (const HostConfigurationAction& action : actions) { |
| 67 | + if (!action.description.empty()) { |
| 68 | + std::cout << "Purpose: " << action.description << std::endl; |
| 69 | + } |
| 70 | + if (action.command.empty()) { |
| 71 | + std::cout |
| 72 | + << "Manual intervention required (no automated command available)." |
| 73 | + << std::endl; |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + std::cout << "Running: " << absl::StrJoin(action.command, " ") |
| 78 | + << std::endl; |
| 79 | + const int status = Execute(action.command); |
| 80 | + CF_EXPECTF(status == 0, "Failed to execute command: `{}`, exit code: {}", |
| 81 | + absl::StrJoin(action.command, " "), status); |
| 82 | + } |
| 83 | + |
| 84 | + std::cout << "Setup completed successfully." << std::endl; |
| 85 | + return {}; |
| 86 | + } |
| 87 | + |
| 88 | + cvd_common::Args CmdList() const override { return {"setup"}; } |
| 89 | + |
| 90 | + Result<std::string> SummaryHelp() const override { return kSummaryHelpText; } |
| 91 | + |
| 92 | + bool RequiresDeviceExists() const override { return false; } |
| 93 | + |
| 94 | + bool RequiresHostConfiguration() const override { return false; } |
| 95 | + |
| 96 | + Result<std::string> DetailedHelp(const CommandRequest&) const override { |
| 97 | + return kDetailedHelpText; |
| 98 | + } |
| 99 | +}; |
| 100 | + |
| 101 | +} // namespace |
| 102 | + |
| 103 | +std::unique_ptr<CvdCommandHandler> NewCvdSetupHandler() { |
| 104 | + return std::unique_ptr<CvdCommandHandler>(new CvdSetupHandler()); |
| 105 | +} |
| 106 | + |
| 107 | +} // namespace cuttlefish |
0 commit comments