build-docker 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. '--network', 'host',
  37. '--interactive',
  38. '--privileged',
  39. '--tty',
  40. '--workdir', target_dir,
  41. '--volume', '{}:{}'.format(kwargs['root_dir'], target_dir),
  42. 'ubuntu:20.04',
  43. 'bash',
  44. ])
  45. self.sh.run_cmd([
  46. 'docker',
  47. 'export',
  48. '-o',
  49. kwargs['docker_tar_file'],
  50. container_name,
  51. ])
  52. tar = tarfile.open(kwargs['docker_tar_file'])
  53. tar.extractall(kwargs['docker_tar_dir'])
  54. tar.close()
  55. # sudo not required in theory
  56. # https://askubuntu.com/questions/1046828/how-to-run-libguestfs-tools-tools-such-as-virt-make-fs-without-sudo
  57. self.sh.run_cmd([
  58. 'virt-make-fs',
  59. '--format', 'raw',
  60. '--size', '+1G',
  61. '--type', 'ext2',
  62. kwargs['docker_tar_dir'],
  63. kwargs['docker_rootfs_raw_file'],
  64. ])
  65. self.raw_to_qcow2(prebuilt=True)
  66. def get_build_dir(self):
  67. return kwargs['docker_build_dir']
  68. def get_default_args(self):
  69. return {'docker': True}
  70. Main().cli()