-
Notifications
You must be signed in to change notification settings - Fork 12
/
kerndev-create.chroot.sh
executable file
·125 lines (106 loc) · 3.62 KB
/
kerndev-create.chroot.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
#!/bin/bash
set -e; set -o pipefail
# Configurable variables.
# Console dimensions, adjust to taste, this works well on my screen!
CONSOLE_ROWS=${CONSOLE_ROWS:-50}
CONSOLE_COLS=${CONSOLE_COLS:-200}
# Passed by calling script.
username=$1 # == $SUDO_USER
uid=$2 # The user ID associated with $username so we can sync bindmount.
password=$3 # Optional, if not provided script will prompt.
# Functions.
# Taken from http://unix.stackexchange.com/a/14346. Non-root version didn't work.
function inside_chroot()
{
[[ "$(stat -c %d:%i /)" != "$(stat -c %d:%i /proc/1/root/.)" ]]
}
function usage()
{
echo "usage: $0 [username] <root password>" >&2
}
# Sanity checks.
if ! inside_chroot; then
echo not inside chroot! >&2
exit 1
fi
if [[ -z "$username" ]]; then
usage
exit 1
fi
echo Configuring system...
# We want to use the network :)
pacman -S --noconfirm dhcpcd &>/dev/null
systemctl -q enable dhcpcd
# Retrieve wget so we can set it as the transfer command in a moment.
pacman -S --noconfirm wget &>/dev/null
# We assign Google DNS servers (outside this script), so fuck this hook.
echo "nohook resolv.conf" >> /etc/dhcpcd.conf
# No delay for incorrect password reattempt. Pet peeve!
sed -i 's/try_first_pass/try_first_pass nodelay/' /etc/pam.d/system-auth
echo tux > /etc/hostname
# Set up directory for kernel source bindmount.
mkdir -p /src/kernel
# Now get the packages we want.
echo Installing packages...
pacman -Sy --noconfirm nano strace zsh lsof openssh patch git &>/dev/null
# We don't need the linux package, we link to the kernel via a qemu switch.
pacman -R --noconfirm linux &>/dev/null || true
echo Setting up root password...
if [[ -z "$password" ]]; then
# Don't let a typo ruin our day!
while ! passwd; do
echo Try again!
done
else
echo root:$password | chpasswd
fi
echo Setting up user $username with auto-login...
useradd -m $username -u $uid -G wheel >&/dev/null || true
passwd -d $username >/dev/null
echo "%wheel ALL=(ALL) ALL" >> /etc/sudoers
mkdir -p /etc/systemd/system/serial-getty@ttyS0.service.d
mkdir -p /etc/systemd/system/serial-getty@ttyAMA0.service.d
# I've found xterm works the best.
cat >/etc/systemd/system/serial-getty@ttyAMA0.service.d/autologin.conf <<EOF
[Service]
ExecStart=
ExecStart=-/usr/bin/agetty --autologin $username -s %i 115200,38400,9600 xterm
EOF
cat >/etc/systemd/system/serial-getty@ttyS0.service.d/autologin.conf <<EOF
[Service]
ExecStart=
ExecStart=-/usr/bin/agetty --autologin $username -s %i 115200,38400,9600 xterm
EOF
# Create script to 'leave' dev env to prevent accidentally running `sudo
# poweroff` in the host by mistake (I've done this... more than once :)
cat >/usr/bin/leave <<EOF
#!/bin/bash
sudo poweroff
EOF
chmod +x /usr/bin/leave
mv /.ssh /home/$username/
chown -R $username:$username /home/$username/.ssh
chown -R $username:$username /src
echo "kernel /src/kernel 9p trans=virtio,version=9p2000.L 0 0" >> /etc/fstab
echo Configuring zsh...
user_zshrc=/home/$username/.zshrc
# Install oh-my-zsh
rm -rf /tmp/ls__omz
sudo -u $username git clone https://aur.archlinux.org/oh-my-zsh-git.git /tmp/ls__omz &>/dev/null
cd /tmp/ls__omz
sudo -u $username makepkg -si --noconfirm &>/dev/null
cp /usr/share/oh-my-zsh/zshrc $user_zshrc
sed -i 's/ZSH_THEME=".*"/ZSH_THEME="gallois"/' $user_zshrc
chsh -s /usr/bin/zsh $username >/dev/null
# For some reason qemu is sending carriage returns... :( fix!!
cat >>$user_zshrc <<EOF
stty icrnl
stty rows $CONSOLE_ROWS cols $CONSOLE_COLS
# The bindmounted kernel source chokes on fancy git plugin. Stub it out.
git_custom_status() {
}
EOF
chown $username:$username $user_zshrc
# Failing to sync causes updates to not be written correctly.
echo Syncing changes...
sync