-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdistroshare.sh
executable file
·317 lines (259 loc) · 9.09 KB
/
distroshare.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
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
315
316
317
#!/bin/bash
#Distroshare Ubuntu Imager - https://www.distroshare.com
#
#Makes a Live cd that is installable from your current installation
#This is intended to work on Ubuntu and its derivatives
#
#Based on this tutorial:
#https://help.ubuntu.com/community/MakeALiveCD/DVD/BootableFlashFromHarddiskInstall
#GPL2 License
VERSION="1.0.4"
echo "
################################################
###### ######
###### ######
###### Distroshare $VERSION ######
###### ######
###### ######
################################################
"
#Configuration file name and path
CONFIG_FILE="./distroshare-ubuntu-imager.config"
#Current directory
CURRENT_DIR=`pwd`
#Convience function to unmount filesystems
unmount_filesystems() {
echo "Unmounting filesystems"
umount "${WORK}"/rootfs/proc > /dev/null 2>&1
umount "${WORK}"/rootfs/sys > /dev/null 2>&1
umount -l "${WORK}"/rootfs/dev/pts > /dev/null 2>&1
umount -l "${WORK}"/rootfs/dev > /dev/null 2>&1
}
#Starting the process
#We depend on the umask being 022
umask 022
#Source the config file
if [ -r "$CONFIG_FILE" ]; then
. "$CONFIG_FILE"
else
echo "Can't read config file. Exiting"
exit 1
fi
#Set some other variables based on the config file
CD="${WORK}"/CD
CASPER="${CD}"/casper
#Checking for root
if [ "$USER" != "root" ]; then
echo "You aren't root, so I'm exiting. Become root and try again."
exit 1
fi
#Make the directories
echo "Making the necessary directories"
mkdir -p "${CD}"/{casper,boot/grub}
mkdir -p "${WORK}"/rootfs
#Install essential tools
echo "Installing the essential tools"
apt-get -q=2 update
apt-get -q=2 install grub2 xorriso squashfs-tools
echo "Installing Ubiquity"
apt-get -q=2 install casper lupin-casper
echo "GTK is: $GTK"
if [ "$GTK" == "YES" ]; then
apt-get -q=2 install ubiquity-frontend-gtk
else
apt-get -q=2 install ubiquity-frontend-qt
fi
if [ -n "$EXTRA_PKGS" ]; then
echo "Adding extra packages to installed system"
apt-get -q=2 install "$EXTRA_PKGS"
fi
#Copy the filesystem
echo "Copying the current system to the new directories"
rsync -a --one-file-system --exclude=/proc/* --exclude=/dev/* \
--exclude=/sys/* --exclude=/tmp/* --exclude=/run/* \
--exclude=/home/* --exclude=/lost+found \
--exclude=/var/tmp/* --exclude=/boot --exclude=/root/* \
--exclude=/var/mail/* --exclude=/var/spool/* --exclude=/media/* \
--exclude=/etc/hosts \
--exclude=/etc/timezone --exclude=/etc/shadow* --exclude=/etc/gshadow* \
--exclude=/etc/X11/xorg.conf* --exclude=/etc/gdm/custom.conf \
--exclude=/etc/lightdm/lightdm.conf --exclude="${WORK}"/rootfs / "${WORK}"/rootfs
#Copy boot partition
echo "Copying the boot dir/partition"
rsync -a --one-file-system /boot/ "${WORK}"/rootfs/boot
#Create some links and dirs in /dev
echo "Creating some links and dirs in /dev"
mkdir "${WORK}"/rootfs/dev/mapper
mkdir "${WORK}"/rootfs/dev/pts
ln -s /proc/kcore "${WORK}"/rootfs/dev/core
ln -s /proc/self/fd "${WORK}"/rootfs/dev/fd
cd "${WORK}"/rootfs/dev
ln -s fd/2 stderr
ln -s fd/0 stdin
ln -s fd/1 stdout
ln -s ram ram1
rsync -a /dev/urandom urandom
cd "${CURRENT_DIR}"
#Copy the resolv.conf file - needed for newer Ubuntus
echo "Copying resolv.conf"
mv "${WORK}"/rootfs/etc/resolv.conf "${WORK}"/rootfs/etc/resolv.conf.old
cp /etc/resolv.conf "${WORK}"/rootfs/etc/resolv.conf
#Unmount the filesystems in case the script failed before
unmount_filesystems
#Mount dirs into copied distro
echo "Mounting system file dirs"
mount --bind /dev/ "${WORK}"/rootfs/dev
mount --bind /dev/pts "${WORK}"/rootfs/dev/pts
mount -t proc proc "${WORK}"/rootfs/proc
mount -t sysfs sysfs "${WORK}"/rootfs/sys
#Remove non-system users
echo "Removing non-system users"
for i in `cat "${WORK}"/rootfs/etc/passwd | awk -F":" '{print $1}'`
do
uid=`cat "${WORK}"/rootfs/etc/passwd | grep "^${i}:" | awk -F":" '{print $3}'`
[ "$uid" -gt "998" -a "$uid" -ne "65534" ] && \
chroot "${WORK}"/rootfs /bin/bash -c "userdel --force ${i} 2> /dev/null"
done
#Run commands in chroot
echo "Creating script to run in chrooted env"
cat > "${WORK}"/rootfs/distroshare_imager.sh <<EOF
#!/bin/bash
umask 022
#Modify copied distro
if [ -n "$UBIQUITY_KERNEL_PARAMS" ]; then
echo "Replacing ubiquity default extra kernel params with: $UBIQUITY_KERNEL_PARAMS"
sed -i "s/defopt_params=\"\"/defopt_params=\"${UBIQUITY_KERNEL_PARAMS}\"/" \
/usr/share/grub-installer/grub-installer
fi
#Update initramfs
echo "Updating initramfs"
depmod -a $(uname -r)
update-initramfs -u -k all > /dev/null 2>&1
#Clean up downloaded packages
echo "Cleaning up files that are not needed in the new image"
apt-get clean
#Clean up files
#rm -f /etc/X11/xorg.conf*
rm -f /etc/{hosts,hostname,mtab*,fstab}
rm -f /etc/udev/rules.d/70-persistent*
rm -f /etc/cups/ssl/{server.crt,server.key}
rm -f /etc/NetworkManager/system-connections/*
rm -f /etc/ssh/*key*
rm -f /var/lib/dbus/machine-id
rm -f /etc/resolv.conf
mv /etc/resolv.conf.old /etc/resolv.conf
truncate -s 0 /etc/printcap > /dev/null 2>&1
truncate -s 0 /etc/cups/printers.conf
rm -rf /var/lib/sudo/*
rm -rf /var/lib/AccountsService/users/*
rm -rf /var/lib/kdm/*
rm -rf /var/lib/lightdm/*
rm -rf /var/lib/lightdm-data/*
rm -rf /var/lib/gdm/*
rm -rf /var/lib/gdm-data/*
rm -rf /var/lib/mdm/*
rm -rf /var/lib/mdm-data/*
rm -rf /var/run/console/*
#If /var/run is a link, then it is pointing to /run
if [ ! -L /var/run ]; then
find /var/run/ -type f -exec rm -f {} \;
fi
#If /var/lock is a link, then it is pointing to /run/lock
if [ ! -L /var/lock ]; then
find /var/lock/ -type f -exec rm -f {} \;
fi
#Clean up files - taken from BlackLab Imager
find /var/backups/ /var/spool/ /var/mail/ \
/var/tmp/ /var/crash/ \
/var/lib/ubiquity/ -type f -exec rm -f {} \;
#Remove archived logs
find /var/log -type f -name '*.[0-9]*' -exec rm -f {} \;
#Truncate all logs
find /var/log -type f -exec truncate -s 0 {} \;
EOF
echo "Running script in chrooted env"
chmod 700 "${WORK}"/rootfs/distroshare_imager.sh
chown root:root "${WORK}"/rootfs/distroshare_imager.sh
chroot "${WORK}"/rootfs /distroshare_imager.sh
rm -f "${WORK}"/rootfs/distroshare_imager.sh
. /etc/lsb-release
#Set flavour in /etc/casper.conf
echo "export FLAVOUR=\"${DISTRIB_ID}\"" >> "${WORK}"/rootfs/etc/casper.conf
echo "Copying over kernel and initrd"
export kversion=`cd "${WORK}"/rootfs/boot && ls -1 vmlinuz-* | tail -1 | sed 's@vmlinuz-@@'`
cp -p "${WORK}"/rootfs/boot/vmlinuz-${kversion} "${CASPER}"/vmlinuz
cp -p "${WORK}"/rootfs/boot/initrd.img-${kversion} "${CASPER}"/initrd.img
cp -p "${WORK}"/rootfs/boot/memtest86+.bin "${CD}"/boot
echo "Creating filesystem.manifest"
dpkg-query -W --showformat='${Package} ${Version}\n' > "${CASPER}"/filesystem.manifest
cp "${CASPER}"/filesystem.manifest{,-desktop}
REMOVE='ubiquity ubiquity-frontend-gtk ubiquity-frontend-kde casper user-setup os-prober libdebian-installer4'
for i in $REMOVE
do
sed -i "/${i}/d" "${CASPER}"/filesystem.manifest-desktop
done
echo "Uninstalling Ubiquity"
apt-get -q=2 remove casper lupin-casper ubiquity
if [ -n "$EXTRA_PKGS" ]; then
echo "Removing extra packages from installed system"
apt-get -q=2 remove "$EXTRA_PKGS"
fi
echo "Removing temp files"
rm -rf "${WORK}"/rootfs/tmp/*
rm -rf "${WORK}"/rootfs/run/*
unmount_filesystems
echo "Making squashfs - this is going to take a while"
mksquashfs "${WORK}"/rootfs "${CASPER}"/filesystem.squashfs -noappend
echo "Making filesystem.size"
echo -n $(du -s --block-size=1 "${WORK}"/rootfs | \
tail -1 | awk '{print $1}') > "${CASPER}"/filesystem.size
echo "Making md5sum"
rm -f "${CD}"/md5sum.txt
find "${CD}" -type f -print0 | xargs -0 md5sum | sed "s@${CD}@.@" | \
grep -v md5sum.txt >> "${CD}"/md5sum.txt
echo "Creating release notes url"
mkdir "${CD}"/.disk
echo "${RELEASE_NOTES_URL}" > "${CD}"/.disk/release_notes_url
echo "Creating grub.cfg"
echo "
set default=\"0\"
set timeout=10
menuentry \"Ubuntu GUI\" {
linux /casper/vmlinuz boot=casper $KERNEL_PARAMS quiet splash --
initrd /casper/initrd.img
}
menuentry \"Ubuntu in safe mode\" {
linux /casper/vmlinuz boot=casper $KERNEL_PARAMS xforcevesa quiet splash --
initrd /casper/initrd.img
}
menuentry \"Ubuntu CLI\" {
linux /casper/vmlinuz boot=casper $KERNEL_PARAMS textonly quiet splash --
initrd /casper/initrd.img
}
menuentry \"Ubuntu GUI persistent mode\" {
linux /casper/vmlinuz boot=casper $KERNEL_PARAMS persistent quiet splash --
initrd /casper/initrd.img
}
menuentry \"Ubuntu GUI from RAM\" {
linux /casper/vmlinuz boot=casper $KERNEL_PARAMS toram quiet splash --
initrd /casper/initrd.img
}
menuentry \"Check Disk for Defects\" {
linux /casper/vmlinuz boot=casper $KERNEL_PARAMS integrity-check quiet splash --
initrd /casper/initrd.img
}
menuentry \"Memory Test\" {
linux16 /boot/memtest86+.bin --
}
menuentry \"Boot from the first hard disk\" {
set root=(hd0)
chainloader +1
}
" > "${CD}"/boot/grub/grub.cfg
echo "Creating the iso"
grub-mkrescue -o "${WORK}"/live-cd.iso "${CD}"
echo "We are done."
echo "Is your distro interesting or customized for a specific machine?"
echo "How about sharing it at https://www.distroshare.com?"
echo "You will help others and you could receive donations for your work."
echo ""