1 /*
2  * Copyright (C) 2023 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 <iostream>
18 #include <optional>
19 #include <string>
20 #include <vector>
21 
22 #include <android-base/logging.h>
23 #include <android-base/scopeguard.h>
24 #include <fmt/core.h>
25 #include <google/protobuf/text_format.h>
26 
27 #include "common/libs/fs/shared_fd.h"
28 #include "common/libs/utils/files.h"
29 #include "common/libs/utils/flag_parser.h"
30 #include "common/libs/utils/result.h"
31 #include "host/commands/snapshot_util_cvd/parse.h"
32 #include "host/commands/snapshot_util_cvd/snapshot_taker.h"
33 #include "host/libs/command_util/util.h"
34 #include "host/libs/config/cuttlefish_config.h"
35 #include "run_cvd.pb.h"
36 
37 namespace cuttlefish {
38 namespace {
39 
ToAbsolutePath(const std::string & snapshot_path)40 Result<std::string> ToAbsolutePath(const std::string& snapshot_path) {
41   const InputPathForm default_path_form{
42       .current_working_dir = std::nullopt,
43       .home_dir = std::nullopt,
44       .path_to_convert = snapshot_path,
45       .follow_symlink = false,
46   };
47   return CF_EXPECTF(
48       EmulateAbsolutePath(default_path_form),
49       "The snapshot path, \"{}\", cannot be converted to an absolute path",
50       snapshot_path);
51 }
52 
53 // Send a `LauncherAction` RPC to every instance specified in `parsed`.
BroadcastLauncherAction(const CuttlefishConfig & config,const Parsed & parsed,run_cvd::ExtendedLauncherAction extended_action)54 Result<void> BroadcastLauncherAction(
55     const CuttlefishConfig& config, const Parsed& parsed,
56     run_cvd::ExtendedLauncherAction extended_action) {
57   for (const auto instance_num : parsed.instance_nums) {
58     LOG(INFO) << "Instance #" << instance_num
59               << ": Sending request: " << extended_action.ShortDebugString();
60     auto socket = CF_EXPECT(
61         GetLauncherMonitor(config, instance_num, parsed.wait_for_launcher));
62     CF_EXPECT(RunLauncherAction(socket, extended_action, std::nullopt));
63   }
64   return {};
65 }
66 
SnapshotCvdMain(std::vector<std::string> args)67 Result<void> SnapshotCvdMain(std::vector<std::string> args) {
68   CF_EXPECT(!args.empty(), "No arguments was given");
69   const auto prog_path = args.front();
70   args.erase(args.begin());
71   auto parsed = CF_EXPECT(Parse(args));
72 
73   const CuttlefishConfig* config =
74       CF_EXPECT(CuttlefishConfig::Get(), "Failed to obtain config object");
75 
76   switch (parsed.cmd) {
77     case SnapshotCmd::kSuspend: {
78       run_cvd::ExtendedLauncherAction extended_action;
79       extended_action.mutable_suspend();
80       CF_EXPECT(BroadcastLauncherAction(*config, parsed, extended_action));
81       return {};
82     }
83     case SnapshotCmd::kResume: {
84       run_cvd::ExtendedLauncherAction extended_action;
85       extended_action.mutable_resume();
86       CF_EXPECT(BroadcastLauncherAction(*config, parsed, extended_action));
87       return {};
88     }
89     case SnapshotCmd::kSnapshotTake: {
90       CF_EXPECT(!parsed.snapshot_path.empty(), "--snapshot_path is required");
91       parsed.snapshot_path = CF_EXPECT(ToAbsolutePath(parsed.snapshot_path));
92       if (parsed.force &&
93           FileExists(parsed.snapshot_path, /* follow symlink */ false)) {
94         CF_EXPECT(RecursivelyRemoveDirectory(parsed.snapshot_path),
95                   "Failed to delete preexisting snapshot dir");
96       }
97       CF_EXPECTF(!FileExists(parsed.snapshot_path, /* follow symlink */ false),
98                  "Delete the destination directory \"{}\" first",
99                  parsed.snapshot_path);
100 
101       // Automatically suspend and resume if requested.
102       if (parsed.auto_suspend) {
103         run_cvd::ExtendedLauncherAction extended_action;
104         extended_action.mutable_suspend();
105         CF_EXPECT(BroadcastLauncherAction(*config, parsed, extended_action));
106       }
107       auto maybe_resume_on_exit =
108           android::base::ScopeGuard([&parsed, &config]() {
109             if (!parsed.auto_suspend) {
110               return;
111             }
112             run_cvd::ExtendedLauncherAction extended_action;
113             extended_action.mutable_resume();
114             Result<void> result =
115                 BroadcastLauncherAction(*config, parsed, extended_action);
116             if (!result.ok()) {
117               LOG(FATAL) << "RunLauncherAction failed: "
118                          << result.error().FormatForEnv();
119             }
120           });
121 
122       // Delete incomplete snapshot if we fail partway.
123       android::base::ScopeGuard delete_snapshot_on_fail([&parsed]() {
124         if (!parsed.cleanup_snapshot_path) {
125           return;
126         }
127         LOG(ERROR) << "Snapshot take failed, so running clean-up.";
128         Result<void> result = RecursivelyRemoveDirectory(parsed.snapshot_path);
129         if (!result.ok()) {
130           LOG(ERROR) << "Failed to delete incomplete snapshot: "
131                      << result.error().FormatForEnv();
132         }
133       });
134 
135       // Snapshot group-level host runtime files and generate snapshot metadata
136       // file.
137       const std::string meta_json_path =
138           CF_EXPECT(HandleHostGroupSnapshot(parsed.snapshot_path),
139                     "Failed to back up the group-level host runtime files.");
140       // Snapshot each instance.
141       run_cvd::ExtendedLauncherAction extended_action;
142       extended_action.mutable_snapshot_take()->set_snapshot_path(
143           meta_json_path);
144       CF_EXPECT(BroadcastLauncherAction(*config, parsed, extended_action));
145       delete_snapshot_on_fail.Disable();
146       return {};
147     }
148     default: {
149       return CF_ERRF("unknown cmd: {}", (int)parsed.cmd);
150     }
151   }
152 }
153 
154 }  // namespace
155 }  // namespace cuttlefish
156 
main(int argc,char ** argv)157 int main(int argc, char** argv) {
158   ::android::base::InitLogging(argv, android::base::StderrLogger);
159   std::vector<std::string> all_args = cuttlefish::ArgsToVec(argc, argv);
160   auto result = cuttlefish::SnapshotCvdMain(std::move(all_args));
161   if (!result.ok()) {
162     LOG(ERROR) << result.error().FormatForEnv();
163     return EXIT_FAILURE;
164   }
165   return EXIT_SUCCESS;
166 }
167