From: Jeremy Kerr Date: Fri, 21 Aug 2015 05:12:19 +0000 (+0800) Subject: pb-plugin: Keep chroot persistent, and create wrapper scripts X-Git-Tag: v1.0.0~49^2~7 X-Git-Url: http://git.ozlabs.org/?p=petitboot;a=commitdiff_plain;h=cb4a99c73726f316180f1591deb2f23900226920 pb-plugin: Keep chroot persistent, and create wrapper scripts Rather than having to 'pb-plugin run' multiple times, we want users to be able to execute multiple plugin binaries with more accessible command-line statements. This change reverts to the previous 'install' (rather than 'run') behaviour, and creates wrapper scripts to call into the chroot. Signed-off-by: Jeremy Kerr --- diff --git a/utils/pb-plugin b/utils/pb-plugin index 4e41652..598530a 100755 --- a/utils/pb-plugin +++ b/utils/pb-plugin @@ -7,6 +7,7 @@ plugin_file=pb-plugin.cpio.gz plugin_meta=pb-plugin.conf plugin_meta_dir=etc/preboot-plugins/ plugin_meta_path=$plugin_meta_dir$plugin_meta +plugin_wrapper_dir=/usr/bin usage() { @@ -14,9 +15,9 @@ usage() Usage: $0 Where is one of: - run - run plugin from FILE/URL - scan - look for available plugins on attached devices - create - create a new plugin archive from DIR + install - install plugin from FILE/URL + scan - look for available plugins on attached devices + create - create a new plugin archive from DIR EOF } @@ -62,41 +63,55 @@ plugin_info() echo " (version $PLUGIN_VERSION)" } -__run_init() +do_wrap() { - local base dir + local base binary dir base=$1 + binary=$2 + shift 2 - for dir in etc dev sys proc + for dir in etc dev sys proc var do - mkdir -p $base/$dir + [ -e "$base/$dir" ] || mkdir -p "$base/$dir" done cp /etc/resolv.conf $base/etc mount -o bind /dev $base/dev mount -o bind /sys $base/sys mount -o bind /proc $base/proc + mount -o bind /var $base/var + + chroot "$base" "$binary" "$@" + + umount $base/dev + umount $base/sys + umount $base/proc + umount $base/var } -__run_cleanup() +__create_wrapper() { - local base + local base binary wrapper base=$1 + binary=$2 + wrapper=$plugin_wrapper_dir/$(basename $binary) - [ -e $base/dev/null ] && umount $base/dev - [ -e $base/sys/kernel ] && umount $base/sys - [ -e $base/proc/stat ] && umount $base/proc - rm -rf $base + cat < $wrapper +#!/bin/sh + +exec $(realpath $0) __wrap '$base' '$binary' "\$@" +EOF + + chmod a+x $wrapper } -do_run() +do_install() { - local url executable + local url name file __dest url=$1 - executable=$2 if [ -z "$url" ] then @@ -129,7 +144,7 @@ do_run() echo sha256sum "$file" | cut -f1 -d' ' echo - echo "Do you want to run this plugin? (y/N)" + echo "Do you want to install this plugin? (y/N)" read resp case $resp in @@ -153,20 +168,13 @@ do_run() . $__dest/$plugin_meta_path - ( - executable=${PLUGIN_EXECUTABLE:-$executable} - - __run_init $__dest - - printf "Entering plugin\n" - plugin_info - - chroot $__dest $executable - - printf "\nExiting plugin & cleaning up\n" - __run_cleanup $__dest - ) + for binary in ${PLUGIN_EXECUTABLES} + do + __create_wrapper "$__dest" "$binary" + done + echo "Plugin installed" + plugin_info } do_scan() @@ -259,10 +267,13 @@ EOF cat <2 exit 1 fi - if [ ! -n "$PLUGIN_EXECUTABLE" ] + if [ ! -n "$PLUGIN_EXECUTABLES" ] then - echo "error: no PLUGIN_EXECUTABLE defined in metadata" \ - &>2 + echo "error: no PLUGIN_EXECUTABLES defined in metadata"\ &>2 exit 1 fi @@ -419,7 +429,7 @@ test_scan() ( echo "PLUGIN_NAME=test" echo "PLUGIN_VERSION=1" - echo "PLUGIN_EXECUTABLE=/bin/sh" + echo "PLUGIN_EXECUTABLES=/bin/sh" ) > $mnt_dir/$plugin_meta_path ( cd $mnt_dir; @@ -508,9 +518,9 @@ do_tests() } case "$1" in -run) +install) shift - do_run $@ + do_install $@ ;; scan) shift @@ -520,6 +530,10 @@ create) shift do_create $@ ;; +__wrap) + shift + do_wrap $@ + ;; __test) shift do_tests $@