stage3.sh (6599B)
1 #!/bin/sh 2 3 . "$(dirname $0)/clfs-env.sh" 4 5 echo "Building $TARGET userland..." 6 7 # build toybox 8 echo "Building toybox..." 9 cp -r $SOURCES/$TOYBOX_DIR $WORK/$TOYBOX_DIR 10 cd $WORK/$TOYBOX_DIR 11 12 make distclean 13 make defconfig 14 make PREFIX=$SYSROOT install -j$(nproc) 15 16 # build iana-etc 17 echo "Building iana-etc..." 18 cp -r $SOURCES/$IANA_ETC_DIR $WORK/$IANA_ETC_DIR 19 cd $WORK/$IANA_ETC_DIR 20 21 cp protocols services $SYSROOT/etc/ 22 23 # TODO: build packages that would allow clfs to become self-hosting 24 # -- 25 # llvm (clang + lld) 26 # curl 27 # GNU tar 28 # GNU make 29 # cmake 30 # TODO: all linux kernel build-time deps 31 # 32 # TODO: build packages that would make using clfs nicer 33 # GNU bash 34 # meson 35 # git 36 37 ## NOTE: the remainder of stage3.sh simply configures the expected files under 38 ## $SYSROOT/etc and friends for the clfs system, no new binary packages will 39 ## be installed past this point 40 41 # generate bsd-style init system 42 # -- 43 # https://www.linuxfromscratch.org/hints/downloads/files/bsd-init.txt 44 45 cat > $SYSROOT/etc/inittab <<'EOF' 46 # /etc/inittab 47 48 id:2:initdefault: 49 50 si:S:sysinit:/etc/rc.d/rc.sysinit 51 52 l0:0:wait:/etc/rc.d/rc.0 # shutdown 53 l1:1:wait:/etc/rc.d/rc.1 # single user 54 l2:2:wait:/etc/rc.d/rc.2 # multi user 55 l3:3:wait:/etc/rc.d/rc.3 # linked to rc.1 56 l4:4:wait:/etc/rc.d/rc.4 # linked to rc.1 57 l5:5:wait:/etc/rc.d/rc.5 # linked to rc.1 58 l6:6:wait:/etc/rc.d/rc.6 # reboot, linked to rc.0 59 60 ca:12345:ctrlaltdel:/sbin/shutdown -r now 61 62 su:S1:respawn:/sbin/sulogin 63 64 c1:2345:respawn:/sbin/agetty tty1 38400 linux 65 c2:2345:respawn:/sbin/agetty tty2 38400 linux 66 c3:2345:respawn:/sbin/agetty tty3 38400 linux 67 c4:2345:respawn:/sbin/agetty tty4 38400 linux 68 c5:2345:respawn:/sbin/agetty tty5 38400 linux 69 c6:2345:respawn:/sbin/agetty tty6 38400 linux 70 EOF 71 72 mkdir -p $SYSROOT/etc/rc.d 73 74 cat > $SYSROOT/etc/rc.d/rc.sysinit <<'EOF' 75 #!/bin/sh 76 77 echo "Mounting root device read-only..." 78 /bin/mount -n -o remount,ro / 79 80 /sbin/fsck -A -a -C 81 if [ $? -gt 1 ]; then 82 echo 83 echo "ERROR:" 84 echo "Your filesystem has been severely damaged. Dropping to rescue shell." 85 echo "After repair is complete, the system will reboot." 86 export PS1="(fsck)# " 87 /sbin/sulogin 88 /bin/umount -a -r 89 /sbin/reboot -f 90 fi 91 92 echo "Remounting root device read-write..." 93 /bin/mount -n -o remount,rw / 94 95 echo "Mounting /dev ..." 96 /bin/mount -t devtmpfs none /dev 97 98 echo "Mounting /proc ..." 99 /bin/mount -t proc none /proc 100 101 echo "mounting /sys ..." 102 /bin/mount -t sysfs none /sys 103 104 echo "Mounting other local filesystems..." 105 /bin/mount -a -V -tnonfs 106 107 echo "Setting hostname..." 108 /bin/hostname $(cat /etc/hostname | cut -d . -f1) 109 /bin/domainname $(cat /etc/hostname | cut -d . -f2-) 110 111 echo "Removing stale PID files..." 112 /bin/rm /var/run/*.pid 113 /bin/rm /etc/dhcpc/*.pid 114 115 echo "Loading keymap..." 116 /usr/bin/loadkeys -d 117 118 echo "Setting system time from hardware clock..." 119 /sbin/hwclock --hctosys --utc 120 121 echo "Starting system and kernel log daemons..." 122 /usr/sbin/sysklogd 123 /usr/sbin/klogd -c3 124 125 echo "Updating module dependencies..." 126 /sbin/depmod -a 127 128 echo "Starting daemons..." 129 if [ -x /etc/rc.d/rc.daemons ]; then 130 /etc/rc.d/rc.daemons 131 fi 132 EOF 133 134 # reboot runlevel 135 cat > $SYSROOT/etc/rc.d/rc.0 <<'EOF' 136 #!/bin/sh 137 138 echo "Sending all processes the TERM signal..." 139 /sbin/killall5 -15 140 sleep 1 141 142 echo "Sending all processes the KILL signal..." 143 /sbin/killall5 -9 144 sleep 1 145 146 echo "Saving the system time to hardware clock..." 147 /sbin/hwclock --systohc --utc 148 149 echo "Unmounting remote filesystems..." 150 /bin/umount -a -f -tnfs 151 152 case "$0" in 153 *6) /sbin/reboot -w ;; 154 *0) /sbin/halt -w ;; 155 esac 156 157 echo "Remounting root filesystem read-only..." 158 /bin/mount -n -o remount,ro / 159 160 echo "Flushing filesystem buffers..." 161 /bin/sync 162 163 echo "Umounting local filesystems..." 164 /bin/umount -a -t nonfs 165 166 case "$0" in 167 *6) 168 echo "Rebooting..." 169 /sbin/reboot -d -f -i 170 ;; 171 *0) 172 echo "Shutting down..." 173 /sbin/halt -d -f -p 174 ;; 175 esac 176 EOF 177 178 ln -s rc.0 $SYSROOT/etc/rc.d/rc.6 179 180 # single-user runlevel 181 cat > $SYSROOT/etc/rc.d/rc.1 <<'EOF' 182 #!/bin/sh 183 184 echo "Unmounting remote filesystems..." 185 /bin/umount -a -tnfs 186 187 # TODO: bring down network interfaces 188 #/sbin/ifconfig eth0 down 189 190 echo "Sending all processes the TERM signal..." 191 /sbin/killall5 -15 192 sleep 1 193 194 echo "Sending all processes the KILL signal..." 195 /sbin/killall5 -9 196 EOF 197 198 # multi-user runlevel 199 cat > $SYSROOT/etc/rc.d/rc.2 << 'EOF' 200 #!/bin/sh 201 202 echo "Setting up loopback networking..." 203 /sbin/ifconfig lo 127.0.0.1 204 /sbin/route add -net 127.0.0.0 netmask 255.0.0.0 lo 205 206 echo "Setting up eth0..." 207 /sbin/ifconfig eth0 192.168.0.1 broadcast 192.168.0.255 netmask 255.255.255.0 208 /sbin/route add -net default gw 192.168.0.1 netmask 0.0.0.0 209 210 echo "Mounting remote filesystems..." 211 /bin/mount -a -v -tnfs 212 213 if [ -x /etc/rc.d/rc.local ]; then 214 /etc/rc.d/rc.local 215 fi 216 EOF 217 218 ln -s rc.2 $SYSROOT/etc/rc.d/rc.3 219 ln -s rc.2 $SYSROOT/etc/rc.d/rc.4 220 ln -s rc.2 $SYSROOT/etc/rc.d/rc.5 221 222 cat > $SYSROOT/etc/rc.d/rc.daemons <<'EOF' 223 #!/bin/sh 224 EOF 225 226 cat > $SYSROOT/etc/rc.d/rc.local <<'EOF' 227 #!/bin/sh 228 EOF 229 230 chmod 754 \ 231 $SYSROOT/etc/rc.d/rc.sysinit \ 232 $SYSROOT/etc/rc.d/rc.0 \ 233 $SYSROOT/etc/rc.d/rc.1 \ 234 $SYSROOT/etc/rc.d/rc.2 \ 235 $SYSROOT/etc/rc.d/rc.daemons \ 236 $SYSROOT/etc/rc.d/rc.local 237 238 # fixup sysroot directories 239 touch $SYSROOT/var/log/lastlog 240 chmod 664 $SYSROOT/var/log/lastlog 241 242 # generate default /etc/passwd 243 cat > $SYSROOT/etc/passwd <<EOF 244 root::0:0:root:/root:/bin/sh 245 bin:x:1:1:bin:/bin:/bin/false 246 daemon:x:2:6:daemon:/sbin:/bin/false 247 adm:x:3:16:adm:/var/adm:/bin/false 248 operator:x:50:50:operator:/root:/bin/sh 249 nobody:x:65534:65534:nobody:/:/bin/false 250 EOF 251 252 # generate default /etc/group 253 cat > $SYSROOT/etc/group <<EOF 254 root:x:0: 255 bin:x:1: 256 sys:x:2: 257 kmem:x:3: 258 tty:x:4: 259 tape:x:5: 260 daemon:x:6: 261 floppy:x:7: 262 disk:x:8: 263 lp:x:9: 264 dialout:x:10: 265 audio:x:11: 266 video:x:12: 267 utmp:x:13: 268 usb:x:14: 269 cdrom:x:15: 270 adm:x:16:root,adm,daemon 271 console:x:17: 272 users:x:100: 273 nogroup:x:65533: 274 nobody:x:65534: 275 EOF 276 277 # generate default /etc/fstab 278 cat > $SYSROOT/etc/fstab <<EOF 279 # file-system mount-point type options dump fsck 280 EOF 281 282 # generate default /etc/hostname 283 cat > $SYSROOT/etc/hostname <<EOF 284 clfs 285 EOF 286 287 # generate default /etc/hosts 288 cat > $SYSROOT/etc/hosts <<EOF 289 # /etc/hosts 290 291 127.0.0.1 localhost 292 ::1 localhost 293 EOF 294 295 # generate default /etc/profile 296 cat > $SYSROOT/etc/profile <<'EOF' 297 # /etc/profile 298 299 export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 300 301 export USER=$(/usr/bin/id -un) 302 export LOGNAME=$USER 303 export HOSTNAME=$(/bin/hostname) 304 export HISTSIZE=1000 305 export HISTFILESIZE=1000 306 307 #export PAGER=/bin/less 308 #export EDITOR=/usr/bin/vim 309 EOF 310 311 # build sysroot archive 312 echo "Building $TARGET sysroot tar archive..." 313 ( cd $SYSROOT ; find . | tar -cf $BOOTROOT/$ROOTFS -T - ) 314 315 echo "Built $TARGET userland!"