build-docker 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python3
  2. import os
  3. import subprocess
  4. import tarfile
  5. import common
  6. class DockerComponent(self.Component):
  7. def get_argparse_args(self):
  8. return {
  9. 'description': '''\
  10. Build a guest root filesystem based on prebuilt Docker Ubuntu root filesystems.
  11. See also:https://github.com/cirosantilli/linux-kernel-module-cheat#ubuntu-guest-setup
  12. '''
  13. }
  14. def do_build(self, args):
  15. build_dir = self.get_build_dir(args)
  16. container_name = 'lkmc-guest'
  17. target_dir = os.path.join('/root', 'linux-kernel-module-cheat')
  18. os.makedirs(build_dir, exist_ok=True)
  19. containers = subprocess.check_output([
  20. 'docker',
  21. 'ps',
  22. '-a',
  23. '--format', '{{.Names}}',
  24. ]).decode()
  25. if container_name in containers.split():
  26. self.sh.run_cmd([
  27. 'docker',
  28. 'rm',
  29. container_name,
  30. ])
  31. self.sh.run_cmd([
  32. 'docker',
  33. 'create',
  34. '--name', container_name,
  35. '--net',
  36. 'host',
  37. '-i',
  38. '--privileged',
  39. '-t',
  40. '-w', target_dir,
  41. '-v', '{}:{}'.format(kwargs['root_dir'], target_dir),
  42. 'ubuntu:18.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, args):
  67. return kwargs['docker_build_dir']
  68. def get_default_args(self):
  69. return {'docker': True}
  70. DockerComponent().build()