Tuesday, May 7, 2024
0
rated 0 times [  0] [ 0]  / answers: 1 / hits: 671  / 1 Year ago, sat, may 13, 2023, 1:42:54

I want to set a particular directory as top directory temporarily. Let's say I open terminal in /home/me/mychroot how can I set mychroot directory as top directory for that terminal?


More From » gnome-terminal

 Answers
6

Closest solution to my question is proot but it doesn't work as intended. For example when I run it like


proot -w  ~/mychroot

and when I change to parent directory


cd .. 

and run


ls

it really changes to parent directory, it must have confined in ~/mychroot directory


Anyway I found a script in one of the forums and modified it to my needs. In original script it doesn't work with /usr/sbin/chroot I replaced it with fakechroot fakeroot chroot and added a few new lines.


#!/bin/bash

if [ $# -ne 1 ]; then
echo "Usage: $0 <enter_name_of_your_chroot_directory>"
else
#remove trail slash
DESTINATION_PATH=$PWD/${1%/}
mkdir -p $DESTINATION_PATH
if [ ! -d "$DESTINATION_PATH" ]; then
echo "Invalid destination path ${DESTINATION_PATH} it does not exists"
exit
fi

if [ ! -d "${DESTINATION_PATH}/dev" ]; then
echo "Create dir path ${DESTINATION_PATH}/dev"
mkdir -p ${DESTINATION_PATH}"/"dev
fi

if ! grep -qs ${DESTINATION_PATH}"/"dev /proc/mounts; then
mount --bind /dev ${DESTINATION_PATH}"/"dev
if [ $? -eq 0 ]; then
echo "Mount success ${DESTINATION_PATH}"/"dev"
else
echo "Something went wrong with the mount ${DESTINATION_PATH}"/"dev"
fi
fi

if [ ! -d "${DESTINATION_PATH}/proc" ]; then
echo "Create dir path ${DESTINATION_PATH}/proc"
mkdir -p ${DESTINATION_PATH}"/"proc
fi

if ! grep -qs ${DESTINATION_PATH}"/"proc /proc/mounts; then
mount --bind /proc ${DESTINATION_PATH}"/"proc
if [ $? -eq 0 ]; then
echo "Mount success ${DESTINATION_PATH}"/"proc"
else
echo "Something went wrong with the mount ${DESTINATION_PATH}"/"proc"
fi
fi

if [ ! -d "${DESTINATION_PATH}/sys" ]; then
echo "Create dir path ${DESTINATION_PATH}/sys"
mkdir -p ${DESTINATION_PATH}"/"sys
fi

if ! grep -qs ${DESTINATION_PATH}"/"sys /proc/mounts; then
mount --bind /sys ${DESTINATION_PATH}"/"sys
if [ $? -eq 0 ]; then
echo "Mount success ${DESTINATION_PATH}"/"sys"
else
echo "Something went wrong with the mount ${DESTINATION_PATH}"/"sys"
fi
fi

if [ ! -d "${DESTINATION_PATH}/dev/pts" ]; then
echo "Create dir path ${DESTINATION_PATH}/dev/pts"
mkdir -p ${DESTINATION_PATH}"/"dev/pts
fi

if ! grep -qs ${DESTINATION_PATH}"/"dev/pts /proc/mounts; then
mount --bind /dev/pts ${DESTINATION_PATH}"/"dev/pts
if [ $? -eq 0 ]; then
echo "Mount success ${DESTINATION_PATH}"/"dev/pts"
else
echo "Something went wrong with the mount ${DESTINATION_PATH}"/"dev/pts"
fi
fi

if [ ! -d "${DESTINATION_PATH}/etc" ]; then
echo "Create dir path ${DESTINATION_PATH}/etc"
mkdir -p ${DESTINATION_PATH}"/"etc
cp /etc/resolv.conf ${DESTINATION_PATH}"/"etc/resolv.conf
fi

for i in $( ldd /bin/bash | grep -v dynamic | cut -d " " -f 3 | sed 's/://' | sort | uniq )
do
cp --parents $i ${DESTINATION_PATH}
done

# ARCH amd64
if [ -f /lib64/ld-linux-x86-64.so.2 ]; then
cp --parents /lib64/ld-linux-x86-64.so.2 /${DESTINATION_PATH}
fi

# ARCH i386
if [ -f /lib/ld-linux.so.2 ]; then
cp --parents /lib/ld-linux.so.2 /${DESTINATION_PATH}
fi

echo "Chroot jail is ready: ${DESTINATION_PATH}"

if [ ! -d "${DESTINATION_PATH}/bin" ]; then
echo "Create dir path ${DESTINATION_PATH}/bin"
mkdir -p ${DESTINATION_PATH}"/"bin
cp /bin/{cat,echo,rm,bash,sh,ls,mkdir} ${DESTINATION_PATH}"/bin/"
fi

fakechroot fakeroot chroot ${DESTINATION_PATH}
fi

[#22315] Sunday, May 14, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
deringach

Total Points: 412
Total Questions: 107
Total Answers: 101

Location: Palestine
Member since Tue, Jul 20, 2021
3 Years ago
deringach questions
Sat, Oct 30, 21, 17:38, 3 Years ago
Fri, Oct 21, 22, 16:34, 2 Years ago
Tue, Feb 15, 22, 22:33, 2 Years ago
Tue, Feb 7, 23, 03:57, 1 Year ago
;