summaryrefslogtreecommitdiffstats
path: root/build.sh
blob: a81264a72e9e1593140785d2a663cd42e19f4728 (plain) (blame)
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
steps="system packages user config ufs font uzip ramdisk boot image"
pwdir="$(realpath "$(dirname "$0")")"

system () {
    install -o root -g wheel -m 755 -d "${root}"
    cd "${src}" || return 1
    make installworld  DESTDIR="${root}" || return 1
    make installkernel DESTDIR="${root}" || return 1
    make distribution  DESTDIR="${root}" || return 1
}

packages () {
    sed -i.bak 's|yes|no|' "${root}/etc/pkg/FreeBSD.conf"
    cat << EOF > "${root}/etc/pkg/packages.conf"
packages: {
   url: "file:///packages",
   enabled: yes
}
EOF
    install -d "${root}/packages"
    mount_nullfs "${repo}" "${root}/packages"
    pkg -c "${root}" install -fy ${pkgs} || {
        umount "${root}/packages"
        return 1
    }
    rm -rf "${root}/var/cache/pkg/All"
    umount "${root}/packages"
    rm -f "${root}/etc/pkg/packages.conf"
    mv -f "${root}/etc/pkg/FreeBSD.conf.bak" "${root}/etc/pkg/FreeBSD.conf"
}

user () {
    install -o root -g wheel -m 755 -d "${root}/home"
    cat << EOF > "${root}/users"
#!/bin/sh
set -x
for user in liveuser; do
    pw add user "\$user" -G wheel -s /bin/tcsh -M 755
    chpass -p '' "\$user"
    install -o "\$user" -g "\$user" -m 755 -d "/home/\$user"
    echo 'limit coredumpsize 0' > "/home/\$user/.login"
    chown "\$user:\$user" "/home/\$user/.login"
done
EOF
    chmod 755 "${root}/users"
    chroot "${root}" "/users"
    rm -f "${root}/users"
}

config () {
    cd "${pwdir}"
    install -o root -g wheel -m 644 "rc.conf" "${root}/etc/"
    cat << EOF > "${root}/etc/fstab"
fdesc   /dev/fd         fdescfs         rw      0       0
EOF
    cat << EOF > "${root}/etc/sysctl.conf"
kern.coredump=0
EOF
}

ufs () {
    install -o root -g wheel -m 755 -d "${cdroot}"
    mkdir "${cdroot}/data"
    makefs "${cdroot}/data/system.ufs" "${root}"
}

font () {
    md_unit="$(mdconfig -a -t vnode -f "${cdroot}/data/system.ufs")"
    mount "/dev/${md_unit}" "${root}"
    rm -rf "${root}/var/db/fontconfig"
    chroot "${root}" fc-cache -fsv
    umount "${root}"
    mdconfig -d -u "${md_unit}"
}

uzip () {
    mkuzip -o "${cdroot}/data/system.uzip" "${cdroot}/data/system.ufs"
    rm -f "${cdroot}/data/system.ufs"
}

ramdisk () {
    ramdisk_root="${cdroot}/data/ramdisk"
    mkdir -p "${ramdisk_root}"
    cd "${root}"
    tar -cf - rescue | tar -xf - -C "${ramdisk_root}"
    cd "${pwdir}"
    install -o root -g wheel -m 755 "init.sh.in" "${ramdisk_root}/init.sh"
    sed "s/@VOLUME@/${vol}/" "init.sh.in" > "${ramdisk_root}/init.sh"
    mkdir "${ramdisk_root}/dev"
    mkdir "${ramdisk_root}/etc"
    touch "${ramdisk_root}/etc/fstab"
    makefs -b '10%' "${cdroot}/data/ramdisk.ufs" "${ramdisk_root}"
    gzip "${cdroot}/data/ramdisk.ufs"
    rm -rf "${ramdisk_root}"
}

boot () {
    cd "${root}"
    tar -cf - --exclude boot/kernel boot | tar -xf - -C "${cdroot}"
    for kfile in kernel geom_uzip.ko nullfs.ko tmpfs.ko unionfs.ko; do
        tar -cf - boot/kernel/${kfile} | tar -xf - -C "${cdroot}"
    done
    cd "${pwdir}"
    install -o root -g wheel -m 644 "loader.conf" "${cdroot}/boot/"
}

image () {
    cd "${cdroot}"
    mkisofs -iso-level 4 -R -l -ldots -allow-lowercase -allow-multidot -V \
        "${vol}" -o "${image}" -no-emul-boot -b boot/cdboot .
}


if [ "$(id -u)" '!=' '0' ]; then
    echo 'Sorry, you are not root ...'
    exit 1
fi

# This check cannot be done reliably, but it should be able to catch the
# most common mistake.
if [ "$(basename "$0")" = 'build.sh' ] && [ "${pwdir}" = "$(realpath "$(pwd)")" ]; then
    echo 'This script cannot be run directly.'
    echo 'Please create a wrapper script that sources this file instead.'
    echo 'You can use build-release.sh and build-debug.sh as examples.'
    exit 1
fi

# Check whether required variables are defined.
assert_dir_or_file () {
    if [ -z "$2" ] || [ "$(realpath "$2" 2>/dev/null)" = '/' ]; then
        echo "Variable '$1' is unset or set to the root directory."
        exit 1
    fi
}
assert_has_pkg () {
    if [ -z "$2" ]; then
        echo "Variable '$1' is empty."
        echo "If no package have to be installed, set it to 'pkg'."
        exit 1
    fi
}
assert_not_empty () {
    if [ -z "$2" ]; then
        echo "Variable '$1' is empty."
        exit 1
    fi
}
assert_dir_or_file src "${src}"
assert_dir_or_file root "${root}"
assert_dir_or_file cdroot "${cdroot}"
assert_dir_or_file image "${image}"
assert_dir_or_file repo "${repo}"
assert_has_pkg pkgs "${pkgs}"
assert_not_empty vol "${vol}"
unset assert_dir_or_file
unset assert_has_pkg
unset assert_not_empty

# Check whether the volume name is too long
if [ "${#vol}" -gt "31" ]; then
    echo "Variable 'vol' is too long. The length must not exceed 31 bytes."
    exit 1
fi

printf 'All steps: \033[1;33m%s\033[m\n' "${steps}"
printf '(E)dit or (R)un? '
read -r edit_or_run

case "${edit_or_run}" in
    E*|e*)
        printf 'New steps: '
        read -r steps
        ;;
    R*|r*)
        ;;
    *)
        echo 'Unknown action ...'
        exit 1
        ;;
esac

for task in ${steps}; do
    if type "${task}" >/dev/null 2>/dev/null; then :; else
        printf '==> Task \033[1;31m%s\033[m not found\n' "${task}"
        exit 1
    fi
done

for task in ${steps}; do
    printf '==> Running task \033[1;33m%s\033[m\n' "${task}"
    set -x
    if "${task}"; then
        set +x
        printf '=> Task \033[1;32m%s\033[m OK\n' "${task}"
    else
        set +x
        printf '=> Task \033[1;31m%s\033[m failed\n' "${task}"
        exit 1
    fi
done