aboutsummaryrefslogtreecommitdiffstats
path: root/stage2/01-sys-tweaks/01-run.sh
blob: c200ae22a1b14b5e23e030ea65c64f548742a89b (plain)
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
227
228
#!/bin/bash -e

if [ "${ROOTFS_RO}" = "1" ] ; then
    install -v -m 644 files/fstab-rootfs_ro "${ROOTFS_DIR}/etc/fstab"
fi
install -m 644 files/overlay_mount.service          "${ROOTFS_DIR}/lib/systemd/system/"
install -m 755 files/overlay_mount	                "${ROOTFS_DIR}/etc/init.d/"

install -m 644 files/rotatelog_init_rootfs.service	"${ROOTFS_DIR}/lib/systemd/system/"
install -m 755 files/rotatelog_init_rootfs	        "${ROOTFS_DIR}/etc/init.d/"

install -m 755 files/resize2fs_once	"${ROOTFS_DIR}/etc/init.d/"

install -d				"${ROOTFS_DIR}/etc/systemd/system/rc-local.service.d"
install -m 644 files/ttyoutput.conf	"${ROOTFS_DIR}/etc/systemd/system/rc-local.service.d/"

if [ "${TARGET_RASPI}" = "1" ]; then
    install -m 644 files/50raspi		"${ROOTFS_DIR}/etc/apt/apt.conf.d/"
fi

install -m 644 files/console-setup   	"${ROOTFS_DIR}/etc/default/"

install -m 755 files/rc.local		"${ROOTFS_DIR}/etc/"

if [ -n "${PUBKEY_SSH_FIRST_USER}" ]; then
	install -v -m 0700 -o 1000 -g 1000 -d "${ROOTFS_DIR}"/home/"${FIRST_USER_NAME}"/.ssh
	echo "${PUBKEY_SSH_FIRST_USER}" >"${ROOTFS_DIR}"/home/"${FIRST_USER_NAME}"/.ssh/authorized_keys
    if [ -n "${PUBKEY2_SSH_FIRST_USER}" ]; then
        echo "${PUBKEY2_SSH_FIRST_USER}" >>"${ROOTFS_DIR}"/home/"${FIRST_USER_NAME}"/.ssh/authorized_keys
    fi
	chown 1000:1000 "${ROOTFS_DIR}"/home/"${FIRST_USER_NAME}"/.ssh/authorized_keys
	chmod 0600 "${ROOTFS_DIR}"/home/"${FIRST_USER_NAME}"/.ssh/authorized_keys
fi

if [ "${PUBKEY_ONLY_SSH}" = "1" ]; then
	sed -i -Ee 's/^#?[[:blank:]]*PubkeyAuthentication[[:blank:]]*no[[:blank:]]*$/PubkeyAuthentication yes/
s/^#?[[:blank:]]*PasswordAuthentication[[:blank:]]*yes[[:blank:]]*$/PasswordAuthentication no/' "${ROOTFS_DIR}"/etc/ssh/sshd_config
fi

on_chroot << EOF
    if [ "${TARGET_RASPI}" = "1" ]; then
        systemctl disable hwclock.sh
    fi

    systemctl disable rsync
    systemctl mask rsync
    systemctl disable nfs-common
    systemctl mask nfs-common
    systemctl disable rpcbind
    systemctl mask rpcbind

    if [ "${TARGET_RASPI}" != "1" ]; then
        # ensure sysfsutils is enabled at boot
        systemctl is-enabled sysfsutils
    fi

    if [ "${ENABLE_SSH}" == "1" ]; then
        systemctl enable ssh
    else
        systemctl disable ssh
    fi
    if [ "${ROOTFS_RO}" = "1" ] ; then
        systemctl disable regenerate_ssh_host_keys
        systemctl mask regenerate_ssh_host_keys
    else
        systemctl enable regenerate_ssh_host_keys
    fi

    if [ "${ROOTFS_RO}" = "1" ] ; then
        sed -i -e 's/^D \/tmp/#D \/tmp/g' /usr/lib/tmpfiles.d/tmp.conf

        systemctl unmask overlay_mount
        systemctl enable overlay_mount

        systemctl unmask rotatelog_init_rootfs
        systemctl enable rotatelog_init_rootfs
    else
        systemctl disable overlay_mount
        systemctl mask    overlay_mount

        systemctl disable rotatelog_init_rootfs
        systemctl mask    rotatelog_init_rootfs
    fi

    # Setup unique system folder /boot/sys_arm64_000
    mkdir -p /boot/sys_${TARGET_ARCH}_000

    find /boot -maxdepth 1 -name \*.dtb -exec mv \{\} /boot/sys_${TARGET_ARCH}_000/ \;

    find /boot -maxdepth 1 -name kernel\*.img -exec mv \{\} /boot/sys_${TARGET_ARCH}_000/ \;

    for i in /boot/COPYING.linux /boot/LICENCE.broadcom \
             /boot/issue.txt /boot/rootfs.img ; do
        if [ -f "\${i}" ]; then
             mv "\${i}" /boot/sys_${TARGET_ARCH}_000/
        fi
    done
    
    if [ -d /boot/overlays ] ; then
        mv /boot/overlays /boot/sys_${TARGET_ARCH}_000/
    fi

    for i in /boot/config.txt /boot/cmdline.txt /boot/initrd.img ; do
        if [ -f "\${i}" ]; then
             mv "\${i}" /boot/sys_${TARGET_ARCH}_000/\`basename \${i}\`.orig
        fi
    done

    if [ "${ROOTFS_RO}" = "1" ] ; then
        systemctl disable resize2fs_once
        systemctl mask resize2fs_once

        systemctl disable apt-daily
        systemctl mask apt-daily
        systemctl disable apt-daily.timer
        systemctl mask apt-daily.timer
        systemctl disable apt-daily-upgrade.timer
        systemctl mask apt-daily-upgrade.timer

        systemctl disable logrotate.timer
        systemctl mask logrotate.time
        systemctl disable man-db.timer
        systemctl mask man-db.timer

        sed -i -e 's/#Storage=auto/Storage=volatile/g;s/#Compress=yes/Compress=yes/g;s/#RuntimeMaxUse=/RuntimeMaxUse=1M/g;s/#ForwardToSyslog=yes/ForwardToSyslog=no/g;s/#ForwardToKMsg=no/ForwardToKMsg=no/g;s/#ForwardToConsole=no/ForwardToConsole=no/g;s/#ForwardToWall=yes/ForwardToWall=no/g;s/#TTYPath=\/dev\/console/TTYPath=\/dev\/tty3/g' /etc/systemd/journald.conf

        if [ "${TARGET_RASPI}" = "1" ]; then
            sed -i -e 's/MODULES=most/MODULES=dep/g;s/BUSYBOX=auto/BUSYBOX=y/g;s/COMPRESS=gzip/COMPRESS=lzop/g' /etc/initramfs-tools/initramfs.conf
        else
            sed -i -e 's/BUSYBOX=auto/BUSYBOX=y/g;s/COMPRESS=gzip/COMPRESS=lzop/g' /etc/initramfs-tools/initramfs.conf
        fi

        if [ "${TARGET_RASPI}" = "1" ]; then
            KMODULES="squashfs"
        else
            KMODULES="nls_cp437 nls_ascii fat vfat squashfs loop usb-common usbcore libahci ahci libata scsi_mod sd_mod usb-storage"
        fi
        for i in \${KMODULES} ; do
            echo "\$i" >> /etc/modules
            echo "\$i" >> /etc/initramfs-tools/modules
        done
    else
        systemctl enable resize2fs_once
    fi
EOF

if [ "${ROOTFS_RO}" = "1" ] ; then
    if [ "${TARGET_RASPI}" = "1" ]; then
        install -m 644 files/boot/config-rootfs_ro.txt 	     "${ROOTFS_DIR}/boot/config.txt"
        install -m 644 files/boot/config-rootfs_ro.txt 	     "${ROOTFS_DIR}/boot/sys_arm64_000/config.txt"
        install -m 644 files/boot/sys_arm64_000/cmdline-rootfs_ro.txt  "${ROOTFS_DIR}/boot/sys_arm64_000/cmdline.txt"
    else
        install -m 644 files/grub/custom.cfg                 "${ROOTFS_DIR}/boot/grub/custom.cfg"
        sed -i 's/sys_arm64_000/sys_${TARGET_ARCH}_000/g'    "${ROOTFS_DIR}/boot/grub/custom.cfg"
        cp "${ROOTFS_DIR}/boot/grub/custom.cfg"              "${ROOTFS_DIR}/boot/sys_${TARGET_ARCH}_000/"
        sed -i 's/GRUB_DEFAULT=0/GRUB_DEFAULT=loop_rootfs/g' "${ROOTFS_DIR}/etc/default/grub"
        #sed -i 's/GRUB_TIMEOUT=.*$/GRUB_TIMEOUT=0/g'         "${ROOTFS_DIR}/etc/default/grub"
    fi

    install -m 755 files/initramfs/loop_rootfs-premount 	"${ROOTFS_DIR}/etc/initramfs-tools/scripts/init-premount/loop_rootfs"
    sed -i 's/sys_arm64_000/sys_${TARGET_ARCH}_000/g' "${ROOTFS_DIR}/etc/initramfs-tools/scripts/init-premount/loop_rootfs"
    if [ "${TARGET_RASPI}" != "1" ]; then
        sed -i 's/USES_RASPI_CONFIG=1/USES_RASPI_CONFIG=0/g' "${ROOTFS_DIR}/etc/initramfs-tools/scripts/init-premount/loop_rootfs"
    fi
    install -m 755 files/initramfs/loop_rootfs-bottom 	"${ROOTFS_DIR}/etc/initramfs-tools/scripts/init-bottom/loop_rootfs"
    install -m 755 files/initramfs/fsck_custom 	"${ROOTFS_DIR}/etc/initramfs-tools/hooks/"
    install -m 755 files/initramfs/extra_execs  "${ROOTFS_DIR}/etc/initramfs-tools/hooks/"
else
    if [ "${TARGET_RASPI}" = "1" ]; then
        install -m 644 files/boot/config-rootfs_rw.txt 	     "${ROOTFS_DIR}/boot/config.txt"
        install -m 644 files/boot/config-rootfs_rw.txt 	     "${ROOTFS_DIR}/boot/sys_arm64_000/config.txt"
        install -m 644 files/boot/sys_arm64_000/cmdline-rootfs_rw.txt  "${ROOTFS_DIR}/boot/sys_arm64_000/cmdline.txt"
    fi
fi

if [ "${USE_QEMU}" = "1" ]; then
	echo "enter QEMU mode"
	install -m 644 files/90-qemu.rules "${ROOTFS_DIR}/etc/udev/rules.d/"
	echo "leaving QEMU mode"
fi

on_chroot <<EOF
    for GRP in input spi i2c gpio; do
        groupadd -f -r "\$GRP"
    done
    for GRP in adm dialout cdrom audio users sudo video games plugdev input gpio spi i2c netdev; do
      adduser $FIRST_USER_NAME \$GRP
    done

    setupcon --force --save-only -v
    usermod --pass='*' root

    if [ "${ROOTFS_RO}" = "1" ]; then
        KVERSION=\$(ls /lib/modules/ | tail -n 1)
        if [ "${TARGET_RASPI}" = "1" ]; then
            echo "mkinitramfs for kernel version: \${KVERSION}"
            /usr/sbin/mkinitramfs -o /boot/sys_${TARGET_ARCH}_000/initrd.img \${KVERSION}
        else
            update-initramfs -u -k \${KVERSION}
            if [ -f "/boot/vmlinuz-\${KVERSION}" ] ; then
                mv -f "/boot/vmlinuz-\${KVERSION}" /boot/sys_${TARGET_ARCH}_000/vmlinuz
            fi
            if [ -f "/boot/initrd.img-\${KVERSION}" ] ; then
                mv -f "/boot/initrd.img-\${KVERSION}" /boot/sys_${TARGET_ARCH}_000/initrd.img
            fi
            if [ -f "/boot/config-\${KVERSION}" ] ; then
                mv -f "/boot/config-\${KVERSION}" /boot/sys_${TARGET_ARCH}_000/config
            fi
            if [ -f "/boot/System.map-\${KVERSION}" ] ; then
                mv -f "/boot/System.map-\${KVERSION}" /boot/sys_${TARGET_ARCH}_000/System.map
            fi
        fi

        mkdir -p /data/sdcard
        find  /boot/ -maxdepth 1 -type f \
              -exec cp -d --preserve=all \{\} /data/sdcard/ \;
        cp -a /boot/sys_${TARGET_ARCH}_000 /data/sdcard/
    fi
    if [ "${TARGET_RASPI}" != "1" ]; then
        update-grub
        grub-install ${NBD_DEV}
    fi
EOF

if [ "${ROOTFS_RO}" != "1" ] ; then
    rm -f "${ROOTFS_DIR}/etc/ssh/"ssh_host_*_key*
fi