etch

#!/bin/sh
#$Id$
#This file is part of EdgeBSD etch
#Copyright (c) 2014-2021 Pierre Pronchery <khorben@edgebsd.org>
#
#Redistribution and use in source and binary forms, with or without
#modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
#DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
#FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
#DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
#SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
#OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#variables
DEVNULL="/dev/null"
NETBSDGZ="netbsd.gz"
PREFIX="/usr/pkg"
ROOT="wd0a"
SHELL="/usr/pkg/bin/bash"
VND="vnd0"
#executables
CHGRP="chgrp"
CHMOD="chmod"
CHOWN="chown"
CP="cp -f"
DIRNAME="dirname"
DISKLABEL="disklabel"
FDISK="fdisk"
[ $VERBOSE -eq 2 ] && FDISK="$FDISK -v"
[ $VERBOSE -ge 3 ] && FDISK="$FDISK -vv"
FIND="find"
FTP="ftp"
GPG="gpg"
GREP="grep"
INSTALL="install"
INSTALLBOOT="installboot"
[ $VERBOSE -ge 2 ] && INSTALLBOOT="$INSTALLBOOT -v"
LN="ln -f"
MKTEMP="mktemp /tmp/etch.sh-XXXXXX"
MOUNT="mount"
NEWFS="newfs -V 0"
[ $VERBOSE -ge 2 ] && NEWFS="newfs -V 1"
[ $VERBOSE -ge 3 ] && NEWFS="newfs -V 2"
PKG_ADD="pkg_add -I"
[ $VERBOSE -ge 3 ] && PKG_ADD="$PKG_ADD -v"
PKG_INFO="pkg_info"
PROGRESS="_progress"
PWD_MKDB="pwd_mkdb -p"
SED="sed"
[ -z "$SUDO" ] && SUDO=
SYSCTL="sysctl"
TAR="tar"
UMOUNT="umount"
UPDATEMIMEDATABASE="$PREFIX/bin/update-mime-database"
VNCONFIG="vnconfig"
#platform-specific
[ -z "$EDGEBSD_ARCH" ] && EDGEBSD_ARCH=$(uname -m)
[ -z "$EDGEBSD_BOOT_CLEAR" ] && EDGEBSD_BOOT_CLEAR=1
[ -z "$EDGEBSD_BOOT_DEFAULT" ] && EDGEBSD_BOOT_DEFAULT=1
EDGEBSD_BOOT_SPLASH=
[ -z "$EDGEBSD_BOOT_TIMEOUT" ] && EDGEBSD_BOOT_TIMEOUT=5
[ -z "$EDGEBSD_KERNEL" ] && EDGEBSD_KERNEL="netbsd-GENERIC.gz"
[ -z "$EDGEBSD_MACHINE" ] && EDGEBSD_MACHINE=$(uname -p)
[ -z "$EDGEBSD_MIRROR" ] && EDGEBSD_MIRROR="http://ftp.edgebsd.org"
[ -z "$EDGEBSD_PACKAGES" ] && EDGEBSD_PACKAGES="bash pkgin vim"
[ -z "$EDGEBSD_SERVICES" ] && EDGEBSD_SERVICES="devpubd dhcpcd estd postfix powerd sshd"
[ -z "$EDGEBSD_SETS" ] && EDGEBSD_SETS="base etc modules"
[ -z "$EDGEBSD_VERSION" ] && EDGEBSD_VERSION="9"
[ -z "$EDGEBSD_RELEASEDIR" ] && EDGEBSD_RELEASEDIR="pub/EdgeBSD/EdgeBSD-$EDGEBSD_VERSION"
#functions
#download
_download_ftp()
{
#XXX very much duplicates _download_http() from etch.sh
url="$1"
[ -f "$url" ] && return 0
dirname=$($DIRNAME "$url")
$DEBUG $MKDIR -- "$dirname" || return 2
_info "Downloading ${url}..."
$DEBUG $FTP -o "${url}.part" -- "$url"
if [ $? -ne 0 ]; then
$DEBUG $RM -- "${url}.part"
return 2
fi
$DEBUG $MV -- "${url}.part" "$url" || return 2
}
_download_http()
{
url="$1"
#re-use the FTP code
_download_ftp "$url"
}
#import_gpg_key
_import_gpg_key()
{
_import_gpg_key_echo | $DEBUG $GPG --import || return 2
}
#import_gpg_key_echo
_import_gpg_key_echo()
{
echo "-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.4.15 (NetBSD)
mQINBFIfeJIBEADJGl9yol0AiwMxnG8Y0CFV6qNuH+Jnl5bJdCXOuvCWGu5h3M4h
VA2/x1M7XB2ezj6NdZCL4IKWJlntCaN/uCCRiJghaUl0FPNEZUlmFwBjmrXAocK5
TgKOlC2hRvkNM1FPNlP7WhNt22r1+6Cj0SOxwK3Vf42bN0atGfO+G7gmVOIvhtcP
BdsxiZEiZYW+KYyS6De/3aoFUCpJWU4S84mwZ231dJHO5bZbR7dpFsaefil5ewnN
y1uNdSumoAQLSoArux+caJgp4IGLWPZuWn6d3dKJUlIr7O9gAo8bZ/sLuihlz3vN
Ie877ts2WIEzpexRBC1m86/nYNGF3YL6Ghrsh/V02DjwLZFTXh+ht/m20JtwY3g6
ee/2sma6nklkivOrDVZij8OSdjcvS7gn8vl4FwGlEBBKdwXC5yGKkCKOYmRtvNjz
HvZYTqP8/GaEs4x2RsqgJ1iTSVPAXKpW+ulEubtmQYoE/FlKuVQcURHY6hFjfBap
tN2CDHBfa6pyvqLktTLlwO8ydzBsXDfjg2E28nIhICrxqz5KaXhz7epyFWGuCXJs
ynqecQZBK/rETyVYEeyXzx4RoqqSO9xwqGIRIr6oyvyqIcJJcSBWCSbMp9Ce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ugur66Qw
l1016UcoQx/9UnBOKqnrJ424wMdvQummVd30OqvBMAIh2z2IVK21ahuc/w19uYvC
9XPSOHIcWKiC9qmhAsZN6MZaFRI892QFp+WukFeSzZqRyuuZa83WI4Tv9jfhlm70
30ltLmhTuSqwr5fI8zAiMvWaPCeUx6K3eVgOSJZAhCp0r1XopknyutuhABEBAAGJ
Ah8EGAECAAkFAlIfeJICGwwACgkQOnuabW869eJEfQ/8CJIeynbYjdH37qjCAk32
V+dcMjp1bBq/8P27JFSpXP7uIt2IHnufmFXHHNB7C9AqKTIPcxiTMRj0bkV39UtS
WZIF/09eW4q8ZeTZAxsiJZvuJ7whslJYhN3pUFczYrO56kCzkKEyM0YdEXmfocaR
sj6oQOfxO3L6UIatqLcjscIgtC8YU0RFvP2ar/wXJYVKQ8OamYY3vHRc320OqDGd
z2lcZqW9cpF/y9djwGt5EPrQHqiaESyGWMG3sPU0carUrk81GtTazqtW4xD42QEV
siX12OKUQqHowNCgodn+28yjJaZT/RmtcBp7zTDlZ+nPAK1DK9JuRCCv7UPiH+TV
vY+oYP4OGBi1lwG1kovXv3aqMFyP5pzzRGpcgPfQUBHuTnIt1YIEoX0ChB1HXb6b
bEqx01UHR10N1woxMNI8mvQMwX3ienwXN2Mbl6kVIXvgBE8yMht7Yu5BdL+knwSV
NKfGBRl64knM9Y4HyvRnVrS8iMvCKlirYEv7QjLFqrmTNA9EbvhkLa0zMs1611V4
VXx/JOIWR/lsr3QcGSOo8PPA8qk24Q/1C1B3GdSbHGV/Z2dnIo/0oBndfyyJPl68
GiUJNDk0kLK5XnXz8Yz3WreOBH+VR4OfUiKt33DqVsLIlsCapvORr8eMk1zw77Uq
Ynpi+eaA/ElAFui4NqcncB4=
=CXTn
-----END PGP PUBLIC KEY BLOCK-----"
}
#install_base
_install_base()
{
#sets
sets="$EDGEBSD_MIRROR/$EDGEBSD_RELEASEDIR/$EDGEBSD_ARCH/binary/sets"
for set in $EDGEBSD_SETS; do
url="$sets/${set}.tgz"
filename=$(_download "$url") || return 2
[ -f "$DESTDIR/etc/mtree/set.$set" ] && continue
_info "Extracting set ${set}..."
$DEBUG $MKDIR -m 0755 -- "$DESTDIR" || return 2
(cd "$DESTDIR" && $DEBUG $PROGRESS "$filename" $SUDO $TAR -xzpf -)
if [ $? -ne 0 ]; then
$DEBUG $RM -- "$DESTDIR/etc/mtree/set.$set"
return 2
fi
done
}
#install_bootloader
_install_bootloader()
{
splash=
_info "Installing the second-stage bootloader..."
$DEBUG $CP -- "$DESTDIR/usr/mdec/boot" "$DESTDIR/boot" || return 2
_info "Configuring the bootloader..."
[ -n "$EDGEBSD_BOOT_SPLASH" ] && splash=";splash $EDGEBSD_BOOT_SPLASH"
$DEBUG $CAT > "$DESTDIR/boot.cfg" << EOF
menu=Boot normally:rndseed /var/db/entropy-file;vesa 800x600x16$splash;boot $NETBSDGZ
menu=Boot single user:rndseed /var/db/entropy-file;load /cgdroot.kmod;boot $NETBSDGZ -s
menu=Disable ACPI:rndseed /var/db/entropy-file;load /cgdroot.kmod;boot $NETBSDGZ -2
menu=Disable ACPI and SMP:rndseed /var/db/entropy-file;load /cgdroot.kmod;boot $NETBSDGZ -12
menu=Drop to boot prompt:prompt
default=$EDGEBSD_BOOT_DEFAULT
timeout=$EDGEBSD_BOOT_TIMEOUT
clear=$EDGEBSD_BOOT_CLEAR
EOF
[ $? -eq 0 ] || return 2
}
#install_configure
_install_configure()
{
_install_configure_fstab || return 2
_install_configure_hostname || return 2
_install_configure_mime || return 2
_install_configure_motd || return 2
_install_configure_services || return 2
_install_configure_timezone || return 2
_install_configure_user || return 2
_install_configure_pkgin || return 2
}
_install_configure_fstab()
{
#/etc/fstab
_info "Configuring the system partitions..."
$DEBUG $MKDIR -m 0755 -- "$DESTDIR/mnt/cdrom" \
"$DESTDIR/mnt/usb" || return 2
$DEBUG $MKDIR -m 0755 "$DESTDIR/etc" || return 2
$DEBUG $CAT > "$DESTDIR/etc/fstab" << EOF
/dev/$ROOT / ffs rw,log 1 1
tmpfs /tmp tmpfs rw,-m=1777,-s=64m,noexec
/dev/cd0a /mnt/cdrom cd9660 ro,noauto,noexec
/dev/sd0e /mnt/usb msdos rw,-u=1000,-g=1000,-M=0700,-m=0600,noauto,noexec
EOF
[ $? -eq 0 ] || return 2
}
_install_configure_hostname()
{
#/etc/hosts
_info "Configuring the hostname..."
$DEBUG $CAT > "$DESTDIR/etc/hosts" << EOF
# \$NetBSD: hosts,v 1.8 2009/07/03 22:32:55 hubertf Exp \$
#
# Host Database
# This file should contain the addresses and aliases
# for local hosts that share this file.
# It is used only for "ifconfig" and other operations
# before the nameserver is started.
#
#
::1 localhost localhost.
127.0.0.1 localhost localhost.
#
# RFC 1918 specifies that these networks are "internal":
# 10.0.0.0 - 10.255.255.255 (10/8 prefix)
# 172.16.0.0 - 172.31.255.255 (172.16/12 prefix)
# 192.168.0.0 - 192.168.255.255 (192.168/16 prefix)
127.0.0.1 $HOSTNAME ${HOSTNAME%%.*}
EOF
[ $? -eq 0 ] || return 2
}
_install_configure_mime()
{
[ -d "$DESTDIR$PREFIX/share/mime" ] || return 0
[ -x "$DESTDIR$PREFIX/bin/update-mime-database" ] || return 0
[ -x "$UPDATEMIMEDATABASE" ] || return 2
#$PREFIX/share/mime/globs
[ -f "$DESTDIR$PREFIX/share/mime/globs" ] && return 0
_info "Configuring MIME types..."
$UPDATEMIMEDATABASE "$DESTDIR$PREFIX/share/mime" || return 2
}
_install_configure_motd()
{
#/etc/motd
_info "Creating the MOTD..."
$DEBUG $CAT > "$DESTDIR/etc/motd" << EOF
$PLATFORM $EDGEBSD_VERSION ($EDGEBSD_KERNEL)
Welcome to $PLATFORM!
EOF
[ $? -eq 0 ] || return 2
}
_install_configure_pkgin()
{
#configure pkgin only if relevant
[ -x "$DESTDIR$PREFIX/bin/pkgin" ] || return 0
#$PREFIX/etc/pkgin/repositories.conf
$DEBUG $MKDIR -m 0755 -- "$DESTDIR$PREFIX/etc/pkgin" || return 2
$DEBUG $CAT > "$DESTDIR$PREFIX/etc/pkgin/repositories.conf" << EOF
$EDGEBSD_MIRROR/$EDGEBSD_RELEASEDIR/$EDGEBSD_ARCH/binary/packages/All
EOF
[ $? -eq 0 ] || return 2
}
_install_configure_services()
{
#/etc/rc.conf
_info "Configuring the services..."
$DEBUG $CAT > "$DESTDIR/etc/rc.conf" << EOF
# \$NetBSD\$
#
# see rc.conf(5) for more information.
#
# Use program=YES to enable program, NO to disable it. program_flags are
# passed to the program on the command line.
#
# Load the defaults in from /etc/defaults/rc.conf (if it's readable).
# These can be overridden below.
#
if [ -r /etc/defaults/rc.conf ]; then
. /etc/defaults/rc.conf
fi
# If this is not set to YES, the system will drop into single-user mode.
#
rc_configured=YES
# Add local overrides below
#
accounting=NO
clear_tmp=NO
fsck_flags="-pP"
hostname=$HOSTNAME
newsyslog=YES
no_swap=YES
quota=NO
savecore=NO
virecover=NO
wscons=YES
#services
EOF
[ $? -eq 0 ] || return 2
#services enabled
for service in $EDGEBSD_SERVICES; do
echo "$service=YES" >> "$DESTDIR/etc/rc.conf" || return 2
done
#services disabled
for i in "$DESTDIR/usr/pkg/share/examples/rc.d/"*; do
[ -f "$i" ] || continue
service="${i##*/}"
[ -f "$DESTDIR/etc/rc.d/$service" ] || continue
for i in $EDGEBSD_SERVICES; do
[ "$i" = "$service" ] && service=
done
if [ -n "$service" ]; then
echo "$service=NO" >> "$DESTDIR/etc/rc.conf"
[ $? -eq 0 ] || return 2
fi
done
}
_install_configure_timezone()
{
#/etc/localtime
_info "Setting the timezone to ${TIMEZONE}..."
$DEBUG $LN -s "/usr/share/zoneinfo/$TIMEZONE" \
"$DESTDIR/etc/localtime" || return 2
}
_install_configure_user()
{
password="*"
id=1000
#default user
$DEBUG $GREP "^$USERNAME:" "$DESTDIR/etc/group" > "$DEVNULL"
ret=$?
if [ $ret -eq 1 ]; then
_info "Adding group ${USERNAME}..."
_file_append "/etc/group" << EOF
$USERNAME:*:$id:
EOF
elif [ $ret -ne 0 ]; then
_error "An error occured while creating the group"
fi
$DEBUG $SUDO $GREP -- "^$USERNAME:" "$DESTDIR/etc/master.passwd" \
> "$DEVNULL"
ret=$?
if [ $ret -eq 1 ]; then
_info "Adding user ${USERNAME}..."
[ -n "$PASSWORD" ] && password="$PASSWORD"
_file_append "/etc/master.passwd" << EOF
$USERNAME:$password:$id:$id::0:0::/home/$USERNAME:$SHELL
EOF
$DEBUG $SUDO $PWD_MKDB -d "$DESTDIR" -u "$USERNAME" \
"$DESTDIR/etc/master.passwd" || return 2
elif [ $ret -ne 0 ]; then
_error "An error occured while creating the user"
return 2
fi
$DEBUG $SUDO $MKDIR -m 0700 -- "$DESTDIR/home/$USERNAME" \
|| return 2
$DEBUG $SUDO $CHOWN -- "$id:$id" "$DESTDIR/home/$USERNAME" \
|| return 2
}
#install_kernel
_install_kernel()
{
kernel="$EDGEBSD_MIRROR/$EDGEBSD_RELEASEDIR/$EDGEBSD_ARCH/binary/kernel/$EDGEBSD_KERNEL"
filename=$(_download "$kernel") || return 2
[ -f "$DESTDIR/$NETBSDGZ" ] && return 0
_info "Installing the kernel..."
$DEBUG $SUDO $MKDIR -m 0755 "$DESTDIR" || return 2
$DEBUG $SUDO $INSTALL -m 0755 "$filename" "$DESTDIR/$NETBSDGZ" \
|| return 2
}
#install_package
_install_package()
{
if [ $# -ne 1 ]; then
_usage "_package package"
return $?
fi
package="$1"
pkg_path="$EDGEBSD_MIRROR/$EDGEBSD_RELEASEDIR/$EDGEBSD_ARCH/binary/packages/All"
#check if the package is already installed
$PKG_INFO -K "$DESTDIR/var/db/pkg" -q -E "$package" && return 0
_info "Installing package ${package}..."
$DEBUG $SUDO $MKDIR -m 0755 -- "$DESTDIR/var/db/pkg" || return 2
#XXX $DEBUG does not work here (environment)
PKG_PATH="$pkg_path" $SUDO $PKG_ADD \
-C "$DESTDIR/etc/pkg_install.conf" \
-P "$DESTDIR" -m "$EDGEBSD_MACHINE" "$package" || return 2
_install_package_configure "$package" || return 2
}
#install_package_configure
_install_package_configure()
{
if [ $# -ne 1 ]; then
_usage "_package_configure package"
return $?
fi
package="$1"
return 0
}
#install_packages
_install_packages()
{
#make sure gnupg is installed first
for package in $EDGEBSD_PACKAGES; do
if [ "$package" = "gnupg" ]; then
_install_package "gnupg" || return 2
_install_packages_pkg_install || return 2
break
fi
done
#import the GPG key
_import_gpg_key || return 2
#install every package
for package in $EDGEBSD_PACKAGES; do
_install_package "${package}" || return 2
done
_install_packages_configure "$package" || return 2
}
_install_packages_pkg_install()
{
#configure pkg_install only if relevant
[ -x "$DESTDIR$PREFIX/bin/gpg" ] || return 0
#/etc/pkg_install.conf
$DEBUG $SUDO $MKDIR -m 0755 -- "$DESTDIR/etc" || return 2
_file_create "/etc/pkg_install.conf" << EOF
GPG=$PREFIX/bin/gpg
VERIFIED_INSTALLATION=always
EOF
[ $? -eq 0 ] || return 2
#/root/.gnupg
$DEBUG $SUDO $MKDIR "$DESTDIR/root/.gnupg" || return 2
$DEBUG $SUDO $CHMOD -- 0700 "$DESTDIR/root/.gnupg" || return 2
_import_gpg_key_echo | $DEBUG $GPG --homedir "$DESTDIR/root/.gnupg" \
--import || return 2
$DEBUG $SUDO $CHOWN -R -- 0:0 "$DESTDIR/root/.gnupg" || return 2
#$PREFIX/etc/pkg_install.conf
$DEBUG $SUDO $MKDIR -m 0755 -- "$DESTDIR$PREFIX/etc" || return 2
$DEBUG $SUDO $LN -s "/etc/pkg_install.conf" \
"$DESTDIR$PREFIX/etc/pkg_install.conf" || return 2
}
#install_packages_configure
_install_packages_configure()
{
_info "Configuring the packages..."
$DEBUG $FIND "$DESTDIR/var/db/pkg" -type d | while read folder; do
[ "$folder" = "$DESTDIR/var/db/pkg" ] && continue
[ -f "$folder/+INSTALL" ] || continue
while read comment type line; do
[ "$comment" = "#" ] || continue
#FIXME break on errors
case "$type" in
"DIR:")
_install_packages_configure_dir $line
;;
"FILE:")
_install_packages_configure_file $line
;;
"FONTS:")
#FIXME implement
;;
"GROUP:")
_install_packages_configure_group \
"$line"
;;
"INFO:")
#FIXME implement
;;
"PERMS:")
_install_packages_configure_perms $line
;;
"SHELL:")
_install_packages_configure_shell \
"$line"
;;
"USER:")
_install_packages_configure_user "$line"
;;
[A-Z][A-Z]*:)
_warning "$type: Unknown type"
;;
esac
done < "$folder/+INSTALL"
done
return 0
}
_install_packages_configure_dir()
{
if [ $# -lt 2 -o $# -gt 5 ]; then
_usage "_install_packages_configure_dir directory flag [mode [owner [group]]]"
return $?
fi
dir="$DESTDIR$1"
[ "${1#/}" = "$1" ] && dir="$DESTDIR$PREFIX/$1"
mode="0755"
$DEBUG $MKDIR -m "$mode" -- "$dir"
[ "$2" = "m" -a $# -ge 3 -a -n "$3" ] && mode="$3"
$DEBUG $CHMOD -- "$mode" "$dir"
if [ $# -ge 4 -a -n "$4" ]; then
uid="$(_user_get_id "$4")"
[ $? -eq 0 ] || return 2
$DEBUG $CHOWN -- "$uid" "$dir" || return 2
fi
if [ $# -ge 5 -a -n "$5" ]; then
gid="$(_group_get_id "$5")"
[ $? -eq 0 ] || return 2
$DEBUG $CHGRP -- "$gid" "$dir" || return 2
fi
}
_install_packages_configure_file()
{
if [ $# -lt 3 -o $# -gt 6 ]; then
_usage "_install_packages_configure_file file flag source [mode [owner [group]]]"
return $?
fi
mode="0644"
[ $# -ge 4 -a -n "$4" ] && mode="$4"
#XXX report errors
file="$DESTDIR$1"
[ "${1#/}" = "$1" ] && file="$DESTDIR$PREFIX/$1"
if [ -n "$2" -a -n "$3" ]; then
dirname=$($DIRNAME "$file")
$DEBUG $MKDIR -m 0755 -- "$dirname" &&
$DEBUG $INSTALL -m "$mode" \
"$DESTDIR$PREFIX/$3" "$file"
[ $# -ge 4 -a -n "$4" ] && $DEBUG $CHMOD -- "$mode" "$file"
if [ $# -ge 5 -a -n "$5" ]; then
uid="$(_user_get_id "$5")"
[ $? -eq 0 ] || return 2
$DEBUG $CHOWN -- "$uid" "$file" || return 2
fi
if [ $# -ge 6 -a -n "$6" ]; then
gid="$(_group_get_id "$6")"
[ $? -eq 0 ] || return 2
$DEBUG $CHGRP -- "$gid" "$file" || return 2
fi
fi
}
_install_packages_configure_group()
{
if [ $# -ne 1 ]; then
_usage "_install_packages_configure_group group"
return $?
fi
group="$1"
gid="${group#*:}"
group="${group%:*}"
if [ "$gid" = "$group" ]; then
#calculate the gid to use
gid=0
while read line; do
g="${line#*:*:}"
g="${g%:*}"
[ $g -lt 1000 -a $g -ge $gid ] && gid=$((g + 1))
done < "$DESTDIR/etc/group"
fi
$DEBUG $GREP -- "^$group:" "$DESTDIR/etc/group" > "$DEVNULL"
ret=$?
if [ $ret -eq 1 ]; then
_info "Adding group ${group}..."
_file_append "/etc/group" << EOF
$group:*:$gid:
EOF
fi
}
_install_packages_configure_perms()
{
if [ $# -lt 2 -o $# -gt 4 ]; then
_usage "_install_packages_configure_perms file mode [owner [group]]"
return $?
fi
file="$DESTDIR$1"
[ "${1#/}" = "$1" ] && file="$DESTDIR$PREFIX/$1"
[ -n "$2" ] && $DEBUG $CHMOD -- "$2" "$file"
if [ $# -ge 3 -a -n "$3" ]; then
uid="$(_user_get_id "$3")"
[ $? -eq 0 ] || return 2
$DEBUG $CHOWN -- "$uid" "$file" || return 2
fi
if [ $# -ge 4 -a -n "$4" ]; then
gid="$(_group_get_id "$4")"
[ $? -eq 0 ] || return 2
$DEBUG $CHGRP -- "$gid" "$file" || return 2
fi
}
_install_packages_configure_shell()
{
if [ $# -ne 1 ]; then
_usage "_install_packages_configure_shell shell"
return $?
fi
shell="$PREFIX/$1"
$DEBUG $GREP "^$shell\$" "$DESTDIR/etc/shells" > "$DEVNULL"
ret=$?
if [ $ret -eq 1 ]; then
_info "Adding $shell as a valid shell..."
echo "$shell" >> "$DESTDIR/etc/shells"
fi
}
_install_packages_configure_user()
{
if [ $# -ne 1 ]; then
_usage "_install_packages_configure_user user"
return $?
fi
user="$1"
password="*"
group="${user#*:}"
[ "$group" = "$user" ] && group=
uid="${group#*:}"
[ "$uid" = "$group" ] && uid=
gecos="${uid#*:}"
[ "$gecos" = "$uid" ] && gecos=
home="${gecos#*:}"
[ "$home" = "$gecos" ] && home=
shell="${home#*:}"
[ "$shell" = "$home" ] && shell=
#cleanup
user="${user%%:*}"
group="${group%%:*}"
uid="${uid%%:*}"
gecos="${gecos%%:*}"
home="${home%%:*}"
shell="${shell%%:*}"
[ -z "$uid" ] && uid=100
[ -z "$home" ] && home="/nonexistent"
[ -z "$shell" ] && shell="/sbin/nologin"
while read line; do
#calculate the uid to use
usr="${line%%:*}"
u="${line#*:*:}"
u="${u%%:*}"
if [ "$usr" = "$user" ]; then
uid="$u"
break
fi
[ $u -lt 1000 -a $u -ge $uid ] && uid=$((u + 1))
done < "$DESTDIR/etc/master.passwd"
gid=100
while read line; do
#identify the gid to use
grp="${line%%:*}"
g="${line#*:*:}"
g="${g%:*}"
if [ "$grp" = "$group" ]; then
gid="$g"
break
fi
done < "$DESTDIR/etc/group"
$DEBUG $SUDO $GREP "^$user:" "$DESTDIR/etc/master.passwd" > "$DEVNULL"
ret=$?
if [ $ret -eq 1 ]; then
_info "Adding user ${user}..."
_file_append "/etc/master.passwd" << EOF
$user:$password:$uid:$gid::0:0:$gecos:$home:$shell
EOF
$DEBUG $SUDO $PWD_MKDB -d "$DESTDIR" -u "$user" \
"$DESTDIR/etc/master.passwd"
fi
}
#progress
_progress()
{
if [ $# -lt 2 ]; then
_usage "_progress filename command [arguments...]"
return $?
fi
filename="$1"
shift
#only use progress if in interactive and verbose mode
if [ -n "$PS1" -a $VERBOSE -ge 2 ]; then
$DEBUG /usr/bin/progress -f "$filename" -- "$@"
else
$DEBUG $CAT -- "$filename" | "$@"
fi
}
#volume_format
_volume_format()
{
disknames=$($SYSCTL "hw.disknames")
i=0
for diskname in $disknames; do
[ "$diskname" = "vnd$i" ] && i=$((i + 1))
done
VND="vnd$i"
$DEBUG $SUDO $VNCONFIG -c "$VND" "$TARGET" || return 2
tmpfile=$($MKTEMP)
if [ $? -ne 0 ]; then
$DEBUG $SUDO $VNCONFIG -u "$VND"
return 2
fi
$DEBUG $SUDO $DISKLABEL "$VND" | $GREP -q -- '^ a:'
res=$?
if [ $res -eq 1 ]; then
($DEBUG $SUDO $DISKLABEL "$VND" | $DEBUG $SED -e 's/^ e:/ a:/' \
> "$tmpfile" &&
$DEBUG $SUDO $DISKLABEL -R "$VND" "$tmpfile")
res=$?
fi
$RM -- "$tmpfile"
if [ $res -ne 0 ]; then
$DEBUG $SUDO $VNCONFIG -u "$VND"
return 2
fi
_info "Formatting partition ${VND}a..."
$DEBUG $SUDO $NEWFS "${VND}a"
if [ $? -ne 0 ]; then
$DEBUG $SUDO $VNCONFIG -u "$VND"
return 2
fi
}
#volume_mount
_volume_mount()
{
$DEBUG $SUDO $MKDIR -- "$DESTDIR" || return 2
$DEBUG $SUDO $MOUNT "/dev/${VND}a" "$DESTDIR"
}
#volume_partition
_volume_partition()
{
size=$((IMAGE_SIZE * IMAGE_BS / 512 - 63))
_info "Partitioning the disk..."
$DEBUG $SUDO $FDISK -Ffau0s "169/63/$size" "$TARGET" || return 2
}
#volume_umount
_volume_umount()
{
ret=0
if [ -n "$INSTALLBOOT" ]; then
_info "Installing the first-stage bootloader..."
$DEBUG $SUDO $INSTALLBOOT "/dev/r${VND}a" \
"$DESTDIR/usr/mdec/bootxx_ffsv1" || ret=2
fi
$DEBUG $SUDO $UMOUNT "/dev/${VND}a" || return 2
$DEBUG $SUDO $VNCONFIG -u "$VND" || return 2
return $ret
}