Subject: pkg/27191: better support for unprivileged bootstrapping of pkgsrc
To: None <gnats-bugs@gnats.NetBSD.org>
From: None <apb@cequrux.com>
List: pkgsrc-bugs
Date: 10/08/2004 10:56:21
>Number:         27191
>Category:       pkg
>Synopsis:       better support for unprivileged bootstrapping of pkgsrc
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    pkg-manager
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Fri Oct 08 08:58:00 UTC 2004
>Closed-Date:
>Last-Modified:
>Originator:     Alan Barrett
>Release:        NetBSD 2.99.9
>Organization:
	Not much
>Environment:
Architecture: i386
Machine: i386
>Description:
The appended patch makes it easier to bootstrap pkgsrc
as an unprivileged user.  The new "--unprivileged" option
to the bootstrap script changes several defaults,
and causes several additional variables to be added
to the generated mk.conf.example file.

I have tested this on NetBSD and Linux, using commands like

    ./bootstrap --unprivileged

and

    b=$HOME/pkg
    ./bootstrap --ignore-user-check --prefix=$b --pkgdbdir=$b/pkgdb

>How-To-Repeat:
>Fix:

Index: pkgsrc/bootstrap/README
--- bootstrap/README	23 Jun 2004 19:06:40 -0000	1.4
+++ bootstrap/README	8 Oct 2004 08:50:25 -0000
@@ -1,17 +1,29 @@
 $NetBSD: README,v 1.4 2004/06/23 19:06:40 wiz Exp $
 
 To try to get pkgsrc working on your system, please try the following
-as root:
+command.  If possible, you should do this as "root" (or your operating
+system's superuser account).  There is limited support for bootstrapping
+and using pkgsrc as an unprivileged user (with everything being
+installed inside your home directory), but some features will not work.
 
 # ./bootstrap [ --prefix=${PREFIX} ] [ --pkgdbdir=${PKG_DBDIR} ] \
 	[ --sysconfdir=${PKG_SYSCONFBASE} ] [ --workdir=working directory] \
-	[ --ignore-case-check ] [ --ignore-user-check ] [ --help ]
+	[ --ignore-case-check ] [ --ignore-user-check ] [ --unprivileged ] \
+	[ --help ]
 
 The defaults for the arguments are as follows:
 
 	--prefix	/usr/pkg
 	--pkgdbdir	/var/db/pkg
-	--sysconfdir	/usr/pkg/etc
+	--sysconfdir	${PREFIX}/etc (/usr/pkg/etc)
+	--workdir	work
+
+If the --unprivileged option is specified, the defaults are changed
+as follows:
+
+	--prefix	${HOME}/pkg
+	--pkgdbdir	${PREFIX}/pkgdb (${HOME}/pkg/pkgdb)
+	--sysconfdir	${PREFIX}/etc (${HOME}/pkg/etc)
 	--workdir	work
 
 It is perfectly acceptable to place ${PKG_DBDIR} under ${PREFIX}.
@@ -19,8 +31,7 @@
 The working directory will be created if it doesn't exist and has to be
 writable by the user executing ./bootstrap.
 
-Make sure that you have a working C compiler and make(1) binary in
-your path.
+Make sure that you have a working C compiler in your path.
 
 See http://www.netbsd.org/Documentation/software/packages.html for
 more information about bootstrapping and using pkgsrc.
Index: pkgsrc/bootstrap/bootstrap
--- bootstrap/bootstrap	27 Aug 2004 06:48:58 -0000	1.20
+++ bootstrap/bootstrap	8 Oct 2004 08:50:25 -0000
@@ -37,13 +37,14 @@
 
 BOOTSTRAP_VERSION=20040821
 
-# set defaults for system locations
-prefix=/usr/pkg
-pkgdbdir=/var/db/pkg
-sysconfdir=${prefix}/etc
+# set defaults for system locations.  These will be processed by "eval".
+default_prefix=/usr/pkg
+default_pkgdbdir=/var/db/pkg
+default_sysconfdir='${prefix}/etc'
 
 ignorecasecheck=no
 ignoreusercheck=no
+unprivileged=no
 
 # where the building takes place
 wrkdir=work # default: relative to pkgsrc/bootstrap
@@ -57,6 +58,7 @@
     [ --sysconfdir=<sysconfdir> ]
     [ --ignore-case-check ]
     [ --ignore-user-check ]
+    [ --unprivileged ]
     [ --help ]'
 
 # this replicates some of the logic in bsd.prefs.mk. until
@@ -180,7 +182,8 @@
 }
 
 build_start=`date`
-echo_msg "bootstrap command: $0 $@"
+bootstrap_command="$0 $*"
+echo_msg "bootstrap command: $bootstrap_command"
 echo_msg "bootstrap started: $build_start"
 
 if [ "x${PRESERVE_PATH}" != "xyes" ]; then
@@ -365,12 +368,15 @@
 while [ $# -gt 0 ]; do
 	case $1 in
 	--workdir=*)	wrkdir=`echo $1 | $sedprog -e 's|--workdir=||'` ;;
-	--prefix=*)	prefix=`echo $1 | $sedprog -e 's|--prefix=||'`
-			sysconfdir=${prefix}/etc ;;
+	--prefix=*)	prefix=`echo $1 | $sedprog -e 's|--prefix=||'` ;;
 	--pkgdbdir=*)	pkgdbdir=`echo $1 | $sedprog -e 's|--pkgdbdir=||'` ;;
 	--sysconfdir=*)	sysconfdir=`echo $1 | $sedprog -e 's|--sysconfdir=||'`;;
 	--ignore-case-check) ignorecasecheck=yes ;;
 	--ignore-user-check) ignoreusercheck=yes ;;
+	--unprivileged)	unprivileged=yes
+			ignoreusercheck=yes
+			default_prefix='${HOME}/pkg'
+			default_pkgdbdir='${prefix}/pkgdb' ;;
 	--help)		echo "$usage"; exit ;;
 	-h)		echo "$usage"; exit ;;
 	--*)		echo "$usage"; exit 1 ;;
@@ -378,6 +384,10 @@
 	shift
 done
 
+if [ "${prefix}" = "" ];	then eval prefix=${default_prefix} ; fi
+if [ "${sysconfdir}" = "" ];	then eval sysconfdir=${default_sysconfdir} ; fi
+if [ "${pkgdbdir}" = "" ];	then eval pkgdbdir=${default_pkgdbdir} ; fi
+
 if [ ! -d ${wrkdir} ]; then
 	if mkdir ${wrkdir}; then
 		:
@@ -393,6 +403,9 @@
 	exit 1
 fi
 echo "Working directory is: ${wrkdir}"
+echo "prefix is:	${prefix}"
+echo "pkgdbdir is:	${pkgdbdir}"
+echo "sysconfdir is:	${sysconfdir}"
 
 if [ ! -x ${wrkdir}/install-sh ]; then
 	run_cmd "$sedprog -e 's|@DEFAULT_INSTALL_MODE@|'${default_install_mode-0755}'|' files/install-sh.in > $wrkdir/install-sh"
@@ -405,7 +418,7 @@
 	group=$root_group
 else
 	if [ $ignoreusercheck = "no" ]; then
-		die "You must be root to install bootstrap-pkgsrc."
+		die "You must be root (or specify --ignore-user-check) to bootstrap pkgsrc."
 	fi
 
 	user=`$whoamiprog`
@@ -457,11 +470,22 @@
 MKCONF_EXAMPLE=${wrkdir}/mk.conf.example
 export MKCONF_EXAMPLE
 echo_msg "Creating mk.conf.example in ${wrkdir}"
-echo "# Example ${sysconfdir}/mk.conf file produced by bootstrap-pkgsrc" > ${MKCONF_EXAMPLE}
-echo "# `date`" >> ${MKCONF_EXAMPLE}
-echo "" >> ${MKCONF_EXAMPLE}
-echo ".ifdef BSD_PKG_MK	# begin pkgsrc settings" >> ${MKCONF_EXAMPLE}
-echo "" >> ${MKCONF_EXAMPLE}
+cat <<_EOF_ >${MKCONF_EXAMPLE}
+# Example ${sysconfdir}/mk.conf file
+# for use with ${prefix}/bin/bmake.
+#
+# Produced by ${bootstrapdir}/${0##*/}
+# at ${build_start}, with the following options:
+#     ${bootstrap_command}
+
+.ifdef BSD_PKG_MK	# begin pkgsrc settings
+
+# You might need the following environment variables:
+# PATH=$prefix/bin:$prefix/sbin:\${PATH}; export PATH
+# MANPATH=$prefix/man:\${MANPATH}; export MANPATH
+# PKG_DBDIR=$pkgdbdir; export PKG_DBDIR
+
+_EOF_
 
 # IRIX64 needs to be set to IRIX, for example
 if [ "$set_opsys" = "yes" ]; then
@@ -623,23 +647,55 @@
 # opsys specific fiddling
 opsys_finish
 
+# store settings for unprivileged user in example mk.conf
+if [ $unprivileged = "yes" ]; then
+    echo "" >> ${MKCONF_EXAMPLE}
+    cat >> ${MKCONF_EXAMPLE} <<_EOF_
+# Allow builds by unprivileged user $user / group $group.
+SU_CMD= \${SH} -c
+CHOWN= :
+CHGRP= :
+NO_MTREE= yes
+ROOT_USER= $user
+ROOT_GROUP= $group
+BINOWN= $user
+BINGRP= $group
+GAMEOWN= $user
+GAMEGRP= $group
+SHAREOWN= $user
+SHAREGRP= $group
+MANOWN= $user
+MANGRP= $group
+_EOF_
+fi
+
 echo "" >> ${MKCONF_EXAMPLE}
 echo ".endif			# end pkgsrc settings" >> ${MKCONF_EXAMPLE}
 
-echo ""
-echo "Please remember to add $prefix/bin to your PATH environment variable"
-echo "and $prefix/man to your MANPATH environment variable, if necessary."
-echo ""
-echo "An example mk.conf file \"${MKCONF_EXAMPLE}\" with the settings you"
-echo "provided to \"bootstrap\" has been created for you."
-echo "Please copy ${MKCONF_EXAMPLE} to ${sysconfdir}/mk.conf."
-echo ""
-echo "You can find extensive documentation of the NetBSD Packages Collection"
-echo "in $pkgsrcdir/Packages.txt and packages(7)."
-echo ""
-echo "Hopefully everything is now complete."
-echo "Thank you"
+echo_msg "pkgsrc bootstrap is now complete"
+cat <<_EOF_
+Please note the following:
+
+* You can find extensive documentation of the NetBSD Packages Collection
+  in $pkgsrcdir/Packages.txt and packages(7).
+
+* Wherever the documentation refers to "make", you must use
+  "${prefix}/bin/bmake".
+
+* An example mk.conf file has been created with
+  the settings you provided to "bootstrap".
+  Please copy "${MKCONF_EXAMPLE}"
+  to "${sysconfdir}/mk.conf",
+  and edit it if necessary.
+
+* You might need to adjust some environment variables, as described in
+  comments in the example mk.conf file.
+
+Hopefully everything is now complete.
+Thank you
+_EOF_
 
+echo_msg "bootstrap command: $bootstrap_command"
 echo_msg "bootstrap started: $build_start"
 echo_msg "bootstrap ended:   `date`"
 
>Release-Note:
>Audit-Trail:
>Unformatted: