Skip to content

Instantly share code, notes, and snippets.

@rubo77
Last active May 25, 2023 08:22
Show Gist options
  • Save rubo77/eac3313ea5302acfca60 to your computer and use it in GitHub Desktop.
Save rubo77/eac3313ea5302acfca60 to your computer and use it in GitHub Desktop.
Mounts all needed mount points to change into another system from within a live-CD
#!/bin/bash
# mounts all needed mount points to change into another system from within a live-CD
#-------------- Author: -------------#
# by Ruben Barkow (Rubo77)
if [ "$(whoami &2>/dev/null)" != "root" ] && [ "$(id -un &2>/dev/null)" != "root" ] ; then
echo "You must be root to run this script!"; echo "use 'sudo !!'"; exit 1
fi
if [ $1 = "-i" -o $1 = "--interactive" ]; then
#interactive
tmp=$(mktemp)
IFS=
eval dialog --menu \"Please choose a partition:\" 50 50 10 $(lsblk -f | sed -r 's/^/"/;s/$/" " "/' | tr $'\n' ' ') 2>$tmp
D=$(tr -d '│├└─' < $tmp | sed 's/^[ \t]*//' | cut -d' ' -f1)
read -p "mount /dev/$D to /var/tmp/$D? [yN] " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[YyJj]$ ]]; then
# do dangerous stuff
$0 /dev/$D /var/tmp/$D
fi
exit
fi
if [ $# -ne 2 ]; then
# --help
echo "------------------------------"
(set -x
fdisk -l
lsblk -f
)
echo "------------------------------"
echo "DESCRIPTION:
this script mounts all needed mount points to change into another system
from within a live-CD
GLOBAL OPTIONS:
Mandatory arguments to long options are mandatory for short options too.
-i, --interactive
select the partition in an interactive interface
-h, --help
Display output of fdisk and lsblk and this help documentation
USAGE EXAMPLES:
calling this script with only two options with the device and mountpoint
# mounting the partition /dev/sda1 into /media/other/ and chroot into it
mount-root /dev/sda1 /media/other/
DEVELOPER INFO
requires the packages dialog and cut
This procect on git: https://gist.github.com/rubo77/eac3313ea5302acfca60
"
exit
fi
D=$(echo $1 | sed 's:/$::')
M=$(echo $2 | sed 's:/$::')
echo mounting $D to $M
mkdir -p $M
mount $D $M
mount -t proc none $M/proc
mount --bind /dev $M/dev
mount -t sysfs sysfs $M/sys
mount --bind /dev/pts $M/dev/pts
cp /proc/mounts $M/etc/mtab
echo chrooting now...
chroot $M/ /bin/bash
#!/usr/bin/env bash
# lets you select a partition with dialog
#-------------- Author: -------------#
# by Ruben Barkow (Rubo77)
# git: https://gist.github.com/rubo77/eac3313ea5302acfca60
tmp=$(mktemp)
IFS=
eval dialog --menu \"Please choose a partition:\" 50 50 10 $(lsblk -f | sed -r 's/^/"/;s/$/" " "/' | tr $'\n' ' ') 2>$tmp
D=$(tr -d '│├└─' < $tmp | sed 's/^[ \t]*//' | cut -d' ' -f1)
printf "You chose:\n%s\n" "$D"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment