Skip to content
21 changes: 15 additions & 6 deletions oracle-linux-image-tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ The tool currently supports:

- Distributions:
- Oracle Linux 7 update 9 -- Slim (x86_64)
- Oracle Linux 8 update 5 -- Slim (x86_64 and aarch64)
- Oracle Linux 8 update 6 -- Slim (x86_64 and aarch64)
__Note__: for aarch64, only Generic and OCI clouds are supported
- Oracle Linux 9 update 0 -- Slim (x86_64 and aarch64)
__Note__: for aarch64, only Generic and OCI clouds are supported
- Clouds:
- Microsoft Azure cloud
Expand Down Expand Up @@ -87,12 +89,13 @@ The build script requires a Linux environment and has been tested on Oracle Linu
Instead of providing an Oracle Linux distribution ISO you can use a _boot_ ISO image.
In that case, you will have to provide an URL to an installation tree and optionally additional yum repositories required by the installation.

Example for an Oracle Linux 8 Update 5 using the UEK boot ISO:
Example for an Oracle Linux 9 using the UEK boot ISO:

```Shell
ISO_URL="https://yum.oracle.com/ISOS/OracleLinux/OL8/u5/x86_64/x86_64-boot-uek.iso"
REPO_URL="https://yum.oracle.com/repo/OracleLinux/OL8/baseos/latest/x86_64"
REPO[AppStream]="https://yum.oracle.com/repo/OracleLinux/OL8/appstream/x86_64"
ISO_URL="https://yum.oracle.com/ISOS/OracleLinux/OL9/u0/x86_64/OracleLinux-R9-U0-x86_64-boot-uek.iso"
REPO_URL="https://yum.oracle.com/repo/OracleLinux/OL9/baseos/latest/x86_64"
REPO[AppStream]="https://yum.oracle.com/repo/OracleLinux/OL9/appstream/x86_64"
REPO[ol9_UEKR7]="https://yum.oracle.com/repo/OracleLinux/OL9/UEKR7/x86_64"
```

### Customizing builds
Expand Down Expand Up @@ -211,7 +214,8 @@ The builder will process the directories in the following order:
- cloud_distr::cleanup
- cloud::cleanup
- distr::cleanup
1. Image cleanup: the generated image is mounted on the host and the `image-scripts` scripts are run:
- distr::seal[^1]
1. Image cleanup: the generated image is mounted on the host and the `image-scripts` scripts are run[^1]:
- custom::cleanup
- cloud_distr::cleanup
- cloud::cleanup
Expand All @@ -222,6 +226,11 @@ The builder will process the directories in the following order:
- cloud_distr::image_package
- cloud::image_package

[^1]: `provision` `seal` vs. `image-scripts` `cleanup`.
These functions have the same purpose: _seal_ the image before packaging.
The difference is that the former runs in the VM while the latter runs on the host.
Sealing on the host might be more efficient, but when it is not possible to mount the image disk on the host, in-VM sealing can be used. When no `image-scripts` `cleanup` are defined, no attempt will be made to mount the filesystem on the host.

## Feedback

Please provide feedback of any kind via GitHub issues on this repository.
Expand Down
123 changes: 66 additions & 57 deletions oracle-linux-image-tools/bin/build-image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Create minimal Oracle Linux images
#
# Copyright (c) 2019-2022 Oracle and/or its affiliates.
# Copyright (c) 2019, 2022 Oracle and/or its affiliates.
# Licensed under the Universal Permissive License v 1.0 as shown at
# https://oss.oracle.com/licenses/upl.
#
Expand Down Expand Up @@ -545,7 +545,7 @@ image_cleanup() {
cd "${WORKSPACE}/${VM_NAME}"
if common::is_vbox ; then
tar -xf "${VM_NAME}.ova"
rm "${VM_NAME}.ova"
mv "${VM_NAME}.ova" System.ova
mv -f "${VM_NAME}"-disk*.vmdk System.vmdk
vbox-img convert \
--srcfilename System.vmdk \
Expand All @@ -555,68 +555,76 @@ image_cleanup() {
rm -f System.vmdk
fi

echo_message "Loopback mount image"
# Loopback mount the image
# We will have the following subdirectories:
# - 1: /boot
# - 2: root filesystem (/)
# In case of a btrfs filesystem, / will be in root subvolume
# Should /boot be part of the btrfs volume we then have:
# - 1: btrfs volume with boot and root subvolumes
rm -rf "${mnt}"
mkdir "${mnt}"
sudo "${MOUNT_IMAGE}" System.img "${mnt}"
if [[ $(stat -f -c "%T" "${mnt}/1") = "btrfs" ]]; then
# Both / and /boot are on BTRFS
boot_fs="${mnt}/1/boot"
root_fs="${mnt}/1/root"
else
boot_fs="${mnt}/1"
if [[ $(stat -f -c "%T" "${mnt}/2") = "btrfs" ]]; then
root_fs="${mnt}/2/root"
# Run cleanup scripts
if [[ "$(type -t custom::image_cleanup)" = 'function' ||
"$(type -t cloud_distr::image_cleanup)" = 'function' ||
"$(type -t cloud::image_cleanup)" = 'function' ||
"$(type -t distr::image_cleanup)" = 'function' ]]; then
# Only mount the image if we have an image_cleanup function defined
echo_message "Loopback mount image"
# Loopback mount the image
# We will have the following subdirectories:
# - 1: /boot
# - 2: root filesystem (/)
# In case of a btrfs filesystem, / will be in root subvolume
# Should /boot be part of the btrfs volume we then have:
# - 1: btrfs volume with boot and root subvolumes
rm -rf "${mnt}"
mkdir "${mnt}"
sudo "${MOUNT_IMAGE}" System.img "${mnt}"
if [[ $(stat -f -c "%T" "${mnt}/1") = "btrfs" ]]; then
# Both / and /boot are on BTRFS
boot_fs="${mnt}/1/boot"
root_fs="${mnt}/1/root"
else
root_fs="${mnt}/2"
boot_fs="${mnt}/1"
if [[ $(stat -f -c "%T" "${mnt}/2") = "btrfs" ]]; then
root_fs="${mnt}/2/root"
else
root_fs="${mnt}/2"
fi
fi

# Basic check to see if we have the "right" partitions mounted
if [[ ! -d "${root_fs}/etc" || ! -d "${boot_fs}/grub2" ]]; then
sudo "${MOUNT_IMAGE}" -u System.img
rm -rf "${mnt}"
error "Loopback mount failed"
fi

# Run cleanup scripts
if [[ "$(type -t custom::image_cleanup)" = 'function' ]]; then
echo_message "Run custom cleanup"
custom::image_cleanup "${root_fs}" "${boot_fs}"
fi
if [[ "$(type -t cloud_distr::image_cleanup)" = 'function' ]]; then
echo_message "Run cloud distribution cleanup"
cloud_distr::image_cleanup "${root_fs}" "${boot_fs}"
fi
if [[ "$(type -t cloud::image_cleanup)" = 'function' ]]; then
echo_message "Run cloud cleanup"
cloud::image_cleanup "${root_fs}" "${boot_fs}"
fi
if [[ "$(type -t distr::image_cleanup)" = 'function' ]]; then
echo_message "Run distribution cleanup"
distr::image_cleanup "${root_fs}" "${boot_fs}"
fi
fi

# Basic check to see if we have the "right" partitions mounted
if [[ ! -d "${root_fs}/etc" || ! -d "${boot_fs}/grub2" ]]; then
# Ensure we are still in the image directory
cd "${WORKSPACE}/${VM_NAME}"
# unmount and trim image
echo_message "Unmount and trim image"
sudo -- bash -c '
sync; sync; sync;
fstrim "'"${boot_fs}"'";
fstrim "'"${root_fs}"'";
'
sudo "${MOUNT_IMAGE}" -u System.img
rm -rf "${mnt}"
error "Loopback mount failed"
fi

# Run cleanup scripts
if [[ "$(type -t custom::image_cleanup)" = 'function' ]]; then
echo_message "Run custom cleanup"
custom::image_cleanup "${root_fs}" "${boot_fs}"
fi
if [[ "$(type -t cloud_distr::image_cleanup)" = 'function' ]]; then
echo_message "Run cloud distribution cleanup"
cloud_distr::image_cleanup "${root_fs}" "${boot_fs}"
cp --sparse=always System.img System.img.sparse
mv -f System.img.sparse System.img
fi
if [[ "$(type -t cloud::image_cleanup)" = 'function' ]]; then
echo_message "Run cloud cleanup"
cloud::image_cleanup "${root_fs}" "${boot_fs}"
fi
if [[ "$(type -t distr::image_cleanup)" = 'function' ]]; then
echo_message "Run distribution cleanup"
distr::image_cleanup "${root_fs}" "${boot_fs}"
fi

# Ensure we are still in the image directory
cd "${WORKSPACE}/${VM_NAME}"
# unmount and trim image
echo_message "Unmount and trim image"
sudo -- bash -c '
sync; sync; sync;
fstrim "'"${boot_fs}"'";
fstrim "'"${root_fs}"'";
'
sudo "${MOUNT_IMAGE}" -u System.img
cp --sparse=always System.img System.img.sparse
mv -f System.img.sparse System.img
rm -rf "${mnt}"

echo_message "Package image"
if [[ "$(type -t custom::image_package)" = 'function' ]]; then
Expand All @@ -631,6 +639,7 @@ image_cleanup() {

if common::is_vbox ; then
rm "${WORKSPACE}/${VM_NAME}/${VM_NAME}.ovf"
rm "${WORKSPACE}/${VM_NAME}/System.ova"
fi
}

Expand Down
49 changes: 44 additions & 5 deletions oracle-linux-image-tools/bin/provision.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
#
# Packer main provisioning script
#
# Copyright (c) 2019,2020 Oracle and/or its affiliates.
# Copyright (c) 2019, 2022 Oracle and/or its affiliates.
# Licensed under the Universal Permissive License v 1.0 as shown at
# https://oss.oracle.com/licenses/upl.
#
# Description: provision image by calling child provisioners
# Description:
# - provision image by calling child provisioners
# - Seal image by calling distribution seal function (final cleanup
# cleanup before packaging)
#
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
#
Expand Down Expand Up @@ -57,7 +60,9 @@ echo_error() {
load_env() {
local dir

source "${ENV_FILE}"
if [[ -r "${ENV_FILE}" ]]; then
source "${ENV_FILE}"
fi

if [[ -n "${PROXY_URL}" ]]; then
export http_proxy="${PROXY_URL}"
Expand All @@ -79,9 +84,9 @@ load_env() {
}

#######################################
# Main
# provision
#######################################
main () {
provision () {
echo_header "Load environment"
load_env
if [[ "$(type -t distr::provision)" = 'function' ]]; then
Expand Down Expand Up @@ -118,4 +123,38 @@ main () {
fi
}

#######################################
# seal
#######################################
seal () {
echo_header "Load environment"
load_env
if [[ "$(type -t distr::seal)" = 'function' ]]; then
echo_header "Seal VM image"
distr::seal
else
echo_message "No seal function defined"
fi
}

#######################################
# Main
#######################################
main () {
if [[ -z ${OLIT_ACTION} ]]; then
echo_error "OLIT_ACTION undefined"
fi
case "${OLIT_ACTION}" in
provision)
provision
;;
seal)
seal
;;
*)
echo_error "Unexpected action: ${OLIT_ACTION}"
;;
esac
}

main "$@"
8 changes: 4 additions & 4 deletions oracle-linux-image-tools/cloud/azure/image-scripts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# Cleanup and package image for Azure
#
# Copyright (c) 2019-2022 Oracle and/or its affiliates.
# Copyright (c) 2019, 2022 Oracle and/or its affiliates.
# Licensed under the Universal Permissive License v 1.0 as shown at
# https://oss.oracle.com/licenses/upl
#
Expand All @@ -25,9 +25,9 @@
# Returns:
# None
#######################################
cloud::image_cleanup() {
:
}
# cloud::image_cleanup() {
# :
# }

#######################################
# Image packaging
Expand Down
8 changes: 4 additions & 4 deletions oracle-linux-image-tools/cloud/none/image-scripts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# Cleanup and package image for the "None" image
#
# Copyright (c) 2019-2022 Oracle and/or its affiliates.
# Copyright (c) 2019, 2022 Oracle and/or its affiliates.
# Licensed under the Universal Permissive License v 1.0 as shown at
# https://oss.oracle.com/licenses/upl
#
Expand All @@ -25,9 +25,9 @@
# Returns:
# None
#######################################
cloud::image_cleanup() {
:
}
# cloud::image_cleanup() {
# :
# }

#######################################
# Image packaging:
Expand Down
8 changes: 4 additions & 4 deletions oracle-linux-image-tools/cloud/oci/image-scripts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# Cleanup and package image for OCI
#
# Copyright (c) 2020-2022 Oracle and/or its affiliates.
# Copyright (c) 2020, 2022 Oracle and/or its affiliates.
# Licensed under the Universal Permissive License v 1.0 as shown at
# https://oss.oracle.com/licenses/upl
#
Expand All @@ -25,9 +25,9 @@
# Returns:
# None
#######################################
cloud::image_cleanup() {
:
}
# cloud::image_cleanup() {
# :
# }

#######################################
# Image packaging - creates a PVM and PVHVM OVA
Expand Down
8 changes: 4 additions & 4 deletions oracle-linux-image-tools/cloud/olvm/image-scripts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# Cleanup and package image for OLVM
#
# Copyright (c) 2020-2022 Oracle and/or its affiliates.
# Copyright (c) 2020, 2022 Oracle and/or its affiliates.
# Licensed under the Universal Permissive License v 1.0 as shown at
# https://oss.oracle.com/licenses/upl
#
Expand Down Expand Up @@ -40,9 +40,9 @@ cloud::validate() {
# Returns:
# None
#######################################
cloud::image_cleanup() {
:
}
# cloud::image_cleanup() {
# :
# }

#######################################
# Image packaging - creates an OVA
Expand Down
3 changes: 2 additions & 1 deletion oracle-linux-image-tools/cloud/olvm/mk-envelope.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
Generate OLVM compatible OVF file.

Copyright (c) 2020-2022 Oracle and/or its affiliates.
Copyright (c) 2020, 2022 Oracle and/or its affiliates.
Licensed under the Universal Permissive License v 1.0 as shown at
https://oss.oracle.com/licenses/upl

Expand All @@ -26,6 +26,7 @@
'OL6': 5002,
'OL7': 5003,
'OL8': 5006,
'OL9': 5006, # Use OL8 ID for now, to support older OLVM versions
}


Expand Down
Loading