summaryrefslogtreecommitdiff
path: root/buildlive
blob: 3de3b03616d582f25d90197aaee56c4c3b1d9ad0 (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
#!/bin/bash
# Copyright (C)  2016-2018 Iru Cai <mytbk920423@gmail.com>
# SPDX-License-Identifier: GPL-3.0-or-later

set -e

DISTROLIST=()
ISOLIST=()
BOOTPATH=
ROOTPATH=
DEVNAME=
KERNELDIR=
DATADIR=
INSTALL_GRUB=1
GRUBCFG_ONLY=0
DOWNLOAD_ONLY=0

. functions.sh

msg() {
	echo -e "$1" >&2
}

fatalerror() {
	msg "\x1b[1;31m$1\x1b[0m"
	exit 1
}

usage() {
	>&2 cat << EOF
$0 [--root <path>] [options] [distro 1] [distro 2] ...
use $0 -L to list available distros
options:
  --root <path>
  --root=<path>: set the path to put the live USB data files
  --boot <path>
  --boot=<path>: set the path to put the kernel and loader files
  --no-grub: do not install GRUB loader (still generate grub.cfg)
  --grubcfg: do not do real install, only generate grub.cfg file
  --downloadonly: only download the ISO files
  --dev=<diskdev>: write boot sector to <diskdev> (default <diskdev> is autodetected from <rootpath>)
EOF
}

unset ISOPATH
CFGFILE="$HOME/.liveusb-builder"
test -f "$CFGFILE" && source "$CFGFILE" || true
ISOPATH=${ISOPATH:-"$HOME/isofiles"}

while [[ -n "$1" ]]
do
	case "$1" in
		-L)
			exec ./listisos.sh
			;;
		--boot=*)
			BOOTPATH="${1/--boot=}"
			;;
		--boot)
			shift
			BOOTPATH="$1"
			;;
		--root=*)
			ROOTPATH="${1/--root=}"
			;;
		--root)
			shift
			ROOTPATH="$1"
			;;
		--dev=*)
			DEVNAME=${1/--dev=}
			;;
		--no-grub)
			INSTALL_GRUB=0
			;;
		--grubcfg)
			GRUBCFG_ONLY=1
			;;
		--downloadonly)
			DOWNLOAD_ONLY=1
			;;
		*=*|-*)
			usage
			exit 1
			;;
		*)
			if [ -f "distro/$1/isoinfo" ]
			then
				DISTROLIST=(${DISTROLIST[@]} "$1")
			else
				fatalerror "directory distro/$1/isoinfo not found"
			fi
			;;
	esac
	shift
done

if [[ -z "$ROOTPATH" && "$DOWNLOAD_ONLY" == 0 ]]; then
	usage
	exit 1
fi

if [[ -z "$BOOTPATH" ]]; then
	BOOTPATH="$ROOTPATH"
fi

if [[ "$DOWNLOAD_ONLY" == 0 ]]; then
	UUID="$(findmnt -o UUID --raw --noheadings $ROOTPATH \
		|| fatalerror "UUID of $ROOTPATH not found, not a mountpoint?")"
	BOOTPART="$(findmnt -o SOURCE --raw --noheadings $BOOTPATH \
		|| fatalerror "$BOOTPATH seems not a mountpoint!")"
	# liveusb kernel and data should be placed on one disk
	# so get DEVNAME by the mountpoint of ROOTPATH is ok
	DEVNAME="/dev/$(getdiskbypart "/dev/disk/by-uuid/$UUID")"

	KERNELDIR="$BOOTPATH/liveusb-kernel"
	DATADIR="$ROOTPATH/liveusb-data"
	GRUBCFG="$BOOTPATH/grub/grub.cfg"
	SYSLINUXCFG="$BOOTPATH/syslinux/syslinux.cfg"
	install -d "$BOOTPATH/grub" "$BOOTPATH/syslinux"
	install -d "$KERNELDIR" "$DATADIR"
	msg "Files will be copy to $ROOTPATH"
fi

if [[ "${#DISTROLIST[@]}" == 0 ]]; then
	usage
	exit 1
fi

if [ ! -z "$DEVNAME" ]; then
	msg "Boot sector will be written to $DEVNAME"
fi

for i in ${DISTROLIST[@]}
do
	process_isoinfo "$i"
	ISOLIST=("${ISOLIST[@]}" "$ISOFILE")
	ISONAMELIST=("${ISONAMELIST[@]}" "$ISONAME")

	if [ -f "$ISOPATH/$ISOFILE" ] && checksum_verify; then
		true
	else
		download_iso
	fi
done

if [[ "$DOWNLOAD_ONLY" == 1 ]]; then
	exit 0
fi

grubcfg_header > "$GRUBCFG"
syslinux_header > "$SYSLINUXCFG"

for i in `seq 1 ${#DISTROLIST[@]}`
do
	ISOFILE="${ISOLIST[$i-1]}"
	ISONAME="${ISONAMELIST[$i-1]}"
	DISTRO="${DISTROLIST[$i-1]}"
	process_distro "$DISTRO"

	if [ "$GRUBCFG_ONLY" == 0 ]; then
		install_live
	fi
	gen_grubcfg "$DISTRO" >> "$GRUBCFG"
	gen_syslinux "$DISTRO" >> "$SYSLINUXCFG"
done

if [ "$GRUBCFG_ONLY" == 1 ]; then
	msg 'Only generate grub.cfg and syslinux.cfg, will not install bootloader.'
elif [ "$INSTALL_GRUB" == 1 ]; then
	read -p 'Answer Y to install bootloader.[Y/n] '
	if [[ "$REPLY" =~ ^[Yy]|^$ ]]; then
		if ! as-root ./install_syslinux "$DEVNAME" "$BOOTPART" "$BOOTPATH"; then
			msg 'Failed to install syslinux, try to install GRUB as legacy BIOS bootloader.'
			as-root grub-install --force --boot-directory="$BOOTPATH" --target=i386-pc "$DEVNAME"
		fi
		as-root grub-install --boot-directory="$BOOTPATH" --efi-directory="$BOOTPATH" --bootloader-id=grub --target=x86_64-efi --removable --no-nvram "$DEVNAME"
	fi
fi