forked from Distroshare/distroshare-ubuntu-imager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
distroshare-ubuntu-imager.sh
executable file
·608 lines (505 loc) · 16 KB
/
distroshare-ubuntu-imager.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
#!/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.15"
echo "
################################################
###### ######
###### ######
###### Distroshare Ubuntu Imager $VERSION ######
###### ######
###### ######
###### Brought to you by distroshare.com ######
###### ######
###### ######
################################################
"
#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
mkdir -p "${CD}"/boot/grub
mkdir -p "${WORK}"/rootfs
#Install essential tools
echo "Installing the essential tools"
apt-get -q=2 update
apt-get -q=2 install xorriso squashfs-tools dmraid lvm2 samba-common
GRUB2_INSTALLED=`apt-cache policy grub-pc | grep Installed | grep -v none`
#EFI support requires a different grub version.
if [ "$EFI" == "YES" ]
then
ARCH=`/usr/bin/arch`
if [ "$ARCH" == "x86_64" ]
then
apt-get -q=2 install grub-efi-amd64
else
apt-get -q=2 install grub-efi-ia32
fi
else
apt-get -q=2 install grub-pc
fi
echo "Installing Ubiquity"
apt-get -q=2 install casper lupin-casper
if [ "$GTK" == "YES" ]; then
apt-get -q=2 install ubiquity-frontend-gtk
else
apt-get -q=2 install ubiquity-frontend-kde
fi
if [ -n "$EXTRA_PKGS" ]; then
echo "Adding extra packages to installed system"
apt-get -q=2 install "$EXTRA_PKGS"
fi
echo "Patching Ubiquity to fix a possible installer crash"
cp /usr/share/ubiquity/plugininstall.py .
patch < plugininstall.patch
cp plugininstall.py /usr/share/ubiquity/plugininstall.py
rm -f plugininstall.py
echo "Patching Ubiquity to stop it from freaking out if zram is enabled"
cp /usr/bin/ubiquity .
patch < ubiquity.patch
cp ubiquity /usr/bin/
rm -f ubiquity
if [ "$GTK" == "YES" ]
then
echo "Patching Ubiquity Gtk Frontend to make the dialogs smaller"
cp /usr/lib/ubiquity/ubiquity/frontend/gtk_ui.py .
patch < ubiquity_frontend_gtk_dialog_size.patch
cp gtk_ui.py /usr/lib/ubiquity/ubiquity/frontend/gtk_ui.py
rm -f gtk_ui.py
fi
if [ "$DISTROSHARE_UPDATER" == "YES" ]
then
echo "Patching Ubiquity to rsync skel files from distroshare updater"
cp /usr/lib/ubiquity/user-setup/user-setup-apply .
patch < user-setup-apply.patch
cp user-setup-apply /usr/lib/ubiquity/user-setup/user-setup-apply
rm -f user-setup-apply
echo "Patching user-setup to rsync skel files from distroshare updater"
cp /usr/lib/user-setup/user-setup-apply .
patch < user-setup-apply.patch
cp user-setup-apply /usr/lib/user-setup/user-setup-apply
rm -f user-setup-apply
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/default/locale \
--exclude=/etc/timezone --exclude=/etc/shadow* --exclude=/etc/gshadow* \
--exclude=/etc/X11/xorg.conf* --exclude=/etc/gdm/custom.conf --exclude=/etc/mdm/mdm.conf \
--exclude=/etc/lightdm/lightdm.conf --exclude="${WORK}"/rootfs \
--exclude=/etc/default/du-firstrun --delete / "${WORK}"/rootfs
#Copy boot partition
echo "Copying the boot dir/partition"
rsync -a --one-file-system /boot/ "${WORK}"/rootfs/boot
#Unmount the filesystems in case the script failed before
unmount_filesystems
#Create devices in /dev
echo "Creating some links and dirs in /dev"
mkdir "${WORK}"/rootfs/dev/mapper > /dev/null 2>&1
mkdir "${WORK}"/rootfs/dev/pts > /dev/null 2>&1
ln -s /proc/kcore "${WORK}"/rootfs/dev/core > /dev/null 2>&1
ln -s /proc/self/fd "${WORK}"/rootfs/dev/fd > /dev/null 2>&1
cd "${WORK}"/rootfs/dev
ln -s fd/2 stderr > /dev/null 2>&1
ln -s fd/0 stdin > /dev/null 2>&1
ln -s fd/1 stdout > /dev/null 2>&1
ln -s ram1 ram > /dev/null 2>&1
ln -s shm /run/shm > /dev/null 2>&1
mknod agpgart c 10 175
chown root:video agpgart
chmod 660 agpgart
mknod audio c 14 4
mknod audio1 c 14 20
mknod audio2 c 14 36
mknod audio3 c 14 52
mknod audioctl c 14 7
chown root:audio audio*
chmod 660 audio*
mknod console c 5 1
chown root:tty console
chmod 600 console
mknod dsp c 14 3
mknod dsp1 c 14 19
mknod dsp2 c 14 35
mknod dsp3 c 14 51
chown root:audio dsp*
chmod 660 dsp*
mknod full c 1 7
chown root:root full
chmod 666 full
mknod fuse c 10 229
chown root:messagebus fuse
chmod 660 fuse
mknod kmem c 1 2
chown root:kmem kmem
chmod 640 kmem
mknod loop0 b 7 0
mknod loop1 b 7 1
mknod loop2 b 7 2
mknod loop3 b 7 3
mknod loop4 b 7 4
mknod loop5 b 7 5
mknod loop6 b 7 6
mknod loop7 b 7 7
chown root:disk loop*
chmod 660 loop*
cd mapper
mknod control c 10 236
chown root:root control
chmod 600 control
cd ..
mknod mem c 1 1
chown root:kmem mem
chmod 640 mem
mknod midi0 c 35 0
mknod midi00 c 14 2
mknod midi01 c 14 18
mknod midi02 c 14 34
mknod midi03 c 14 50
mknod midi1 c 35 1
mknod midi2 c 35 2
mknod midi3 c 35 3
chown root:audio midi*
chmod 660 midi*
mknod mixer c 14 0
mknod mixer1 c 14 16
mknod mixer2 c 14 32
mknod mixer3 c 14 48
chown root:audio mixer*
chmod 660 mixer*
mknod mpu401data c 31 0
mknod mpu401stat c 31 1
chown root:audio mpu401*
chmod 660 mpu401*
mknod null c 1 3
chown root:root null
chmod 666 null
mknod port c 1 4
chown root:kmem port
chmod 640 port
mknod ptmx c 5 2
chown root:tty ptmx
chmod 666 ptmx
mknod ram0 b 1 0
mknod ram1 b 1 1
mknod ram2 b 1 2
mknod ram3 b 1 3
mknod ram4 b 1 4
mknod ram5 b 1 5
mknod ram6 b 1 6
mknod ram7 b 1 7
mknod ram8 b 1 8
mknod ram9 b 1 9
mknod ram10 b 1 10
mknod ram11 b 1 11
mknod ram12 b 1 12
mknod ram13 b 1 13
mknod ram14 b 1 14
mknod ram15 b 1 15
mknod ram16 b 1 16
chown root:disk ram*
chmod 660 ram*
mknod random c 1 8
chown root:root random
chmod 666 random
mknod rmidi0 c 35 64
mknod rmidi1 c 35 65
mknod rmidi2 c 35 66
mknod rmidi3 c 35 67
chown root:audio rmidi*
chmod 660 rmidi*
mknod sequencer c 14 1
chown root:audio sequencer
chmod 660 sequencer
mknod smpte0 c 35 128
mknod smpte1 c 35 129
mknod smpte2 c 35 130
mknod smpte3 c 35 131
chown root:audio smpte*
chmod 660 smpte*
mknod sndstat c 14 6
chown root:audio sndstat
chmod 660 sndstat
mknod tty c 5 0
mknod tty0 c 4 0
mknod tty1 c 4 1
mknod tty2 c 4 2
mknod tty3 c 4 3
mknod tty4 c 4 4
mknod tty5 c 4 5
mknod tty6 c 4 6
mknod tty7 c 4 7
mknod tty8 c 4 8
mknod tty9 c 4 9
chown root:tty tty*
chmod 600 tty*
mknod urandom c 1 9
chown root:root urandom
chmod 666 urandom
mknod zero c 1 5
chown root:root zero
chmod 666 zero
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
#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
#Source lsb-release for DISTRIB_ID
. /etc/lsb-release
#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
#Set flavour in /etc/casper.conf
echo "export FLAVOUR=\"${DISTRIB_ID}\"" >> /etc/casper.conf
echo "Setting up display manager for autologin if needed"
#Testing for MDM and applying specific changes for it
if [ "${DM}" == "MDM" ]; then
sed -i 's/gdm\/custom.conf/mdm\/mdm.conf/' \
/usr/share/initramfs-tools/scripts/casper-bottom/15autologin
mkdir -p /etc/mdm
echo "[daemon]
#AutomaticLoginEnable = false
#AutomaticLogin = none
#TimedLoginEnable = false
" > /etc/mdm/mdm.conf
#Copy /etc/apt/sources.list to /etc/apt/sources.list.new
cp /etc/apt/sources.list /etc/apt/sources.list.new
fi
#Testing for GDM and applying specific changes for it
if [ "${DM}" == "GDM" ]; then
mkdir -p /etc/gdm
echo "[daemon]
#AutomaticLoginEnable = false
#AutomaticLogin = none
#TimedLoginEnable = false
" > /etc/gdm/custom.conf
fi
if [ "${DM}" == "KDM" ]; then
mkdir -p /etc/kde4/kdm
echo "[X-:0-Core]
AutoLoginEnable=false
AutoLoginUser=none
AutoReLogin=false
" > /etc/kde4/kdm/kdmrc
fi
if [ "${DM}" == "LIGHTDM_UBUNTU_MATE" ]; then
sed -i 's/\/etc\/lightdm\/lightdm.conf/\/usr\/share\/lightdm\/lightdm.conf.d\/60-lightdm-gtk-greeter.conf/' \
/usr/share/initramfs-tools/scripts/casper-bottom/15autologin
sed -i 's/autologin-session=lightdm-autologin/user-session=mate/' \
/usr/share/initramfs-tools/scripts/casper-bottom/15autologin
fi
if [ "${DM}" == "LIGHTDM_ZORIN" ]; then
echo "[SeatDefaults]
user-session=zorin_desktop
" > /etc/lightdm/lightdm.conf
sed -i 's/autologin-session=lightdm-autologin/user-session=zorin_desktop/' \
/usr/share/initramfs-tools/scripts/casper-bottom/15autologin
fi
if [ "${DM}" == "LIGHTDM_KODIBUNTU" ]; then
echo "[SeatDefaults]
xserver-command=/usr/bin/X -bs -nolisten tcp
user-session=kodi
allow-guest=false
greeter-session=lightdm-gtk-greeter
" > /etc/lightdm/lightdm.conf
sed -i 's/autologin-session=lightdm-autologin/user-session=kodi/' \
/usr/share/initramfs-tools/scripts/casper-bottom/15autologin
fi
if [ "${DM}" == "LIGHTDM_DEEPIN" ]; then
echo "[SeatDefaults]
greeter-session=lightdm-deepin-greeter
user-session=deepin
" > /etc/lightdm/lightdm.conf
sed -i 's/autologin-session=lightdm-autologin/user-session=deepin/' \
/usr/share/initramfs-tools/scripts/casper-bottom/15autologin
#Fix for installer icon on desktop
sed -i 's/ubiquity.desktop/ubiquity-gtkui.desktop/' \
/usr/share/initramfs-tools/scripts/casper-bottom/25adduser
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
rm -f /etc/hostname
rm -f /etc/mtab*
rm -f /etc/fstab
rm -f /etc/udev/rules.d/70-persistent*
rm -f /etc/cups/ssl/server.crt
rm -f /etc/cups/ssl/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
echo "Copying over kernel and initrd"
cp -p "${WORK}"/rootfs/boot/vmlinuz-"${KERNEL_VERSION}" "${CASPER}"/vmlinuz
cp -p "${WORK}"/rootfs/boot/initrd.img-"${KERNEL_VERSION}" "${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 "${CASPER}"/filesystem.manifest-desktop
REMOVE='ubiquity ubiquity-frontend-gtk ubiquity-frontend-kde casper user-setup os-prober libdebian-installer4 apt-clone archdetect-deb dpkg-repack gir1.2-json-1.0 gir1.2-timezonemap-1.0 gir1.2-xkl-1.0 libdebian-installer4 libparted-fs-resize0 libtimezonemap-data libtimezonemap1 python3-icu python3-pam rdate sbsigntool ubiquity-casper ubiquity-ubuntu-artwork localechooser-data cifs-utils gir1.2-appindicator3-0.1 gir1.2-javascriptcoregtk-3.0 gir1.2-vte-2.90 gir1.2-webkit-3.0'
for i in $REMOVE
do
sed -i "/${i}/d" "${CASPER}"/filesystem.manifest-desktop
done
#Remove the extra script created to prevent an error message
rm -f "$CASPER_EXTRA_SCRIPT"
echo "Uninstalling Ubiquity"
apt-get -q=2 remove casper lupin-casper ubiquity user-setup
if [ -n "$EXTRA_PKGS" ]; then
echo "Removing extra packages from installed system"
apt-get -q=2 remove "$EXTRA_PKGS"
fi
if [ -n "$GRUB2_INSTALLED" -a "$EFI" == "YES" ]
then
sudo apt-get install grub-pc
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 \"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 ""