-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchange_os_root
executable file
·107 lines (65 loc) · 2.88 KB
/
change_os_root
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
#!/usr/bin/env bash
source /home/bhaskar/colors.sh
NOCOLOR="\033[0m"
BASH=$(command -v bash)
MUNT=$(command -v mount)
copy=$(command -v cp)
change_root=$(command -v chroot)
change_systemd_root=$(command -v systemd-nspawn)
change_prompt="export PS1=\"(chroot) $PS1\""
sys_init=$(strings /sbin/init | gawk 'match($0, /(upstart|systemd|sysvinit)/) { print toupper(substr($0, RSTART, RLENGTH));exit; }')
#Usage help
usage() {
printf "${Bright}${Red} You forgot to mention either the target directory name or the partiton or both ...aborting.${NOCOLOR} \
${Reverse}Usage : change_os_root partition_number target_dir ${NOCOLOR}
(sd[abc][123]) (/mnt or /whereever you like)
\n\n"
exit 1
}
#One should have superuser privilege to work with this script.
if [[ $UID != "0" ]];then
printf "${Bright}${Red} You don't have enough privilege to utilise this tool.Use it with sudo or root :( ${NOCOLOR} \n\n"
usage
exit 1
fi
#enlist all the partitions
cat $HOME/part_stat
printf "\n\n${Bright}${Green} Which partition or OS root you want to access: %s"
read -r slice
printf "\n Well, you want to access $slice \n\n"
ls /
printf "\n${Bright}${Yellow} Under which directory you want to mount the partition? : %s"
read -r chroot_dir
printf "\n You wanted to mount it on $chroot_dir \n"
if [ -z "$slice" ] || [ -z "$chroot_dir" ];then
usage
#Checkin system INIT type and taking appropriate action to respective to that system.
elif [[ "$sys_init" == "SYSTEMD" ]];then
printf "${Bright}${Green} Chrooting to other OS .... ${NOCOLOR} \n\n\n"
printf "${Bright}${LimeYellow} This is a ${Reverse}SYSTEMD ${NOCOLOR} system, taking appropriate actions... \n\n"
$MUNT /dev/$slice $chroot_dir
$change_systemd_root -bD $chroot_dir
elif [[ "$sys_init" == "SYSVINIT" ]];then
printf "${Bright}${Green} Chrooting to other OS .... ${NOCOLOR} \n\n\n"
printf "${Bright}${PowderBlue} This is a ${Reverse}SYSVINIT ${NOCOLOR} system,taking appropriate actions... \n\n"
printf "${Bright}${Cyan} Doing other related stuff...wait ${NOCOLOR} \n\n"
$MUNT /dev/$slice $chroot_dir
$MUNT --rbind /dev $chroot_dir/dev
$MUNT --make-rslave $chroot_dir/dev
$MUNT -t proc /proc $chroot_dir/proc
$MUNT --rbind /sys $chroot_dir/sys
$MUNT --make-rslave $chroot_dir/sys
$MUNT --rbind /tmp $chroot_dir/tmp
$copy /etc/resolv.conf $chroot_dir/etc/
printf "${Bright}${LimeYellow} Getting into the chroot environment and change the prompt for recognization ,please run at shell. ${NOCOLOR}
export PS1=\"(chroot) \$PS1\"
\n\n"
$change_root $chroot_dir $BASH
else
printf "${Bright}${Red} Error encounter...inspect manually.${NOCOLOR}"
usage
fi
printf "${Bright}${PowderBlue} Well, leaving the chroot shell and unmounting the slice from mounted dir ... ${NOCOLOR} \n\n"
trap EXIT
umount -l $chroot_dir
exit 0