build-linux-surface.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/usr/bin/env python3
  2. import argparse
  3. import subprocess
  4. import sys
  5. from pathlib import Path
  6. #####################################################################
  7. ##
  8. ## The name of the modified kernel package.
  9. ##
  10. PACKAGE_NAME = "surface"
  11. ##
  12. ## https://gitlab.com/cki-project/kernel-ark/-/tags
  13. ##
  14. ## Fedora tags: kernel-X.Y.Z
  15. ## Upstream tags: vX.Y.Z
  16. ##
  17. PACKAGE_TAG = "kernel-6.3.12-0"
  18. ##
  19. ## The release number of the modified kernel package.
  20. ## e.g. 300 for kernel-6.3.1-300.fc38.foo
  21. ##
  22. PACKAGE_RELEASE = "1"
  23. ##
  24. ## Build options for configuring which parts of the kernel package are enabled.
  25. ##
  26. ## We disable all userspace components because we only want the kernel + modules.
  27. ## We also don't care too much about debug info or UKI.
  28. ##
  29. ## To list the available options, run make dist-full-help in the kernel-ark tree.
  30. ##
  31. KERNEL_BUILDOPTS = "+up +baseonly -debuginfo -doc -headers -efiuki"
  32. #####################################################################
  33. parser = argparse.ArgumentParser(usage="Build a Fedora kernel with linux-surface patches")
  34. parser.add_argument(
  35. "--ark-dir",
  36. help="The local path to the kernel-ark repository.",
  37. default="kernel-ark",
  38. )
  39. parser.add_argument(
  40. "--ark-url",
  41. help="The remote path to the kernel-ark repository.",
  42. default="https://gitlab.com/cki-project/kernel-ark",
  43. )
  44. parser.add_argument(
  45. "--mode",
  46. help="Whether to build a source RPM or binary RPMs.",
  47. choices=["rpms", "srpm"],
  48. default="rpms",
  49. )
  50. parser.add_argument(
  51. "--outdir",
  52. help="The directory where the built RPM files will be saved.",
  53. default="out",
  54. )
  55. args = parser.parse_args()
  56. # The directory where this script is saved.
  57. script = Path(sys.argv[0]).resolve().parent
  58. # The root of the linux-surface repository.
  59. linux_surface = script / ".." / ".." / ".."
  60. # Determine the major version of the kernel.
  61. kernel_version = PACKAGE_TAG.split("-")[1]
  62. kernel_major = ".".join(kernel_version.split(".")[:2])
  63. # Determine the patches directory and config file.
  64. patches = linux_surface / "patches" / kernel_major
  65. config = linux_surface / "configs" / ("surface-%s.config" % kernel_major)
  66. sb_cert = script / "secureboot" / "MOK.crt"
  67. sb_key = script / "secureboot" / "MOK.key"
  68. # Check if the major version is supported.
  69. if not patches.exists() or not config.exists():
  70. print("ERROR: Could not find patches / configs for kernel %s!" % kernel_major)
  71. sys.exit(1)
  72. # Check if Secure Boot keys are available.
  73. sb_avail = sb_cert.exists() and sb_key.exists()
  74. # If we are building without secureboot, require user input to continue.
  75. if not sb_avail:
  76. print("")
  77. print("Secure Boot keys were not configured! Using Red Hat testkeys.")
  78. print("The compiled kernel will not boot with Secure Boot enabled!")
  79. print("")
  80. input("Press enter to continue: ")
  81. # Expand globs
  82. surface_patches = sorted(patches.glob("*.patch"))
  83. cmd = [script / "build-ark.py"]
  84. cmd += ["--ark-dir", args.ark_dir]
  85. cmd += ["--ark-url", args.ark_url]
  86. cmd += ["--mode", args.mode]
  87. cmd += ["--outdir", args.outdir]
  88. cmd += ["--package-name", PACKAGE_NAME]
  89. cmd += ["--package-tag", PACKAGE_TAG]
  90. cmd += ["--package-release", PACKAGE_RELEASE]
  91. cmd += ["--patch"] + surface_patches
  92. cmd += ["--config", config]
  93. cmd += ["--buildopts", KERNEL_BUILDOPTS]
  94. local_patches = sorted((script / "patches").glob("*.patch"))
  95. local_configs = sorted((script / "configs").glob("*.config"))
  96. local_files = sorted((script / "files").glob("*"))
  97. if len(local_patches) > 0:
  98. cmd += ["--patch"] + local_patches
  99. if len(local_configs) > 0:
  100. cmd += ["--config"] + local_configs
  101. if len(local_files) > 0:
  102. cmd += ["--file"] + local_files
  103. if sb_avail:
  104. sb_patches = sorted((script / "secureboot").glob("*.patch"))
  105. sb_configs = sorted((script / "secureboot").glob("*.config"))
  106. if len(sb_patches) > 0:
  107. cmd += ["--patch"] + sb_patches
  108. if len(sb_configs) > 0:
  109. cmd += ["--config"] + sb_configs
  110. cmd += ["--file", sb_cert, sb_key]
  111. subprocess.run(cmd, check=True)