summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrei Gherzan <andrei.gherzan@huawei.com>2022-08-24 01:55:26 +0200
committerRichard Purdie <richard.purdie@linuxfoundation.org>2022-08-25 11:08:06 +0100
commit29bb84b82be41409f696e22dcb8a7aa4dcadf2fa (patch)
tree6810701494d3e3b92593aa9d21191297f2ad36fc
parent1f83192d0ea556e69a4c8147d6a406555a8335b7 (diff)
downloadpoky-29bb84b82be41409f696e22dcb8a7aa4dcadf2fa.tar.gz
rootfspostcommands.py: Restructure sort_passwd and related functions
This change proposes a restructure of the functions in rootfspostcommandstests.py to clarify the purpose of each function and also, make it scalable for other use cases (for example adding support for removing subid backup files). The main function of interest here is 'tidy_shadowutils_files' which brings in the functionality of the old 'sort_passwd' making it clear that it doesn't only sort the passwd file: - delete backup files - it sorts passwd, group and the associated shadow files The other functions are also renamed for consistency and clarity and more documentation was added. (From OE-Core rev: 81a0a4dbfb0313b967a7a98eb7fd1e13edb1a9be) Signed-off-by: Andrei Gherzan <andrei.gherzan@huawei.com> Signed-off-by: Luca Ceresoli <luca.ceresoli@bootlin.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
-rw-r--r--meta/lib/rootfspostcommands.py60
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
7import os 7import os
8 8
9def sort_file(filename, mapping): 9def 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
38def remove_backup(filename): 40def 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
46def 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
55def 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
64def 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
80def 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