-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediagen
executable file
·220 lines (176 loc) · 4.42 KB
/
mediagen
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
#!/usr/bin/env bash
#
# Copyright (C) 2018 Christopher Obbard <[email protected]>
#
#
# command-line arguments
PDK_WORKSPACE="$1"
PDK_COMPONENT="$2"
# full path to the component meta xml file
PDK_COMPONENT_FILE="$PDK_WORKSPACE/$PDK_COMPONENT"
# full path to the apt repo
PDK_REPO="$PDK_WORKSPACE/repo"
# full path to build env
BUILDENV="$PDK_WORKSPACE/tmp"
# temp image file
IMAGE="$BUILDENV/tmp.img"
# full path to the rootfs
ROOTFS="$BUILDENV/rootfs"
# full path to the mediagen script
SCRIPTPATH="$(cd "$(dirname "$0")"; pwd -P)"
# include mediagen functions
. $SCRIPTPATH/functions.sh
# check root is running the script
if [ $EUID -ne 0 ]; then
error "you must run as root"
exit 1
fi
# check workspace exists
if [ ! -d $PDK_WORKSPACE ]; then
error "cannot find PDK workspace"
exit 1
fi
# check component file exists
if [ ! -f $PDK_COMPONENT_FILE ]; then
error "cannot find PDK component"
exit 1
fi
# check repo has been created
if [ ! -d $PDK_REPO ]; then
error "cannot find repo! please run repogen first"
exit 1
fi
# get configuration options
# TODO: sanitize options better
RECIPE=$(read_meta "mediagen.recipe")
if [ -z "$RECIPE" ]; then
error "mediagen.recipe not found"
exit 1
fi
ARCH=$(read_meta "mediagen.debarch")
if [ -z "$ARCH" ]; then
error "mediagen.debarch not found"
exit 1
fi
ROOTSIZE=$(read_meta "mediagen.rootsize")
if [ -z "$ROOTSIZE" ]; then
error "mediagen.rootsize not found"
exit 1
fi
BOOTSIZE=$(read_meta "mediagen.bootsize")
if [ -z "$BOOTSIZE" ]; then
error "mediagen.bootsize not found"
exit 1
fi
OUTFILE=$(read_meta "mediagen.outfile")
if [ -z "$OUTFILE" ]; then
error "mediagen.outfile not found"
exit 1
fi
# TODO: move these config options elsewhere?
HOSTNAME=$(read_meta "mediagen.hostname")
if [ -z "$HOSTNAME" ]; then
info "mediagen.hostname not found; defaulting to 'pdk'"
HOSTNAME="pdk"
fi
ROOT_PASSWORD=$(read_meta "mediagen.root-password")
if [ -z "$ROOT_PASSWORD" ]; then
info "mediagen.root-password not found; defaulting to 'toor'"
ROOT_PASSWORD="toor"
fi
PRESEED=$(read_meta "mediagen.preseed-conf")
if [ -z "$PRESEED" ]; then
info "mediagen.preseed-conf not found; no preseed file selected"
fi
POSTINST=$(read_meta "mediagen.postinst-script")
if [ -z "$POSTINST" ]; then
info "mediagen.postinst-script not found; no postinst script selected"
fi
REPO_URL=$(read_meta "mediagen.repo-url")
if [ -z "$REPO_URL" ]; then
info "mediagen.repo-url not found; set this to the apt repository url to use in target installation"
fi
# get the codename
# TODO: read codename directly from pdk
CODENAME=$(ls "$PDK_REPO/dists" | head -1)
if [ -z "$CODENAME" ]; then
error "codename not found"
exit 1
fi
# purge old build env
rm -rf $IMAGE
rm -rf $BUILDENV
mkdir -p $BUILDENV
# create blank image $ROOTSIZE blocks long
info "creating blank image"
dd if=/dev/zero of=$IMAGE bs=1MiB count=$ROOTSIZE >& /dev/null
# create partition table
#
# first 2kb bootloader & partition table,
# next ${BOOTSIZE} mb FAT32,
# rest of disk rootfs
info "creating filesystems"
fdisk $IMAGE &>/dev/null << EOF
n
p
1
2048
+${BOOTSIZE}M
t
c
a
1
n
p
2
w
EOF
# create device map
info "creating loop device"
DEVICE=`kpartx -a -v $IMAGE | sed -E 's/.*(loop[0-9])p.*/\1/g' | head -1`
sleep 1
DEVICE_BOOTFS="/dev/mapper/${DEVICE}p1"
DEVICE_ROOTFS="/dev/mapper/${DEVICE}p2"
info "created loop device $DEVICE"
# format
info "formatting filesystems"
mkfs.vfat $DEVICE_BOOTFS >& /dev/null
mkfs.ext4 $DEVICE_ROOTFS >& /dev/null
# mount
info "mounting rootfs"
mkdir -p $ROOTFS
mount $DEVICE_ROOTFS $ROOTFS
mkdir -p ${ROOTFS}/boot
mount $DEVICE_BOOTFS ${ROOTFS}/boot
# do evil things to the rootfs here
# loop over all of the scripts
for SHELL_SCRIPT in $SCRIPTPATH/scripts/*.sh; do
# get the name of the script without the full path
SHELL_SCRIPT_NAME=$(echo $SHELL_SCRIPT | rev | cut -d "/" -f1 | rev)
# run it!
info "Running $SHELL_SCRIPT_NAME"
. "$SHELL_SCRIPT"
done
# stop the doing evil things to the rootfs here
# unmount
info "cleaning up"
info "unmounting rootfs"
cd $SCRIPTPATH
sleep 2
umount $DEVICE_BOOTFS
umount $DEVICE_ROOTFS
# remove the device maps
info "removing loop devices"
kpartx -d $IMAGE >& /dev/null
sleep 2
# move the image from the tmp dir to the workspace
# TODO: don't do this if it has failed
OUTFILE="$PDK_WORKSPACE/$OUTFILE"
rm -rf "$OUTFILE"
mv "$IMAGE" "$OUTFILE"
chown --reference="$PDK_COMPONENT_FILE" "$OUTFILE"
# remove the tmp dir
rm -rf $BUILDENV
# exit
info "completed!"
exit 0