summaryrefslogtreecommitdiff
path: root/adelie-build-cd
blob: 396ea64a620f12f13511f2c4e708ba0796b0495a (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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/bin/sh

def_arch=$(uname -m)
def_ver="1.0-alpha3"
declare -r PROGNAME=$(basename $0)


warn() {
    printf '>>> '
    printf '\033[01;33mWARNING\033[00;39m '
    printf '>>> '
    printf '\033[01;33mWARNING\033[00;39m '
    printf '>>> '
    printf '\033[01;33mWARNING\033[00;39m '
    printf '>>> '
    printf '\033[01;33mWARNING\033[00;39m '
    printf '>>>\n'
}


fatal() {
    printf '>>> '
    printf '\033[01;31mERROR\033[00;39m '
    printf '>>> '
    printf '\033[01;31mERROR\033[00;39m '
    printf '>>> '
    printf '\033[01;31mERROR\033[00;39m '
    printf '>>> '
    printf '\033[01;31mERROR\033[00;39m '
    printf '>>>\n'
}


ensure_commands() {
    if ! type apk>/dev/null; then
        fatal
        printf 'You must have apk installed.  On Gentoo, see sys-devel/apk-tools.\n'
        exit -1
    fi

    if ! type cpio>/dev/null; then
        fatal
        printf 'You must have cpio installed.  On Gentoo, see app-arch/cpio.\n'
        exit -1
    fi

    if ! type mksquashfs>/dev/null; then
        fatal
        printf 'You must have mksquashfs installed.  On Gentoo, see sys-fs/squashfs-tools.\n'
        exit -1
    fi

    if ! type mkisofs>/dev/null; then
        fatal
        printf 'You must have mkisofs installed.  Try cdrtools or cdrkit.\n'
        exit -1
    fi
}


usage() {
    printf 'usage: %s [-a ARCH] [-f|--full] [-p|--phase PHASE] [--help]\n\n' $PROGNAME
    printf 'Create an Adélie Linux CD image (.ISO) using the specified parameters.\n'
    printf 'Default ARCH: %s\n' $def_arch
    printf 'Valid phases: clean install initrd iso all\n'
}


while [ -n "$1" ]; do
    case $1 in
        -a | --arch)
            shift
            declare -r MY_ARCH=$1
            ;;
        -h | --help)
            usage
            exit
            ;;
	-f | --full)
	    shift
	    declare -r DO_FULL=full
	    ;;
	-p | --phase)
	    shift
	    declare -r MY_PHASE=$1
	    ;;
	-v | --version)
	    shift
	    declare -r MY_VER=$1
	    ;;
        *)
            usage >&2
            exit -1
            ;;
    esac
    shift
done

set -a
declare -r ARCH=${MY_ARCH:-$def_arch}
declare -r LDARCH=${LDARCH:-$ARCH}
declare -r PHASE=${MY_PHASE:-all}
declare -r VERSION=${MY_VER:-$def_ver}
set +a

ensure_commands

header() {
	printf '\033[01;32m * \033[37m%s\033[00;39m\n' "$1"
}

header 'Adélie Linux CD Creation Tool'
printf '\n'

clean_dirs() {
	warn
	printf 'This will erase all files at the directories %s/cdroot-%s\n' `pwd` $ARCH
	printf '%s/initrd-%s and %s/squashroot-%s.\n\n' `pwd` $ARCH `pwd` $ARCH
	printf 'When you are ready, press RETURN.  To cancel, press Ctrl-C.\n'
	read

	rm -rf cdroot-$ARCH
	rm -rf initrd-$ARCH
	rm -rf squashroot-$ARCH
	mkdir cdroot-$ARCH
	mkdir initrd-$ARCH
	mkdir squashroot-$ARCH
}

install_pkgs() {
	header "Installing base system to squash root..."

	declare -r PACKAGES=$(cat packages 2>/dev/null || fatal 'No core packages specified')
	declare -r ARCH_PKGS=$(cat packages-$ARCH 2>/dev/null || echo '')

	mkdir -p squashroot-$ARCH/etc/apk/keys
	cp 'packages@adelielinux.org.pub' squashroot-$ARCH/etc/apk/keys/
	apk --arch $ARCH -X "https://distfiles.adelielinux.org/adelie/$VERSION/system/$EXTRA_MIRROR" -U --root squashroot-$ARCH --initdb add $PACKAGES $ARCH_PKGS
}

make_structure() {
	mkdir -p squashroot-$ARCH/home/live
	mkdir squashroot-$ARCH/target
	mkdir -p squashroot-$ARCH/media/live
	ln -s /bin/bash squashroot-$ARCH/bin/sh

	echo 'hostname="adelie"' > squashroot-$ARCH/etc/conf.d/hostname
	echo 'mtab_is_file=no' > squashroot-$ARCH/etc/conf.d/mtab

	cp -RPp squashroot-$ARCH/usr/share/openrc/runlevels squashroot-$ARCH/etc/runlevels
	ln -s /etc/init.d/udev squashroot-$ARCH/etc/runlevels/sysinit/udev
	ln -s /etc/init.d/udev-trigger squashroot-$ARCH/etc/runlevels/sysinit/udev-trigger
	ln -s /etc/init.d/lvmetad squashroot-$ARCH/etc/runlevels/sysinit/lvmetad

	cat >squashroot-$ARCH/etc/fstab <<- FSTAB
		# Welcome to Adélie Linux.
		# This fstab(5) is for the live media only.  Do not edit or use for your installation.

		tmpfs	/tmp		tmpfs	defaults	0	1
		tmpfs	/var/log	tmpfs	size=8m		0	1
		tmpfs	/root		tmpfs	size=16m	0	1
		tmpfs	/home/live	tmpfs	size=16m	0	1
		proc	/proc		proc	defaults	0	1
	FSTAB

	cp passwd-$ARCH squashroot-$ARCH/etc/passwd
	cp group-$ARCH squashroot-$ARCH/etc/group
	cp shadow-$ARCH squashroot-$ARCH/etc/shadow

	cat >squashroot-$ARCH/etc/shells <<- SHELLS
		/bin/bash
		/bin/zsh
	SHELLS

	cat >squashroot-$ARCH/etc/resolv.conf <<- RESOLVE
		nameserver 84.200.69.80
		nameserver 2001:1608:10:25::1c04:b12f
	RESOLVE

	cat >squashroot-$ARCH/etc/apk/repositories <<-REPOS
		https://distfiles.adelielinux.org/adelie/$VERSION/system/$EXTRA_MIRROR
		https://distfiles.adelielinux.org/adelie/$VERSION/user/$EXTRA_MIRROR
	REPOS

	cat >squashroot-$ARCH/etc/issue <<-ISSUE
		Welcome to Adélie Linux!
		You may log in as 'root' to install, or 'live' to play around.

		Have fun.

	ISSUE

	chmod 600 squashroot-$ARCH/etc/shadow

	if test -n "${DO_FULL+full}"; then
		declare -r PACKAGES_DIR=squashroot-$ARCH/packages/$ARCH
		mkdir -p $PACKAGES_DIR
		apk --arch $ARCH --root squashroot-$ARCH fetch -o $PACKAGES_DIR $(apk --root squashroot-$ARCH info)
		if test -n "${SIGNING_KEY+use_key}"; then
			apk index -o .tmp.APKINDEX.unsigned.tar.gz $PACKAGES_DIR/*.apk
			openssl dgst -sha256 -sign $SIGNING_KEY -out .SIGN.RSA.packages\@adelielinux.org.pub .tmp.APKINDEX.unsigned.tar.gz
			tar cf .tmp.signature.tar .SIGN.RSA.packages\@adelielinux.org.pub
			cat .tmp.signature.tar | abuild-tar --cut | gzip -9 > .tmp.signature.tar.gz
			cat .tmp.signature.tar.gz .tmp.APKINDEX.unsigned.tar.gz > .tmp.APKINDEX.tar.gz
			rm .tmp.APKINDEX.unsigned.tar.gz .tmp.signature.tar.gz .tmp.signature.tar .SIGN.RSA.packages\@adelielinux.org.pub
			mv .tmp.APKINDEX.tar.gz $PACKAGES_DIR/APKINDEX.tar.gz
		fi
	fi

	cp AdelieTux.icns cdroot-$ARCH/.VolumeIcon.icns
}

squash_it() {
	header 'Creating compressed file system image...'

	mksquashfs squashroot-$ARCH cdroot-$ARCH/adelie.squashfs -noappend
}

make_initrd() {
	header 'Creating initrd structure...'

	# mount points
	mkdir initrd-$ARCH/dev
	mkdir initrd-$ARCH/media
	mkdir initrd-$ARCH/newroot
	mkdir initrd-$ARCH/proc
	mkdir initrd-$ARCH/sys

	# manual /dev nodes for initial udev startup
	mknod -m 600 initrd-$ARCH/dev/console c 5 1
	mknod -m 666 initrd-$ARCH/dev/null c 1 3
	mknod -m 666 initrd-$ARCH/dev/ptmx c 5 2
	mknod -m 666 initrd-$ARCH/dev/random c 1 8
	mknod -m 666 initrd-$ARCH/dev/tty c 5 0
	mknod -m 620 initrd-$ARCH/dev/tty1 c 4 1
	mknod -m 666 initrd-$ARCH/dev/urandom c 1 9
	mknod -m 666 initrd-$ARCH/dev/zero c 1 5

	# base
	mkdir initrd-$ARCH/lib
	cp squashroot-$ARCH/usr/lib/libc.so initrd-$ARCH/lib/ld-musl-$LDARCH.so.1
	cp squashroot-$ARCH/lib/libblkid.so.1 initrd-$ARCH/lib/
	cp squashroot-$ARCH/lib/libuuid.so.1 initrd-$ARCH/lib/

	# udev
	mkdir -p initrd-$ARCH/etc/udev
	mkdir initrd-$ARCH/run
	mkdir initrd-$ARCH/sbin
	cp squashroot-$ARCH/bin/udevadm initrd-$ARCH/sbin/
	cp squashroot-$ARCH/sbin/udevd initrd-$ARCH/sbin/
	cp squashroot-$ARCH/lib/libkmod.so.2 initrd-$ARCH/lib/
	cp squashroot-$ARCH/lib/libudev.so.1 initrd-$ARCH/lib/
	cp squashroot-$ARCH/etc/udev/hwdb.bin initrd-$ARCH/etc/udev/

	# init
	cp cdinit-$ARCH initrd-$ARCH/init

	header 'Compressing initrd...'

	pushd initrd-$ARCH
	find . | cpio -H newc -o > ../cdroot-$ARCH/initrd
	popd
	gzip -9 cdroot-$ARCH/initrd
	mv cdroot-$ARCH/initrd.gz cdroot-$ARCH/initrd
}

prepare_cdroot() {
	header 'Adding kernel...'

	cp squashroot-$ARCH/boot/vmlinu* cdroot-$ARCH/bzImage

	if test -f post-$ARCH.sh; then
		header 'Running architecture-specific scripts...'
		sh post-$ARCH.sh
	fi
}

create_iso() {
	header 'Creating the CD...'

	declare -r CD_PARAMS=$(cat iso-params-$ARCH)
	mkisofs -o adelie-${DO_FULL:-live}-$ARCH-$VERSION-$(date +%Y%m%d).iso -joliet -rational-rock -V "Adelie $VERSION $ARCH" ${CD_PARAMS} cdroot-$ARCH
}

case $PHASE in
	clean)
		clean_dirs
		;;
	install)
		install_pkgs
		make_structure
		;;
	initrd)
		make_initrd
		;;
	iso)
		squash_it
		prepare_cdroot
		create_iso
		;;
	all)
		clean_dirs
		install_pkgs
		make_structure
		make_initrd
		squash_it
		prepare_cdroot
		create_iso
		;;
	*)
		fatal
		printf 'Unknown phase %s.  Stop.\n' $PHASE
		;;
esac