OLD | NEW |
---|---|
(Empty) | |
1 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 """This module manages interactions between an image and a public key.""" | |
6 | |
7 import os | |
8 import tempfile | |
9 | |
10 import cros_build_lib as cros_lib | |
11 | |
12 class PublicKeyManager(object): | |
13 """Class wrapping interactions with a public key on an image.""" | |
14 target_key_path = 'usr/share/update_engine/update-payload-key.pub.pem' | |
petkov
2011/03/16 22:03:48
TARGET_KEY_PATH?
sosa
2011/03/16 22:47:02
Done.
| |
15 | |
16 def __init__(self, image_path, key_path): | |
17 """Initializes a manager with image_path and key_path we plan to insert.""" | |
18 self.image_path = image_path | |
19 self.key_path = key_path | |
20 self._rootfs_dir = tempfile.mkdtemp(suffix='rootfs', prefix='tmp') | |
21 self._stateful_dir = tempfile.mkdtemp(suffix='stateful', prefix='tmp') | |
22 | |
23 # Gather some extra information about the image. | |
24 try: | |
25 cros_lib.MountImage(image_path, self._rootfs_dir, self._stateful_dir, | |
26 read_only=True) | |
27 self._full_target_key_path = os.path.join( | |
28 self._rootfs_dir, PublicKeyManager.target_key_path) | |
29 self._is_key_new = True | |
30 if os.path.exists(self._full_target_key_path): | |
31 diff_output = cros_lib.RunCommand(['diff', | |
32 self.key_path, | |
33 self._full_target_key_path], | |
34 print_cmd=False, redirect_stdout=True, | |
35 redirect_stderr=True, error_ok=True) | |
36 | |
37 if not diff_output: self._is_key_new = False | |
38 | |
39 finally: | |
40 cros_lib.UnmountImage(self._rootfs_dir, self._stateful_dir) | |
41 | |
42 def __del__(self): | |
43 """Remove our temporary directories we created in init.""" | |
44 os.rmdir(self._rootfs_dir) | |
45 os.rmdir(self._stateful_dir) | |
46 | |
47 def AddKeyToImage(self): | |
48 """Adds the key specified in init to the image.""" | |
49 if not self._is_key_new: | |
50 cros_lib.Info('Public key already on image %s. No work to do.' % | |
51 self.image_path) | |
petkov
2011/03/16 22:03:48
missing return
sosa
2011/03/16 22:47:02
Done.
| |
52 | |
53 try: | |
54 cros_lib.Info('Copying %s into %s' % (self.key_path, self.image_path)) | |
petkov
2011/03/16 22:03:48
move before try
sosa
2011/03/16 22:47:02
Done.
| |
55 cros_lib.MountImage(self.image_path, self._rootfs_dir, self._stateful_dir, | |
56 read_only=False) | |
57 | |
58 dir_path = os.path.dirname(self.target_key_path) | |
59 if not os.path.exists(dir_path): | |
petkov
2011/03/16 22:03:48
why check rather than always mkdir?
sosa
2011/03/16 22:47:02
Done.
| |
60 cros_lib.RunCommand(['sudo', 'mkdir', '--parents', dir_path], | |
61 print_cmd=False) | |
62 | |
63 cros_lib.RunCommand(['sudo', 'cp', '--force', '-p', self.key_path, | |
64 self._full_target_key_path], print_cmd=False) | |
65 finally: | |
66 cros_lib.UnmountImage(self._rootfs_dir, self._stateful_dir) | |
67 self._MakeImageBootable() | |
68 | |
69 def RemoveKeyFromImage(self): | |
70 """Removes the key specified in init from the image.""" | |
71 cros_lib.Info('Removing public key from image %s.' % self.image_path) | |
72 try: | |
73 cros_lib.MountImage(self.image_path, self._rootfs_dir, self._stateful_dir, | |
74 read_only=False) | |
75 cros_lib.RunCommand(['sudo', 'rm', '--force', self._full_target_key_path], | |
76 print_cmd=False) | |
77 finally: | |
78 cros_lib.UnmountImage(self._rootfs_dir, self._stateful_dir) | |
79 self._MakeImageBootable() | |
80 | |
81 def _MakeImageBootable(self): | |
82 """Makes the image bootable. Only useful on non-vm images""" | |
petkov
2011/03/16 22:03:48
second sentence is not a sentence and doesn't have
| |
83 crosutils_dir = os.path.abspath(__file__).rsplit('/', 3)[0] | |
petkov
2011/03/16 22:03:48
this should be in a library somewhere, no? can't y
sosa
2011/03/16 22:47:02
Done.
| |
84 from_dir = os.path.dirname(self.image_path) | |
85 image = os.path.basename(self.image_path) | |
86 if not 'qemu' in os.path.basename(self.image_path): | |
petkov
2011/03/16 22:03:48
make this check at the beginning of the routine an
sosa
2011/03/16 22:47:02
Done.
| |
87 cros_lib.RunCommand(['bin/cros_make_image_bootable', | |
88 cros_lib.ReinterpretPathForChroot(from_dir), | |
89 image], print_cmd=False, redirect_stdout=True, | |
90 redirect_stderr=True, enter_chroot=True, | |
91 cwd=crosutils_dir) | |
OLD | NEW |