-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlinux.sh
138 lines (119 loc) · 3.37 KB
/
linux.sh
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/sh
# Set LINUX_PARTITION variable to the linux's partition on the SD card: LINUX_PARTITION="/dev/block/mmcblk1p1"
LINUX_PARTITION=""
# Set linux partition's file system in the PARTITION_FS variable
PARTITION_FS="ext4"
LINUX_ROOT="/data/debian"
BIND_MOUNTS="dev dev/pts proc sys"
### Check if LINUX_PARTITION is set
if [ "$LINUX_PARTITION" == "" ]; then
echo "'LINUX_PARTITION' variable is not set in the $0 file!"
exit
fi
### If -h or --help switch is passed, then show the help and exit
if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
echo "Chroot to linux"
echo "Usage: $0 [OPTIONS]"
echo "OPTIONS:"
echo -e " -f\tForce chrooting if already chrooted."
echo -e " -k\tKill the current chroot."
exit
fi
clear
### If -k switch is passed, then kill the previus chroot and exit
if [ "$1" == "-k" ]; then
### Kill linux processes
echo "Killing linux processes:"
PID=$(lsof | grep $LINUX_ROOT | /bin/head -n 1 | /bin/awk '{print $2}') # Get linux PID
while [ "$PID" ]
do
echo -n " PID: $PID: "
kill "$PID"
echo "done"
PID=$(lsof | grep "$LINUX_ROOT" | /bin/head -n 1 | /bin/awk '{print $2}') # Get linux PID
done
### Unmount bind mounts
echo "Unmounting bind mounts: "
### Reverse bind mounts order to unmount them
BIND_UNMOUNTS=""
for f in $BIND_MOUNTS
do
BIND_UNMOUNTS="$f $BIND_UNMOUNTS"
done
### Unmount bind mounts
for f in $BIND_UNMOUNTS
do
echo -n " $f: "
if grep -qs "$LINUX_ROOT/$f" /proc/mounts; then
umount "$LINUX_ROOT/$f"
echo "done"
else
echo "already unmounted"
fi
done
### Unmount other mounts
echo "Unmounting other mounts: "
MP=$(mount | grep "$LINUX_ROOT" | /bin/awk '{print $2}') # Get other mount points
while [ "$MP" ]
do
echo -n " $MP: "
umount "$MP"
echo "done"
MP=$(mount | grep "$LINUX_ROOT" | /bin/awk '{print $2}') # Get other mount points
done
### Unmount linux partition
echo -n "Unmounting linux partition: "
if grep -qs "$LINUX_ROOT" /proc/mounts; then
umount "$LINUX_ROOT"
echo "done"
else
echo "already unmounted"
fi
exit
fi
### Do chroot
### Create linux root directory
echo -n "Creating linux root directory: "
mkdir -p "$LINUX_ROOT"
echo "done"
### Mount linux partition
echo -n "Mounting linux partition: "
if grep -qs "$LINUX_ROOT" /proc/mounts; then
CHROOTED="1"
echo "already mounted"
else
CHROOTED="0"
mount -o exec,dev,suid -t "$PARTITION_FS" "$LINUX_PARTITION" "$LINUX_ROOT"
echo "done"
fi
### Bind mount required files
echo "Bind mounting:"
for f in $BIND_MOUNTS
do
echo -n " $f: "
if grep -qs "$LINUX_ROOT/$f" /proc/mounts; then
echo "already mounted"
else
mount -o bind /$f "$LINUX_ROOT/$f"
echo "done"
fi
done
### Prepare for chroot
echo -n "Preparing for chroot: "
export USER=root
export HOME=/root
export SHELL=/bin/bash
export TERM=linux
export PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin
export LC_ALL=C
export LANGUAGE=C
export LANG=C
export TMPDIR=/tmp
echo "done"
### chroot
if [ "$CHROOTED" == "0" ] || [ "$1" == "-f" ]; then
echo "Chrooting..."
chroot "$LINUX_ROOT" /bin/bash --login
else
echo "Already chrooted. Use -f to force chrooting. Use -k to kill chroot."
fi