parent
c76149141f
commit
8c975e5c46
|
@ -34,6 +34,29 @@
|
||||||
# with that.
|
# with that.
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
from contextlib import contextmanager
|
||||||
|
|
||||||
|
|
||||||
|
# Create a chroot context manager
|
||||||
|
# This can be used later in the script to chroot to the destination directory
|
||||||
|
# for instance to run commands within the target.
|
||||||
|
@contextmanager
|
||||||
|
def chroot_target(destination):
|
||||||
|
try:
|
||||||
|
real_root = os.open("/", os.O_RDONLY)
|
||||||
|
os.chroot(destination)
|
||||||
|
fake_root = os.open("/", os.O_RDONLY)
|
||||||
|
os.fchdir(fake_root)
|
||||||
|
yield
|
||||||
|
finally:
|
||||||
|
os.fchdir(real_root)
|
||||||
|
os.chroot(".")
|
||||||
|
os.fchdir(real_root)
|
||||||
|
os.close(fake_root)
|
||||||
|
os.close(real_root)
|
||||||
|
del fake_root
|
||||||
|
del real_root
|
||||||
|
|
||||||
|
|
||||||
# Installation function - performs a debootstrap install of a Debian system
|
# Installation function - performs a debootstrap install of a Debian system
|
||||||
# Note that the only arguments are keyword arguments.
|
# Note that the only arguments are keyword arguments.
|
||||||
|
@ -193,13 +216,7 @@ GRUB_DISABLE_LINUX_UUID=false
|
||||||
fh.write(data)
|
fh.write(data)
|
||||||
|
|
||||||
# Chroot, do some in-root tasks, then exit the chroot
|
# Chroot, do some in-root tasks, then exit the chroot
|
||||||
# EXITING THE CHROOT IS VERY IMPORTANT OR THE FOLLOWING STAGES OF THE PROVISIONER
|
with chroot_target(temporary_directory):
|
||||||
# WILL FAIL IN UNEXPECTED WAYS! Keep this in mind when using chroot in your scripts.
|
|
||||||
real_root = os.open("/", os.O_RDONLY)
|
|
||||||
os.chroot(temporary_directory)
|
|
||||||
fake_root = os.open("/", os.O_RDONLY)
|
|
||||||
os.fchdir(fake_root)
|
|
||||||
|
|
||||||
# Install and update GRUB
|
# Install and update GRUB
|
||||||
os.system(
|
os.system(
|
||||||
"grub-install --force /dev/rbd/{}/{}_{}".format(root_disk['pool'], vm_name, root_disk['disk_id'])
|
"grub-install --force /dev/rbd/{}/{}_{}".format(root_disk['pool'], vm_name, root_disk['disk_id'])
|
||||||
|
@ -219,15 +236,6 @@ GRUB_DISABLE_LINUX_UUID=false
|
||||||
"systemctl enable cloud-init.target"
|
"systemctl enable cloud-init.target"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Restore our original root/exit the chroot
|
|
||||||
# EXITING THE CHROOT IS VERY IMPORTANT OR THE FOLLOWING STAGES OF THE PROVISIONER
|
|
||||||
# WILL FAIL IN UNEXPECTED WAYS! Keep this in mind when using chroot in your scripts.
|
|
||||||
os.fchdir(real_root)
|
|
||||||
os.chroot(".")
|
|
||||||
os.fchdir(real_root)
|
|
||||||
os.close(fake_root)
|
|
||||||
os.close(real_root)
|
|
||||||
|
|
||||||
# Unmount the bound devfs
|
# Unmount the bound devfs
|
||||||
os.system(
|
os.system(
|
||||||
"umount {}/dev".format(
|
"umount {}/dev".format(
|
||||||
|
@ -235,8 +243,4 @@ GRUB_DISABLE_LINUX_UUID=false
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
# Clean up file handles so paths can be unmounted
|
|
||||||
del fake_root
|
|
||||||
del real_root
|
|
||||||
|
|
||||||
# Everything else is done via cloud-init user-data
|
# Everything else is done via cloud-init user-data
|
||||||
|
|
|
@ -29,7 +29,7 @@
|
||||||
# This script will run under root privileges as the provisioner does. Be careful
|
# This script will run under root privileges as the provisioner does. Be careful
|
||||||
# with that.
|
# with that.
|
||||||
|
|
||||||
# Installation function - performs a debootstrap install of a Debian system
|
# Installation function - performs no actions then returns
|
||||||
# Note that the only arguments are keyword arguments.
|
# Note that the only arguments are keyword arguments.
|
||||||
def install(**kwargs):
|
def install(**kwargs):
|
||||||
# The provisioner has already mounted the disks on kwargs['temporary_directory'].
|
# The provisioner has already mounted the disks on kwargs['temporary_directory'].
|
||||||
|
|
Loading…
Reference in New Issue