#!/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='
-- NEW SUMS --
58e79130c7afe7b056b1076aefcf78a4da4323c0c2b258a1c9ca9aca721813a2 all_all_base
740f414bfb0c4b64357c7844a61221716dddd6bb2977812081067a267a618c72 all_ao751h_base
a3952d5104e218a995d61e29a452bbc0f73ad6da1057c83a8f1c97507ce36bc9 all_dimension2400_base
186bc0eac85d85e72636a8c6f8e48637e3b0c7a1c4a61136b9f0169f96ad64fc amd64-linux-glibc_all_base
1d27131b564c5998d11167079cf2ff173a391e3805154168112c61af660d6883 amd64-linux-glibc_dev_base
07825d2d612b369be7940ecc71a414f088b5a229cb40cac9aa6e63f7a421e526 i686-linux-glibc_all_base
324d3ee811a28347e8708c156c9b8aa8f4d5cae02450511764e3812469f348f3 i686-linux-glibc_ao751h_base
fd4b4bac9cfb90d3acc3e71d7b063a4328b358a99efd0e239862d58d0cb40871 i686-linux-glibc_dev_base
e8a12b99587e91f275445091e4932401eca108c1d54c5ea71b2ff81481012ea8 i686-linux-glibc_dimension2400_base
d690749ef87c2e4227725a6127ad7e811f54f53b44ac32fcf0ec88889e22a5a3 src_all_base
-- OLD SUMS --
58e79130c7afe7b056b1076aefcf78a4da4323c0c2b258a1c9ca9aca721813a2 all_all_base
740f414bfb0c4b64357c7844a61221716dddd6bb2977812081067a267a618c72 all_ao751h_base
a3952d5104e218a995d61e29a452bbc0f73ad6da1057c83a8f1c97507ce36bc9 all_dimension2400_base
186bc0eac85d85e72636a8c6f8e48637e3b0c7a1c4a61136b9f0169f96ad64fc amd64-linux-glibc_all_base
4292b1078f86b41e6dc5d49a4557c96521d109ebc76fd3f4593aad1ac74bd4fc amd64-linux-glibc_dev_base
07825d2d612b369be7940ecc71a414f088b5a229cb40cac9aa6e63f7a421e526 i686-linux-glibc_all_base
acbf65f94d224d8289052d6976159e50bcab1c67f401690cc25fd322660b0926 i686-linux-glibc_ao751h_base
72565eca7c3556c29c696348851892f9e6739f225fa2cdb9b8a25448685f1448 i686-linux-glibc_dev_base
4ee56fcdf9180a98d047a5862cdff79c6418a70b84094ca14f235b298b2f698e i686-linux-glibc_dimension2400_base
57059ca5033292d44394ef25a15a8667d37446f0e56810678a3d9c429d3932c6 src_all_base
-- END SUMS --
'
PKGS=' base-files busybox '\
'libc-bin libc.6 libopkg.1 opkg linux-image proteanos-branding '
INSTALL_SERVICE=\
'#!/bin/sh /etc/rc.common
start()
{
log "Configuring packages"
printf "disabled\n" >/etc/rc.policy
opkg install $(opkg list-installed | cut -d " " -f 1)
printf "enabled\n" >/etc/rc.policy
rm "${SCRIPT}"
}
'
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 foreign=
local root=
local uname_m=
local uname_s=
local aps_list=
local aps=
local url_aps=
local feed=
local sha256sum=
local line=
local package=
local filename=
usage='[-a ] [-P ] [-m ] [-F] '
while getopts 'a:P:m:F' opt; do
case ${opt} in
a) arch="${OPTARG}";;
P) plat="${OPTARG}";;
m) mirror="${OPTARG}";;
F) foreign=true;;
?)
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 % 1))
case ${rand} in
0)
mirror='http://mirror.gnu.dk/pub/proteanos'
;;
# 1)
# mirror='http://mirror.oss.maxcdn.com/proteanos'
# ;;
esac
fi
info 'Using mirror %s' "${mirror}"
if [ "x${foreign}" = 'x' ]; then
foreign=false
fi
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'
aps_list="${arch}_${plat}_base ${arch}_all_base"
[ "x${plat}" = 'xdev' ] || aps_list="${aps_list} all_${plat}_base"
aps_list="${aps_list} all_all_base src_all_base"
for aps in ${aps_list}; 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%s' \
'; try fetching the latest miniprokit script'
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 "${root}" "${foreign}"
}
get_pkg()
{
local root="${1}"
local mirror="${2}"
local package="${3}"
local filename="${4}"
local sha256sum="${5}"
local file=
local control=
local field=
local printed=
info 'Downloading %s...' "${package}"
filename="${filename#../../../../../../}"
wget -q -O "${root}/var/cache/opkg/archives/${filename##*/}" \
"${mirror}/${filename}" || error 2 'Failed to download package'
filename="var/cache/opkg/archives/${filename##*/}"
printf '%s %s\n' "${sha256sum}" "${root}/${filename}" | \
sha256sum -c >/dev/null 2>&1 \
|| error 2 'Checksum of source package file failed'
info 'Unpacking %s...' "${package}"
mkdir "${root}/tmp/opkg/${package}"
(
cd "${root}"
tar -xzOf "${filename}" data.tar.gz \
>"tmp/opkg/${package}/data.tar.gz"
tar -xzf "tmp/opkg/${package}/data.tar.gz"
cd "tmp/opkg/${package}"
tar -xzOf "../../../${filename}" control.tar.gz | \
tar -xz
)
tar -tzf "${root}/tmp/opkg/${package}/data.tar.gz" | sed 's/^\.//' \
>"${root}/var/lib/opkg/info/${package}.list"
rm -f "${root}/tmp/opkg/${package}/data.tar.gz"
for file in "${root}/tmp/opkg/${package}/"*; do
mv "${file}" "${root}/var/lib/opkg/info/${package}.${file##*/}"
done
rmdir "${root}/tmp/opkg/${package}"
# Write status file.
exec 3>>"${root}/var/lib/opkg/status"
control="${root}/var/lib/opkg/info/${package}.control"
for field in Package Version Depends Recommends Suggests \
Provides Replaces Conflicts; do
grep "^${field}: " "${control}" >&3
done
printf 'Status: install ok unpacked\n' >&3
for field in Essential Architecture; do
grep "^${field}: " "${control}" >&3
done
if [ -r "${root}/var/lib/opkg/info/${package}.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/${package}.conffiles"
fi
printf 'Installed-Time: %s\n\n' "$(date '+%s')" >&3
exec 3>&-
rm -f "${root}/${filename}"
}
configure()
{
local root="${1}"
local foreign="${2}"
if ! ${foreign}; then
info 'Configuring packages...'
printf 'disabled\n' >"${root}/etc/rc.policy"
chroot "${root}" /bin/sh -c \
'opkg install $(opkg list-installed | cut -d " " -f 1)'
printf 'enabled\n' >"${root}/etc/rc.policy"
[ -r /etc/resolv.conf ] && cp /etc/resolv.conf "${root}/etc"
[ -r /etc/hostname ] && cp /etc/hostname "${root}/etc"
[ -e "${root}/etc/passwd" ] || printf \
'root::0:0:root:/root:/bin/sh\n' >"${root}/etc/passwd"
else
printf '%s' "${INSTALL_SERVICE}" >"${root}/etc/rc.d/S10install"
chmod 0755 "${root}/etc/rc.d/S10install"
printf 'proteanos\n' >"${root}/etc/hostname"
[ -e "${root}/etc/passwd" ] || printf \
'root::0:0:root:/root:/bin/sh\n' >"${root}/etc/passwd"
fi
}
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 "${@}"