Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions oracle-linux-image-tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ The tool currently supports:
- Oracle VM Server (OVM)
Target packages: oracle-template-config + vmapi
Image format: OVA
- Vagrant (VirtualBox provider)
Target packages: VirtualBox guest additions
Image format: box
- Generic (No cloud setup)
Target packages: none
Image format: OVA
Expand All @@ -33,6 +36,7 @@ The tool currently supports:
`yum --enablerepo=ol7_developer install packer VirtualBox-6.0`
1. For `OCI` or `OLVM` images, install the ` qemu-img` package:
`yum install qemu-img`
1. For `Vagrant` box (VirtualBox provider), install [Vagrant](https://vagrantup.com/)
1. Clone this repo:
`git clone https://github.com/oracle/ol-sample-scripts.git`
1. The build script need root privileges during the build.
Expand Down
9 changes: 6 additions & 3 deletions oracle-linux-image-tools/bin/build-image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# Create minimal Oracle Linux images
#
# Copyright (c) 1982-2019 Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 1982-2020 Oracle and/or its affiliates. All rights reserved.
# Licensed under the Universal Permissive License v 1.0 as shown at
# https://oss.oracle.com/licenses/upl.
#
Expand Down Expand Up @@ -191,6 +191,9 @@ load_env() {
error "missing private key file: ${SSH_KEY_FILE}"
readonly SSH_PASSWORD SSH_KEY_FILE SSH_PUB_KEY

[[ "${LOCK_ROOT,,}" =~ ^(yes)|(no)$ ]] || error "LOCK_ROOT must be yes or no"
readonly LOCK_ROOT

[[ -z "${DISTR_NAME}" && -z "${BUILD_NUMBER}" ]] &&
error "missing distribution name / build number"
VM_NAME="${DISTR_NAME}-${CLOUD}-b${BUILD_NUMBER}"
Expand Down Expand Up @@ -356,8 +359,8 @@ packer_conf() {
"output_directory": "${WORKSPACE}/${VM_NAME}",
"vm_name": "${VM_NAME}",
"hard_drive_interface": "sata",
"disk_size": "${DISK_SIZE_MB}",
"guest_additions_mode": "disable",
"disk_size": "${DISK_SIZE_MB}",
"guest_additions_mode": "attach",
"format": "ova",
"headless": "true",
"ssh_username": "root",
Expand Down
42 changes: 39 additions & 3 deletions oracle-linux-image-tools/bin/mnt-img.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# Mount VM images
#
# Copyright (c) 1982-2019 Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 1982-2020 Oracle and/or its affiliates. All rights reserved.
# Licensed under the Universal Permissive License v 1.0 as shown at
# https://oss.oracle.com/licenses/upl.
#
Expand Down Expand Up @@ -181,6 +181,10 @@ fi
# lock
echo "MOUNTED IMAGE:$IMAGE_FILE $LPDEVICE" > $WORK_HOME/"$ID"

# Get host volume groups
host_vgs=$(vgs --noheadings -o vg_name)
guest_vg=0

# map device
if ! kpartx -a "$LPDEVICE" &>/dev/null; then
losetup -d "$LPDEVICE"
Expand All @@ -190,18 +194,50 @@ fi
mapped_devices=$(kpartx -l "$LPDEVICE" | grep "$LPDEVICE" | awk '{print $1}')
ct=1
for d in $mapped_devices; do
mkdir -p "$MOUNT_POINT"/$ct
file -sL /dev/mapper/"$d"
# mount ext3, ext4, btrfs, xfs partitions
if file -sL /dev/mapper/"$d" | egrep -q -i "ext3|ext4|btrfs|xfs"; then
mkdir -p "$MOUNT_POINT"/$ct
mount /dev/mapper/"$d" "$MOUNT_POINT"/$ct
echo "MOUNTED DIR:/dev/mapper/$d $MOUNT_POINT/$ct" |tee -a $WORK_HOME/"$ID"
ct=$((ct+1))
elif file -sL /dev/mapper/"$d" | egrep -q -i "lvm2"; then
echo "LVM detected"
guest_vg=1
pvscan --cache /dev/mapper/"$d"
else
echo "/dev/mapper/$d not mounted - Unknown filesystem."
fi

done

if [[ ${guest_vg} == 1 ]]; then
# Image has LVM
# This only works for simple setups (where the whole VG is on the device)
# Scan for Volume Groups
all_vgs=$(vgscan | grep "Found volume group" | sed -e 's/^[^"]*"\([^"]*\)".*$/\1/')
for vg in ${all_vgs}; do
# Only consider new VGs
if ! grep -w -q "${vg}" <<<${host_vgs}; then
# Ensure the VG is active and register it
vgchange -ay "${vg}"
echo "LVM GROUP:${vg}" | tee -a "${WORK_HOME}/${ID}"
lvscan >/dev/null
# Search for filesystems in the VG
for fs in "/dev/${vg}"/*; do
file -sL "${fs}"
if file -sL "${fs}" | egrep -q -i "ext3|ext4|btrfs|xfs"; then
mkdir -p "${MOUNT_POINT}/${ct}"
mount "${fs}" "${MOUNT_POINT}/${ct}"
echo "MOUNTED DIR:${fs} ${MOUNT_POINT}/${ct}" | tee -a "${WORK_HOME}/${ID}"
ct=$((ct+1))
else
echo "${fs} not mounted - Unknown filesystem."
fi
done
fi
done
fi

;;

############################################################################
Expand Down
7 changes: 6 additions & 1 deletion oracle-linux-image-tools/bin/provision.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# Packer main provisioning script
#
# Copyright (c) 1982-2019 Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 1982-2020 Oracle and/or its affiliates. All rights reserved.
# Licensed under the Universal Permissive License v 1.0 as shown at
# https://oss.oracle.com/licenses/upl.
#
Expand Down Expand Up @@ -35,6 +35,11 @@ echo_message() {
echo "--- $@ ---"
}

echo_error() {
echo "--- $@ ---" >&2
exit 1
}

#######################################
# Load environment variables and provisioning scripts
# Globals:
Expand Down
12 changes: 12 additions & 0 deletions oracle-linux-image-tools/cloud/vagrant-virtualbox/env.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Default parameters for the vagrant-virtualbox cloud.
# Do NOT change anything in this file, customisation must be done in separate
# env file.

# Memory and CPU to allocate to the box by default at runtime (default: use
# build VM parameters)
# VAGRANT_VIRTUALBOX_MEM_SIZE=
# VAGRANT_VIRTUALBOX_CPU_NUM=

# Additional disk to attach to the VM.
# Empty means no disk, otherwhise it is the size in GB of the extra disk
# VAGRANT_VIRTUALBOX_EXTRA_DISK_GB=
84 changes: 84 additions & 0 deletions oracle-linux-image-tools/cloud/vagrant-virtualbox/image-scripts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/usr/bin/env bash
#
# Cleanup and package image for the "vagrant-virtualbox" image
#
# Copyright (c) 1982-2020 Oracle and/or its affiliates. All rights reserved.
# Licensed under the Universal Permissive License v 1.0 as shown at
# https://oss.oracle.com/licenses/upl.
#
# Description: this module provides 2 functions:
# cloud::image_cleanup: cloud specific actions to cleanup the image
# This function is optional
# cloud::image_package: Package the raw image for the target cloud.
# This function must be defined either at cloud or cloud/distribution level
#
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
#

#######################################
# Parameter validation
# Globals:
# VAGRANT_VIRTUALBOX_CPU, VAGRANT_VIRTUALBOX_MEMORY,
# VAGRANT_VIRTUALBOX_EXTRA_DISK_GB
# Arguments:
# None
# Returns:
# None
#######################################
cloud::image_cleanup() {
[[ ${VAGRANT_VIRTUALBOX_CPU_NUM} =~ ^[0-9]*$ ]] || error "vagrant cpu count is not numeric"
[[ ${VAGRANT_VIRTUALBOX_MEM_SIZE} =~ ^[0-9]*$ ]] || error "vagrant memory is not numeric"
[[ ${VAGRANT_VIRTUALBOX_EXTRA_DISK_GB} =~ ^[0-9]*$ ]] || error "vagrant disk size is not numeric"
}

#######################################
# Cleanup actions run directly on the image
# Globals:
# None
# Arguments:
# root filesystem directory
# boot filesystem directory
# Returns:
# None
#######################################
cloud::image_cleanup() {
:
}

#######################################
# Image packaging: generate box using vagrant tool
# Globals:
# VM_NAME, VAGRANT_VIRTUALBOX_CPU, VAGRANT_VIRTUALBOX_MEMORY,
# VAGRANT_VIRTUALBOX_EXTRA_DISK_GB
# Arguments:
# None
# Returns:
# None
#######################################
cloud::image_package() {
local cpu="${VAGRANT_VIRTUALBOX_CPU_NUM:-$CPU_NUM}"
local memory="${VAGRANT_VIRTUALBOX_MEM_SIZE:-$MEM_SIZE}"
# convert back to VMDK
local vmdk=$(grep "ovf:href" "${VM_NAME}.ovf" | sed -r -e 's/.*ovf:href="([^"]+)".*/\1/')
vboxmanage convertfromraw System.img --format VMDK "${vmdk}" --variant Stream
rm System.img
# re-create the OVA file
tar cvf "${VM_NAME}.ova" "${VM_NAME}.ovf" "${vmdk}"
rm "${vmdk}"
# Import in VirtualBox and adjust cpu/memory for the box
vboxmanage import "${VM_NAME}.ova" \
--vsys 0 --vmname "${VM_NAME}" \
--vsys 0 --ostype "Oracle_64" \
--vsys 0 --cpus $cpu \
--vsys 0 --memory $memory
rm "${VM_NAME}.ova"
# Add additional disk
if [[ -n $VAGRANT_VIRTUALBOX_EXTRA_DISK_GB ]]; then
local disk_size_mb=$(( ${VAGRANT_VIRTUALBOX_EXTRA_DISK_GB} * 1024 ))
vboxmanage createhd --filename ./extra_disk.vdi --size $disk_size_mb --format VDI --variant fixed
vboxmanage storageattach "${VM_NAME}" --storagectl "SATA Controller" --port 1 --device 0 --type hdd --medium ./extra_disk.vdi
fi
# Create the box
vagrant package --base "${VM_NAME}" --output "${VM_NAME}.box"
vboxmanage unregistervm "${VM_NAME}" --delete
}
Loading