Skip to content

Commit

Permalink
Introduce dynamic mount options for enhanced customization and adapta…
Browse files Browse the repository at this point in the history
…bility.
  • Loading branch information
estebanways committed Jan 8, 2024
1 parent 310dc47 commit d402cad
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .shellcheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ shell=bash
# Enable specific checks (comma-separated)
# To disable a check, prefix it with "no"
#enable=SC1000,noSC2000
disable=SC1090,SC2012,SC2129
disable=SC1090,SC2129,SC1091

# Ignore specific errors in certain lines
# Example: Ignore SC1000 and SC2000 in lines 5 and 10
Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,30 @@ Done! Now, to start your chroot just execute `sudo pandoras --start-box`.

For more command options and shortcuts execute `pandoras --help`.

## Advanced topics

### Modify mount points

You can modify the default mount points before creating a new Pandoras box in the script create_box.sh. These changes will remain until the next modification of the file.

For example, to add a mount point for /dev/pts:

```shell
sudo nano /var/pandoras/includes/create_box.sh
```

Uncomment the next line:

```shell
#echo "/dev/pts $dir/environment/dev/pts" >> "$dir/images/$chroot.filesystems.mnt"
```

Save changes and then create your new box.

The mount points will be stored in a brand-new Pandoras box file <box-name>.filesystems.mnt.

You can also modify the mount points in the Pandoras Box file <box-name>.filesystems.mnt before starting the box. These changes will remain until the next modification of the file.

## Extras

### Upsize chroots
Expand Down
1 change: 1 addition & 0 deletions env/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
RUNNING_IMAGE_NAME=""
8 changes: 8 additions & 0 deletions includes/create_box.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ create_box() {
chroot $dir/environment /bin/su -c 'echo "#!/bin/sh" > /boot/boot.sh' &&
chroot $dir/environment /bin/su -c 'echo "" > /etc/resolv.conf'
umount $dir/environment

# Create the default mount points file
echo "/etc/resolv.conf $dir/environment/etc/resolv.conf" > "$dir/images/$chroot.filesystems.mnt"
echo "/dev $dir/environment/dev" >> "$dir/images/$chroot.filesystems.mnt"
#echo "/dev/pts $dir/environment/dev/pts" >> "$dir/images/$chroot.filesystems.mnt"
echo "/proc $dir/environment/proc" >> "$dir/images/$chroot.filesystems.mnt"
echo "/sys $dir/environment/sys" >> "$dir/images/$chroot.filesystems.mnt"
echo "Mount points file created successfully at: $dir/images/$chroot.filesystems.mnt"
}

if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
Expand Down
1 change: 1 addition & 0 deletions includes/duplicate_box.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ duplicate_box() {
read -r -p "Type your old image name: " old
read -r -p "Type your new image name: " new
cp $dir/images/"$old".img $dir/images/"$new".img
cp $dir/images/"$old".filesystems.mnt $dir/images/"$new".filesystems.mnt
}

if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
Expand Down
7 changes: 6 additions & 1 deletion includes/list_boxes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ list_boxes() {
export dir=/var/pandoras

echo "Your chroot images:
$(ls $dir/images/ | sed 's/.img//g' | less)
$(find "$dir/images" -type f -name "*.img" -exec basename {} .img \; | less)
"

# If your device does not support or have find, switch to ls:
#echo "Your chroot images:
#$(ls $dir/images/ | sed 's/.img//g' | less)
#"
}

if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
Expand Down
50 changes: 50 additions & 0 deletions includes/list_filesystems.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/env bash
################################################################################
# Pandoras #
# #
# Universal Chroot environment that can be deployed to most Linux distros #
# #
# Change History #
# 12/11/2023 Esteban Herrera Original code. #
# Add new history entries as needed. #
# #
# #
################################################################################
################################################################################
################################################################################
# #
# Copyright (c) 2023-present Esteban Herrera C. #
# stv.herrera@gmail.com #
# #
# 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, write to the Free Software #
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #

# Lists filesystems
list_filesystems() {

export dir=/var/pandoras

echo "Your chroot image filesystems:
$(find "$dir/images" -type f -name "*.mnt" -exec basename {} .mnt \; | less)
"

# If your device does not support or have find, switch to ls:
#echo "Your chroot images:
#$(ls $dir/images/ | sed 's/.mnt//g' | less)
#"
}

if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
(list_filesystems)
fi
30 changes: 25 additions & 5 deletions includes/start_box.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@
# Starts chroot
start_box() {

# Path to the env file
ENV_FILE="../env/.env"

export dir=/var/pandoras

sudo bash ./list_boxes.sh
read -r -p "Type your image name: " image

# Ask if a custom partition should be added
# Ask if a custom partition or directory should be added
read -r -p "Do you want to add a custom partition or directory? (y/n): " add_custom_part

mount $dir/images/"$image".img $dir/environment
Expand All @@ -47,6 +50,9 @@ start_box() {
# Ask for the device path or directory path
read -r -p "Type the device or directory path (e.g., /dev/sdXn or /path/to/directory): " custom_part

# Create the temporary custom mount points file
echo "$custom_part $dir/environment/$custom_part" > "$dir/images/tmp_mounts.mnt"

# Check if it's a directory or a device
if [ -b "$custom_part" ]; then
# It's a block device (partition)
Expand All @@ -60,10 +66,24 @@ start_box() {
fi
fi

mount -o bind /etc/resolv.conf $dir/environment/etc/resolv.conf
mount -o bind /dev $dir/environment/dev
mount -o bind /proc $dir/environment/proc
mount -o bind /sys $dir/environment/sys
# Check if the mount file exists
if [ ! -f "$dir/images/$image.filesystems.mnt" ]; then
echo "File not found: $dir/images/$image.filesystems.mnt"
exit 1
fi

# Mount the mount file content
while IFS= read -r line; do
# Use cut to extract the first and second fields
source_point=$(echo "$line" | cut -d' ' -f1)
target_point=$(echo "$line" | cut -d' ' -f2)

mount -o bind "$source_point" "$target_point"
done < "$dir/images/$image.filesystems.mnt"

# Update the running image name
sed -i "s/^RUNNING_IMAGE_NAME=.*/RUNNING_IMAGE_NAME=\"$image\"/" $ENV_FILE

chroot $dir/environment /bin/su -c 'sh /boot/boot.sh'
}

Expand Down
54 changes: 38 additions & 16 deletions includes/stop_box.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,10 @@
# Stops chroot
stop_box() {

export dir=/var/pandoras

# Ask if there is custom partition to unmont
read -r -p "Did you mount a custom partition or directory? (y/n): " mounted_custom_part
# Path to the env file
source ../env/.env

if [ "$mounted_custom_part" == "y" ]; then
if grep -qs "$dir"/environment/mnt /proc/mounts; then
umount "$dir/environment/mnt"
else
echo "Specified partition or directory is not currently mounted."
fi
fi
export dir=/var/pandoras

lsof | grep "/environment/" > $dir/process/1
cut -c11-15 $dir/process/1 > $dir/process/2
Expand All @@ -55,11 +47,41 @@ stop_box() {
sed ':a;N;$!ba;s/\n/\nkill -9 /g' $dir/process/5 > $dir/process/6
sed '1 d' $dir/process/6 > $dir/process/7
sh $dir/process/6
umount $dir/environment/sys
umount $dir/environment/proc
umount $dir/environment/dev
umount $dir/environment/etc/resolv.conf
umount $dir/environment

# Unmount custom mounts

# Check if the mount file exists
if [ ! -f "$dir/images/tmp_mounts.mnt" ]; then
echo "File not found: $dir/images/tmp_mounts.mnt"
else
# Unmount the mount file content
while IFS= read -r line; do
# Use cut to extract the first field
target_point=$(echo "$line" | cut -d' ' -f2)

umount "$target_point"
done < "$dir/images/tmp_mounts.mnt"

rm $dir/images/tmp_mounts.mnt
fi

# Unmount filesystems

# Check if the mount file exists
if [ ! -f "$dir/images/$RUNNING_IMAGE_NAME.filesystems.mnt" ]; then
echo "File not found: $dir/images/$RUNNING_IMAGE_NAME.filesystems.mnt"
exit 1
else
# Unmount the mount file content
while IFS= read -r line; do
# Use cut to extract the second fields
target_point=$(echo "$line" | cut -d' ' -f2)

umount "$target_point"
done < "$dir/images/$RUNNING_IMAGE_NAME.filesystems.mnt"

umount $dir/environment
fi
}

if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
Expand Down
5 changes: 5 additions & 0 deletions pandoras.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Boxes Options:
-d, --duplicate-box Duplicates chroot
-e, --enter-box Enters the chroot
-l, --list-boxes Lists chroots
-f, --list-filesystems Lists chroots filesystems
-u, --upsize-box Upsizes chroot
More Options:
Expand Down Expand Up @@ -122,6 +123,10 @@ main() {
cd /var/pandoras/includes || { echo "Error: Unable to change directory."; exit 1; }
bash list_boxes.sh list_boxes || { echo "Error: list_boxes.sh failed."; exit 1; }
;;
'-f' | '--list-filesystems')
cd /var/pandoras/includes || { echo "Error: Unable to change directory."; exit 1; }
bash list_filesystems.sh list_boxes || { echo "Error: list_filesystems.sh failed."; exit 1; }
;;
'-u' | '--upsize-box')
cd /var/pandoras/includes || { echo "Error: Unable to change directory."; exit 1; }
bash upsize_box.sh upsize_box || { echo "Error: upsize_box.sh failed."; exit 1; }
Expand Down

0 comments on commit d402cad

Please sign in to comment.