-
Notifications
You must be signed in to change notification settings - Fork 4
/
to-gentoo
executable file
·226 lines (194 loc) · 5.79 KB
/
to-gentoo
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
225
226
#!/bin/bash
# Converts a random Linux installation to Gentoo
# crash early
set -e
export LANG=C
# see https://www.gentoo.org/downloads/mirrors/
#MIRROR="https://mirror.init7.net/gentoo/"
MIRROR="http://distfiles.gentoo.org/"
GENTOO="/gentoo"
OLD="/orig"
# check preconditions
if [ "$(uname -s)" != "Linux" ]; then
echo "Only Linux can be converted. Your system is $(uname -s)." >&2
exit 1
fi
if [ "$(uname -m)" != "x86_64" ]; then
echo "Only x86_64 is supported. Your system is $(uname -m)." >&2
exit 1
fi
if [ "$(whoami)" != "root" ]; then
echo "The script needs to be run as root. You are $(whoami)." >&2
exit 1
fi
for f in sed tar wget curl tar grep awk mount bash; do
which "${f}" >/dev/null || {
echo "${f} missing. Please install." >&2
exit 1
}
done
for f in sed tar grep awk bash; do
if [ "$(basename $(readlink -f $(which ${f})))" = "busybox" ]; then
echo "binary $(which $f) is from BusyBox, please install the real (GNU) version" >&2
exit 1
fi
done
for f in mount; do
if [ "$(basename $(readlink -f $(which ${f})))" = "bbsuid" ]; then
echo "binary $(which $f) is from BusyBox, please install the version from util-linux" >&2
exit 1
fi
done
RUNLEVEL=""
which runlevel &>/dev/null && RUNLEVEL="$(runlevel | cut -d ' ' -f 2)"
if [ -z "${RUNLEVEL}" ]; then
which rc-status &>/dev/null && RUNLEVEL="$(rc-status -r)"
fi
if [ -z "${RUNLEVEL}" ]; then
which systemctl &>/dev/null && RUNLEVEL="systemd"
fi
case "${RUNLEVEL}" in
S | unknown | single | systemd | 1)
;;
*)
echo "Please run this script from single-user mode: init s or openrc single or append single to kernel cmd line" >&2
exit 1
;;
esac
FREE=$(df -m / | sed '1d' | awk '{ print $4; }')
MIN=8000
if [ "${FREE}" -lt "${MIN}" ]; then
echo "No enough free disk space. Need ${MIN}K, there is only ${FREE}K" >&2
exit 1
fi
STAGE3=$(curl -s "${MIRROR}releases/amd64/autobuilds/latest-stage3-amd64-openrc.txt" | sed '/^#/d' | cut -d ' ' -f 1)
if [ -z "${STAGE3}" ]; then
echo "Is the network up and DNS works?" >&2
exit 1
fi
echo "Installing Gentoo base system into ${GENTOO}. Old system will be moved to ${OLD}."
mkdir "${GENTOO}"
mkdir "${OLD}"
cd "${GENTOO}"
echo "Downloading ${STAGE3}..."
wget "${MIRROR}releases/amd64/autobuilds/${STAGE3}"
STAGE3=$(ls -1 *.tar.xz)
echo "Unpacking ${STAGE3}"
# FIXME BusyBox tar has no --xattrs
tar xpf "${STAGE3}" --xattrs
rm "${STAGE3}"
echo "Copying stuff from existing system:"
# root password
echo " - root password"
sed -i '/^root:/ d' etc/shadow
grep ^root: /etc/shadow >>etc/shadow
# extra users and groups
awk -F: '$3 > 999 && $3 < 65000 { print $0; }' /etc/passwd >>etc/passwd
awk -F: '$3 > 999 && $3 < 65000 { print $0; }' /etc/group >>etc/group
awk -F: '$3 > 999 && $3 < 65000 { print $1; }' /etc/passwd | while read N; do
echo " - user ${N}"
grep "^${N}:" /etc/shadow >>etc/shadow
done
echo " - /usr/local"
cp -ra /usr/local/* usr/local/ || true
echo " - SSH keys"
cp -a /etc/ssh/*key* etc/ssh || true
echo " - kernel modules"
cp -ra /lib/modules lib/ || true
echo " - firmware"
cp -ra /lib/firmware lib/ || true
echo " - hostname"
echo "hostname=$(hostname)" >etc/conf.d/hostname
cp /etc/hosts etc/
echo " - DNS config"
cp -L "/etc/resolv.conf" etc/ || cp /etc/resolv.conf etc/
echo " - timezone"
cp -L "/etc/localtime" etc/ || cp /etc/localtime etc/
echo " - fstab"
cp -L "/etc/fstab" etc/ || cp /etc/fstab etc/
if [ -L /dev/shm ]; then
# Gentoo requires /dev/shm to be its own tmpfs
rm /dev/shm
mkdir /dev/shm
mount -t tmpfs -o nosuid,nodev,noexec shm /dev/shm
chmod 1777 /dev/shm
fi
# using static busybox from Gentoo to run mv
if [ -e "${GENTOO}/bin/busybox" ]; then
cp "${GENTOO}/bin/busybox" /tmp
MYMV="/tmp/busybox mv"
elif [ -e /bin/mv ]; then
echo "Creating a working standalone mv"
MV=$(mktemp -d)
cp /bin/mv "${MV}"
ldd /bin/mv | sed "s/\t//" | while read f ; do
g=$(echo "${f}" | cut -d' ' -f 3)
f=$(echo "${f}" | cut -d' ' -f 1)
if [ "${f}" == "linux-vdso.so.1" ] ||
[ "${f}" == "linux-gate.so.1" ]; then
continue
fi
if [ -e "${f}" ]; then
cp "${f}" "${MV}"
elif [ -e "/${g}" ]; then
cp "${g}" "${MV}"
fi
done
# glibc, musl are supported here
MYMV="${MV}/ld-*-x86?64.so.? --library-path ${MV} ${MV}/mv"
else
echo "missing /bin/mv" >&2
exit 1
fi
# test it
touch /tmp/a
${MYMV} /tmp/a /tmp/b
if [ ! -e /tmp/b ]; then
echo "didn't manage to create a working standalone mv" >&2
echo "This doesn't work as expected:" >&2
echo "${MYMV}"
exit 1
fi
rm /tmp/b
echo "Now switching over your installation!"
DIRS="etc lib lib32 lib64 var usr sbin bin"
cd /
# FIXME deal with systems that mount /var/run tmpfs
echo "moving to ${OLD}/:"
[ -e /linuxrc ] && mv /linuxrc "${OLD}/"
for d in ${DIRS}; do
echo " - ${d}"
[ -e "${d}" -o -L "${d}" ] && ${MYMV} "${d}" "${OLD}/"
done
echo "replacing with Gentoo:"
for d in ${DIRS}; do
echo " - ${d}"
[ -d "${GENTOO}/${d}" ] && ${MYMV} "${GENTOO}/${d}" .
done
source /etc/profile
hash -r
echo "Cleaning up"
rm -rf "/tmp/busybox"
rm -rf "${GENTOO}"
echo "Downloading Portage tree snapshot"
mkdir -p /etc/portage/repos.conf
cp /usr/share/portage/config/repos.conf /etc/portage/repos.conf/gentoo.conf
emerge-webrsync
emerge --sync
emerge -v --noreplace net-misc/netifrc net-misc/dhcpcd
rc-update add sshd default
echo "Symlinking net.lo"
ip link show up | awk '/^[0-9][0-9]?:/ && $2 != "lo:" { print $2; }' | cut -d: -f1 | while read f ; do
cd /etc/init.d/
echo " - ${f}"
ln -s net.lo net.${f}
done
grep -q '^root:!' /etc/shadow && {
echo "root account is disabled. Gentoo uses root. Please set a password."
passwd
}
chsh -s /bin/bash
echo "Your old system is in ${OLD}. Remove manually."
echo "Keeping your kernel and /boot. Please review..."
echo "It is advisable to install a new kernel and install and configure grub."
echo "DONE! You should reboot now."