-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow for custom image partition schemes.
This commit provides preliminary support for custom partitioning schemes/image file types. For discussion refer to: issue #10: #10 This change assumes some artefacts provided by PR #28 are available: #28 Specifically: br_image_dir() and br_image_path() functions. This change does **not** address integration within brickstrap.sh itself (should be trivial, though).
- Loading branch information
Showing
1 changed file
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
#!/bin/bash | ||
# | ||
# This file is part of brickstrap. | ||
# | ||
# brickstrap - create a foreign architecture rootfs using kernel namespaces, | ||
# multistrap, and qemu usermode emulation and create a disk image | ||
# using libguestfs | ||
# | ||
# Copyright (C) 2016 Johan Ouwerkerk <[email protected]> | ||
# | ||
|
||
# | ||
# Note: this file is not meant to be executed, source it as a library of | ||
# functions instead. Variables used by the functions (other than stack) are | ||
# namespaced using the 'BR_' or 'BRP_' prefixes. Function names are namespaced | ||
# similarly, using the 'br_' and 'brp_' prefixes. See docs/namespacing.md for | ||
# more information on namespacing in brickstrap. | ||
# | ||
|
||
# | ||
# Default image type 'driver'. | ||
# This function creates a 'boot+root' MBR type image (type extension: img). | ||
# $1: path to the image file to generate. | ||
# | ||
function brp_image_default_driver() | ||
{ | ||
debug "IMAGE_FILE_SIZE: ${IMAGE_FILE_SIZE}" | ||
[ $# -eq 1 -a -n "$1" ] && guestfish -N \ | ||
"$1"=bootroot:vfat:ext4:${IMAGE_FILE_SIZE}:48M:mbr \ | ||
part-set-mbr-id /dev/sda 1 0x0b : \ | ||
set-label /dev/sda2 EV3_FILESYS : \ | ||
mount /dev/sda2 / : \ | ||
tar-in "$(br_tarball_path)" / : \ | ||
mkdir-p /media/mmc_p1 : \ | ||
mount /dev/sda1 /media/mmc_p1 : \ | ||
glob mv /boot/flash/* /media/mmc_p1/ : \ | ||
|
||
# Hack to set the volume label on the vfat partition since guestfish does | ||
# not know how to do that. Must be null padded to exactly 11 bytes. | ||
echo -e -n "EV3_BOOT\0\0\0" | \ | ||
dd of="$1" bs=1 seek=32811 count=11 conv=notrunc >/dev/null 2>&1 | ||
} | ||
|
||
# | ||
# Displays an error message if a image type driver failed to create an image | ||
# file successfully. Returns the given status code. | ||
# $1: the status code returned by the driver function | ||
# $2: the name of the partitioning/imaging scheme implemented in the disk image | ||
# $3: the file path of the disk image | ||
# | ||
function brp_image_err_msg() | ||
{ | ||
error "Unable to create image: '$3'. | ||
Driver ($(brp_get_image_type_driver "$2")) returned status code: $1" | ||
return $1 | ||
} | ||
|
||
# | ||
# Create a disk image file (type). This function looks up the | ||
# relevant driver and invokes it with the given path. Before creating the image | ||
# this function checks it does not already exist or, if it does, that it may be | ||
# overwritten. | ||
# $1: the name of the partitioning/imaging scheme implemented in the disk image | ||
# | ||
function brp_create_image_type() | ||
{ | ||
if [ $# -eq 0 -o -z "$1" ]; then | ||
error "Image type required!" | ||
return 1 | ||
elif [ -z "$(brp_get_image_type_driver "$1")" ]; then | ||
error "Unable to create image: '$2'. | ||
No driver for image type: '$1'" | ||
return 1 | ||
fi | ||
BRP_CUR_IMG=$(br_image_path "$1" "$(brp_get_image_type_extension $1)") | ||
debug "IMAGE: $BRP_CUR_IMG" | ||
if [ -z "$BR_FORCE" -a -f "$BRP_CUR_IMG" ]; then | ||
error "$BRP_CUR_IMG already exists. Use -f option to overwrite." | ||
return 1 | ||
else | ||
eval "$(brp_get_image_type_driver "$1")" "$BRP_CUR_IMG" || \ | ||
brp_image_err_msg "$?" "$1" "$BRP_CUR_IMG" | ||
fi | ||
} | ||
|
||
# | ||
# Look up the driver function for a given image type name. | ||
# $1: the name of the partitioning/imaging scheme implemented in the disk image | ||
# | ||
function brp_get_image_type_driver() | ||
{ | ||
[ $# -eq 1 -a -n "$1" ] && case "$1" in | ||
default) | ||
echo -n brp_image_default_driver | ||
*) | ||
eval echo -n "\$BRP_IMG_DRV_REGISTRY_$1" | ||
esac | ||
} | ||
|
||
# | ||
# Look up the file type extension for a given image type name. | ||
# $1: the name of the partitioning/imaging scheme implemented in the disk image | ||
# | ||
function brp_get_image_type_extension() | ||
{ | ||
[ $# -eq 1 -a -n "$1" ] && case "$1" in | ||
default) | ||
echo -n img | ||
*) | ||
eval echo -n "\$BRP_IMG_EXT_REGISTRY_$1" | ||
esac | ||
} | ||
|
||
# | ||
# Register a custom driver with brickstrap for a custom image type. | ||
# $1: the name of the partitioning/imaging scheme implemented in the disk image | ||
# $2: name of the function which will create the image (type), taking the image | ||
# file path as argument. | ||
# $3: the image file type extension, without leading dot. | ||
# | ||
function br_register_image_type() | ||
{ | ||
[ $# -eq 3 -a -n "$1" -a -n "$2" -a -n "$3" ] && \ | ||
if [ -z "$(brp_get_image_type_driver "$1")" ]; then | ||
eval "BRP_IMG_DRV_REGISTRY_$1=$2" | ||
eval "BRP_IMG_EXT_REGISTRY_$1=$3" | ||
else | ||
fail "Rejected duplicate driver '$2' for image type: '$1' | ||
Previous setting was: $(brp_get_image_type_driver "$1")" | ||
fi | ||
} | ||
|
||
# | ||
# Creates disk image files for a given list of image types to create. | ||
# $1: list of image types for which an image file should be created | ||
# $2: list of previously created images (if applicable). This is used to avoid | ||
# attempting to generate the same image twice. | ||
# | ||
function brp_create_image_types() | ||
{ | ||
BRP_IMAGE_TYPE_RETURN_CODE=0 | ||
[ $# -eq 1 -a -n "$1" ] && for BRP_CUR_IMG in "$1"; do | ||
eval BRP_CUR_IMG="$BRP_CUR_IMG" | ||
echo "$2" | fgrep -q "'$BRP_CUR_IMG'" || \ | ||
brp_create_image_type "$BRP_CUR_IMG" || \ | ||
BRP_IMAGE_TYPE_RETURN_CODE=$? | ||
done && return $BRP_IMAGE_TYPE_RETURN_CODE | ||
} | ||
|
||
# | ||
# Creates disk images for the configured image types. | ||
# | ||
function brp_create_images() | ||
{ | ||
info "Creating image files..." | ||
debug "TARBALL: $(br_tarball_path)" | ||
[ ! -f "$(br_tarball_path)" ] && fail "Could not find $(br_tarball_path)" | ||
|
||
# source custom-image driver scripts | ||
if br_list_paths custom-image -r >/dev/null; then | ||
br_for_each_path "$(br_list_paths custom-image -r)" brp_run_hook_impl \ | ||
'loading' | ||
fi | ||
|
||
mkdir -p "$(br_image_dir)" | ||
|
||
BRP_IMAGE_RETURN_CODE=0 | ||
if [ -n "$BR_IMAGE_TYPES" ]; then | ||
brp_create_image_types "$BR_IMAGE_TYPES" || BRP_IMAGE_RETURN_CODE=$? | ||
fi | ||
if [ -n "$IMAGE_TYPES" ]; then | ||
brp_create_image_types "$IMAGE_TYPES" "$BR_IMAGE_TYPES" || \ | ||
BRP_IMAGE_RETURN_CODE=$? | ||
fi | ||
if [ -z "$BR_IMAGE_TYPES" -a -z "$IMAGE_TYPES" ]; then | ||
brp_create_image_type img || BRP_IMAGE_RETURN_CODE=$? | ||
fi | ||
|
||
return $BRP_IMAGE_RETURN_CODE | ||
} |