Skip to content

Instantly share code, notes, and snippets.

@cy-lee
Last active December 21, 2021 07:34
Show Gist options
  • Save cy-lee/48bd7e1810e78f1f8938b3609b27933f to your computer and use it in GitHub Desktop.
Save cy-lee/48bd7e1810e78f1f8938b3609b27933f to your computer and use it in GitHub Desktop.
Cobbler 3.2.2 import Ubuntu image cannot PXE boot correctly because initrd.gz point to wrong path.
#!/usr/bin/python3
# cylee: This module try to modify all Ubuntu distro. initrd file to netboot based
import logging
import os
import sys
from xmlrpc.client import Server
# Place this file under /var/lib/cobbler/triggers/task/import/post
# it will auto trigger once imported an image
def register():
return "/var/lib/cobbler/triggers/task/import/post/*"
def run(api, args, logger):
main(logger)
def main(logger):
# change 10.2.0.2 to your Cobbler IP
rpc_server = Server("http://10.2.0.2/cobbler_api")
token = rpc_server.login("cobbler", "cobbler")
distros = rpc_server.get_distros()
for distro in distros:
if distro['breed'] != "ubuntu":
continue
distro_name = distro['name']
print("Found Ubuntu -> {}".format(distro_name))
logger.info("Found Ubuntu -> {}".format(distro_name))
initrd_path = distro['initrd']
initrd_dir = os.path.dirname(initrd_path)
initrd_filename = os.path.basename(initrd_path)
if "netboot" in initrd_dir:
print("Skip, it already point to netboot initrd image, {}".format(initrd_path))
logger.info("Skip, it already point to netboot initrd image, {}".format(initrd_path))
continue
netboot_dir = os.path.join(initrd_dir, "netboot/ubuntu-installer/amd64")
netboot_path = os.path.join(netboot_dir, initrd_filename)
distro_handle = rpc_server.get_distro_handle(distro_name, token)
print("netboot path -> {}".format(netboot_path))
logger.info("netboot path -> {}".format(netboot_path))
rpc_server.modify_distro(distro_handle, "initrd", netboot_path, token)
print("Change initrd path to -> {}".format(netboot_path))
logger.info("Change initrd path to -> {}".format(netboot_path))
success = rpc_server.save_distro(distro_handle, token)
if not success:
print("Fail to update initrd path")
logger.error("Fail to update initrd path")
continue
return 0
if __name__ == "__main__":
logger = logging.getLogger('provision')
sys.exit(main(logger))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment