Serenity Operating System
1#!/bin/sh
2
3set -e
4
5wheel_gid=1
6phys_gid=3
7utmp_gid=5
8window_uid=13
9window_gid=13
10
11CP="cp"
12
13# cp on macOS and BSD systems do not support the --preserve= option.
14# gcp comes with coreutils, which is already a dependency.
15OS="$(uname -s)"
16if [ "$OS" = "Darwin" ] || echo "$OS" | grep -qe 'BSD$'; then
17 CP="gcp"
18fi
19
20die() {
21 echo "die: $*"
22 exit 1
23}
24
25[ -z "$SERENITY_SOURCE_DIR" ] && die "SERENITY_SOURCE_DIR is not set"
26[ -d "$SERENITY_SOURCE_DIR/Base" ] || die "$SERENITY_SOURCE_DIR/Base doesn't exist"
27
28umask 0022
29
30printf "installing base system... "
31if ! command -v rsync >/dev/null; then
32 die "Please install rsync."
33fi
34
35if rsync --chown 2>&1 | grep "missing argument" >/dev/null; then
36 rsync -aH --chown=0:0 --inplace --update "$SERENITY_SOURCE_DIR"/Base/ mnt/
37 rsync -aH --chown=0:0 --inplace --update Root/ mnt/
38else
39 rsync -aH --inplace --update "$SERENITY_SOURCE_DIR"/Base/ mnt/
40 rsync -aH --inplace --update Root/ mnt/
41 chown -R 0:0 mnt/
42fi
43
44SERENITY_ARCH="${SERENITY_ARCH:-x86_64}"
45LLVM_VERSION="${LLVM_VERSION:-14.0.1}"
46
47if [ "$SERENITY_TOOLCHAIN" = "Clang" ]; then
48 TOOLCHAIN_DIR="$SERENITY_SOURCE_DIR"/Toolchain/Local/clang/
49 $CP --preserve=timestamps "$TOOLCHAIN_DIR"/lib/"$SERENITY_ARCH"-pc-serenity/* mnt/usr/lib
50 mkdir -p mnt/usr/include/"$SERENITY_ARCH"-pc-serenity
51 $CP --preserve=timestamps -r "$TOOLCHAIN_DIR"/include/c++ mnt/usr/include
52 $CP --preserve=timestamps -r "$TOOLCHAIN_DIR"/include/"$SERENITY_ARCH"-pc-serenity/c++ mnt/usr/include/"$SERENITY_ARCH"-pc-serenity
53else
54 $CP --preserve=timestamps -r "$SERENITY_SOURCE_DIR"/Toolchain/Local/"$SERENITY_ARCH"/"$SERENITY_ARCH"-pc-serenity/lib/* mnt/usr/lib
55 $CP --preserve=timestamps -r "$SERENITY_SOURCE_DIR"/Toolchain/Local/"$SERENITY_ARCH"/"$SERENITY_ARCH"-pc-serenity/include/c++ mnt/usr/include
56fi
57
58# If umask was 027 or similar when the repo was cloned,
59# file permissions in Base/ are too restrictive. Restore
60# the permissions needed in the image.
61chmod -R g+rX,o+rX "$SERENITY_SOURCE_DIR"/Base/* mnt/
62
63chmod 660 mnt/etc/WindowServer.ini
64chown $window_uid:$window_gid mnt/etc/WindowServer.ini
65echo "/bin/sh" > mnt/etc/shells
66
67if [ -f mnt/bin/su ]; then
68 chown 0:$wheel_gid mnt/bin/su
69 chmod 4750 mnt/bin/su
70fi
71if [ -f mnt/bin/passwd ]; then
72 chown 0:$wheel_gid mnt/bin/passwd
73 chmod 4755 mnt/bin/passwd
74fi
75if [ -f mnt/bin/ping ]; then
76 chown 0:$wheel_gid mnt/bin/ping
77 chmod 4755 mnt/bin/ping
78fi
79if [ -f mnt/bin/traceroute ]; then
80 chown 0:$wheel_gid mnt/bin/traceroute
81 chmod 4755 mnt/bin/traceroute
82fi
83if [ -f mnt/bin/keymap ]; then
84 chown 0:$phys_gid mnt/bin/keymap
85 chmod 4750 mnt/bin/keymap
86fi
87if [ -f mnt/bin/shutdown ]; then
88 chown 0:$phys_gid mnt/bin/shutdown
89 chmod 4750 mnt/bin/shutdown
90fi
91if [ -f mnt/bin/reboot ]; then
92 chown 0:$phys_gid mnt/bin/reboot
93 chmod 4750 mnt/bin/reboot
94fi
95if [ -f mnt/bin/pls ]; then
96 chown 0:$wheel_gid mnt/bin/pls
97 chmod 4750 mnt/bin/pls
98fi
99if [ -f mnt/bin/Escalator ]; then
100 chown 0:$wheel_gid mnt/bin/Escalator
101 chmod 4750 mnt/bin/Escalator
102fi
103if [ -f mnt/bin/utmpupdate ]; then
104 chown 0:$utmp_gid mnt/bin/utmpupdate
105 chmod 2755 mnt/bin/utmpupdate
106fi
107if [ -f mnt/bin/timezone ]; then
108 chown 0:$phys_gid mnt/bin/timezone
109 chmod 4750 mnt/bin/timezone
110fi
111if [ -f mnt/usr/Tests/Kernel/TestMemoryDeviceMmap ]; then
112 chown 0:0 mnt/usr/Tests/Kernel/TestMemoryDeviceMmap
113 chmod 4755 mnt/usr/Tests/Kernel/TestMemoryDeviceMmap
114fi
115if [ -f mnt/usr/Tests/Kernel/TestProcFSWrite ]; then
116 chown 0:0 mnt/usr/Tests/Kernel/TestProcFSWrite
117 chmod 4755 mnt/usr/Tests/Kernel/TestProcFSWrite
118fi
119
120if [ -f mnt/res/kernel.map ]; then
121 chmod 0400 mnt/res/kernel.map
122fi
123
124if [ -f mnt/boot/Kernel ]; then
125 chmod 0400 mnt/boot/Kernel
126fi
127
128if [ -f mnt/boot/Kernel.debug ]; then
129 chmod 0400 mnt/boot/Kernel.debug
130fi
131
132chmod 600 mnt/etc/shadow
133chmod 755 mnt/res/devel/templates/*.postcreate
134echo "done"
135
136printf "creating initial filesystem structure... "
137for dir in bin etc proc mnt tmp boot mod var/run usr/local usr/bin; do
138 mkdir -p mnt/$dir
139done
140chmod 700 mnt/boot
141chmod 700 mnt/mod
142chmod 1777 mnt/tmp
143echo "done"
144
145printf "creating utmp file... "
146echo "{}" > mnt/var/run/utmp
147chown 0:$utmp_gid mnt/var/run/utmp
148chmod 664 mnt/var/run/utmp
149echo "done"
150
151printf "setting up device nodes folder... "
152mkdir -p mnt/dev
153echo "done"
154
155printf "setting up sysfs folder... "
156mkdir -p mnt/sys
157echo "done"
158
159printf "installing users... "
160mkdir -p mnt/root
161mkdir -p mnt/home/anon
162mkdir -p mnt/home/anon/Desktop
163mkdir -p mnt/home/anon/Downloads
164mkdir -p mnt/home/nona
165# FIXME: Handle these test copies using CMake install rules
166rm -fr mnt/home/anon/Tests/js-tests mnt/home/anon/Tests/web-tests mnt/home/anon/Tests/cpp-tests
167mkdir -p mnt/home/anon/Tests/cpp-tests/
168cp "$SERENITY_SOURCE_DIR"/README.md mnt/home/anon/
169cp -r "$SERENITY_SOURCE_DIR"/Userland/Libraries/LibJS/Tests mnt/home/anon/Tests/js-tests
170cp -r "$SERENITY_SOURCE_DIR"/Userland/Libraries/LibWeb/Tests mnt/home/anon/Tests/web-tests
171cp -r "$SERENITY_SOURCE_DIR"/Userland/Libraries/LibCodeComprehension/Cpp/Tests mnt/home/anon/Tests/cpp-tests/comprehension
172cp -r "$SERENITY_SOURCE_DIR"/Userland/Libraries/LibCpp/Tests/parser mnt/home/anon/Tests/cpp-tests/parser
173cp -r "$SERENITY_SOURCE_DIR"/Userland/Libraries/LibCpp/Tests/preprocessor mnt/home/anon/Tests/cpp-tests/preprocessor
174cp -r "$SERENITY_SOURCE_DIR"/Userland/Libraries/LibJS/Tests/test-common.js mnt/home/anon/Tests/wasm-tests
175cp -r "$SERENITY_SOURCE_DIR"/Userland/Applications/Spreadsheet/Tests mnt/home/anon/Tests/spreadsheet-tests
176
177if [ -n "$SERENITY_COPY_SOURCE" ] ; then
178 printf "\ncopying Serenity's source... "
179 rm -fr mnt/home/anon/Source/serenity
180 mkdir -p mnt/home/anon/Source/serenity
181 git clone --depth=1 file://"$SERENITY_SOURCE_DIR" mnt/home/anon/Source/serenity
182 rm -fr mnt/home/anon/Source/serenity/.git
183fi
184
185chmod 700 mnt/root
186chmod 700 mnt/home/anon
187chmod 700 mnt/home/nona
188chown -R 100:100 mnt/home/anon
189chown -R 200:200 mnt/home/nona
190echo "done"
191
192printf "adding some desktop icons... "
193ln -sf /bin/Browser mnt/home/anon/Desktop/
194ln -sf /bin/TextEditor mnt/home/anon/Desktop/Text\ Editor
195ln -sf /bin/Help mnt/home/anon/Desktop/
196ln -sf /home/anon mnt/home/anon/Desktop/Home
197chown -R 100:100 mnt/home/anon/Desktop
198echo "done"
199
200printf "installing shortcuts... "
201ln -sf Shell mnt/bin/sh
202ln -sf test mnt/bin/[
203ln -sf less mnt/bin/more
204ln -sf /bin/env mnt/usr/bin/env
205echo "done"
206
207printf "installing 'checksum' variants... "
208ln -sf checksum mnt/bin/md5sum
209ln -sf checksum mnt/bin/sha1sum
210ln -sf checksum mnt/bin/sha256sum
211ln -sf checksum mnt/bin/sha512sum
212echo "done"
213
214# Run local sync script, if it exists
215if [ -f "${SERENITY_SOURCE_DIR}/sync-local.sh" ]; then
216 sh "${SERENITY_SOURCE_DIR}/sync-local.sh"
217fi