X-Git-Url: https://git.ozlabs.org/?p=petitboot;a=blobdiff_plain;f=utils%2Fpb-plugin;h=598530a66e7a7f5581649a2f76088c94e13abbad;hp=e71e981a249cc1a74574600d5743be102fc1f595;hb=cb4a99c73726f316180f1591deb2f23900226920;hpb=a240e296ac2ecf8934f71cb23946fc77101cdfd6 diff --git a/utils/pb-plugin b/utils/pb-plugin index e71e981..598530a 100755 --- a/utils/pb-plugin +++ b/utils/pb-plugin @@ -2,8 +2,12 @@ __dest=/ __pb_mount_dir=/var/petitboot/mnt/dev/ -plugin_dev_meta=pb-plugin.conf -plugin_installed_meta_dir=/etc/preboot-plugins/ +__plugin_basedir=/tmp/ +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() { @@ -11,10 +15,9 @@ usage() Usage: $0 Where is one of: - install - install plugin from FILE/URL - scan - look for available plugins on attached devices - list - list currently-installed plugins - 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 } @@ -60,9 +63,53 @@ plugin_info() echo " (version $PLUGIN_VERSION)" } +do_wrap() +{ + local base binary dir + + base=$1 + binary=$2 + shift 2 + + for dir in etc dev sys proc var + do + [ -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 +} + +__create_wrapper() +{ + local base binary wrapper + + base=$1 + binary=$2 + wrapper=$plugin_wrapper_dir/$(basename $binary) + + cat < $wrapper +#!/bin/sh + +exec $(realpath $0) __wrap '$base' '$binary' "\$@" +EOF + + chmod a+x $wrapper +} + do_install() { - local url + local url name file __dest url=$1 @@ -72,18 +119,6 @@ do_install() exit 1 fi - if [ ! -d "$__dest" ] - then - echo "error: destination directory '$__dest' doesn't exist" >&2 - exit 1 - fi - - if [ ! -w "$__dest" ] - then - echo "error: destination directory isn't writeable" >&2 - exit 1 - fi - name=${url##*/} if is_url "$url" @@ -109,7 +144,7 @@ do_install() echo sha256sum "$file" | cut -f1 -d' ' echo - echo "Do you want to install into the pre-boot environment? (y/N)" + echo "Do you want to install this plugin? (y/N)" read resp case $resp in @@ -121,33 +156,61 @@ do_install() ;; esac + __dest=$(mktemp -d) gunzip -c "$file" | ( cd $__dest && cpio -i -d) if [ $? -ne 0 ] then echo "error: Failed to extract archive $url, exiting" + rm -rf $__dest exit 1 fi + + . $__dest/$plugin_meta_path + + for binary in ${PLUGIN_EXECUTABLES} + do + __create_wrapper "$__dest" "$binary" + done + + echo "Plugin installed" + plugin_info } do_scan() { - local found + local found dev plugin_path __meta_tmp found=0 for mnt in $__pb_mount_dir/* do dev=$(basename $mnt) - metafile="$mnt/$plugin_dev_meta" - [ -e "$metafile" ] || continue + plugin_path="$mnt/$plugin_file" + + [ -e "$plugin_path" ] || continue + + # extract plugin metadata to a temporary directory + __meta_tmp=$(mktemp -d) + [ -d $__meta_tmp ] || continue + gunzip -c "$plugin_path" | + (cd $__meta_tmp && + cpio -i -d $plugin_meta_path 2>/dev/null) + if ! [ $? = 0 -a -e "$plugin_path" ] + then + rm -rf $__meta_tmp + continue + fi + ( - . $metafile + . $__meta_tmp/$plugin_meta_path + printf "Plugin found on %s:\n" $dev plugin_info printf "\n" - printf "To install this plugin, run:\n" - printf " $0 install $mnt/$PLUGIN_FILE\n" + printf "To run this plugin:\n" + printf " $0 run $plugin_path\n" printf "\n" ) + rm -rf $__meta_tmp found=1 done @@ -157,36 +220,14 @@ do_scan() fi } -do_list() -{ - local found - found=0 - for meta in $plugin_installed_meta_dir/* - do - [ -e "$meta" ] || continue - [ $found = 0 ] && printf "Installed plugins:\n" - found=1 - ( - . $meta - plugin_info - echo - ) - done - - if [ "$found" = 0 ] - then - echo "No plugins installed" - fi -} - guided_meta() { local vendorname vendorshortname local pluginname pluginnhortname - local version date - local dir + local version date executable + local file - dir=$1 + file=$1 cat < $dir/$vendorshortname-$pluginshortname + cat < $file PLUGIN_VENDOR='$vendorname' PLUGIN_NAME='$pluginname' PLUGIN_VERSION='$version' PLUGIN_DATE='$date' +PLUGIN_EXECUTABLES='$executables' EOF } do_create() { - local src found meta_dir_abs meta_file + local src meta_dir_abs meta_file src=$1 if [ -z "$src" ] @@ -255,16 +307,9 @@ do_create() exit 1 fi - meta_dir_abs="$src/$plugin_installed_meta_dir" - found=0 - for meta in $meta_dir_abs/* - do - [ -e "$meta" ] || continue - found=$(($found+1)) - meta_file=$meta - done + meta_file=$src/$plugin_meta_path - if [ $found = 0 ] + if [ ! -e $meta_file ] then echo "No plugin metadata file found. " \ "Would you like to create one? (Y/n)" @@ -275,14 +320,7 @@ do_create() exit 1 ;; esac - guided_meta $meta_dir_abs || exit - meta_file=$meta_dir_abs/* - fi - - if [ $found -gt 1 ] - then - echo "error: Multiple metadata files found in $meta_dir_abs" >&2 - exit 1 + guided_meta $meta_file || exit fi # Sanity check metadata file @@ -308,18 +346,20 @@ do_create() echo "error: no PLUGIN_DATE defined in metadata" &>2 exit 1 fi + if [ ! -n "$PLUGIN_EXECUTABLES" ] + then + echo "error: no PLUGIN_EXECUTABLES defined in metadata"\ &>2 + exit 1 + fi ) || exit 1 - outfile=pb-plugin.cpio.gz + outfile=$plugin_file ( cd $src find -mindepth 1 | cpio -o -Hnewc -v - ) | gzip -c > pb-plugin.cpio.gz - - cp $meta_file $plugin_dev_meta - echo "PLUGIN_FILE='$outfile'" >> $plugin_dev_meta + ) | gzip -c > $outfile echo echo "Plugin metadata:" @@ -339,12 +379,9 @@ do_create() cat < $mnt_dir/$plugin_dev_meta + echo "PLUGIN_EXECUTABLES=/bin/sh" + ) > $mnt_dir/$plugin_meta_path + ( + cd $mnt_dir; + find -mindepth 1 | cpio -o -Hnewc 2>/dev/null + ) | gzip -c > $mnt_dir/$plugin_file do_scan | grep -q 'test 1' rc=$? @@ -401,6 +442,8 @@ test_scan() test_empty_scan() { + __pb_mount_dir="$test_tmpdir/mnt" + mkdir -p $__pb_mount_dir do_scan | grep -q "No plugins" } @@ -410,10 +453,6 @@ test_setup() test_tmpdir="$tests_tmpdir/$n" mkdir "$test_tmpdir" - __test_dest="$test_tmpdir/base" - mkdir "$__test_dest" - [ -d "$__test_dest" ] || exit 1 - __dest=$__test_dest } test_teardown() @@ -487,14 +526,14 @@ scan) shift do_scan $@ ;; -list) - shift - do_list $@ - ;; create) shift do_create $@ ;; +__wrap) + shift + do_wrap $@ + ;; __test) shift do_tests $@