-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdlc_base.cc
985 lines (866 loc) · 33.2 KB
/
dlc_base.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
// Copyright 2020 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "dlcservice/dlc_base.h"
#include <algorithm>
#include <cinttypes>
#include <memory>
#include <optional>
#include <utility>
#include <vector>
#include <base/check.h>
#include <base/logging.h>
#include <base/notreached.h>
#include <base/strings/stringprintf.h>
#include <base/strings/string_number_conversions.h>
#include <base/strings/string_util.h>
#include <brillo/errors/error.h>
#include <brillo/files/file_util.h>
#include <chromeos/constants/imageloader.h>
#include <chromeos/dbus/service_constants.h>
#include <dbus/dlcservice/dbus-constants.h>
#include <dlcservice/proto_bindings/dlcservice.pb.h>
#include "dlcservice/error.h"
#include "dlcservice/prefs.h"
#include "dlcservice/system_state.h"
#include "dlcservice/utils.h"
#include "dlcservice/utils/utils.h"
using base::FilePath;
using brillo::ErrorPtr;
using std::string;
using std::vector;
namespace dlcservice {
// TODO(ahassani): Instead of initialize function, create a factory method so
// we can develop different types of DLC classes.
bool DlcBase::Initialize() {
const auto* system_state = SystemState::Get();
const auto& manifest_dir = system_state->manifest_dir();
package_ = *ScanDirectory(manifest_dir.Append(id_)).begin();
manifest_ = GetDlcManifest(system_state->manifest_dir(), id_, package_);
if (!manifest_) {
// Failing to read the manifest will be considered a blocker.
LOG(ERROR) << "Failed to read the manifest of DLC " << id_;
return false;
}
const auto& content_dir = system_state->content_dir();
content_id_path_ = JoinPaths(content_dir, id_);
content_package_path_ = JoinPaths(content_id_path_, package_);
prefs_path_ = JoinPaths(system_state->dlc_prefs_dir(), id_);
prefs_package_path_ = JoinPaths(prefs_path_, package_);
preloaded_image_path_ = JoinPaths(system_state->preloaded_content_dir(), id_,
package_, kDlcImageFileName);
factory_install_image_path_ = JoinPaths(system_state->factory_install_dir(),
id_, package_, kDlcImageFileName);
deployed_image_path_ = JoinPaths(system_state->deployed_content_dir(), id_,
package_, kDlcImageFileName);
state_.set_state(DlcState::NOT_INSTALLED);
state_.set_id(id_);
state_.set_progress(0);
state_.set_last_error_code(kErrorNone);
if (manifest_->mount_file_required()) {
if (!Prefs(prefs_package_path_).Delete(kDlcRootMount))
LOG(ERROR)
<< "Failed to delete indirect root mount file during Initialization: "
<< JoinPaths(prefs_package_path_, kDlcRootMount);
}
if (!base::ReadFileToString(SystemState::Get()->verification_file(),
&verification_value_)) {
LOG(WARNING) << "Failed to read DLC verification value file.";
}
// Verify that the verification is actually valid.
if (Prefs(*this, system_state->active_boot_slot()).Exists(kDlcPrefVerified)) {
std::string value;
state_.set_is_verified(Prefs(*this, system_state->active_boot_slot())
.GetKey(kDlcPrefVerified, &value) &&
value == verification_value_);
}
// If factory install isn't allowed, free up the space.
if (!IsFactoryInstall()) {
brillo::DeleteFile(factory_install_image_path_);
}
// TODO(kimjae): Efficiently overlap factory images with cache.
reserve_ = manifest_->reserved();
if (reserve_) {
if (SystemState::Get()->IsDeviceRemovable()) {
LOG(WARNING)
<< "Booted from removable device, skipping reserve space for DLC="
<< id_;
} else {
ErrorPtr tmp_err;
if (!CreateDlc(&tmp_err))
LOG(ERROR) << "Failed to reserve space for DLC=" << id_;
}
}
return true;
}
const DlcId& DlcBase::GetId() const {
return id_;
}
const std::string& DlcBase::GetName() const {
return manifest_->name();
}
const std::string& DlcBase::GetDescription() const {
return manifest_->description();
}
DlcState DlcBase::GetState() const {
return state_;
}
bool DlcBase::IsInstalling() const {
return state_.state() == DlcState::INSTALLING;
}
bool DlcBase::IsInstalled() const {
if (state_.state() != DlcState::INSTALLED) {
return false;
}
auto root_mount = GetRoot();
if (root_mount.empty()) {
LOG(WARNING) << "Validating against predefined root mount.";
// Keep in sync with imageloader's set mount path.
return base::PathExists(base::FilePath(imageloader::kImageloaderMountBase)
.Append(id_)
.Append(package_)
.Append(kRootDirectoryInsideDlcModule));
}
return base::PathExists(root_mount);
}
bool DlcBase::IsVerified() const {
return state_.is_verified();
}
bool DlcBase::IsScaled() const {
return manifest_->scaled();
}
bool DlcBase::HasContent() const {
for (const auto& path :
{GetImagePath(BootSlot::Slot::A), GetImagePath(BootSlot::Slot::B)}) {
if (base::PathExists(path))
return true;
}
return false;
}
uint64_t DlcBase::GetUsedBytesOnDisk() const {
uint64_t total_size = 0;
for (const auto& path :
{GetImagePath(BootSlot::Slot::A), GetImagePath(BootSlot::Slot::B)}) {
if (!base::PathExists(path))
continue;
int64_t size = 0;
if (!base::GetFileSize(path, &size)) {
LOG(WARNING) << "Failed to get file size for path: " << path.value();
}
total_size += size;
}
return total_size;
}
bool DlcBase::IsPreloadAllowed() const {
return manifest_->preload_allowed() &&
!SystemState::Get()->system_properties()->IsOfficialBuild();
}
bool DlcBase::IsFactoryInstall() const {
return manifest_->factory_install();
}
base::FilePath DlcBase::GetRoot() const {
if (mount_point_.empty())
return {};
return JoinPaths(mount_point_, kRootDirectoryInsideDlcModule);
}
bool DlcBase::InstallCompleted(ErrorPtr* err) {
if (!MarkVerified()) {
state_.set_last_error_code(kErrorInternal);
*err = Error::Create(
FROM_HERE, state_.last_error_code(),
base::StringPrintf("Failed to mark active DLC=%s as verified.",
id_.c_str()));
return false;
}
return true;
}
bool DlcBase::UpdateCompleted(ErrorPtr* err) {
if (!Prefs(*this, SystemState::Get()->inactive_boot_slot())
.Create(kDlcPrefVerified)) {
*err = Error::Create(
FROM_HERE, kErrorInternal,
base::StringPrintf("Failed to mark inactive DLC=%s as verified.",
id_.c_str()));
return false;
}
return true;
}
FilePath DlcBase::GetImagePath(BootSlot::Slot slot) const {
return JoinPaths(content_package_path_, BootSlot::ToString(slot),
kDlcImageFileName);
}
bool DlcBase::CreateDlc(ErrorPtr* err) {
// Create content directories.
for (const auto& path :
{content_id_path_, content_package_path_, prefs_path_}) {
if (!CreateDir(path)) {
*err = Error::CreateInternal(
FROM_HERE, error::kFailedToCreateDirectory,
base::StringPrintf("Failed to create directory %s for DLC=%s",
path.value().c_str(), id_.c_str()));
state_.set_last_error_code(Error::GetErrorCode(*err));
return false;
}
}
// Creates image A and B.
for (const auto& slot : {BootSlot::Slot::A, BootSlot::Slot::B}) {
FilePath image_path = GetImagePath(slot);
// If resuming from hibernate, space on stateful is limited by the
// dm-snapshots set up on top of it. Avoid creating new DLCs during this
// transient period.
if (SystemState::Get()->resuming_from_hibernate()) {
int64_t existing_size;
if (!GetFileSize(image_path, &existing_size) ||
(existing_size < manifest_->preallocated_size())) {
*err = Error::CreateInternal(
FROM_HERE, error::kFailedCreationDuringHibernateResume,
base::StringPrintf(
"Not creating file while resuming from hibernate, DLC=%s",
id_.c_str()));
state_.set_last_error_code(Error::GetErrorCode(*err));
return false;
}
}
// For reserve requested/reserved DLCs, must allocate the required full
// preallocated space, not only the actual bits of the DLC image to not
// re-sparsify the DLC images.
if (!CreateFile(image_path, manifest_->preallocated_size())) {
if (reserve_) {
state_.set_last_error_code(kErrorAllocation);
*err = Error::Create(
FROM_HERE, state_.last_error_code(),
base::StringPrintf("Failed to create image file %s for DLC=%s",
image_path.value().c_str(), id_.c_str()));
return false;
}
if (!CreateFile(image_path, manifest_->size())) {
state_.set_last_error_code(kErrorAllocation);
*err = Error::Create(
FROM_HERE, state_.last_error_code(),
base::StringPrintf("Failed to create image file %s for DLC=%s",
image_path.value().c_str(), id_.c_str()));
return false;
} else if (!ResizeFile(image_path, manifest_->preallocated_size())) {
LOG(WARNING) << "Unable to allocate up to preallocated size: "
<< manifest_->preallocated_size() << " for DLC=" << id_;
}
}
}
return true;
}
bool DlcBase::MakeReadyForUpdate() const {
// Deleting the inactive verified pref should always happen before anything
// else here otherwise if we failed to delete, on a reboot after an update, we
// might assume the image is verified, which is not.
if (!Prefs(*this, SystemState::Get()->inactive_boot_slot())
.Delete(kDlcPrefVerified)) {
PLOG(ERROR) << "Failed to mark inactive DLC=" << id_ << " as not-verified.";
return false;
}
if (!IsVerified()) {
return false;
}
// Scaled DLCs will not A/B update with the OS until deltas are supported.
if (manifest_->scaled()) {
LOG(WARNING) << "Scaled DLC=" << id_ << " will not update with the OS.";
return false;
}
return MakeReadyForUpdateInternal();
}
bool DlcBase::MakeReadyForUpdateInternal() const {
const FilePath& inactive_image_path =
GetImagePath(SystemState::Get()->inactive_boot_slot());
if (!CreateFile(inactive_image_path, manifest_->size())) {
LOG(ERROR) << "Failed to create inactive image "
<< inactive_image_path.value() << " when making DLC=" << id_
<< " ready for update.";
return false;
} else if (!ResizeFile(inactive_image_path, manifest_->preallocated_size())) {
LOG(WARNING) << "Unable to allocate up to preallocated size: "
<< manifest_->preallocated_size() << " when making DLC=" << id_
<< " ready for update.";
}
return true;
}
bool DlcBase::MarkVerified() {
state_.set_is_verified(true);
return Prefs(*this, SystemState::Get()->active_boot_slot())
.SetKey(kDlcPrefVerified, verification_value_);
}
bool DlcBase::MarkUnverified() {
state_.set_is_verified(false);
return Prefs(*this, SystemState::Get()->active_boot_slot())
.Delete(kDlcPrefVerified);
}
bool DlcBase::Verify() {
auto image_path = GetImagePath(SystemState::Get()->active_boot_slot());
vector<uint8_t> image_sha256;
if (!VerifyInternal(image_path, &image_sha256)) {
LOG(ERROR) << "Failed to verify DLC=" << id_;
return false;
}
const auto& manifest_image_sha256 = manifest_->image_sha256();
if (image_sha256 != manifest_image_sha256) {
LOG(WARNING) << "Verification failed for image file: " << image_path.value()
<< ". Expected: "
<< base::HexEncode(manifest_image_sha256.data(),
manifest_image_sha256.size())
<< " Found: "
<< base::HexEncode(image_sha256.data(), image_sha256.size());
return false;
}
if (!MarkVerified()) {
LOG(WARNING) << "Failed to mark the image as verified, but temporarily"
<< " we assume the image is verified.";
}
return true;
}
bool DlcBase::VerifyInternal(const base::FilePath& image_path,
vector<uint8_t>* image_sha256) {
if (!HashFile(image_path, manifest_->size(), image_sha256)) {
LOG(ERROR) << "Failed to hash image file: " << image_path.value();
return false;
}
return true;
}
bool DlcBase::PreloadedCopier(ErrorPtr* err) {
int64_t preloaded_image_size;
if (!base::GetFileSize(preloaded_image_path_, &preloaded_image_size)) {
auto err_str = base::StringPrintf("Failed to get preloaded DLC (%s) size.",
id_.c_str());
*err = Error::Create(FROM_HERE, kErrorInternal, err_str);
return false;
}
if (preloaded_image_size != manifest_->size()) {
auto err_str = base::StringPrintf(
"Preloaded DLC (%s) is (%" PRId64 ") different than the size (%" PRId64
") in the manifest.",
id_.c_str(), preloaded_image_size, manifest_->size());
*err = Error::Create(FROM_HERE, kErrorInternal, err_str);
return false;
}
// Before touching the image, we need to mark it as unverified.
MarkUnverified();
FilePath image_path = GetImagePath(SystemState::Get()->active_boot_slot());
vector<uint8_t> image_sha256;
if (!CopyAndHashFile(preloaded_image_path_, image_path, manifest_->size(),
&image_sha256)) {
auto err_str =
base::StringPrintf("Failed to copy preload DLC (%s) into path %s",
id_.c_str(), image_path.value().c_str());
*err = Error::Create(FROM_HERE, kErrorInternal, err_str);
return false;
}
auto manifest_image_sha256 = manifest_->image_sha256();
if (image_sha256 != manifest_image_sha256) {
auto err_str = base::StringPrintf(
"Image is corrupted or modified for DLC=%s. Expected: %s Found: %s",
id_.c_str(),
base::HexEncode(manifest_image_sha256.data(),
manifest_image_sha256.size())
.c_str(),
base::HexEncode(image_sha256.data(), image_sha256.size()).c_str());
*err = Error::Create(FROM_HERE, kErrorInternal, err_str);
return false;
}
if (!MarkVerified())
LOG(ERROR) << "Failed to mark the image verified for DLC=" << id_;
return true;
}
bool DlcBase::FactoryInstallCopier() {
int64_t factory_install_image_size;
if (!base::GetFileSize(factory_install_image_path_,
&factory_install_image_size)) {
LOG(ERROR) << "Failed to get factory installed DLC (" << id_ << ") size.";
return false;
}
if (factory_install_image_size != manifest_->size()) {
LOG(WARNING) << "Factory installed DLC (" << id_ << ") is ("
<< factory_install_image_size << ") different than the "
<< "size (" << manifest_->size() << ") in the manifest.";
brillo::DeletePathRecursively(
JoinPaths(SystemState::Get()->factory_install_dir(), id_));
return false;
}
// Before touching the image, we need to mark it as unverified.
MarkUnverified();
FilePath image_path = GetImagePath(SystemState::Get()->active_boot_slot());
vector<uint8_t> image_sha256;
if (!CopyAndHashFile(factory_install_image_path_, image_path,
manifest_->size(), &image_sha256)) {
LOG(WARNING) << "Failed to copy factory installed DLC (" << id_
<< ") into path " << image_path;
return false;
}
auto manifest_image_sha256 = manifest_->image_sha256();
if (image_sha256 != manifest_image_sha256) {
LOG(WARNING) << "Factory installed image is corrupt or modified for DLC ("
<< id_ << "). Expected="
<< base::HexEncode(manifest_image_sha256.data(),
manifest_image_sha256.size())
<< " Found="
<< base::HexEncode(image_sha256.data(), image_sha256.size());
brillo::DeletePathRecursively(
JoinPaths(SystemState::Get()->factory_install_dir(), id_));
return false;
}
if (!MarkVerified()) {
LOG(WARNING) << "Failed to mark the image verified for DLC=" << id_;
}
if (!brillo::DeletePathRecursively(
JoinPaths(SystemState::Get()->factory_install_dir(), id_))) {
LOG(WARNING) << "Failed to delete the factory installed DLC=" << id_;
}
return true;
}
bool DlcBase::DeployCopier(ErrorPtr* err) {
int64_t deployed_image_size;
if (!base::GetFileSize(deployed_image_path_, &deployed_image_size)) {
auto err_str = base::StringPrintf("Failed to get deployed DLC (%s) size.",
id_.c_str());
*err = Error::Create(FROM_HERE, kErrorInternal, err_str);
return false;
}
if (deployed_image_size != manifest_->size()) {
auto err_str = base::StringPrintf(
"Deployed DLC (%s) is (%" PRId64 ") different than the size (%" PRId64
") in the manifest.",
id_.c_str(), deployed_image_size, manifest_->size());
*err = Error::Create(FROM_HERE, kErrorInternal, err_str);
return false;
}
// Before touching the image, we need to mark it as unverified.
MarkUnverified();
FilePath image_path = GetImagePath(SystemState::Get()->active_boot_slot());
vector<uint8_t> image_sha256;
if (!CopyAndHashFile(deployed_image_path_, image_path, manifest_->size(),
&image_sha256)) {
auto err_str =
base::StringPrintf("Failed to copy deployed DLC (%s) into path %s",
id_.c_str(), image_path.value().c_str());
*err = Error::Create(FROM_HERE, kErrorInternal, err_str);
return false;
}
auto manifest_image_sha256 = manifest_->image_sha256();
if (image_sha256 != manifest_image_sha256) {
auto err_str = base::StringPrintf(
"Image is corrupted or modified for DLC=%s. Expected: %s Found: %s",
id_.c_str(),
base::HexEncode(manifest_image_sha256.data(),
manifest_image_sha256.size())
.c_str(),
base::HexEncode(image_sha256.data(), image_sha256.size()).c_str());
*err = Error::Create(FROM_HERE, kErrorInternal, err_str);
return false;
}
if (!MarkVerified())
LOG(ERROR) << "Failed to mark the image verified for DLC=" << id_;
return true;
}
bool DlcBase::Install(ErrorPtr* err) {
switch (state_.state()) {
case DlcState::NOT_INSTALLED: {
bool active_image_existed = IsActiveImagePresent();
// Always try to create the DLC files and directories to make sure they
// all exist before we start the install.
if (!CreateDlc(err)) {
ErrorPtr tmp_err;
if (!CancelInstall(*err, &tmp_err))
LOG(ERROR) << "Failed to cancel the install correctly.";
return false;
}
// Only set the DLC installing after creation is successful to have finer
// control of state changes.
ChangeState(DlcState::INSTALLING);
// Finish the installation for verified images so they can be mounted.
if (IsVerified()) {
LOG(INFO) << "Installing already verified DLC=" << id_;
break;
}
// Try verifying images that already existed before creation. If verified,
// finish the installation so they can be mounted.
if (active_image_existed && Verify()) {
LOG(INFO) << "Verified existing, but previously not verified DLC="
<< id_;
break;
}
// Hibernate resume runs on limited sized dm-snapshots. Avoid generating
// lots of writes for stateful DLCs, and avoid possibly changing LVM
// metadata for LVM DLCs.
if (SystemState::Get()->resuming_from_hibernate()) {
LOG(ERROR) << "Not writing while resuming from hibernate for DLC="
<< id_;
return false;
}
// Load the factory installed DLC if allowed otherwise clear the image.
if (IsFactoryInstall() && base::PathExists(factory_install_image_path_)) {
if (FactoryInstallCopier()) {
// Continue to mount the DLC image.
LOG(INFO) << "Factory installing DLC=" << id_;
break;
} else {
LOG(WARNING) << "Failed to copy factory installed image for DLC="
<< id_;
}
}
// Preload the DLC if possible.
if (IsPreloadAllowed() && base::PathExists(preloaded_image_path_)) {
if (!PreloadedCopier(err)) {
LOG(ERROR)
<< "Preloading failed, so assuming installation failed for DLC="
<< id_;
ErrorPtr tmp_err;
if (!CancelInstall(*err, &tmp_err))
LOG(ERROR) << "Failed to cancel the install from preloading.";
return false;
}
LOG(INFO) << "Preloading DLC=" << id_;
break;
}
// By now the image is not verified, so it needs to be installed
// through update_engine. So don't go any further.
return true;
}
case DlcState::INSTALLING:
// If the image is already in this state, nothing need to be done. It is
// already being installed.
// Skip reporting this scenario to the metrics, since the Install call
// might be from the same client, and reporting this is not useful.
return true;
case DlcState::INSTALLED:
// If the image is already installed, we need to finish the install so it
// gets mounted in case it has been unmounted externally.
break;
default:
NOTREACHED();
return false;
}
// Let's try to finish the installation.
if (!FinishInstall(/*installed_by_ue=*/false, err)) {
return false;
}
// Note: Don't remove preloaded DLC images. F20 transition to provision DLC
// images will allow for preloading to be deprecated.
return true;
}
bool DlcBase::FinishInstall(bool installed_by_ue, ErrorPtr* err) {
DCHECK(err);
DCHECK(err->get() == NULL); // Check there is no error set.
switch (state_.state()) {
case DlcState::INSTALLED:
case DlcState::INSTALLING:
if (!IsVerified()) {
// If the image is not verified, try to verify it. This is to combat
// update_engine failing to call into |InstallCompleted()| even after a
// successful DLC installation.
if (Verify()) {
LOG(WARNING) << "Missing verification mark for DLC=" << id_
<< ", but verified to be a valid image.";
}
}
if (IsVerified()) {
if (Mount(err))
break;
// Do not |CancelInstall| on mount failure.
state_.set_last_error_code(Error::GetErrorCode(*err));
ChangeState(DlcState::NOT_INSTALLED);
MarkUnverified();
SystemState::Get()->metrics()->SendInstallResultFailure(err);
LOG(ERROR) << "Mount failed during install finalization for DLC="
<< id_;
return false;
} else {
// Check if the failure was because update_engine finished the
// installation with "noupdate".
if (installed_by_ue &&
SystemState::Get()->update_engine_status().last_attempt_error() ==
static_cast<int32_t>(update_engine::ErrorCode::kNoUpdate)) {
*err = Error::CreateInternal(
FROM_HERE, kErrorNoImageFound,
base::StringPrintf(
"Update engine could not install DLC=%s, since "
"Omaha could not provide the image.",
id_.c_str()));
} else {
// The error is empty since verification was not successful.
*err = Error::CreateInternal(
FROM_HERE, error::kFailedToVerifyImage,
base::StringPrintf("Cannot verify image for DLC=%s",
id_.c_str()));
}
SystemState::Get()->metrics()->SendInstallResultFailure(err);
ErrorPtr tmp_err;
if (!CancelInstall(*err, &tmp_err))
LOG(ERROR) << "Failed during install finalization for DLC=" << id_;
return false;
}
case DlcState::NOT_INSTALLED:
// Should not try to finish install on a not-installed DLC.
default:
NOTREACHED();
return false;
}
// Now that we are sure the image is installed, we can go ahead and set it as
// active. Failure to set the metadata flags should not fail the install.
SetActiveValue(true);
SystemState::Get()->metrics()->SendInstallResultSuccess(installed_by_ue);
return true;
}
bool DlcBase::CancelInstall(const ErrorPtr& err_in, ErrorPtr* err) {
state_.set_last_error_code(Error::GetErrorCode(err_in));
ChangeState(DlcState::NOT_INSTALLED);
// Consider as not installed even if delete fails below, correct errors
// will be propagated later and should not block on further installs.
if (!Delete(err)) {
LOG(ERROR) << "Failed during install cancellation for DLC=" << id_;
return false;
}
return true;
}
bool DlcBase::Mount(ErrorPtr* err) {
string mount_point;
if (!MountInternal(&mount_point, err)) {
return false;
}
mount_point_ = FilePath(mount_point);
// Creates a file which holds the root mount path, allowing for indirect
// access for processes/scripts which can't access DBus.
if (manifest_->mount_file_required() &&
!Prefs(prefs_package_path_).SetKey(kDlcRootMount, GetRoot().value())) {
// TODO(kimjae): Test this by injecting |Prefs| class.
LOG(ERROR) << "Failed to create indirect root mount file: "
<< JoinPaths(prefs_package_path_, kDlcRootMount);
ErrorPtr tmp_err;
Unmount(&tmp_err);
return false;
}
ChangeState(DlcState::INSTALLED);
return true;
}
bool DlcBase::MountInternal(std::string* mount_point, ErrorPtr* err) {
// TODO(kimjae): Make this async as well as the top level DLC operations.
if (!SystemState::Get()->image_loader()->LoadDlcImage(
id_, package_,
SystemState::Get()->active_boot_slot() == BootSlot::Slot::A
? imageloader::kSlotNameA
: imageloader::kSlotNameB,
mount_point, nullptr,
/*timeout_ms=*/60 * 1000)) {
*err =
Error::CreateInternal(FROM_HERE, error::kFailedToMountImage,
"Imageloader is unavailable for LoadDlcImage().");
state_.set_last_error_code(Error::GetErrorCode(*err));
return false;
}
if (mount_point->empty()) {
*err = Error::CreateInternal(FROM_HERE, error::kFailedToMountImage,
"Imageloader LoadDlcImage() call failed.");
state_.set_last_error_code(Error::GetErrorCode(*err));
return false;
}
return true;
}
bool DlcBase::Unmount(ErrorPtr* err) {
bool success = false;
if (!SystemState::Get()->image_loader()->UnloadDlcImage(id_, package_,
&success, nullptr)) {
state_.set_last_error_code(kErrorInternal);
*err = Error::Create(FROM_HERE, state_.last_error_code(),
"Imageloader is unavailable for UnloadDlcImage().");
return false;
}
if (!success) {
state_.set_last_error_code(kErrorInternal);
*err = Error::Create(FROM_HERE, state_.last_error_code(),
"Imageloader UnloadDlcImage() call failed.");
return false;
}
if (manifest_->mount_file_required()) {
if (!Prefs(prefs_package_path_).Delete(kDlcRootMount))
LOG(ERROR) << "Failed to delete indirect root mount file: "
<< JoinPaths(prefs_package_path_, kDlcRootMount);
}
mount_point_.clear();
return true;
}
bool DlcBase::IsActiveImagePresent() const {
return base::PathExists(GetImagePath(SystemState::Get()->active_boot_slot()));
}
bool DlcBase::Delete(brillo::ErrorPtr* err) {
// If we're deleting the image, we need to set it as unverified.
MarkUnverified();
if (reserve_) {
LOG(INFO) << "Skipping delete for reserved DLC=" << id_;
return true;
}
return DeleteInternal(err);
}
bool DlcBase::DeleteInternal(ErrorPtr* err) {
vector<string> undeleted_paths;
for (const auto& path : GetPathsToDelete(id_)) {
if (base::PathExists(path)) {
if (!brillo::DeletePathRecursively(path)) {
PLOG(ERROR) << "Failed to delete path=" << path;
undeleted_paths.push_back(path.value());
} else {
LOG(INFO) << "Deleted path=" << path;
}
}
}
if (!undeleted_paths.empty()) {
state_.set_last_error_code(kErrorInternal);
*err = Error::Create(
FROM_HERE, state_.last_error_code(),
base::StringPrintf("DLC directories (%s) could not be deleted.",
base::JoinString(undeleted_paths, ",").c_str()));
return false;
}
return true;
}
bool DlcBase::Uninstall(ErrorPtr* err) {
// If the DLC is not verified, its not being updated, so there is no danger
// purging it.
auto ue_operation =
SystemState::Get()->update_engine_status().current_operation();
bool ue_is_busy = ue_operation != update_engine::IDLE &&
ue_operation != update_engine::UPDATED_NEED_REBOOT;
if (IsVerified() && ue_is_busy) {
*err = Error::Create(FROM_HERE, kErrorBusy,
"Install or update is in progress.");
return false;
}
// Whatever state the DLC was in, disable the reserve.
SetReserve(false);
switch (state_.state()) {
case DlcState::NOT_INSTALLED:
// We still have to uninstall the DLC, in case we never mounted in this
// session.
LOG(WARNING) << "Trying to uninstall not installed DLC=" << id_;
[[fallthrough]];
case DlcState::INSTALLED: {
ErrorPtr tmp_err;
// Even if unmount fails continue in trying to delete the images.
Unmount(&tmp_err);
ChangeState(DlcState::NOT_INSTALLED);
break;
}
case DlcState::INSTALLING:
// We cannot uninstall the image while it is being installed by the
// update_engine.
state_.set_last_error_code(kErrorBusy);
*err = Error::Create(
FROM_HERE, state_.last_error_code(),
base::StringPrintf("Trying to uninstall an installing DLC=%s",
id_.c_str()));
return false;
default:
NOTREACHED();
return false;
}
SetActiveValue(false);
return Delete(err);
}
void DlcBase::SetActiveValue(bool active) {
LOG(INFO) << "Setting active value for DLC=" << id_ << " to "
<< (active ? "true" : "false");
SystemState::Get()->update_engine()->SetDlcActiveValueAsync(
active, id_,
base::BindOnce(&DlcBase::OnSetActiveValueSuccess,
weak_ptr_factory_.GetWeakPtr()),
base::BindOnce(&DlcBase::OnSetActiveValueError,
weak_ptr_factory_.GetWeakPtr()));
}
void DlcBase::OnSetActiveValueSuccess() {
LOG(INFO) << "Successfully set active value for DLC=" << id_;
}
void DlcBase::OnSetActiveValueError(brillo::Error* err) {
if (err)
LOG(ERROR) << "Failed to set active value for DLC=" << id_
<< ", err=" << Error::ToString(err->Clone());
}
void DlcBase::ChangeState(DlcState::State state) {
switch (state) {
case DlcState::NOT_INSTALLED:
state_.set_state(state);
state_.set_progress(0);
state_.clear_root_path();
break;
case DlcState::INSTALLING:
state_.set_state(state);
state_.set_progress(0);
state_.set_last_error_code(kErrorNone);
break;
case DlcState::INSTALLED:
state_.set_state(state);
state_.set_progress(1.0);
state_.set_root_path(GetRoot().value());
break;
default:
NOTREACHED();
}
LOG(INFO) << "Changing DLC=" << id_ << " state to "
<< DlcState::State_Name(state_.state());
SystemState::Get()->state_change_reporter()->DlcStateChanged(state_);
}
void DlcBase::ChangeProgress(double progress) {
if (state_.state() != DlcState::INSTALLING) {
LOG(WARNING) << "Cannot change the progress if DLC is not being installed.";
return;
}
// Make sure the progress is not decreased.
if (state_.progress() < progress) {
state_.set_progress(std::min(progress, 1.0));
SystemState::Get()->state_change_reporter()->DlcStateChanged(state_);
}
}
bool DlcBase::SetReserve(std::optional<bool> reserve) {
if (reserve.has_value()) {
if ((reserve_ = reserve.value())) {
LOG(INFO) << "Enabling DLC=" << id_ << " reserve.";
} else {
LOG(INFO) << "Disabling DLC=" << id_ << " reserve.";
}
}
return reserve_;
}
bool DlcBase::Deploy(ErrorPtr* err) {
// Only allow deploy in unofficial build, e.g. test image.
if (SystemState::Get()->system_properties()->IsOfficialBuild()) {
*err = Error::Create(FROM_HERE, kErrorInternal,
"Deploy is not allowed in official build.");
return false;
}
if (state_.state() == DlcState::NOT_INSTALLED) {
// Only deploy not already installed DLC.
if (!base::PathExists(deployed_image_path_)) {
*err = Error::Create(
FROM_HERE, kErrorNoImageFound,
base::StringPrintf(
"The DLC=%s is not found in deployed image path=%s.", id_.c_str(),
deployed_image_path_.value().c_str()));
return false;
}
if (!CreateDlc(err)) {
ErrorPtr tmp_err;
if (!CancelInstall(*err, &tmp_err))
LOG(ERROR) << "Failed to cancel deploying DLC=" << id_;
return false;
}
if (!DeployCopier(err)) {
LOG(ERROR) << "Failed to load deployed image for DLC=" << id_;
ErrorPtr tmp_err;
if (!CancelInstall(*err, &tmp_err))
LOG(ERROR) << "Failed to cancel deploying DLC=" << id_;
return false;
}
return true;
} else {
*err = Error::Create(
FROM_HERE, kErrorInternal,
base::StringPrintf("Trying to deploy an %s DLC=%s",
DlcState::State_Name(state_.state()).c_str(),
id_.c_str()));
return false;
}
}
} // namespace dlcservice