summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2015-05-25 20:33:22 (EDT)
committer P. J. McDermott <pj@pehjota.net>2015-05-25 20:33:22 (EDT)
commit65489cd1069b6e9cb592508103211984c43d3bbf (patch)
treebc76a67b979f8bf072c580ef90f3663dcb06c3e3 /lib
parentb76ff4956a6a6904d07b9b89e5d75decbd488675 (diff)
parent2f95b9bef0fdcd737fbd58cdb5acc405a6b58da4 (diff)
Merge branch 'feature/block-device-mounting'
Diffstat (limited to 'lib')
-rw-r--r--lib/block.sh92
-rw-r--r--lib/cmd/build.sh15
-rw-r--r--lib/cmd/install.sh12
-rw-r--r--lib/cmd/opkg.sh12
-rw-r--r--lib/cmd/shell.sh12
-rw-r--r--lib/local.mk2
-rw-r--r--lib/vardata.sh56
7 files changed, 200 insertions, 1 deletions
diff --git a/lib/block.sh b/lib/block.sh
new file mode 100644
index 0000000..ef1eed9
--- /dev/null
+++ b/lib/block.sh
@@ -0,0 +1,92 @@
+# Functions for mounting and unmounting block devices
+#
+# Copyright (C) 2015 Patrick "P. J." McDermott
+#
+# This file is part of the ProteanOS Development Kit.
+#
+# The ProteanOS Development Kit 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.
+#
+# The ProteanOS Development Kit 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 the ProteanOS Development Kit. If not, see
+# <http://www.gnu.org/licenses/>.
+
+[ "x${_BLOCK_SM+set}" = 'xset' ] && return 0
+_BLOCK_SM=1
+
+use rand
+use vardata
+
+is_block()
+{
+ local dev="${1}"
+
+ [ -b "${dev}" ]
+ return ${?}
+}
+
+check_block()
+{
+ local dev="${1}"
+
+ if ! [ -b "${dev}" ]; then
+ error 2 "$(get_msg 'block_device_invalid')" "${dev}"
+ fi
+}
+
+block_mount()
+{
+ local dev="${1}"
+ local dir=
+
+ check_block "${dev}"
+
+ rand
+ dir="$(get_vardata_dir 'mount')/block-$(printf '%010d' ${rand_x})"
+ if ! mkdir "${dir}"; then
+ error 2 "$(get_msg 'block_mkdir_fail')" "${dir}"
+ fi
+
+ if ! mount "${dev}" "${dir}"; then
+ rmdir "${dir}"
+ error 2 "$(get_msg 'block_mount_fail')" "${dev}"
+ fi
+
+ printf '%s' "${dir}"
+ return 0
+}
+
+block_umount()
+{
+ local dir="${1}"
+ local i=
+ local timed_out=
+
+ i=0
+ timed_out=false
+ while ! umount "${dir}"; do
+ i=$(($i + 1))
+ if [ ${i} -eq 10 ]; then
+ timed_out=true
+ break
+ fi
+ sleep 1
+ done
+
+ if ${timed_out}; then
+ error 2 "$(get_msg 'block_umount_fail')" "${dir}"
+ fi
+
+ if ! rmdir "${dir}"; then
+ error 2 "$(get_msg 'block_rmdir_fail')" "${dir}"
+ fi
+
+ return 0
+}
diff --git a/lib/cmd/build.sh b/lib/cmd/build.sh
index 32dca26..d22fa1e 100644
--- a/lib/cmd/build.sh
+++ b/lib/cmd/build.sh
@@ -18,6 +18,7 @@
# along with the ProteanOS Development Kit. If not, see
# <http://www.gnu.org/licenses/>.
+use block
use profile
use session
use rand
@@ -31,6 +32,7 @@ cmd_build_build_deps=
cmd_build_main()
{
local root=
+ local dev=
local opkbuild_optstring=
local arch=
local plat=
@@ -43,9 +45,16 @@ cmd_build_main()
fi
root="${1}"
- cmd_build_root="${root}"
shift 1
+ dev=''
+ if is_block "${root}"; then
+ dev="${root}"
+ root="$(block_mount "${dev}")"
+ fi
+
+ cmd_build_root="${root}"
+
opkbuild_optstring="$(cat "${root}/usr/share/opkbuild/optstring")"
arch="$(cat "${root}/etc/proteanos_arch")"
plat="$(cat "${root}/etc/proteanos_plat")"
@@ -91,6 +100,10 @@ cmd_build_main()
cmd_build_fini
session_end
+
+ if [ "x${dev}" != 'x' ]; then
+ block_umount "${root}"
+ fi
}
cmd_build_make_deps_pkg()
diff --git a/lib/cmd/install.sh b/lib/cmd/install.sh
index ac817e0..a02a77c 100644
--- a/lib/cmd/install.sh
+++ b/lib/cmd/install.sh
@@ -21,6 +21,7 @@
use getopt
use feed
use profile
+use block
use dir
use fd
use pkg
@@ -41,6 +42,7 @@ cmd_install_main()
local profile=
local mirror=
local foreign=
+ local dev=
if ! get_options "${@}"; then
print_cmd_usage 'install' >&2
@@ -97,6 +99,12 @@ cmd_install_main()
foreign=false
fi
+ dev=''
+ if is_block "${chroot}"; then
+ dev="${chroot}"
+ chroot="$(block_mount "${dev}")"
+ fi
+
info "$(get_msg 'cmd_install_setting_up_chroot')"
if [ -d "${chroot}" ] && ! dir_is_empty "${chroot}" 'lost+found'; then
error 2 "$(get_msg 'cmd_install_chroot_dir_exists')" "${chroot}"
@@ -119,6 +127,10 @@ cmd_install_main()
fi
rm "${chroot}/prokit/installing"
+
+ if [ "x${dev}" != 'x' ]; then
+ block_umount "${chroot}"
+ fi
}
cmd_install_find_pkgs()
diff --git a/lib/cmd/opkg.sh b/lib/cmd/opkg.sh
index 9f0c0de..51b90dc 100644
--- a/lib/cmd/opkg.sh
+++ b/lib/cmd/opkg.sh
@@ -18,6 +18,7 @@
# along with the ProteanOS Development Kit. If not, see
# <http://www.gnu.org/licenses/>.
+use block
use profile
use session
use rand
@@ -27,6 +28,7 @@ cmd_opkg_opks=
cmd_opkg_main()
{
local root=
+ local dev=
local first_arg=
local install_cmd=
local arg=
@@ -40,6 +42,12 @@ cmd_opkg_main()
root="${1}"
shift 1
+ dev=''
+ if is_block "${root}"; then
+ dev="${root}"
+ root="$(block_mount "${dev}")"
+ fi
+
first_arg=true
install_cmd=false
cmd_opkg_opks=''
@@ -84,6 +92,10 @@ cmd_opkg_main()
cmd_opkg_fini
session_end
+
+ if [ "x${dev}" != 'x' ]; then
+ block_umount "${root}"
+ fi
}
cmd_opkg_fini()
diff --git a/lib/cmd/shell.sh b/lib/cmd/shell.sh
index dfff07f..b5b6d69 100644
--- a/lib/cmd/shell.sh
+++ b/lib/cmd/shell.sh
@@ -18,12 +18,14 @@
# along with the ProteanOS Development Kit. If not, see
# <http://www.gnu.org/licenses/>.
+use block
use profile
use session
cmd_shell_main()
{
local root=
+ local dev=
if [ ${#} -lt 1 ]; then
print_cmd_usage 'shell' >&2
@@ -33,6 +35,12 @@ cmd_shell_main()
root="${1}"
shift 1
+ dev=''
+ if is_block "${root}"; then
+ dev="${root}"
+ root="$(block_mount "${dev}")"
+ fi
+
. "${root}/etc/os-release"
profile_set "${ID}"
@@ -45,4 +53,8 @@ cmd_shell_main()
session_exec "${@}"
fi
session_end
+
+ if [ "x${dev}" != 'x' ]; then
+ block_umount "${root}"
+ fi
}
diff --git a/lib/local.mk b/lib/local.mk
index 1ce768c..53b2d91 100644
--- a/lib/local.mk
+++ b/lib/local.mk
@@ -4,6 +4,7 @@ pkgdata_sources = \
lib/getopt.sh \
lib/fd.sh \
lib/dir.sh \
+ lib/vardata.sh \
lib/archplat.sh \
lib/deps.sh \
lib/substvars.sh \
@@ -12,6 +13,7 @@ pkgdata_sources = \
lib/pkg.sh \
lib/mutex.sh \
lib/session.sh \
+ lib/block.sh \
lib/opkg.sh \
lib/rand.sh \
lib/cmd.sh \
diff --git a/lib/vardata.sh b/lib/vardata.sh
new file mode 100644
index 0000000..8800003
--- /dev/null
+++ b/lib/vardata.sh
@@ -0,0 +1,56 @@
+# Functions for getting variable/runtime data
+#
+# Copyright (C) 2015 Patrick "P. J." McDermott
+#
+# This file is part of the ProteanOS Development Kit.
+#
+# The ProteanOS Development Kit 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.
+#
+# The ProteanOS Development Kit 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 the ProteanOS Development Kit. If not, see
+# <http://www.gnu.org/licenses/>.
+
+[ "x${_VARDATA_SM+set}" = 'xset' ] && return 0
+_VARDATA_SM=1
+
+PKGLOCALSTATEDIR='@pkglocalstatedir@'
+VARDATA_DIRS='mount'
+
+vardatadir=
+
+init_vardata()
+{
+ local dir=
+
+ if ${in_place}; then
+ vardatadir="${builddir}/var"
+ for dir in ${VARDATA_DIRS}; do
+ mkdir -p "${vardatadir}/${dir}"
+ done
+ else
+ vardatadir="${PKGLOCALSTATEDIR}"
+ fi
+
+ return 0
+}
+
+get_vardata_dir()
+{
+ local dir="${1}"
+
+ case " ${VARDATA_DIRS} " in
+ *" ${dir} "*)
+ printf '%s/%s' "${vardatadir}" "${dir}"
+ ;;
+ esac
+
+ return 0
+}