openpkg/etc.wrapbin.sh

Mon, 28 Jan 2013 17:37:18 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Mon, 28 Jan 2013 17:37:18 +0100
changeset 758
a2c6460cfb16
parent 445
43a74e63d4a3
permissions
-rw-r--r--

Correct socket error reporting improvement with IPv6 portable code,
after helpful recommendation by Saúl Ibarra Corretgé on OSips devlist.

michael@428 1 #!/bin/sh
michael@428 2 #![OpenPKG]
michael@428 3 ##
michael@428 4 ## OpenPKG Binary Bootstrap Package (self-extracting shell script)
michael@428 5 ## Copyright (c) 2000-2012 OpenPKG GmbH <http://openpkg.com/>
michael@428 6 ##
michael@428 7 ## This software is property of the OpenPKG GmbH, DE MUC HRB 160208.
michael@428 8 ## All rights reserved. Licenses which grant limited permission to use,
michael@428 9 ## copy, modify and distribute this software are available from the
michael@428 10 ## OpenPKG GmbH.
michael@428 11 ##
michael@428 12 ## THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
michael@428 13 ## WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
michael@428 14 ## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
michael@428 15 ## IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
michael@428 16 ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@428 17 ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@428 18 ## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
michael@428 19 ## USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
michael@428 20 ## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
michael@428 21 ## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
michael@428 22 ## OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
michael@428 23 ## SUCH DAMAGE.
michael@428 24 ##
michael@428 25
michael@428 26 # configuration
michael@428 27 l_me="$0"
michael@428 28 o_help=no
michael@428 29 o_version=no
michael@428 30 o_tar=no
michael@428 31 l_prefix='@l_prefix@'
michael@428 32 l_musr='@MUSR@'
michael@428 33 l_mgrp='@MGRP@'
michael@428 34 l_platform="@l_platform@"
michael@428 35 l_release="@l_release@"
michael@428 36 l_version="@l_version@"
michael@428 37 l_unprivileged="@l_unprivileged@"
michael@428 38
michael@428 39 # establish standard environment
michael@428 40 PATH="$PATH:/bin:/sbin:/usr/bin:/usr/sbin"
michael@428 41 LC_CTYPE=C
michael@428 42 export LC_CTYPE
michael@428 43 umask 022
michael@428 44
michael@428 45 # parse command line options
michael@428 46 for opt
michael@428 47 do
michael@428 48 case $opt in
michael@428 49 -*=*) arg=`echo "$opt" | sed 's/^[-_a-zA-Z0-9]*=//'` ;;
michael@428 50 *) arg='' ;;
michael@428 51 esac
michael@428 52 case $opt in
michael@428 53 -h | --help ) o_help=yes ;;
michael@428 54 -v | --version ) o_version=yes ;;
michael@428 55 -t | --tar ) o_tar=yes ;;
michael@428 56 --prefix=* ) l_prefix=$arg ;;
michael@428 57 * ) o_help=yes ;;
michael@428 58 esac
michael@428 59 done
michael@428 60 if [ ".$o_version" = .no -a ".$l_prefix" = . ]; then
michael@428 61 o_help=yes
michael@428 62 fi
michael@428 63 if [ ".$o_help" = .yes ]; then
michael@428 64 echo "Usage: sh $l_me" 2>&1
michael@428 65 echo " [--prefix=<prefix>] [-t|--tar]" 2>&1
michael@428 66 echo " [-h|--help] [-v|--version]" 2>&1
michael@428 67 exit 1
michael@428 68 fi
michael@428 69
michael@428 70 # make sure all essential installation tools are available
michael@428 71 for tool in sed mkdir dd tar chown chgrp; do
michael@428 72 found=no
michael@428 73 case $tool in
michael@428 74 /* )
michael@428 75 if [ -f $tool ]; then
michael@428 76 found=yes
michael@428 77 fi
michael@428 78 ;;
michael@428 79 * )
michael@428 80 for p in `IFS=:; echo $PATH`; do
michael@428 81 if [ -f "$p/$tool" ]; then
michael@428 82 found=yes
michael@428 83 break
michael@428 84 fi
michael@428 85 done
michael@428 86 ;;
michael@428 87 esac
michael@428 88 if [ ".$found" = .no ]; then
michael@428 89 echo "$l_me:ERROR: unable to find installation tool \"$tool\"" 1>&2
michael@428 90 exit 1
michael@428 91 fi
michael@428 92 done
michael@428 93
michael@428 94 # optionally extract the embedded tarball only
michael@428 95 if [ ".$o_tar" = .yes ]; then
michael@428 96 tmpdir="${TMPDIR-/tmp}/openpkg.$$"
michael@428 97 ( umask 077 && mkdir $tmpdir) || exit 1
michael@450 98 dd if=$l_me bs=8192 skip=10 2>/dev/null |\
michael@428 99 ( cd $tmpdir || exit 1
michael@428 100 tar xf - 2>/dev/null || exit 1
michael@428 101 ./openpkg.bzip2 -d -c openpkg.tar.bz2
michael@428 102 ) || exit 1
michael@428 103 rm -rf $tmpdir
michael@428 104 exit 0
michael@428 105 fi
michael@428 106
michael@428 107 # display version and copyright header
michael@428 108 echo "OpenPKG ${l_release} Binary Bootstrap Package, version ${l_version}"
michael@428 109 echo "Built for prefix ${l_prefix} on target platform ${l_platform}"
michael@428 110 if [ ".$o_version" = .yes ]; then
michael@428 111 exit 0
michael@428 112 fi
michael@428 113
michael@428 114 # determine current username
michael@428 115 cusr=`(id -un) 2>/dev/null ||\
michael@428 116 (id | sed -e 's;^[^(]*(\([^)]*\)).*;\1;') 2>/dev/null ||\
michael@428 117 (whoami) 2>/dev/null ||\
michael@428 118 (who am i | cut "-d " -f1) 2>/dev/null ||\
michael@428 119 echo ${LOGNAME-"NN"}`
michael@428 120
michael@428 121 # running the embedded %pre script for hooking into the system environment
michael@428 122 echo "++ hooking OpenPKG instance into system environment"
michael@428 123 prefix="$l_prefix"
michael@428 124 susr='@SUSR@'; sgrp='@SGRP@'
michael@428 125 musr='@MUSR@'; mgrp='@MGRP@'
michael@428 126 rusr='@RUSR@'; rgrp='@RGRP@'
michael@428 127 nusr='@NUSR@'; ngrp='@NGRP@'
michael@428 128 suid='@SUID@'; sgid='@SGID@'
michael@428 129 muid='@MUID@'; mgid='@MGID@'
michael@428 130 ruid='@RUID@'; rgid='@RGID@'
michael@428 131 nuid='@NUID@'; ngid='@NGID@'
michael@428 132 unprivileged="$l_unprivileged"
michael@428 133 set -- 1 # emulate RPM's $1 when executing scripts
michael@428 134 # ---- BEGIN EMBEDDED %pre SCRIPT ----
michael@428 135 @PRE@
michael@428 136 # ---- END EMBEDDED %pre SCRIPT ----
michael@428 137
michael@428 138 # make sure prefix/root directory exists
michael@428 139 # and has correct permissions and owner/group
michael@428 140 if [ ! -d $l_prefix ]; then
michael@428 141 # create prefix/root directory from scratch
michael@428 142 echo "++ creating OpenPKG instance root directory \"$l_prefix\""
michael@428 143 d=''
michael@428 144 for c in `IFS=/; echo $l_prefix`; do
michael@428 145 d="$d/$c"
michael@428 146 if [ ! -d $d ]; then
michael@428 147 mkdir $d || exit 1
michael@428 148 chmod 755 $d || exit 1
michael@428 149 if [ ".$cusr" = .root ]; then
michael@428 150 chown $musr $d >/dev/null 2>&1 || true
michael@428 151 chgrp $mgrp $d >/dev/null 2>&1 || true
michael@428 152 fi
michael@428 153 fi
michael@428 154 done
michael@428 155 else
michael@428 156 # adjust already existing prefix/root directory
michael@428 157 echo "++ fixating OpenPKG instance root directory \"$l_prefix\""
michael@428 158 ( cd $l_prefix || exit 1
michael@428 159 chmod 755 . || exit 1
michael@428 160 if [ ".$cusr" = .root ]; then
michael@428 161 chown $musr . >/dev/null 2>&1 || true
michael@428 162 chgrp $mgrp . >/dev/null 2>&1 || true
michael@428 163 fi
michael@428 164 ) || exit 1
michael@428 165 fi
michael@428 166
michael@428 167 # extract and install binary distribution files
michael@428 168 echo "++ extracting OpenPKG binary distribution"
michael@450 169 dd if=$l_me bs=8192 skip=10 2>/dev/null |\
michael@428 170 (cd $l_prefix; tar xf - 2>/dev/null)
michael@428 171 echo "++ installing OpenPKG binary distribution"
michael@428 172 ( cd $l_prefix || exit 1
michael@428 173 ./openpkg.bzip2 -d -c openpkg.tar.bz2 | ./openpkg.tar xf - 2>/dev/null
michael@428 174 rm -f openpkg.tar openpkg.bzip2 openpkg.tar.bz2 >/dev/null 2>&1 || true
michael@428 175 ) || exit 1
michael@428 176
michael@428 177 # immediately activate BOOT license to let the
michael@428 178 # following early RPM usage already work correctly
michael@428 179 echo "BOOT" >$l_prefix/etc/openpkg/license
michael@428 180
michael@428 181 # fixate installation files
michael@445 182 # (ATTENTION: order of chgrp/chown and chmod is important because of "setuid" bits)
michael@428 183 echo "++ fixating OpenPKG instance filesystem hierarchy"
michael@428 184 ( echo 'fixate () {'
michael@428 185 echo ' chgrp "$3" "$4"'
michael@428 186 echo ' chown "$2" "$4"'
michael@428 187 echo ' chmod "$1" "$4"'
michael@428 188 echo '}'
michael@428 189 cd / && $l_prefix/bin/openpkg --keep-privileges rpm -q openpkg \
michael@428 190 --qf '[fixate %7.7{FILEMODES:octal} %{FILEUSERNAME:shescape} %{FILEGROUPNAME:shescape} ::%{FILENAMES:shescape}\n]' |\
michael@428 191 grep -v '(none)' | sed 's/^fixate .../fixate /' | sed -e "s; ::\\(.\\)@l_prefix@; \\1$l_prefix;"
michael@428 192 ) | sh 2>/dev/null || true
michael@428 193
michael@428 194 # running the embedded %post script
michael@445 195 echo "++ postprocessing OpenPKG bootstrap installation"
michael@428 196 prefix="$l_prefix"
michael@428 197 susr='@SUSR@'; sgrp='@SGRP@'
michael@428 198 musr='@MUSR@'; mgrp='@MGRP@'
michael@428 199 rusr='@RUSR@'; rgrp='@RGRP@'
michael@428 200 nusr='@NUSR@'; ngrp='@NGRP@'
michael@428 201 suid='@SUID@'; sgid='@SGID@'
michael@428 202 muid='@MUID@'; mgid='@MGID@'
michael@428 203 ruid='@RUID@'; rgid='@RGID@'
michael@428 204 nuid='@NUID@'; ngid='@NGID@'
michael@428 205 unprivileged="$l_unprivileged"
michael@428 206 set -- 1 # emulate RPM's $1 when executing scripts
michael@428 207 # ---- BEGIN EMBEDDED %post SCRIPT ----
michael@428 208 @POST@
michael@428 209 # ---- END EMBEDDED %post SCRIPT ----
michael@428 210
michael@428 211 # display final information
michael@428 212 ( echo "Congratulations!"
michael@428 213 echo ""
michael@428 214 echo "You have successfully installed an OpenPKG ${l_release} instance"
michael@428 215 echo "under prefix ${l_prefix} on target platform ${l_platform}."
michael@428 216 echo ""
michael@428 217 echo "For details about this OpenPKG instance, run any of the"
michael@428 218 echo "following typical OpenPKG RPM query commands:"
michael@428 219 echo ""
michael@428 220 echo " \$ ${l_prefix}/bin/openpkg rpm -qa"
michael@428 221 echo " \$ ${l_prefix}/bin/openpkg rpm -qi openpkg"
michael@428 222 echo " \$ ${l_prefix}/bin/openpkg rpm -qlv openpkg"
michael@428 223 echo ""
michael@428 224 echo "To check the integrity of the entire OpenPKG instance,"
michael@428 225 echo "run the following OpenPKG RPM verify command:"
michael@428 226 echo ""
michael@428 227 echo " \$ ${l_prefix}/bin/openpkg rpm -Va"
michael@428 228 echo ""
michael@428 229 echo "To install software packages into this OpenPKG instance, run"
michael@428 230 echo "the following two OpenPKG RPM build commands for each package:"
michael@428 231 echo ""
michael@428 232 echo " \$ ${l_prefix}/bin/openpkg rpm --rebuild /path/to/foo-*.src.rpm"
michael@428 233 echo " \$ ${l_prefix}/bin/openpkg rpm -Uvh ${l_prefix}/RPM/PKG/foo-*.rpm"
michael@428 234 echo ""
michael@428 235 echo "To leverage from remote indexes on download.openpkg.org and"
michael@445 236 echo "conveniently generate a shell script for all in one downloading"
michael@428 237 echo "(openpkg curl), building (openpkg rpm --rebuild) and installing"
michael@428 238 echo "(openpkg rpm -Uvh) one or more OpenPKG RPM packages, including all"
michael@428 239 echo "their transitive dependencies, in topologically correct order:"
michael@428 240 echo ""
michael@428 241 echo " \$ ${l_prefix}/bin/openpkg build foo bar quux | sh"
michael@428 242 echo ""
michael@428 243 echo "To remove a software package later, just run:"
michael@428 244 echo ""
michael@428 245 echo " \$ ${l_prefix}/bin/openpkg rpm -e foo"
michael@428 246 echo ""
michael@428 247 echo "To remove the whole OpenPKG instance under prefix ${l_prefix},"
michael@428 248 echo "just remove every package as shown above. As you finally"
michael@428 249 echo "remove the package \"openpkg\", the OpenPKG instance itself"
michael@428 250 echo "will be unlinked from the system and removed as well."
michael@428 251 echo ""
michael@428 252 echo "Thank you for flying OpenPKG..."
michael@428 253 echo " Ralf S. Engelschall"
michael@428 254 echo " OpenPKG GmbH"
michael@428 255 echo " openpkg.com"
michael@428 256 ) | $l_prefix/lib/openpkg/rpmtool msg -b -t info
michael@428 257
michael@428 258 # die explicitly just before the shell would discover
michael@445 259 # that we carry megabytes of data with us... ;-)
michael@428 260 exit 0
michael@428 261
michael@428 262 # the distribution tarball is appended in raw format directly to the
michael@428 263 # end of this script, just leaded by padding whitespaces which make
michael@450 264 # sure that the tarball data starts at the predefined offset of 80KB.
michael@428 265 # This allows us to unpack the tarball by just skipping the leading
michael@450 266 # 80KB (= 8192*10, see above).
michael@428 267

mercurial