build-docker 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python3
  2. import os
  3. import subprocess
  4. import tarfile
  5. import common
  6. from shell_helpers import LF
  7. class DockerComponent(self.Component):
  8. def get_argparse_args(self):
  9. return {
  10. 'description': '''\
  11. Build a guest root filesystem based on prebuilt Docker Ubuntu root filesystems.
  12. See also: https://github.com/cirosantilli/linux-kernel-module-cheatTODO#ubuntu-guest-setup
  13. '''
  14. }
  15. def build(self):
  16. build_dir = self.get_build_dir()
  17. container_name = 'lkmc-guest'
  18. target_dir = os.path.join('/root', 'linux-kernel-module-cheat')
  19. os.makedirs(build_dir, exist_ok=True)
  20. containers = self.sh.check_output([
  21. 'docker',
  22. 'ps',
  23. '-a',
  24. '--format', '{{.Names}}',
  25. ]).decode()
  26. if container_name in containers.split():
  27. self.sh.run_cmd([
  28. 'docker',
  29. 'rm',
  30. container_name,
  31. ])
  32. self.sh.run_cmd([
  33. 'docker',
  34. 'create',
  35. '--name', container_name,
  36. '--net',
  37. 'host',
  38. '-i',
  39. '--privileged',
  40. '-t',
  41. '-w', target_dir,
  42. '-v', '{}:{}'.format(kwargs['root_dir'], target_dir),
  43. 'ubuntu:18.04',
  44. 'bash',
  45. ])
  46. self.sh.run_cmd([
  47. 'docker',
  48. 'export',
  49. '-o',
  50. kwargs['docker_tar_file'],
  51. container_name,
  52. ])
  53. tar = tarfile.open(kwargs['docker_tar_file'])
  54. tar.extractall(kwargs['docker_tar_dir'])
  55. tar.close()
  56. # sudo not required in theory
  57. # https://askubuntu.com/questions/1046828/how-to-run-libguestfs-tools-tools-such-as-virt-make-fs-without-sudo
  58. self.sh.run_cmd([
  59. 'virt-make-fs',
  60. '--format', 'raw',
  61. '--size', '+1G',
  62. '--type', 'ext2',
  63. kwargs['docker_tar_dir'],
  64. kwargs['docker_rootfs_raw_file'],
  65. ])
  66. self.raw_to_qcow2(prebuilt=True)
  67. def get_build_dir(self):
  68. return kwargs['docker_build_dir']
  69. def get_default_args(self):
  70. return {'docker': True}
  71. Main().cli()