-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_duelboot_chroot.sh
More file actions
80 lines (64 loc) · 1.83 KB
/
quick_duelboot_chroot.sh
File metadata and controls
80 lines (64 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/bash
if [ -z "$1" ]; then
echo "No argument provided"
exit 1
fi
PARTITION="/dev/$1"
TARGET="/mnt/$1/"
TARGET_EXISTS=1
if [ ! -e "$PARTITION" ]; then
echo "That partition does not exist"
exit 1
fi
# Ensure the script is run with sudo
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root. Try again with 'sudo'"
exit 1
fi
echo "Creating mount point files..."
# Check if the mount point already exists
if [ ! -d "$TARGET" ]; then
# If it doesn't exist, create it
mkdir "$TARGET"
TARGET_EXISTS=0
# If it does, check if it is empty
elif [ -n "$(ls -A $TARGET)" ]; then
echo "The mount point $TARGET is not empty and may be already mounted"
exit 1
fi
# Mount the partition to the target
echo "Mounting partition..."
mount "$PARTITION" "$TARGET"
# Mount necessary filesystems
echo "Mounting system directories into chroot..."
mount --bind /dev "$TARGET/dev"
mount --bind /dev/pts "$TARGET/dev/pts"
mount --bind /proc "$TARGET/proc"
mount --bind /sys "$TARGET/sys"
mount --bind /run "$TARGET/run"
mount --bind /tmp "$TARGET/tmp"
echo "Copying DNS resolver config..."
mv "$TARGET/etc/resolv.conf" "$TARGET/etc/.tmp_resolv.conf"
cp /etc/resolv.conf "$TARGET/etc/resolv.conf"
# Enter the chroot
echo "Entering chroot environment in $TARGET..."
chroot "$TARGET" /bin/bash
echo "Restoring DNS resolver config..."
mv "$TARGET/etc/.tmp_resolv.conf" "$TARGET/etc/resolv.conf"
# After exiting chroot, unmount everything
echo "Cleaning up mounts..."
umount -l "$TARGET/dev/pts"
umount -l "$TARGET/dev"
umount -l "$TARGET/proc"
umount -l "$TARGET/sys"
umount -l "$TARGET/run"
umount -l "$TARGET/tmp"
echo "Unmounting..."
umount "$TARGET"
# Clean up created files
# Leave them if they already existed
if [ "$TARGET_EXISTS" -eq 0 ]; then
echo "Cleaning mount point files..."
rm -d "$TARGET"
fi
echo "Done."