|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import hashlib |
| 5 | +import os |
| 6 | +import shutil |
| 7 | +import subprocess |
| 8 | +import sys |
| 9 | + |
| 10 | +from collections import defaultdict |
| 11 | +from functools import cached_property |
| 12 | + |
| 13 | +DRY_RUN = False |
| 14 | +def enable_dry_run(enabled): |
| 15 | + global DRY_RUN # pylint: disable=global-statement |
| 16 | + DRY_RUN = enabled |
| 17 | + |
| 18 | +class File: |
| 19 | + def __init__(self, path): |
| 20 | + self.path = path |
| 21 | + |
| 22 | + def __str__(self): |
| 23 | + return self.path |
| 24 | + |
| 25 | + def rmtree(self): |
| 26 | + if DRY_RUN: |
| 27 | + print(f'rmtree {self.path}') |
| 28 | + return |
| 29 | + shutil.rmtree(self.path) |
| 30 | + |
| 31 | + def hardlink(self, src): |
| 32 | + if DRY_RUN: |
| 33 | + print(f'hardlink {self.path} {src}') |
| 34 | + return |
| 35 | + st = self.stats |
| 36 | + os.remove(self.path) |
| 37 | + os.link(src.path, self.path) |
| 38 | + os.chmod(self.path, st.st_mode) |
| 39 | + os.chown(self.path, st.st_uid, st.st_gid) |
| 40 | + os.utime(self.path, times=(st.st_atime, st.st_mtime)) |
| 41 | + |
| 42 | + @property |
| 43 | + def name(self): |
| 44 | + return os.path.basename(self.path) |
| 45 | + |
| 46 | + @cached_property |
| 47 | + def stats(self): |
| 48 | + return os.stat(self.path) |
| 49 | + |
| 50 | + @cached_property |
| 51 | + def size(self): |
| 52 | + return self.stats.st_size |
| 53 | + |
| 54 | + @cached_property |
| 55 | + def checksum(self): |
| 56 | + with open(self.path, 'rb') as f: |
| 57 | + return hashlib.md5(f.read()).hexdigest() |
| 58 | + |
| 59 | +class FileManager: |
| 60 | + def __init__(self, path): |
| 61 | + self.path = path |
| 62 | + self.files = [] |
| 63 | + self.folders = [] |
| 64 | + self.nindex = defaultdict(list) |
| 65 | + self.cindex = defaultdict(list) |
| 66 | + |
| 67 | + def add_file(self, path): |
| 68 | + if not os.path.isfile(path) or os.path.islink(path): |
| 69 | + return |
| 70 | + f = File(path) |
| 71 | + self.files.append(f) |
| 72 | + |
| 73 | + def load_tree(self): |
| 74 | + self.files = [] |
| 75 | + self.folders = [] |
| 76 | + for root, _, files in os.walk(self.path): |
| 77 | + self.folders.append(File(root)) |
| 78 | + for f in files: |
| 79 | + self.add_file(os.path.join(root, f)) |
| 80 | + print(f'loaded {len(self.files)} files and {len(self.folders)} folders') |
| 81 | + |
| 82 | + def generate_index(self): |
| 83 | + print('Computing file hashes') |
| 84 | + for f in self.files: |
| 85 | + self.nindex[f.name].append(f) |
| 86 | + self.cindex[(f.name, f.checksum)].append(f) |
| 87 | + |
| 88 | + def create_hardlinks(self): |
| 89 | + print('Creating hard links') |
| 90 | + for files in self.cindex.values(): |
| 91 | + if len(files) <= 1: |
| 92 | + continue |
| 93 | + orig = files[0] |
| 94 | + for f in files[1:]: |
| 95 | + f.hardlink(orig) |
| 96 | + |
| 97 | +class FsRoot: |
| 98 | + def __init__(self, path): |
| 99 | + self.path = path |
| 100 | + |
| 101 | + def iter_fsroots(self): |
| 102 | + yield self.path |
| 103 | + dimgpath = os.path.join(self.path, 'var/lib/docker/overlay2') |
| 104 | + for layer in os.listdir(dimgpath): |
| 105 | + yield os.path.join(dimgpath, layer, 'diff') |
| 106 | + |
| 107 | + def collect_fsroot_size(self): |
| 108 | + cmd = ['du', '-sb', self.path] |
| 109 | + p = subprocess.run(cmd, text=True, check=False, |
| 110 | + stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) |
| 111 | + return int(p.stdout.split()[0]) |
| 112 | + |
| 113 | + def _remove_root_paths(self, relpaths): |
| 114 | + for root in self.iter_fsroots(): |
| 115 | + for relpath in relpaths: |
| 116 | + path = os.path.join(root, relpath) |
| 117 | + if os.path.isdir(path): |
| 118 | + if DRY_RUN: |
| 119 | + print(f'rmtree {path}') |
| 120 | + else: |
| 121 | + shutil.rmtree(path) |
| 122 | + |
| 123 | + def remove_docs(self): |
| 124 | + self._remove_root_paths([ |
| 125 | + 'usr/share/doc', |
| 126 | + 'usr/share/doc-base', |
| 127 | + 'usr/local/share/doc', |
| 128 | + 'usr/local/share/doc-base', |
| 129 | + ]) |
| 130 | + |
| 131 | + def remove_mans(self): |
| 132 | + self._remove_root_paths([ |
| 133 | + 'usr/share/man', |
| 134 | + 'usr/local/share/man', |
| 135 | + ]) |
| 136 | + |
| 137 | + def remove_licenses(self): |
| 138 | + self._remove_root_paths([ |
| 139 | + 'usr/share/common-licenses', |
| 140 | + ]) |
| 141 | + |
| 142 | + def hardlink_under(self, path): |
| 143 | + fm = FileManager(os.path.join(self.path, path)) |
| 144 | + fm.load_tree() |
| 145 | + fm.generate_index() |
| 146 | + fm.create_hardlinks() |
| 147 | + |
| 148 | + def remove_platforms(self, filter_func): |
| 149 | + devpath = os.path.join(self.path, 'usr/share/sonic/device') |
| 150 | + for platform in os.listdir(devpath): |
| 151 | + if not filter_func(platform): |
| 152 | + path = os.path.join(devpath, platform) |
| 153 | + if DRY_RUN: |
| 154 | + print(f'rmtree platform {path}') |
| 155 | + else: |
| 156 | + shutil.rmtree(path) |
| 157 | + |
| 158 | + def remove_modules(self, modules): |
| 159 | + modpath = os.path.join(self.path, 'lib/modules') |
| 160 | + kversion = os.listdir(modpath)[0] |
| 161 | + kmodpath = os.path.join(modpath, kversion) |
| 162 | + for module in modules: |
| 163 | + path = os.path.join(kmodpath, module) |
| 164 | + if os.path.isdir(path): |
| 165 | + if DRY_RUN: |
| 166 | + print(f'rmtree module {path}') |
| 167 | + else: |
| 168 | + shutil.rmtree(path) |
| 169 | + |
| 170 | + def remove_firmwares(self, firmwares): |
| 171 | + fwpath = os.path.join(self.path, 'lib/firmware') |
| 172 | + for fw in firmwares: |
| 173 | + path = os.path.join(fwpath, fw) |
| 174 | + if os.path.isdir(path): |
| 175 | + if DRY_RUN: |
| 176 | + print(f'rmtree firmware {path}') |
| 177 | + else: |
| 178 | + shutil.rmtree(path) |
| 179 | + |
| 180 | + |
| 181 | + def specialize_aboot_image(self): |
| 182 | + fp = lambda p: '-' not in p or 'arista' in p or 'common' in p |
| 183 | + self.remove_platforms(fp) |
| 184 | + self.remove_modules([ |
| 185 | + 'kernel/drivers/gpu', |
| 186 | + 'kernel/drivers/infiniband', |
| 187 | + ]) |
| 188 | + self.remove_firmwares([ |
| 189 | + 'amdgpu', |
| 190 | + 'i915', |
| 191 | + 'mediatek', |
| 192 | + 'nvidia', |
| 193 | + 'radeon', |
| 194 | + ]) |
| 195 | + |
| 196 | + def specialize_image(self, image_type): |
| 197 | + if image_type == 'aboot': |
| 198 | + self.specialize_aboot_image() |
| 199 | + |
| 200 | +def parse_args(args): |
| 201 | + parser = argparse.ArgumentParser() |
| 202 | + parser.add_argument('fsroot', |
| 203 | + help="path to the fsroot build folder") |
| 204 | + parser.add_argument('-s', '--stats', action='store_true', |
| 205 | + help="show space statistics") |
| 206 | + parser.add_argument('--hardlinks', action='append', |
| 207 | + help="path where similar files need to be hardlinked") |
| 208 | + parser.add_argument('--remove-docs', action='store_true', |
| 209 | + help="remove documentation") |
| 210 | + parser.add_argument('--remove-licenses', action='store_true', |
| 211 | + help="remove license files") |
| 212 | + parser.add_argument('--remove-mans', action='store_true', |
| 213 | + help="remove manpages") |
| 214 | + parser.add_argument('--image-type', default=None, |
| 215 | + help="type of image being built") |
| 216 | + parser.add_argument('--dry-run', action='store_true', |
| 217 | + help="only display what would happen") |
| 218 | + return parser.parse_args(args) |
| 219 | + |
| 220 | +def main(args): |
| 221 | + args = parse_args(args) |
| 222 | + |
| 223 | + enable_dry_run(args.dry_run) |
| 224 | + |
| 225 | + fs = FsRoot(args.fsroot) |
| 226 | + if args.stats: |
| 227 | + begin = fs.collect_fsroot_size() |
| 228 | + print(f'fsroot size is {begin} bytes') |
| 229 | + |
| 230 | + if args.remove_docs: |
| 231 | + fs.remove_docs() |
| 232 | + |
| 233 | + if args.remove_mans: |
| 234 | + fs.remove_mans() |
| 235 | + |
| 236 | + if args.remove_licenses: |
| 237 | + fs.remove_licenses() |
| 238 | + |
| 239 | + if args.image_type: |
| 240 | + fs.specialize_image(args.image_type) |
| 241 | + |
| 242 | + for path in args.hardlinks: |
| 243 | + fs.hardlink_under(path) |
| 244 | + |
| 245 | + if args.stats: |
| 246 | + end = fs.collect_fsroot_size() |
| 247 | + pct = 100 - end / begin * 100 |
| 248 | + print(f'fsroot reduced to {end} from {begin} {pct:.2f}') |
| 249 | + |
| 250 | + return 0 |
| 251 | + |
| 252 | +if __name__ == '__main__': |
| 253 | + sys.exit(main(sys.argv[1:])) |
0 commit comments