-
Notifications
You must be signed in to change notification settings - Fork 0
/
bash_profile
224 lines (209 loc) · 10 KB
/
bash_profile
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# First run script for GentooWSL2
blu=$(tput setaf 4)
cyn=$(tput setaf 6)
grn=$(tput setaf 2)
mgn=$(tput setaf 5)
red=$(tput setaf 1)
ylw=$(tput setaf 3)
txtrst=$(tput sgr0)
diskvol=$(mount | grep -m1 ext4 | cut -f 1 -d " ")
sudo resize2fs $diskvol >/dev/null 2>&1
disksize=$(sudo blockdev --getsize64 $diskvol)
osname=$(/mnt/c/Windows/System32/wbem/wmic.exe os get Caption | sed -n 2p)
width=$(echo $COLUMNS)
if [ "$width" -gt 120 ]; then
width=120
fi
test -e /mnt/c/Users/Public/vhdresize.txt && rm /mnt/c/Users/Public/vhdresize.txt
test -e /mnt/c/Users/Public/shutdown.cmd && rm /mnt/c/Users/Public/shutdown.cmd
test -e ~/vhdresize.txt && rm ~/vhdresize.txt
test -e ~/shutdown.cmd && rm ~/shutdown.cmd
figlet -t -k -f /usr/share/figlet/mini.flf "Welcome to GentooWSL2" | lolcat
echo -e "\033[33;7mDo not interrupt or close the terminal window till script finishes execution!!!\n\033[0m"
setcap cap_net_raw+ep /usr/bin/ping
if [ "$disksize" -le 274877906944 ]; then
echo -e ${grn}"GentooWSL2's VHD has a default maximum size of 256GB. Disk space errors which occur if size exceeds 256GB can be fixed by expanding the VHD. Would you like to resize your VHD? More information on this process is available at \033[36mhttps://docs.microsoft.com/en-us/windows/wsl/vhd-size\033[32m."${txtrst} | fold -sw $width
select yn in "Yes" "No"; do
case $yn in
Yes)
echo " "
while read -p ${mgn}"Path to virtual disk (e.g. C:\Users\silesh\wsl\ext4.vhdx) : "${txtrst} -r vhdpath; do
if [ "x$vhdpath" = "x" ]; then
echo -e ${red}"Path cannot be blank."${txtrst}
echo -en "\033[1A\033[1A\033[2K"
vhdpath=""
else
wsl_path=$(wslpath -a $vhdpath)
if [ ! -f $wsl_path ]; then
echo -e ${red}"Disk does not exist. "${txtrst}
echo -en "\033[1A\033[1A\033[2K"
vhdpath=""
else
echo "select vdisk file=\"$vhdpath\"" | sudo tee -a ~/vhdresize.txt >/dev/null 2>&1
break
fi
fi
done
while read -p ${mgn}"Size of virtual disk in MegaBytes(e.g. 512000 for 512GB) : "${txtrst} vhdsize; do
if [[ $vhdsize =~ ^-?[0-9]+$ ]]; then
if [ "$vhdsize" -le 256000 ]; then
echo -e ${red}"Disk size should be greater than 256000 MegaBytes."${txtrst}
echo -en "\033[1A\033[1A\033[2K"
vhdsize=0
else
echo -en "\033[1B\033[1A\033[2K"
echo "expand vdisk maximum=$vhdsize" | sudo tee -a ~/vhdresize.txt >/dev/null 2>&1
echo " "
printf "%s" "$(<~/vhdresize.txt)"
echo " "
echo -e ${grn}"\nReview the information displayed above and confirm to proceed."${txtrst}
echo -e ${red}"Edit only your input if you want to make changes!!!"${txtrst}
select yn in "Proceed" "Edit"; do
case $yn in
Proceed)
break
;;
Edit)
"${EDITOR:-nano}" ~/vhdresize.txt
break
;;
esac
done
echo "@echo off" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
echo "wsl --shutdown" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
echo "diskpart /s C:\Users\Public\vhdresize.txt" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
if env | grep "WT_SESSION" >/dev/null 2>&1; then
echo "wt.exe -w 0 nt wsl.exe -d $WSL_DISTRO_NAME" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
else
echo "cmd /c start \"$WSL_DISTRO_NAME\" wsl.exe --cd ~ -d $WSL_DISTRO_NAME" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
fi
cp ~/vhdresize.txt /mnt/c/Users/Public
cp ~/shutdown.cmd /mnt/c/Users/Public && rm ~/shutdown.cmd
break
fi
else
echo -e ${red}"Disk size cannot be blank and has to be numeric. "${txtrst}
echo -en "\033[1A\033[1A\033[2K"
fi
done
secs=3
printf ${ylw}"\nPlease grant diskpart elevated permissions when requested. GentooWSL2 will restart after disk resize.\n"${txtrst}
printf ${red}"Warning!!! Any open wsl distros will be shutdown.\n\n"${txtrst}
while [ $secs -gt 0 ]; do
printf "\r\033[KShutting down in %.d seconds. " $((secs--))
sleep 1
done
/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -command "Start-Process -Verb Open -FilePath 'shutdown.cmd' -WorkingDirectory 'C:\Users\Public' -WindowStyle Hidden"
exec sleep 0
;;
No)
break
;;
esac
done
fi
echo -e ${grn}"Create root password"${txtrst}
passwd
echo " "
echo -e ${grn}"Do you want to create a new user?"${txtrst}
select yn in "Yes" "No"; do
case $yn in
Yes)
echo " "
while read -p "Please enter the username you wish to create : " username; do
if [ "x$username" = "x" ]; then
echo -e ${red}" Blank username entered. Try again!!!"${txtrst}
echo -en "\033[1A\033[1A\033[2K"
username=""
elif grep -q "^$username" /etc/passwd; then
echo -e ${red}"Username already exists. Try again!!!"${txtrst}
echo -en "\033[1A\033[1A\033[2K"
username=""
else
groupadd sudo
useradd -m -g users -G sudo,wheel -s /bin/bash "$username"
mkdir -p /etc/sudoers.d
echo "%wheel ALL=(ALL) ALL" >/etc/sudoers.d/wheel
echo "%sudo ALL=(ALL) ALL" >/etc/sudoers.d/sudo
echo -en "\033[1B\033[1A\033[2K"
passwd $username
sed -i "/\[user\]/a default = $username" /etc/wsl.conf >/dev/null
echo "@echo off" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
echo "wsl.exe --terminate $WSL_DISTRO_NAME" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
if env | grep "WT_SESSION" >/dev/null 2>&1; then
echo "wt.exe -w 0 nt wsl.exe -d $WSL_DISTRO_NAME" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
else
echo "cmd /c start \"$WSL_DISTRO_NAME\" wsl.exe --cd ~ -d $WSL_DISTRO_NAME" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
fi
echo "del C:\Users\Public\shutdown.cmd" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
cp ~/shutdown.cmd /mnt/c/Users/Public && rm ~/shutdown.cmd
if (ps --no-headers -o comm 1 | grep -q "systemd"); then
if echo $(wsl.exe --version | tr -d '\0' | sed -n 1p | cut -f3 -d " " | cut -f1 -d ".") >0 || echo $(wsl.exe --version | tr -d '\0' | sed -n 1p | cut -f3 -d " " | cut -f2 -d ".") >0 || (($(echo $(wsl.exe --version | tr -d '\0' | sed -n 1p | cut -f3 -d " " | cut -f2-3 -d ".") '>' 67.5 | bc))); then
commandline="systemd=true"
echo "$commandline" >>/etc/wsl.conf
else
commandline="command = \"/usr/bin/env -i /usr/bin/unshare --fork --mount --propagation shared --mount-proc --pid -- sh -c 'mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc; [ -x /usr/lib/systemd/systemd ] && exec /usr/lib/systemd/systemd --unit=multi-user.target || exec /lib/systemd/systemd --unit=multi-user.target'\""
echo "$commandline" >>/etc/wsl.conf
wget https://raw.githubusercontent.com/diddledani/one-script-wsl2-systemd/main/src/sudoers -O /etc/sudoers.d/wsl2-systemd
sed -i 's/%sudo/%wheel/g' /etc/sudoers.d/wsl2-systemd
wget https://raw.githubusercontent.com/diddledani/one-script-wsl2-systemd/4dc64fba72251f1d9804ec64718bb005e6b27b62/src/00-wsl2-systemd.sh -P /etc/profile.d/
sed -i '/\\nSystemd/d' /etc/profile.d/00-wsl2-systemd.sh
fi
printf ${ylw}"\nGentooWSL2 will shutdown and restart to setup default user & systemd!!!\n\n"${txtrst}
else
commandline="command = \"/sbin/openrc default\""
echo "$commandline" >>/etc/wsl.conf
printf ${ylw}"\nTo set the new user as the default user, GentooWSL2 will shutdown and restart!!!\n\n"${txtrst}
fi
secs=3
while [ $secs -gt 0 ]; do
printf "\r\033[KShutting down in %.d seconds. " $((secs--))
sleep 1
done
rm ~/.bash_profile
/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -command "Start-Process -Verb Open -FilePath 'shutdown.cmd' -WorkingDirectory 'C:\Users\Public' -WindowStyle Hidden"
exec sleep 0
fi
done
;;
No)
break
;;
esac
done
if (ps --no-headers -o comm 1 | grep -q "systemd"); then
if echo $(wsl.exe --version | tr -d '\0' | sed -n 1p | cut -f3 -d " " | cut -f1 -d ".") >0 || echo $(wsl.exe --version | tr -d '\0' | sed -n 1p | cut -f3 -d " " | cut -f2 -d ".") >0 || (($(echo $(wsl.exe --version | tr -d '\0' | sed -n 1p | cut -f3 -d " " | cut -f2-3 -d ".") '>' 67.5 | bc))); then
commandline="systemd=true"
echo "$commandline" >>/etc/wsl.conf
else
commandline="command = \"/usr/bin/env -i /usr/bin/unshare --fork --mount --propagation shared --mount-proc --pid -- sh -c 'mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc; [ -x /usr/lib/systemd/systemd ] && exec /usr/lib/systemd/systemd --unit=multi-user.target || exec /lib/systemd/systemd --unit=multi-user.target'\""
echo "$commandline" >>/etc/wsl.conf
wget https://raw.githubusercontent.com/diddledani/one-script-wsl2-systemd/main/src/sudoers -O /etc/sudoers.d/wsl2-systemd
sed -i 's/%sudo/%wheel/g' /etc/sudoers.d/wsl2-systemd
wget https://raw.githubusercontent.com/diddledani/one-script-wsl2-systemd/4dc64fba72251f1d9804ec64718bb005e6b27b62/src/00-wsl2-systemd.sh -P /etc/profile.d/
sed -i '/\\nSystemd/d' /etc/profile.d/00-wsl2-systemd.sh
fi
printf ${ylw}"\nGentooWSL2 will shutdown and restart to setup default user & systemd!!!\n\n"${txtrst}
else
commandline="command = \"/sbin/openrc default\""
echo "$commandline" >>/etc/wsl.conf
printf ${ylw}"\nTo set the new user as the default user, GentooWSL2 will shutdown and restart!!!\n\n"${txtrst}
fi
echo "@echo off" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
echo "wsl.exe --terminate $WSL_DISTRO_NAME" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
if env | grep "WT_SESSION" >/dev/null 2>&1; then
echo "wt.exe -w 0 nt wsl.exe -d $WSL_DISTRO_NAME" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
else
echo "cmd /c start \"$WSL_DISTRO_NAME\" wsl.exe --cd ~ -d $WSL_DISTRO_NAME" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
fi
echo "del C:\Users\Public\shutdown.cmd" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
cp ~/shutdown.cmd /mnt/c/Users/Public && rm ~/shutdown.cmd
secs=3
while [ $secs -gt 0 ]; do
printf "\r\033[KShutting down in %.d seconds. " $((secs--))
sleep 1
done
emerge -c lolcat --ask=n && rm /etc/portage/package.accept_keywords/lolcat
rm ~/.bash_profile
/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -command "Start-Process -Verb Open -FilePath 'shutdown.cmd' -WorkingDirectory 'C:\Users\Public' -WindowStyle Hidden"
exec sleep 0