2014-10-08 00:11:53 +02:00
|
|
|
#!/bin/bash
|
|
|
|
# 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; version 2 of the License.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2014-10-08 00:18:23 +02:00
|
|
|
ignore_error() {
|
|
|
|
"$@" 2>/dev/null
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2015-07-01 18:35:13 +02:00
|
|
|
parse_fstab(){
|
2015-07-02 15:52:41 +02:00
|
|
|
echo $(perl -ane 'printf("%s:%s\n", @F[0,1]) if $F[0] =~ m#^UUID=#;' $1/etc/fstab)
|
2015-07-02 22:53:55 +02:00
|
|
|
# perl -ane 'printf("%s:%s\n", @F[0,1]) if $F[0] =~ m#^/dev#;' $1/etc/fstab
|
2015-07-02 12:34:18 +02:00
|
|
|
# perl -ane 'printf("%s:%s\n", @F[0,1]) if $F[0] =~ m#^LABEL=#;' $1/etc/fstab
|
2015-07-01 18:35:13 +02:00
|
|
|
}
|
|
|
|
|
2015-07-03 17:18:42 +02:00
|
|
|
detect(){
|
2015-07-01 18:35:13 +02:00
|
|
|
local detected=( "$(os-prober | tr ' ' '_' | paste -s -d ' ')" )
|
|
|
|
echo ${detected[@]}
|
|
|
|
}
|
|
|
|
|
2015-07-03 11:07:31 +02:00
|
|
|
set_os(){
|
2015-07-03 17:18:42 +02:00
|
|
|
local count=${#os_list[@]}
|
|
|
|
if [[ ${count} -gt 1 ]];then
|
2015-07-03 11:07:31 +02:00
|
|
|
msg "List of found systems"
|
|
|
|
for((i=0; i < ${count}; i++)); do
|
2015-07-03 17:18:42 +02:00
|
|
|
msg3 "$i) $(get_os_name ${os_list[$i]})"
|
2015-07-03 11:07:31 +02:00
|
|
|
done
|
|
|
|
msg "Please enter your choice [0-$((count-1))] : "
|
2015-07-03 17:18:42 +02:00
|
|
|
while read selection; do
|
|
|
|
if [[ -n ${os_list[$selection]} ]];then
|
|
|
|
echo ${selection}
|
|
|
|
break
|
2015-07-03 11:07:31 +02:00
|
|
|
else
|
2015-07-03 17:18:42 +02:00
|
|
|
msg "Please enter your choice [0-$((count-1))] : "
|
2015-07-03 11:07:31 +02:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
else
|
|
|
|
echo 0
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2015-07-03 17:18:42 +02:00
|
|
|
# $1: os-prober array
|
2015-07-02 20:22:56 +02:00
|
|
|
get_os_name(){
|
2015-07-03 21:49:38 +02:00
|
|
|
local str=$1
|
|
|
|
str="${str#*:}"
|
|
|
|
str="${str#*:}"
|
|
|
|
str="${str//:/}"
|
|
|
|
echo "$str"
|
2015-07-02 20:22:56 +02:00
|
|
|
}
|
|
|
|
|
2015-07-03 17:18:42 +02:00
|
|
|
get_chroot_arch(){
|
|
|
|
local elf=$(file $1/usr/bin/file)
|
|
|
|
elf=${elf//*executable,}
|
|
|
|
echo ${elf%%,*}
|
|
|
|
}
|
|
|
|
|
2015-07-02 09:38:23 +02:00
|
|
|
chroot_part_mount() {
|
|
|
|
mount "$@" && CHROOT_ACTIVE_PART_MOUNTS=("$2" "${CHROOT_ACTIVE_PART_MOUNTS[@]}")
|
2015-07-03 20:46:04 +02:00
|
|
|
msg2 "mounted: ${CHROOT_ACTIVE_PART_MOUNTS[@]}"
|
2015-07-02 09:38:23 +02:00
|
|
|
}
|
|
|
|
|
2015-07-03 17:18:42 +02:00
|
|
|
# $1: os-prober string
|
|
|
|
get_root(){
|
2015-07-03 21:49:38 +02:00
|
|
|
local os_string=$1
|
|
|
|
echo ${os_string%%:*}
|
2015-07-03 17:18:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
select_os(){
|
|
|
|
for system in ${os_list[@]};do
|
2015-07-06 00:13:22 +02:00
|
|
|
if [[ "${system##*:}" == 'linux' ]];then
|
|
|
|
chroot_mount_partitions "${chrootdir}" "${system}"
|
|
|
|
fi
|
2015-07-03 17:18:42 +02:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2015-07-01 18:35:13 +02:00
|
|
|
chroot_mount_partitions(){
|
2015-07-02 22:53:55 +02:00
|
|
|
CHROOT_ACTIVE_PART_MOUNTS=()
|
|
|
|
CHROOT_ACTIVE_MOUNTS=()
|
|
|
|
|
|
|
|
[[ $(trap -p EXIT) ]] && die 'Error! Attempting to overwrite existing EXIT trap'
|
|
|
|
trap 'trap_handler' EXIT
|
|
|
|
|
2015-07-03 21:49:38 +02:00
|
|
|
local index=$(set_os)
|
|
|
|
msg "Selected OS: $(get_os_name ${os_list[$index]})"
|
|
|
|
local root=$(get_root "${os_list[$index]}")
|
2015-07-03 17:18:42 +02:00
|
|
|
|
|
|
|
chroot_part_mount ${root} $1
|
|
|
|
|
2015-07-02 22:53:55 +02:00
|
|
|
local mounts=$(parse_fstab "$1")
|
|
|
|
|
|
|
|
for entry in ${mounts[@]}; do
|
|
|
|
entry=${entry//UUID=}
|
|
|
|
local dev=${entry%:*}
|
|
|
|
local mp=${entry#*:}
|
|
|
|
case "${entry#*:}" in
|
2015-07-03 20:48:18 +02:00
|
|
|
'/'|'swap'|'none') continue ;;
|
2015-07-02 22:53:55 +02:00
|
|
|
*) chroot_part_mount "/dev/disk/by-uuid/${dev}" "$1${mp}" ;;
|
2015-07-02 08:52:23 +02:00
|
|
|
esac
|
2015-07-01 18:35:13 +02:00
|
|
|
done
|
2015-07-03 21:49:38 +02:00
|
|
|
|
2015-07-06 00:13:22 +02:00
|
|
|
local chroot_arch=$(get_chroot_arch $1)
|
2015-07-03 17:18:42 +02:00
|
|
|
[[ ${chroot_arch} == x86-64 ]] && chroot_arch=${chroot_arch/-/_}
|
|
|
|
case ${arch} in
|
|
|
|
i686)
|
|
|
|
if [[ ${chroot_arch} == x86_64 ]];then
|
|
|
|
die "You can't chroot from ${arch} host into ${chroot_arch}!"
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
esac
|
2015-07-02 22:53:55 +02:00
|
|
|
|
|
|
|
chroot_mount_conditional "! mountpoint -q '$1'" "$1" "$1" --bind &&
|
|
|
|
chroot_mount proc "$1/proc" -t proc -o nosuid,noexec,nodev &&
|
|
|
|
chroot_mount sys "$1/sys" -t sysfs -o nosuid,noexec,nodev,ro &&
|
|
|
|
# ignore_error chroot_mount_conditional "[[ -d '$1/sys/firmware/efi/efivars' ]]" \
|
|
|
|
# efivarfs "$1/sys/firmware/efi/efivars" -t efivarfs -o nosuid,noexec,nodev &&
|
|
|
|
chroot_mount udev "$1/dev" -t devtmpfs -o mode=0755,nosuid &&
|
|
|
|
chroot_mount devpts "$1/dev/pts" -t devpts -o mode=0620,gid=5,nosuid,noexec &&
|
|
|
|
chroot_mount shm "$1/dev/shm" -t tmpfs -o mode=1777,nosuid,nodev &&
|
|
|
|
chroot_mount run "$1/run" -t tmpfs -o nosuid,nodev,mode=0755 &&
|
|
|
|
chroot_mount tmp "$1/tmp" -t tmpfs -o mode=1777,strictatime,nodev,nosuid
|
|
|
|
chroot_mount /etc/resolv.conf "$1/etc/resolv.conf" --bind
|
2015-07-01 18:35:13 +02:00
|
|
|
}
|
|
|
|
|
2015-01-13 04:42:24 +01:00
|
|
|
chroot_mount() {
|
2014-10-08 00:11:53 +02:00
|
|
|
mount "$@" && CHROOT_ACTIVE_MOUNTS=("$2" "${CHROOT_ACTIVE_MOUNTS[@]}")
|
2015-07-03 20:46:04 +02:00
|
|
|
#msg2 "mounted: ${CHROOT_ACTIVE_MOUNTS[@]}"
|
2014-10-08 00:11:53 +02:00
|
|
|
}
|
|
|
|
|
2015-01-13 04:42:24 +01:00
|
|
|
chroot_mount_conditional() {
|
2015-02-13 15:17:00 +01:00
|
|
|
local cond=$1; shift
|
|
|
|
if eval "$cond"; then
|
|
|
|
chroot_mount "$@"
|
|
|
|
fi
|
2014-10-08 00:11:53 +02:00
|
|
|
}
|
|
|
|
|
2015-01-13 04:42:24 +01:00
|
|
|
chroot_api_mount() {
|
|
|
|
CHROOT_ACTIVE_MOUNTS=()
|
|
|
|
[[ $(trap -p EXIT) ]] && die 'Error! Attempting to overwrite existing EXIT trap'
|
|
|
|
trap 'chroot_api_umount' EXIT
|
|
|
|
|
|
|
|
chroot_mount_conditional "! mountpoint -q '$1'" "$1" "$1" --bind &&
|
|
|
|
chroot_mount proc "$1/proc" -t proc -o nosuid,noexec,nodev &&
|
|
|
|
chroot_mount sys "$1/sys" -t sysfs -o nosuid,noexec,nodev,ro &&
|
|
|
|
# ignore_error chroot_mount_conditional "[[ -d '$1/sys/firmware/efi/efivars' ]]" \
|
2015-01-11 22:31:47 +01:00
|
|
|
# efivarfs "$1/sys/firmware/efi/efivars" -t efivarfs -o nosuid,noexec,nodev &&
|
2015-01-13 04:42:24 +01:00
|
|
|
chroot_mount udev "$1/dev" -t devtmpfs -o mode=0755,nosuid &&
|
|
|
|
chroot_mount devpts "$1/dev/pts" -t devpts -o mode=0620,gid=5,nosuid,noexec &&
|
|
|
|
chroot_mount shm "$1/dev/shm" -t tmpfs -o mode=1777,nosuid,nodev &&
|
|
|
|
chroot_mount run "$1/run" -t tmpfs -o nosuid,nodev,mode=0755 &&
|
|
|
|
chroot_mount tmp "$1/tmp" -t tmpfs -o mode=1777,strictatime,nodev,nosuid
|
2014-10-08 00:11:53 +02:00
|
|
|
}
|
|
|
|
|
2015-07-02 15:52:41 +02:00
|
|
|
chroot_part_umount() {
|
|
|
|
umount "${CHROOT_ACTIVE_PART_MOUNTS[@]}"
|
|
|
|
unset CHROOT_ACTIVE_PART_MOUNTS
|
|
|
|
}
|
|
|
|
|
2015-01-13 04:42:24 +01:00
|
|
|
chroot_api_umount() {
|
2014-10-08 00:11:53 +02:00
|
|
|
umount "${CHROOT_ACTIVE_MOUNTS[@]}"
|
2015-01-13 04:42:24 +01:00
|
|
|
unset CHROOT_ACTIVE_MOUNTS
|
2014-10-08 00:11:53 +02:00
|
|
|
}
|
2015-07-02 15:52:41 +02:00
|
|
|
|
|
|
|
trap_handler(){
|
2015-07-03 21:49:38 +02:00
|
|
|
chroot_api_umount
|
|
|
|
chroot_part_umount
|
2015-07-02 22:53:55 +02:00
|
|
|
}
|