#!/bin/sh
# Mini/Temporary ProteanOS Development Kit
# miniprokit.sh
# Main program file
#
# Copyright (C) 2013, 2014 Patrick "P. J." McDermott
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
set -u
SHA256SUMS='
c126f241291f052b0662e9dac3ee71f6352ccc3e344c4b189bff8ab3e2bc5c81 amd64-linux-glibc_dev_base
db0c809064c8d14854dac0b58e4dbe46523afd60ec658621cac6cb260d68c762 amd64-linux-glibc_all_base
8d4645bca214361dd63e3e4f6aed8040f90124ca8dc228eef4c501eba032768d i686-linux-glibc_dev_base
f55b232e039b026604f167376dde43fdc2bc47736e7c7954e049d3394230be9f i686-linux-glibc_all_base
a222bdcec19963ab4f015ad07dfe7d05d82f950d2f47f343effaeaeb995e025d all_all_base
56d5fe723a968a877c34389116f7f69d8ff3a972f6ecf20eff4ca2e5d1d4378d src_all_base
-- OLD SUMS --
2d39291e0636bfbbc0d8e1b050ddc131f06308d5cfb0123ddb559d83eb062d64 amd64-linux-glibc_all_base
d92cd59405f5bd242058972159b365cef512df5c7d5a3d2e85f77e11dde0f481 i686-linux-glibc_all_base
afbea2111500188f7616377a6830d358b2524f9c12c2129e4f4aa80b1a5cdabe all_all_base
1dbf8d85726ef43f4ce571469d737671378d8d39078f1a8f3746607b120a1728 src_all_base
-- OLDER SUMS --
1c654b2638814d6027698045e0340c3a4c5fa4249c43d65da2cf8e3e39eec9c9 amd64-linux-glibc_all_base
e52e8c2fff69f4091888aec48127d65b8a15b04f13801daa6848abe1ee9fca1a i686-linux-glibc_all_base
b7f82e85eee3a9e36ad6ac23dc89befaa24f73756022238d195d96d95ea84d81 all_all_base
972221d0701a5e3cde1782982f2edb24e6341abef455841248ce6f22ee313dea src_all_base
-- EVEN OLDER SUMS --
ac16de260be8d1594f9a300f4baccf8c92085dccfb685a326bf37561a1ab4f9b amd64-linux-glibc_all_base
f570049c6c304d3471a45c9cb218c50799a6e79e21b12e74c7d7597f3fd37a7a i686-linux-glibc_all_base
c3ab37f17bbc85ab2f222f61b5970d6f94ebb3cc0e5efc9cbec6ee8975738b94 all_all_base
b7294a647b9e73053520fca0eeb5394ca7608f3886d0d1d1e973002257ea4147 src_all_base
'
PKGS=' base-files busybox '\
'libc-bin libc.6 libopkg.1 opkg '
main()
{
local cmd=
if [ ${#} -lt 1 ]; then
print_usage >&2
exit 1
fi
cmd="${1}"
shift 1
case "${cmd}" in
'install')
cmd_install "${@}"
;;
'shell')
cmd_shell "${@}"
;;
esac
return 0
}
print_usage()
{
printf 'Usage: %s []\n\n' "${0}"
printf 'Available commands are:\n'
printf ' install install a ProteanOS system\n'
printf ' shell enter a ProteanOS shell\n'
}
error()
{
local status=${1}
local fmt="${2}"
shift 2
printf "Error: ${fmt}\n" "${@}" >&2
exit ${status}
}
info()
{
local fmt="${1}"
shift 1
printf "${fmt}\n" "${@}"
}
cmd_install()
{
local usage=
local opt=
local arch=
local plat=
local mirror=
local root=
local uname_m=
local uname_s=
local aps=
local url_aps=
local feed=
local sha256sum=
local line=
local package=
local filename=
usage='[-a ] [-P ] [-m ] '
while getopts 'a:P:m:' opt; do
case ${opt} in
a) arch="${OPTARG}";;
P) plat="${OPTARG}";;
m) mirror="${OPTARG}";;
?)
printf 'Usage: %s install %s\n' \
"${0}" "${usage}" >&2
exit 1
;;
esac
done
shift $(($OPTIND - 1))
if [ ${#} -ne 1 ]; then
printf 'Usage: %s install %s\n' "${0}" "${usage}" >&2
exit 1
fi
root="${1}"
if [ "x${arch}" = 'x' ]; then
uname_m="$( (uname -m) 2>/dev/null)" || uname_m='?'
uname_s="$( (uname -s) 2>/dev/null)" || uname_s='?'
case "${uname_m}:${uname_s}" in
'i686:Linux') arch='i686-linux-glibc';;
'x86_64:Linux') arch='amd64-linux-glibc';;
'?:?') error 2 'Error: Architecture unknown';;
*) error 2 'Error: Architecture unsupported';;
esac
fi
info 'Using architecture %s' "${arch}"
if [ "x${plat}" = 'x' ]; then
plat='dev'
fi
info 'Using platform %s' "${plat}"
if [ "x${mirror}" = 'x' ]; then
rand=$(date '+%S')
rand=$(($rand % 2))
case ${rand} in
0)
mirror='http://proteanos.mirror.gnu.dk'
mirror="${mirror}/pub/proteanos"
;;
1)
mirror='http://mirror.oss.maxcdn.com/proteanos'
;;
esac
fi
info 'Using mirror %s' "${mirror}"
info 'Setting up root...'
mkdir -p "${root}/etc/opkg" \
"${root}/var/lib/opkg/lists" "${root}/var/lib/opkg/info" \
"${root}/var/cache/opkg/archives" "${root}/tmp/opkg" || \
error 2 'Failed to set up root'
info 'Configuring opkg and retrieving Packages...'
exec 3>"${root}/etc/opkg/opkg.conf" || \
error 2 'Failed to open opkg.conf'
for aps in "${arch}_${plat}_base" "${arch}_all_base" 'all_all_base' \
'src_all_base'; do
url_aps="$(printf '%s\n' "${aps}" | sed 's|_|/|g')"
feed="${mirror}/feeds/dev/trunk/${url_aps}"
printf 'src %s %s\n' "trunk_${aps}" "${feed}" >&3
wget -q -O "${root}/var/lib/opkg/lists/trunk_${aps}" \
"${feed}/Packages" || \
error 2 'Failed to download %s' "${feed}/Packages"
sha256sum="$(sha256sum \
"${root}/var/lib/opkg/lists/trunk_${aps}" | \
cut -d ' ' -f 1)"
printf '%s' "${SHA256SUMS}" | grep -Fq "${sha256sum} ${aps}" \
|| error 2 'Checksum of Packages index file failed'
done
printf '\ndest root /\n' >&3
printf 'arch %s 1\n' 'all' "${arch}" 'src' >&3
exec 3>&-
while IFS='' read -r line; do
if [ "x${line%: *}" = 'xPackage' ]; then
package="${line#Package: }"
elif [ "x${line%: *}" = 'xFilename' ]; then
filename="${line#Filename: }"
elif [ "x${line%: *}" = 'xSHA256sum' ]; then
sha256sum="${line#SHA256sum: }"
elif [ "x${line}" = 'x' ]; then
printf '%s\n' "${PKGS}" | grep -Fq " ${package} " || \
continue
get_pkg "${root}" "${mirror}" \
"${package}" "${filename}" "${sha256sum}"
fi
done <<-EOF
$(cat "${root}/var/lib/opkg/lists/trunk_"*)
EOF
configure_pkgs "${root}"
[ -r /etc/resolv.conf ] && cp /etc/resolv.conf "${root}/etc"
[ -r /etc/hostname ] && cp /etc/hostname "${root}/etc"
}
get_pkg()
{
local root="${1}"
local mirror="${2}"
local package="${3}"
local filename="${4}"
local sha256sum="${5}"
local file=
info 'Downloading %s...' "${package}"
filename="${filename#../../../../../../}"
wget -q -O "${root}/var/cache/opkg/archives/${filename##*/}" \
"${mirror}/${filename}" || error 2 'Failed to download package'
filename="${root}/var/cache/opkg/archives/${filename##*/}"
printf '%s %s\n' "${sha256sum}" "${filename}" | sha256sum --quiet -c \
|| error 2 'Checksum of source package file failed'
info 'Unpacking %s...' "${package}"
(cd "${root}"; tar -xzOf "${filename#${root}/}" data.tar.gz | tar -xz)
mkdir "${root}/tmp/opkg/${package}"
(
cd "${root}/tmp/opkg/${package}"
tar -xzOf "../../../${filename#${root}/}" control.tar.gz | \
tar -xz
)
for file in "${root}/tmp/opkg/${package}/"*; do
mv "${file}" "${root}/var/lib/opkg/info/${package}.${file##*/}"
done
rmdir "${root}/tmp/opkg/${package}"
}
configure_pkgs()
{
local root="${1}"
local opk=
local pkg=
local script=
local control=
local field=
local printed=
local file=
for opk in "${root}/var/cache/opkg/archives/"*.opk; do
pkg="${opk##*/}"
pkg="${pkg%%_*}"
info 'Configuring %s...' "${pkg}"
for script in 'preinst' 'postinst'; do
if [ -x "${root}/var/lib/opkg/info/${pkg}.${script}" ]
then
chroot "${root}" \
"/var/lib/opkg/info/${pkg}.${script}" \
configure
fi
done
tar -xzOf "${opk}" data.tar.gz | tar tz | sed 's/^\.//' \
>"${root}/var/lib/opkg/info/${pkg}.list"
# Write status file.
exec 3>>"${root}/var/lib/opkg/status"
control="${root}/var/lib/opkg/info/${pkg}.control"
for field in Package Version Depends Recommends Suggests \
Provides Replaces Conflicts; do
grep "^${field}: " "${control}" >&3
done
printf 'Status: install ok installed\n' >&3
for field in Essential Architecture; do
grep "^${field}: " "${control}" >&3
done
if [ -r "${root}/var/lib/opkg/info/${pkg}.conffiles" ]; then
printed=false
while read -r file; do
${printed} || printf 'Conffiles:\n' >&3
printf ' %s %s\n' "${file}" "$(md5sum \
"${root}/${file}" | cut -d ' ' -f 1)" \
>&3
printed=true
done <"${root}/var/lib/opkg/info/${pkg}.conffiles"
fi
printf 'Installed-Time: %s\n\n' "$(date '+%s')" >&3
exec 3>&-
done
}
cmd_shell()
{
local root=
local arch=
if [ ${#} -ne 1 ]; then
printf 'Usage: %s shell \n' "${0}" >&2
exit 1
fi
root="${1}"
if ! [ -d "${root}" ] || ! [ -r "${root}/etc/proteanos_arch" ]; then
error 2 'Error: ProteanOS root file system not found'
fi
arch="$(cat "${root}/etc/proteanos_arch")"
case "${arch}" in
*'-linux-'*)
mount -t proc proc "${root}/proc"
mount -t sysfs sys "${root}/sys"
mount -o bind /dev "${root}/dev"
mount -t devpts -o noexec,nosuid,gid=5,mode=0620 \
devpts "${root}/dev/pts"
;;
*)
error 2 'ProteanOS architecture %s not supported' \
"${arch}"
esac
chroot "${root}" /bin/sh
printf '\nExiting...\n'
case "$(cat "${root}/etc/proteanos_arch")" in
*'-linux-'*)
umount "${root}/dev/pts"
umount "${root}/proc"
umount "${root}/sys"
while ! umount "${root}/dev"; do
# umount sometimes complains that ${root}/dev is
# busy. Here's a kludge to try to handle that.
# This is a bind mount, so we better make sure
# it gets unmounted; otherwise, `rm -Rf ${root}`
# can be painful.
sleep 1
done
;;
esac
}
main "${@}"