diff options
Diffstat (limited to 'meta/lib/rootfspostcommands.py')
-rw-r--r-- | meta/lib/rootfspostcommands.py | 60 |
1 files changed, 43 insertions, 17 deletions
diff --git a/meta/lib/rootfspostcommands.py b/meta/lib/rootfspostcommands.py index b5a51295e2..e344ae2efc 100644 --- a/meta/lib/rootfspostcommands.py +++ b/meta/lib/rootfspostcommands.py | |||
@@ -6,13 +6,14 @@ | |||
6 | 6 | ||
7 | import os | 7 | import os |
8 | 8 | ||
9 | def sort_file(filename, mapping): | 9 | def sort_shadowutils_file(filename, mapping): |
10 | """ | 10 | """ |
11 | Sorts a passwd or group file based on the numeric ID in the third column. | 11 | Sorts a passwd or group file based on the numeric ID in the third column. |
12 | If a mapping is given, the name from the first column is mapped via that | 12 | If a mapping is given, the name from the first column is mapped via that |
13 | dictionary instead (necessary for /etc/shadow and /etc/gshadow). If not, | 13 | dictionary instead (necessary for /etc/shadow and /etc/gshadow). If not, |
14 | a new mapping is created on the fly and returned. | 14 | a new mapping is created on the fly and returned. |
15 | """ | 15 | """ |
16 | |||
16 | new_mapping = {} | 17 | new_mapping = {} |
17 | with open(filename, 'rb+') as f: | 18 | with open(filename, 'rb+') as f: |
18 | lines = f.readlines() | 19 | lines = f.readlines() |
@@ -33,30 +34,55 @@ def sort_file(filename, mapping): | |||
33 | # We overwrite the entire file, i.e. no truncate() necessary. | 34 | # We overwrite the entire file, i.e. no truncate() necessary. |
34 | f.seek(0) | 35 | f.seek(0) |
35 | f.write(b''.join(lines)) | 36 | f.write(b''.join(lines)) |
37 | |||
36 | return new_mapping | 38 | return new_mapping |
37 | 39 | ||
38 | def remove_backup(filename): | 40 | def sort_shadowutils_files(sysconfdir): |
39 | """ | 41 | """ |
40 | Removes the backup file for files like /etc/passwd. | 42 | Sorts shadow-utils 'passwd' and 'group' files in a rootfs' /etc directory |
43 | by ID. | ||
41 | """ | 44 | """ |
42 | backup_filename = filename + '-' | ||
43 | if os.path.exists(backup_filename): | ||
44 | os.unlink(backup_filename) | ||
45 | 45 | ||
46 | def sort_passwd(sysconfdir): | ||
47 | """ | ||
48 | Sorts passwd and group files in a rootfs /etc directory by ID. | ||
49 | Backup files are sometimes are inconsistent and then cannot be | ||
50 | sorted (YOCTO #11043), and more importantly, are not needed in | ||
51 | the initial rootfs, so they get deleted. | ||
52 | """ | ||
53 | for main, shadow in (('passwd', 'shadow'), | 46 | for main, shadow in (('passwd', 'shadow'), |
54 | ('group', 'gshadow')): | 47 | ('group', 'gshadow')): |
55 | filename = os.path.join(sysconfdir, main) | 48 | filename = os.path.join(sysconfdir, main) |
56 | remove_backup(filename) | ||
57 | if os.path.exists(filename): | 49 | if os.path.exists(filename): |
58 | mapping = sort_file(filename, None) | 50 | mapping = sort_shadowutils_file(filename, None) |
59 | filename = os.path.join(sysconfdir, shadow) | 51 | filename = os.path.join(sysconfdir, shadow) |
60 | remove_backup(filename) | ||
61 | if os.path.exists(filename): | 52 | if os.path.exists(filename): |
62 | sort_file(filename, mapping) | 53 | sort_shadowutils_file(filename, mapping) |
54 | |||
55 | def remove_shadowutils_backup_file(filename): | ||
56 | """ | ||
57 | Remove shadow-utils backup file for files like /etc/passwd. | ||
58 | """ | ||
59 | |||
60 | backup_filename = filename + '-' | ||
61 | if os.path.exists(backup_filename): | ||
62 | os.unlink(backup_filename) | ||
63 | |||
64 | def remove_shadowutils_backup_files(sysconfdir): | ||
65 | """ | ||
66 | Remove shadow-utils backup files in a rootfs /etc directory. They are not | ||
67 | needed in the initial root filesystem and sorting them can be inconsistent | ||
68 | (YOCTO #11043). | ||
69 | """ | ||
70 | |||
71 | for filename in ( | ||
72 | 'group', | ||
73 | 'gshadow', | ||
74 | 'passwd', | ||
75 | 'shadow', | ||
76 | ): | ||
77 | filepath = os.path.join(sysconfdir, filename) | ||
78 | remove_shadowutils_backup_file(filepath) | ||
79 | |||
80 | def tidy_shadowutils_files(sysconfdir): | ||
81 | """ | ||
82 | Tidy up shadow-utils files. | ||
83 | """ | ||
84 | |||
85 | remove_shadowutils_backup_files(sysconfdir) | ||
86 | sort_shadowutils_files(sysconfdir) | ||
87 | |||
88 | return True | ||