• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2012 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 "update_engine/payload_generator/delta_diff_generator.h"
18 
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <inttypes.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 
25 #include <algorithm>
26 #include <memory>
27 #include <string>
28 #include <utility>
29 #include <vector>
30 
31 #include <base/logging.h>
32 #include <base/threading/simple_thread.h>
33 
34 #include "update_engine/common/utils.h"
35 #include "update_engine/payload_consumer/delta_performer.h"
36 #include "update_engine/payload_consumer/file_descriptor.h"
37 #include "update_engine/payload_consumer/payload_constants.h"
38 #include "update_engine/payload_generator/ab_generator.h"
39 #include "update_engine/payload_generator/annotated_operation.h"
40 #include "update_engine/payload_generator/blob_file_writer.h"
41 #include "update_engine/payload_generator/cow_size_estimator.h"
42 #include "update_engine/payload_generator/delta_diff_utils.h"
43 #include "update_engine/payload_generator/full_update_generator.h"
44 #include "update_engine/payload_generator/merge_sequence_generator.h"
45 #include "update_engine/payload_generator/payload_file.h"
46 #include "update_engine/update_metadata.pb.h"
47 
48 using std::string;
49 using std::unique_ptr;
50 using std::vector;
51 
52 namespace chromeos_update_engine {
53 
54 // bytes
55 const size_t kRootFSPartitionSize = static_cast<size_t>(2) * 1024 * 1024 * 1024;
56 const size_t kBlockSize = 4096;  // bytes
57 
58 class PartitionProcessor : public base::DelegateSimpleThread::Delegate {
IsDynamicPartition(const std::string & partition_name)59   bool IsDynamicPartition(const std::string& partition_name) {
60     for (const auto& group :
61          config_.target.dynamic_partition_metadata->groups()) {
62       const auto& names = group.partition_names();
63       if (std::find(names.begin(), names.end(), partition_name) !=
64           names.end()) {
65         return true;
66       }
67     }
68     return false;
69   }
70 
71  public:
PartitionProcessor(const PayloadGenerationConfig & config,const PartitionConfig & old_part,const PartitionConfig & new_part,BlobFileWriter * file_writer,std::vector<AnnotatedOperation> * aops,std::vector<CowMergeOperation> * cow_merge_sequence,size_t * cow_size,std::unique_ptr<chromeos_update_engine::OperationsGenerator> strategy)72   explicit PartitionProcessor(
73       const PayloadGenerationConfig& config,
74       const PartitionConfig& old_part,
75       const PartitionConfig& new_part,
76       BlobFileWriter* file_writer,
77       std::vector<AnnotatedOperation>* aops,
78       std::vector<CowMergeOperation>* cow_merge_sequence,
79       size_t* cow_size,
80       std::unique_ptr<chromeos_update_engine::OperationsGenerator> strategy)
81       : config_(config),
82         old_part_(old_part),
83         new_part_(new_part),
84         file_writer_(file_writer),
85         aops_(aops),
86         cow_merge_sequence_(cow_merge_sequence),
87         cow_size_(cow_size),
88         strategy_(std::move(strategy)) {}
89   PartitionProcessor(PartitionProcessor&&) noexcept = default;
90 
Run()91   void Run() override {
92     LOG(INFO) << "Started an async task to process partition "
93               << new_part_.name;
94     bool success = strategy_->GenerateOperations(
95         config_, old_part_, new_part_, file_writer_, aops_);
96     if (!success) {
97       // ABORT the entire process, so that developer can look
98       // at recent logs and diagnose what happened
99       LOG(FATAL) << "GenerateOperations(" << old_part_.name << ", "
100                  << new_part_.name << ") failed";
101     }
102 
103     bool snapshot_enabled =
104         config_.target.dynamic_partition_metadata &&
105         config_.target.dynamic_partition_metadata->snapshot_enabled();
106     if (!snapshot_enabled || !IsDynamicPartition(new_part_.name)) {
107       return;
108     }
109     // Skip cow size estimation if VABC isn't enabled
110     if (!config_.target.dynamic_partition_metadata->vabc_enabled()) {
111       return;
112     }
113     if (!old_part_.path.empty()) {
114       auto generator = MergeSequenceGenerator::Create(*aops_);
115       if (!generator || !generator->Generate(cow_merge_sequence_)) {
116         LOG(FATAL) << "Failed to generate merge sequence";
117       }
118     }
119 
120     LOG(INFO) << "Estimating COW size for partition: " << new_part_.name;
121     // Need the contents of source/target image bytes when doing
122     // dry run.
123     FileDescriptorPtr source_fd{new EintrSafeFileDescriptor()};
124     source_fd->Open(old_part_.path.c_str(), O_RDONLY);
125 
126     auto target_fd = std::make_unique<EintrSafeFileDescriptor>();
127     target_fd->Open(new_part_.path.c_str(), O_RDONLY);
128 
129     google::protobuf::RepeatedPtrField<InstallOperation> operations;
130 
131     for (const AnnotatedOperation& aop : *aops_) {
132       *operations.Add() = aop.op;
133     }
134     *cow_size_ = EstimateCowSize(
135         std::move(target_fd),
136         std::move(operations),
137         {cow_merge_sequence_->begin(), cow_merge_sequence_->end()},
138         config_.block_size,
139         config_.target.dynamic_partition_metadata->vabc_compression_param());
140     if (!new_part_.disable_fec_computation) {
141       *cow_size_ +=
142           new_part_.verity.fec_extent.num_blocks() * config_.block_size;
143     }
144     *cow_size_ +=
145         new_part_.verity.hash_tree_extent.num_blocks() * config_.block_size;
146     LOG(INFO) << "Estimated COW size for partition: " << new_part_.name << " "
147               << *cow_size_;
148   }
149 
150  private:
151   const PayloadGenerationConfig& config_;
152   const PartitionConfig& old_part_;
153   const PartitionConfig& new_part_;
154   BlobFileWriter* file_writer_;
155   std::vector<AnnotatedOperation>* aops_;
156   std::vector<CowMergeOperation>* cow_merge_sequence_;
157   size_t* cow_size_;
158   std::unique_ptr<chromeos_update_engine::OperationsGenerator> strategy_;
159   DISALLOW_COPY_AND_ASSIGN(PartitionProcessor);
160 };
161 
GenerateUpdatePayloadFile(const PayloadGenerationConfig & config,const string & output_path,const string & private_key_path,uint64_t * metadata_size)162 bool GenerateUpdatePayloadFile(const PayloadGenerationConfig& config,
163                                const string& output_path,
164                                const string& private_key_path,
165                                uint64_t* metadata_size) {
166   if (!config.version.Validate()) {
167     LOG(ERROR) << "Unsupported major.minor version: " << config.version.major
168                << "." << config.version.minor;
169     return false;
170   }
171 
172   // Create empty payload file object.
173   PayloadFile payload;
174   TEST_AND_RETURN_FALSE(payload.Init(config));
175 
176   ScopedTempFile data_file("CrAU_temp_data.XXXXXX", true);
177   {
178     off_t data_file_size = 0;
179     BlobFileWriter blob_file(data_file.fd(), &data_file_size);
180     if (config.is_delta) {
181       TEST_AND_RETURN_FALSE(config.source.partitions.size() ==
182                             config.target.partitions.size());
183     }
184     PartitionConfig empty_part("");
185     std::vector<std::vector<AnnotatedOperation>> all_aops;
186     all_aops.resize(config.target.partitions.size());
187 
188     std::vector<std::vector<CowMergeOperation>> all_merge_sequences;
189     all_merge_sequences.resize(config.target.partitions.size());
190 
191     std::vector<size_t> all_cow_sizes(config.target.partitions.size(), 0);
192 
193     std::vector<PartitionProcessor> partition_tasks{};
194     auto thread_count = std::min<int>(diff_utils::GetMaxThreads(),
195                                       config.target.partitions.size());
196     base::DelegateSimpleThreadPool thread_pool{"partition-thread-pool",
197                                                thread_count};
198     for (size_t i = 0; i < config.target.partitions.size(); i++) {
199       const PartitionConfig& old_part =
200           config.is_delta ? config.source.partitions[i] : empty_part;
201       const PartitionConfig& new_part = config.target.partitions[i];
202       LOG(INFO) << "Partition name: " << new_part.name;
203       LOG(INFO) << "Partition size: " << new_part.size;
204       LOG(INFO) << "Block count: " << new_part.size / config.block_size;
205 
206       // Select payload generation strategy based on the config.
207       unique_ptr<OperationsGenerator> strategy;
208       if (!old_part.path.empty()) {
209         // Delta update.
210         LOG(INFO) << "Using generator ABGenerator() for partition "
211                   << new_part.name;
212         strategy.reset(new ABGenerator());
213       } else {
214         LOG(INFO) << "Using generator FullUpdateGenerator() for partition "
215                   << new_part.name;
216         strategy.reset(new FullUpdateGenerator());
217       }
218 
219       // Generate the operations using the strategy we selected above.
220       partition_tasks.push_back(PartitionProcessor(config,
221                                                    old_part,
222                                                    new_part,
223                                                    &blob_file,
224                                                    &all_aops[i],
225                                                    &all_merge_sequences[i],
226                                                    &all_cow_sizes[i],
227                                                    std::move(strategy)));
228     }
229     thread_pool.Start();
230     for (auto& processor : partition_tasks) {
231       thread_pool.AddWork(&processor);
232     }
233     thread_pool.JoinAll();
234 
235     for (size_t i = 0; i < config.target.partitions.size(); i++) {
236       const PartitionConfig& old_part =
237           config.is_delta ? config.source.partitions[i] : empty_part;
238       const PartitionConfig& new_part = config.target.partitions[i];
239       TEST_AND_RETURN_FALSE(
240           payload.AddPartition(old_part,
241                                new_part,
242                                std::move(all_aops[i]),
243                                std::move(all_merge_sequences[i]),
244                                all_cow_sizes[i]));
245     }
246   }
247   data_file.CloseFd();
248 
249   LOG(INFO) << "Writing payload file...";
250   // Write payload file to disk.
251   TEST_AND_RETURN_FALSE(payload.WritePayload(
252       output_path, data_file.path(), private_key_path, metadata_size));
253 
254   LOG(INFO) << "All done. Successfully created delta file with "
255             << "metadata size = " << *metadata_size;
256   return true;
257 }
258 
259 };  // namespace chromeos_update_engine
260