-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmake_rdp_iso.sh
executable file
·279 lines (244 loc) · 10.4 KB
/
make_rdp_iso.sh
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
#!/bin/bash
# The directory containing generic remastering recpies
GIT_REMASTER_DIR=rdp-thinbook-linux
function check_host_arch() {
# We _NEED_ x86_64 (amd64)
# Returns: 0 if host architecture is x86_64; 1 otherwise
local host_arch=$(arch)
if [ "$host_arch" != "x86_64" ]; then
echo "Need x86_64 architecture (current: $host_arch)"
return 1
fi
return 0
}
check_host_arch
if [ $? -ne 0 ]; then
exit 1
fi
function check_pkg_integrity() {
# $1: package name
# Returns: 0 if all files OK, 1 otherwise
# If files are not OK, package name and output of md5sum is printed to stdout
local old_pwd=$(pwd)
local pkg=$1
if [ -z "$pkg" ]; then
echo "No package specified"
return 1
fi
local md5sum_file=/var/lib/dpkg/info/${pkg}.md5sums
if [ ! -f "$md5sum_file" ]; then
md5sum_file=/var/lib/dpkg/info/${pkg}:amd64.md5sums
if [ ! -f "$md5sum_file" ]; then
echo "$pkg : md5sums not found"
return 1
fi
fi
# Assume package integrity if $md5sum_file is zero-sized - this is true
# for some packages, such as python3-dev imagemagick-common python-all-dev libpython-all-dev libpython3-all-dev g++ python3-all
# Find them using this command: find /var/lib/dpkg/info -name '*.md5sums' -size 0
[[ ! -s "$md5sum_file" ]] && return 0
cd /
# files under /usr/share/man in .gz format are sometimes not available
# we ignore these errors
errors=$(md5sum -c "$md5sum_file" 2>&1 1>/dev/null | grep -v '^md5sum: usr/share/man.*: No such file or directory$' | grep -v '^md5sum: WARNING: .* listed files could not be read$')
if [ -n "$errors" ]; then
echo "${pkg}:"
echo -e "$errors" | sed -e 's/^/ /'
return 1
fi
cd $old_pwd
}
function check_required_pkgs {
# IDEALLY REQD_PKGS should be calculated based on whether kernel compilation is requested
local REQD_PKGS="grub-efi-ia32-bin grub-efi-amd64-bin grub-pc-bin grub2-common grub-common util-linux parted gdisk mount xorriso genisoimage squashfs-tools rsync git build-essential kernel-package fakeroot libncurses5-dev libssl-dev ccache libfile-fcntllock-perl curl "
# ZFS compilation additionally requries:
REQD_PKGS="$REQD_PKGS autoconf automake libtool gawk alien dkms libblkid-dev uuid-dev libudev-dev zlib1g-dev libaio-dev libattr1-dev libelf-dev python3 python3-setuptools python3-cffi libffi-dev python3-dev"
local MISSING_PKGS=$(dpkg -l $REQD_PKGS 2>/dev/null | sed -e '1,4d'| grep -v '^ii' | awk '{printf("%s ", $2)}')
MISSING_PKGS="$MISSING_PKGS $(dpkg -l $REQD_PKGS 2>&1 1>/dev/null | sed -e 's/^dpkg-query: no packages found matching //')"
MISSING_PKGS="${MISSING_PKGS%% *}"
if [ -n "${MISSING_PKGS}" ]; then
echo "You do not have all required packages installed"
echo ""
echo "sudo apt-get install $MISSING_PKGS"
exit 1
else
echo "All required packages are already installed"
echo "Required packages:"
echo "$REQD_PKGS" | fmt -w 70 | sed -e 's/^/ /'
echo ""
fi
local FAILED_PKGS=""
for p in $REQD_PKGS
do
check_pkg_integrity "$p"
if [ $? -ne 0 ]; then
FAILED_PKGS="$FAILED_PKGS $p"
fi
done
if [ -n "$FAILED_PKGS" ]; then
echo ""
echo "The following packages are installed, but failed the integrity test"
echo $FAILED_PKGS | fmt -w 70 | sed -e 's/^/ /'
echo ""
echo "You should reinstall these packages using the command:"
echo " sudo apt-get install --reinstall $FAILED_PKGS"
exit 1
fi
echo "All required packages passed the integrity test"
}
function check_avail_disk_space {
# IDEALLY REQD_SPACE_BYTES should be calculated based on whether kernel compilation is requested
REQD_SPACE_BYTES=10000000000
AVAIL_SPACE_BYTES=$(df -B1 --output=avail . | sed -e '1d')
printf "Required space : %18d\n" $REQD_SPACE_BYTES
printf "Available space: %18d\n" $AVAIL_SPACE_BYTES
if [ $AVAIL_SPACE_BYTES -lt $REQD_SPACE_BYTES ]; then
echo "You do not have enough disk space"
exit 1
fi
echo ""
}
function update_gitdir {
# $1: directory
# $2: git url
local oldpwd=$(pwd)
local gitdir=$1
local git_url=$2
if [ -z "$2" ]; then
echo "Usage: update_gitdir <directory_path> <git_url>"
return 1
fi
gitdir=$(basename "$gitdir")
cd "$TOP_DIR"
if [ -d "$1" ]; then
cd "$1"
echo "Pulling latest changes to $gitdir"
git pull
if [ $? -ne 0 ]; then
echo "Could not update $gitdir"
cd "$oldpwd"
exit 1
fi
else
echo "Cloning to $gitdir"
git clone --depth 1 "$git_url" 2>/dev/null "$gitdir"
fi
cd $oldpwd
}
function update_from_git {
# Remote git URLs are ONLY in this function
cd $TOP_DIR
update_gitdir bootutils 'https://github.com/sundarnagarajan/bootutils.git' || exit 1
update_gitdir $GIT_REMASTER_DIR 'https://github.com/sundarnagarajan/rdp-thinbook-linux.git' || exit 1
# Copy scripts from bootutils
\rm -rf $TOP_DIR/$GIT_REMASTER_DIR/remaster/chroot/scripts
cp -a $TOP_DIR/bootutils/scripts $TOP_DIR/$GIT_REMASTER_DIR/remaster/chroot/
}
function copy_linuxutils()
{
local LINUXUTILS_DIR=/usr/local/bin/linuxutils
if [ ! -d "$LINUXUTILS_DIR" ]; then
echo "Directory not found: $LINUXUTILS_DIR"
return
fi
cp -a "$LINUXUTILS_DIR" $TOP_DIR/$GIT_REMASTER_DIR/remaster/chroot/
for file_dir in .git fixrandr.py fixrandr_wrapper.py get_hosts_from_router ipmimon.py ipmimon_type_fan ipmimon_type_temperature ipmimon_type_voltage movewindow_fixes rdp.py repo_ppa_lib.py sas2ircu show_lsisas show_scanners sign_sha256_dir_hierarchy.sh show_ssh ssh_functions.sh sudoers.txt watch_md_iostat.sh xrandr_settings
do
rm -rf $TOP_DIR/$GIT_REMASTER_DIR/remaster/chroot/$(basename "$LINUXUTILS_DIR")/$file_dir
done
}
function compile_kernel {
# Config values are in kernel_build.config
\rm -rf "$TOP_DIR/__kernel_build" "$TOP_DIR/__zfs_build"
\rm -rf "$TOP_DIR/$GIT_REMASTER_DIR/remaster/chroot/kernel-debs" "$TOP_DIR/$GIT_REMASTER_DIR/remaster/chroot/zfs-kernel-debs" "$TOP_DIR/$GIT_REMASTER_DIR/remaster/chroot/zfs-userspace-debs"
cd $TOP_DIR
update_gitdir kernel_build 'https://github.com/sundarnagarajan/kernel_build.git' || exit 1
KERNEL_BUILD_CONFIG="$TOP_DIR/kernel_build.config" $TOP_DIR/kernel_build/scripts/patch_and_build_kernel.sh
if [ $? -ne 0 ]; then
exit 1
fi
# Copy kernel DEBs
if [ $(ls -1 "$TOP_DIR/__kernel_build/debs"/*.deb 2>/dev/null | wc -l) -gt 0 ]; then
echo "Moving kernel DEBs:"
mkdir -p "$TOP_DIR/$GIT_REMASTER_DIR/remaster/chroot/kernel-debs"
mv "$TOP_DIR/__kernel_build/debs/"*.deb "$TOP_DIR/$GIT_REMASTER_DIR/remaster/chroot/kernel-debs"/
( cd "$TOP_DIR/$GIT_REMASTER_DIR/remaster/chroot/kernel-debs"; ls -1 *.deb 2>/dev/null | sed -e 's/^/ /')
fi
# Copy ZFS debs if present
if [ $(ls -1 "$TOP_DIR/__zfs_build/zfs_kernel_debs"/*.deb 2>/dev/null | wc -l) -gt 0 ]; then
echo "Moving ZFS kernel DEBs:"
mkdir -p "$TOP_DIR/$GIT_REMASTER_DIR/remaster/chroot/zfs-kernel-debs"
mv "$TOP_DIR/__zfs_build/zfs_kernel_debs"/*.deb "$TOP_DIR/$GIT_REMASTER_DIR/remaster/chroot/zfs-kernel-debs"/
( cd "$TOP_DIR/$GIT_REMASTER_DIR/remaster/chroot/zfs-kernel-debs"; ls -1 *.deb 2>/dev/null | sed -e 's/^/ /')
fi
if [ $(ls -1 "$TOP_DIR/__zfs_build/zfs_userspace_debs"/*.deb 2>/dev/null | wc -l) -gt 0 ]; then
echo "Moving ZFS userspace DEBs:"
mkdir -p "$TOP_DIR/$GIT_REMASTER_DIR/remaster/chroot/zfs-userspace-debs"
mv "$TOP_DIR/__zfs_build/zfs_userspace_debs"/*.deb "$TOP_DIR/$GIT_REMASTER_DIR/remaster/chroot/zfs-userspace-debs"/
( cd "$TOP_DIR/$GIT_REMASTER_DIR/remaster/chroot/zfs-userspace-debs"; ls -1 *.deb 2>/dev/null | sed -e 's/^/ /')
fi
cd $TOP_DIR
}
function download_virtualbox_guest_dkms_deb() {
# We need virtualbox-guest-dkms version 6.1.2+
# Only available in focal fossa
# So we download it into chroot/virtualbox IFF commands/25_virtualbox_integration.sh
# is executable
if [ -x $TOP_DIR/$GIT_REMASTER_DIR/remaster/chroot/commands/25_virtualbox_integration.sh ]; then
if [ -x $TOP_DIR/$GIT_REMASTER_DIR/remaster/chroot/virtualbox ]; then
echo "Downloading virtualbox-guest DEBs"
local oldpwd=$(pwd)
cd $TOP_DIR/$GIT_REMASTER_DIR/remaster/chroot/virtualbox
wget -q -nd 'http://archive.ubuntu.com/ubuntu/pool/multiverse/v/virtualbox/virtualbox-guest-dkms_6.1.4-dfsg-2_all.deb' -O virtualbox-guest-dkms.deb
wget -q -nd 'http://archive.ubuntu.com/ubuntu/pool/multiverse/v/virtualbox/virtualbox-guest-utils_6.1.4-dfsg-2_amd64.deb' -O virtualbox-guest-utils.deb
wget -q -nd 'http://archive.ubuntu.com/ubuntu/pool/multiverse/v/virtualbox/virtualbox-guest-x11_6.1.4-dfsg-2_amd64.deb' -O virtualbox-guest-x11.deb
ls -l *.deb 2>/dev/null | sed -e 's/^/ /'
cd "$oldpwd"
fi
fi
}
function remaster_iso {
if [ $(id -u) -ne 0 ]; then
echo "Must be run as root"
exit 1
fi
if [ ! -f "$INPUT_ISO" ]; then
echo "INPUT_ISO not found: $INPUT_ISO"
exit 1
fi
if [ -n "${OUTPUT_ISO}" -a -f "${OUTPUT_ISO}" ]; then
sudo rm -f ${OUTPUT_ISO}
fi
sudo REMASTER_CMDS_DIR=${R_DIR} ${TOP_DIR}/bootutils/scripts/ubuntu_remaster_iso.sh ${INPUT_ISO} ${EXTRACT_DIR} ${OUTPUT_ISO}
}
# ------------------------------------------------------------------------
# Main script starts after this
# ------------------------------------------------------------------------
START_TIME=$(date)
export TOP_DIR=$(readlink -e $(dirname $0))
export R_DIR=${TOP_DIR}/$GIT_REMASTER_DIR/remaster
export INPUT_ISO=${TOP_DIR}/ISO/in/source.iso
export EXTRACT_DIR=${TOP_DIR}/ISO/extract
export OUTPUT_ISO=${TOP_DIR}/ISO/out/modified.iso
function cleanup_mounts()
{
if [ -z "$EXTRACT_DIR" ]; then
return
fi
which findmnt 1>/dev/null 2>&1 || return
for d in $(findmnt -n -l | grep "$EXTRACT_DIR" | awk '{print $1}' | sort -r)
do
echo "Unmounting $d"
umount $d
done
rm -rf "$EXTRACT_DIR"
}
trap cleanup_mounts 1 2 3 15
check_required_pkgs
check_avail_disk_space
update_from_git
copy_linuxutils
# download_virtualbox_guest_dkms_deb
compile_kernel
remaster_iso
echo "Start: $START_TIME" ; echo "Ended: $(date)"