forked from sormy/gentoo-quick-installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgentoo-quick-installer.sh
executable file
·217 lines (156 loc) · 5.17 KB
/
gentoo-quick-installer.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
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
#!/bin/sh
##
# GENTOO QUICK INSTALLER
#
# Read more: http://www.artembutusov.com/gentoo-linux-quick-installer-script/
#
# Usage:
#
# export OPTION1=VALUE1
# export OPTION2=VALUE2
# ./gentoo-quick-installer.sh
#
# Bare metal install on /dev/sda with root password:
# ROOT_PASSWORD=Gentoo123 ./gentoo-quick-installer.sh
#
# Remote VM server install with ssh RSA public key:
# TARGET_DISK=/dev/vda SSH_PUBLIC_KEY=$(cat id_rsa.pub) ./gentoo-quick-installer.sh
#
# Options:
#
# SSH_PUBLIC_KEY - ssh public key, pass contents of `cat ~/.ssh/id_rsa.pub` for example
# ROOT_PASSWORD - root password, only SSH key-based authentication will work if not set
# TARGET_DISK - Default is set for IDE/SATA drives.
# /dev/vda is standard for most Virtual Machines.
# /dev/nvme0n1 is standard for NVME drives.
# /dev/mmcblk0 is standard for most eMMC and SD drives
# GENTOO_ARCH - Default is amd64 build with OpenRC.
# Other options are amd64-desktop-openrc, amd64-nomultilib-openrc,
# amd64-hardened-selinux-openrc, amd64-musl, md64-musl-hardened,
# amd64-hardened-selinux-openrc, amd64-hardened-openrc,
# amd64-hardened-nomultilib-selinux-openrc, and amd64-hardened-nomultilib-openrc
#
# Notes:
# This script will _only_ work on a mbr/msdos partition table, not GPT.
# This script does not work with UEFI. only Legacy BIOS.
##
set -e
GENTOO_MIRROR="http://distfiles.gentoo.org"
GENTOO_ARCH="amd64"
GENTOO_STAGE3="${GENTOO_STAGE3:-amd64-openrc}"
TARGET_DISK=${TARGET_DISK:-/dev/sda}
TARGET_BOOT_SIZE=256M
TARGET_SWAP_SIZE=1G
GRUB_PLATFORMS=pc
SSH_PUBLIC_KEY=${SSH_PUBLIC_KEY:-}
ROOT_PASSWORD=${ROOT_PASSWORD:-}
echo "### Checking configuration..."
if [ -z "$SSH_PUBLIC_KEY" ] && [ -z "$ROOT_PASSWORD" ]; then
echo "SSH_PUBLIC_KEY or ROOT_PASSWORD must be set to continue"
exit 1
fi
echo "### Setting time..."
ntpd -gq
echo "### Creating partitions..."
sfdisk ${TARGET_DISK} << END
size=$TARGET_BOOT_SIZE,bootable
size=$TARGET_SWAP_SIZE
;
END
echo "### Formatting partitions..."
yes | mkfs.ext4 ${TARGET_DISK}1
yes | mkswap ${TARGET_DISK}2
yes | mkfs.ext4 ${TARGET_DISK}3
echo "### Labeling partitions..."
e2label ${TARGET_DISK}1 boot
swaplabel ${TARGET_DISK}2 -L swap
e2label ${TARGET_DISK}3 root
echo "### Mounting partitions..."
swapon ${TARGET_DISK}2
mkdir -p /mnt/gentoo
mount ${TARGET_DISK}3 /mnt/gentoo
mkdir -p /mnt/gentoo/boot
mount ${TARGET_DISK}1 /mnt/gentoo/boot
echo "### Setting work directory..."
cd /mnt/gentoo
echo "### Installing stage3..."
STAGE3_PATH_URL="$GENTOO_MIRROR/releases/$GENTOO_ARCH/autobuilds/latest-stage3-$GENTOO_STAGE3.txt"
STAGE3_PATH=$(curl -s "$STAGE3_PATH_URL" | grep -v "^#" | cut -d" " -f1)
STAGE3_URL="$GENTOO_MIRROR/releases/$GENTOO_ARCH/autobuilds/$STAGE3_PATH"
wget "$STAGE3_URL"
tar xvpf "$(basename "$STAGE3_URL")" --xattrs-include='*.*' --numeric-owner
rm -fv "$(basename "$STAGE3_URL")"
echo "### Copying network options..."
cp -v /etc/resolv.conf /mnt/gentoo/etc/
echo "### Configuring fstab..."
cat >> /mnt/gentoo/etc/fstab << END
# added by gentoo installer
LABEL=boot /boot ext4 noauto,noatime 1 2
LABEL=swap none swap sw 0 0
LABEL=root / ext4 noatime 0 1
END
echo "### Mounting proc/sys/dev..."
mount --types proc /proc /mnt/gentoo/proc
mount --rbind /sys /mnt/gentoo/sys
mount --rbind /dev /mnt/gentoo/dev
mount --bind /run /mnt/gentoo/run
echo "### Fixing possible LiveCD issues..."
test -L /dev/shm && rm /dev/shm && mkdir /dev/shm
mount --types tmpfs --options nosuid,nodev,noexec shm /dev/shm
chmod 1777 /dev/shm /run/shm
echo "### Changing root..."
chroot /mnt/gentoo /bin/bash -s << END
#!/bin/bash
set -e
echo "### Upading configuration..."
env-update
source /etc/profile
echo "### Installing portage..."
mkdir -p /etc/portage/repos.conf
cp -f /usr/share/portage/config/repos.conf /etc/portage/repos.conf/gentoo.conf
emerge-webrsync
mv /etc/portage/make.conf /etc/portage/default
mkdir /etc/portage/make.conf
mv /etc/portage/default /etc/portage/make.conf
echo "### Installing kernel..."
# required to allow for linux-firmware (required for binary kernel).
echo "ACCEPT_LICENSE=\"*\"" >> /etc/portage/make.conf/default
emerge sys-kernel/linux-firmware installkernel-gentoo
emerge virtual/dist-kernel sys-kernel/gentoo-kernel-bin
echo "### Installing bootloader..."
emerge grub
cat >> /etc/portage/make.conf/default << IEND
# added by gentoo installer
GRUB_PLATFORMS="$GRUB_PLATFORMS"
IEND
cat >> /etc/default/grub << IEND
# added by gentoo installer
GRUB_CMDLINE_LINUX="net.ifnames=0"
GRUB_DEFAULT=0
GRUB_TIMEOUT=0
IEND
grub-install ${TARGET_DISK}
grub-mkconfig -o /boot/grub/grub.cfg
echo "### Configuring network..."
ln -s /etc/init.d/net.lo /etc/init.d/net.eth0
rc-update add net.eth0 default
emerge net-misc/dhcpcd
if [ -z "$ROOT_PASSWORD" ]; then
echo "### Removing root password..."
passwd -d -l root
else
echo "### Configuring root password..."
echo "root:$ROOT_PASSWORD" | chpasswd
fi
if [ -n "$SSH_PUBLIC_KEY" ]; then
echo "### Configuring SSH..."
rc-update add sshd default
mkdir /root/.ssh
touch /root/.ssh/authorized_keys
chmod 750 /root/.ssh
chmod 640 /root/.ssh/authorized_keys
echo "$SSH_PUBLIC_KEY" > /root/.ssh/authorized_keys
fi
END
echo "### Rebooting..."
reboot