build-linux-surface.py 4.0 KB

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