Commit cf35a6b2 authored by Martin Peres's avatar Martin Peres
Browse files

initscript/cache: allow specifying the block device or partition to use

parent 1904d000
......@@ -50,6 +50,7 @@ Here is a list of options:
* b2c.cache_device: Use a cache to store the image layers, thus saving on network usage:
* none (default): Do not use any cache, guaranteeing a fresh environment every time
* auto: Re-use a previously-setup drive, or pick a suitable one, and partition it! (**WILL ERASE YOUR DATA**)
* /dev/XXX: A path to a block device, or a partition. If the path is to a block device and it does not have a partition labeled `B2C_CACHE`, it will recreate the partition table and format the partition. If the path is to a partition, it will use it directly if it is labeled `B2C_CACHE`, otherwise it will reformat it. (**WILL ERASE YOUR DATA**)
## How to?
......
......@@ -6,6 +6,7 @@ MODULES_PATH=/usr_mods
CONTAINER_MOUNTPOINT=/container
CONTAINER_ROOTFS="$CONTAINER_MOUNTPOINT/rootfs"
CONTAINER_CACHE="$CONTAINER_MOUNTPOINT/cache"
CACHE_PARTITION_LABEL="B2C_CACHE"
function log {
{ set +x; } 2>/dev/null
......@@ -82,7 +83,7 @@ function connect {
}
function find_container_partition {
dev_name=`blkid | grep 'LABEL="B2C_CACHE"' | head -n 1 | cut -d ':' -f 1`
dev_name=`blkid | grep "LABEL=\"$CACHE_PARTITION_LABEL\"" | head -n 1 | cut -d ':' -f 1`
if [ -n "$dev_name" ]; then
echo $dev_name
return 0
......@@ -97,15 +98,15 @@ function format_disk {
parted --script $1 mkpart primary ext4 2048s 100%
container_partition=`lsblk -no PATH $1 | tail -n -1`
mkfs.ext4 -F -L B2C_CACHE "$container_partition"
mkfs.ext4 -F -L "$CACHE_PARTITION_LABEL" "$container_partition"
return 0
return $?
fi
return 1
}
function find_or_create_container_partition {
function find_or_create_cache_partition {
# See if we have an existing block device that would work
container_partition=`find_container_partition` && return 0
......@@ -113,10 +114,58 @@ function find_or_create_container_partition {
sr_disks_majors=`grep ' sr' /proc/devices | sed "s/^[ \t]*//" | cut -d ' ' -f 1 | tr '\n' ',' | sed 's/,$//'`
disk=`lsblk -ndlfp -e "$sr_disks_majors" | head -n 1`
log "No existing cache partition found on this machine, create one from the disk $disk"
# Find a disk, partition it, then format it as ext4
format_disk $disk && return 0
format_disk $disk || return 1
return 1
return 0
}
function try_to_use_cache_device {
# Check if the parameter is a path to a file
if [ -f "$ARG_CACHE_DEVICE" ]; then
log "The caching parameter '$ARG_CACHE_DEVICE' is neither 'none', 'auto', or a path to a block device. Defaulting to 'none'"
return 0
fi
# $ARG_CACHE_DEVICE has to be a path to a drive
# NOTE: Pay attention to the space after $ARG_CACHE_DEVICE, as it
# makes sure that we don't accidentally match /dev/sda1 when asking
# for /dev/sda.
blk_dev=`lsblk -rpno PATH,TYPE,LABEL | grep "$ARG_CACHE_DEVICE "`
if [ -z "$blk_dev" ]; then
log "Error: The device '$ARG_CACHE_DEVICE' is neither a block device, nor a partition. Defaulting to no caching."
return 1
fi
path=$(echo "$blk_dev" | cut -d ' ' -f 1)
type=$(echo "$blk_dev" | cut -d ' ' -f 2)
label=$(echo "$blk_dev" | cut -d ' ' -f 3)
case $type in
part)
container_partition="$path"
if [ -z "$label" ]; then
log "Formating the partition $container_partition"
mkfs.ext4 -F -L "$CACHE_PARTITION_LABEL" "$container_partition"
return $?
fi
;;
disk)
# Look for the first partition from the drive $1, that has the right cache
container_partition=`lsblk -no PATH,LABEL $path | grep "$CACHE_PARTITION_LABEL" | cut -d ' ' -f 1 | head -n 1`
if [ -n "$container_partition" ]; then
return 0
else
log "No existing cache partition on the drive $path, recreate the partition table and format a partition"
format_disk $path
return $?
fi
;;
esac
return 0
}
function mount_cache_partition {
......@@ -130,26 +179,20 @@ function mount_cache_partition {
return 0
;;
auto)
find_or_create_container_partition || return 1
find_or_create_cache_partition || return 0
;;
*)
log "Invalid caching option '$ARG_CACHE_DEVICE'. Defaulting to 'none'"
return 0
try_to_use_cache_device "$ARG_CACHE_DEVICE" || return 0
;;
esac
log "Selected the partition $container_partition as a cache"
if mount "$container_partition" "$CONTAINER_MOUNTPOINT"; then
status_str="DONE"
status=0
else
status_str="FAILED"
status=1
fi
log "Mounting the partition $container_partition to $CONTAINER_MOUNTPOINT: $status_str"
status="DONE"
mount "$container_partition" "$CONTAINER_MOUNTPOINT" || status="FAILED"
log "Mounting the partition $container_partition to $CONTAINER_MOUNTPOINT: $status"
return $status
return 0
}
function setup_container_runtime {
......@@ -202,4 +245,5 @@ setup_container_runtime
start_container $ARG_CONTAINER
# Emergency shell
log "Done executing the container, dropping to an emergency shell"
exec /bin/busybox sh
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment