summaryrefslogtreecommitdiffstats
path: root/meta/lib/rootfspostcommands.py
diff options
context:
space:
mode:
Diffstat (limited to 'meta/lib/rootfspostcommands.py')
-rw-r--r--meta/lib/rootfspostcommands.py64
1 files changed, 47 insertions, 17 deletions
diff --git a/meta/lib/rootfspostcommands.py b/meta/lib/rootfspostcommands.py
index fdb9f5b850..5386eea409 100644
--- a/meta/lib/rootfspostcommands.py
+++ b/meta/lib/rootfspostcommands.py
@@ -1,16 +1,19 @@
1# 1#
2# Copyright OpenEmbedded Contributors
3#
2# SPDX-License-Identifier: GPL-2.0-only 4# SPDX-License-Identifier: GPL-2.0-only
3# 5#
4 6
5import os 7import os
6 8
7def sort_file(filename, mapping): 9def sort_shadowutils_file(filename, mapping):
8 """ 10 """
9 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.
10 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
11 dictionary instead (necessary for /etc/shadow and /etc/gshadow). If not, 13 dictionary instead (necessary for /etc/shadow and /etc/gshadow). If not,
12 a new mapping is created on the fly and returned. 14 a new mapping is created on the fly and returned.
13 """ 15 """
16
14 new_mapping = {} 17 new_mapping = {}
15 with open(filename, 'rb+') as f: 18 with open(filename, 'rb+') as f:
16 lines = f.readlines() 19 lines = f.readlines()
@@ -31,30 +34,57 @@ def sort_file(filename, mapping):
31 # We overwrite the entire file, i.e. no truncate() necessary. 34 # We overwrite the entire file, i.e. no truncate() necessary.
32 f.seek(0) 35 f.seek(0)
33 f.write(b''.join(lines)) 36 f.write(b''.join(lines))
37
34 return new_mapping 38 return new_mapping
35 39
36def remove_backup(filename): 40def sort_shadowutils_files(sysconfdir):
37 """ 41 """
38 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.
39 """ 44 """
40 backup_filename = filename + '-'
41 if os.path.exists(backup_filename):
42 os.unlink(backup_filename)
43 45
44def sort_passwd(sysconfdir):
45 """
46 Sorts passwd and group files in a rootfs /etc directory by ID.
47 Backup files are sometimes are inconsistent and then cannot be
48 sorted (YOCTO #11043), and more importantly, are not needed in
49 the initial rootfs, so they get deleted.
50 """
51 for main, shadow in (('passwd', 'shadow'), 46 for main, shadow in (('passwd', 'shadow'),
52 ('group', 'gshadow')): 47 ('group', 'gshadow')):
53 filename = os.path.join(sysconfdir, main) 48 filename = os.path.join(sysconfdir, main)
54 remove_backup(filename)
55 if os.path.exists(filename): 49 if os.path.exists(filename):
56 mapping = sort_file(filename, None) 50 mapping = sort_shadowutils_file(filename, None)
57 filename = os.path.join(sysconfdir, shadow) 51 filename = os.path.join(sysconfdir, shadow)
58 remove_backup(filename)
59 if os.path.exists(filename): 52 if os.path.exists(filename):
60 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 'subgid',
77 'subuid',
78 ):
79 filepath = os.path.join(sysconfdir, filename)
80 remove_shadowutils_backup_file(filepath)
81
82def tidy_shadowutils_files(sysconfdir):
83 """
84 Tidy up shadow-utils files.
85 """
86
87 remove_shadowutils_backup_files(sysconfdir)
88 sort_shadowutils_files(sysconfdir)
89
90 return True