-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathubuntuliveusb.sh
executable file
·149 lines (133 loc) · 5.13 KB
/
ubuntuliveusb.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
#!/bin/bash
# Linuxium's scripts to create a multiboot Ubuntu LiveUSB
source chroot-variables.txt
[ $# != 1 ] || [ ${1:0:5} != "/dev/" ] || [[ `echo ${#1}` < 6 ]] && echo "Usage: $0 /dev/<usb device>" && exit
USB_DEVICE=$1
VALID_USB=false
FOUND_USB_DEVICES=`for USB in /dev/disk/by-id/usb*; do readlink -f ${USB}; done | grep -v [0-9]`
for USB in ${FOUND_USB_DEVICES}; do
if [ "${USB_DEVICE}" == "${USB}" ]; then
VALID_USB=true
break
fi
done
if ( ! ${VALID_USB} ); then
echo "Invalid USB '${USB_DEVICE}' ... exiting." && exit
fi
echo "Creating a multiboot Ubuntu liveUSB on '${USB_DEVICE}' ..."
sleep 5
[ ! -f ${PATH_TO}/${CANONICAL_ISO} ] && echo "Using ${CANONICAL_ISO} from Downloads." && cp ~/Downloads/${CANONICAL_ISO} ${PATH_TO}/
[ ! -f ${PATH_TO}/${LINUXIUM_ISO} ] && echo "Using ${LINUXIUM_ISO} from local directory." && cp ./${LINUXIUM_ISO} ${PATH_TO}/
# wiping USB
sudo umount ${USB_DEVICE}* 2> /dev/null
sudo sgdisk -Z ${USB_DEVICE} > /dev/null 2>&1
# formatting USB
(echo n; echo p; echo 1; echo; echo; echo a; echo 1; echo t; echo c; echo w) | sudo fdisk ${USB_DEVICE} > /dev/null 2>&1
sudo mkfs.vfat -F 32 -n ULIVEUSB ${USB_DEVICE}1 > /dev/null
# mounting USB and adding GRUB to USB
[ -f mnt_usb ] && rm -f mnt_usb
[ -d mnt_usb ] || mkdir mnt_usb
sudo mount ${USB_DEVICE}1 mnt_usb
sudo cp -rf include/usb_partition/* mnt_usb/
function copy_ISO_to_USB
{
ISO="$1"
echo -n "Copying $ISO to USB ... "
sudo cp ${PATH_TO}/${ISO} mnt_usb/
sudo mkdir -p mnt_usb/boot/${ISO}
[ -f mnt_iso ] && rm -f mnt_iso
mkdir mnt_iso
[ -f mnt_squashfs ] && rm -f mnt_squashfs
mkdir mnt_squashfs
sudo mount ${PATH_TO}/${ISO} mnt_iso 2> /dev/null
sudo mount mnt_iso/casper/filesystem.squashfs mnt_squashfs
if [ `ls mnt_squashfs/boot/vmlinuz* 2> /dev/null | wc -l` -gt 0 ] ; then
sudo cp mnt_squashfs/boot/vmlinuz* mnt_usb/boot/${ISO}/ 2> /dev/null
for INITRAM in mnt_squashfs/boot/initrd.img*
do
INITRAMFS=`basename ${INITRAM}`
# unpack initramfs
[ -d initrd ] && rm -rf initrd
mkdir initrd
cd initrd
sudo dd status=none if=../${INITRAM} | gzip -d | sudo cpio -id 2> /dev/null
# add in UCM files for sound in initramfs
sudo mkdir -p usr/share/alsa/ucm
sudo cp -rf ${PATH_TO}/UCM-master/* usr/share/alsa/ucm
# repack initramfs
sudo rm -f mnt_usb/boot/${ISO}/${INITRAMFS}
sudo find | sudo cpio -o -H newc 2> /dev/null | gzip | sudo tee ../mnt_usb/boot/${ISO}/${INITRAMFS} > /dev/null
cd ..
sudo rm -rf initrd
done
else
sudo cp mnt_iso/casper/vmlinuz.efi mnt_usb/boot/${ISO}/vmlinuz.efi
sudo cp mnt_iso/casper/initrd.lz mnt_usb/boot/${ISO}/initrd.lz
fi
sudo umount mnt_squashfs && rmdir mnt_squashfs
sudo umount mnt_iso && rmdir mnt_iso
echo
}
# copying Canonical ISO to USB
copy_ISO_to_USB ${CANONICAL_ISO}
# copying Linuxium ISO to USB
copy_ISO_to_USB ${LINUXIUM_ISO}
# updating GRUB menu on USB
cat <<+ | sudo tee mnt_usb/boot/grub/grub.cfg > /dev/null
if loadfont /boot/grub/font.pf2 ; then
set gfxmode=auto
insmod efi_gop
insmod efi_uga
insmod gfxterm
terminal_output gfxterm
fi
set menu_color_normal=white/black
set menu_color_highlight=black/light-gray
+
for ISO_PATH in mnt_usb/boot/*
do
ISO=${ISO_PATH#mnt_usb/boot/}
if [ "${ISO}" = "grub" ]; then continue; fi
ISO_NAME=${ISO%.iso}
echo "Adding GRUB entries for ISO ${ISO_NAME} to USB ..."
for KERNEL_PATH in ${ISO_PATH}/vmlinuz*
do
KERNEL=${KERNEL_PATH#$ISO_PATH/}
echo "Adding entry for ${KERNEL} ..."
sudo bash -c "echo -n 'menuentry \"Try kernel ' >> mnt_usb/boot/grub/grub.cfg"
sudo bash -c "echo -n ${KERNEL} >> mnt_usb/boot/grub/grub.cfg"
sudo bash -c "echo -n ' from ' >> mnt_usb/boot/grub/grub.cfg"
sudo bash -c "echo -n ${ISO_NAME} >> mnt_usb/boot/grub/grub.cfg"
sudo bash -c "echo ' ISO without installing\" {' >> mnt_usb/boot/grub/grub.cfg"
sudo bash -c "echo -ne '\tset ISO_FILE=\"/' >> mnt_usb/boot/grub/grub.cfg"
sudo bash -c "echo -n ${ISO} >> mnt_usb/boot/grub/grub.cfg"
sudo bash -c "echo '\"' >> mnt_usb/boot/grub/grub.cfg"
sudo bash -c "echo -e '\tset gfxpayload=keep' >> mnt_usb/boot/grub/grub.cfg"
sudo bash -c "echo -e '\tloopback loop \${ISO_FILE}' >> mnt_usb/boot/grub/grub.cfg"
sudo bash -c "echo -ne '\tlinux /boot/' >> mnt_usb/boot/grub/grub.cfg"
sudo bash -c "echo -n ${ISO} >> mnt_usb/boot/grub/grub.cfg"
sudo bash -c "echo -n '/' >> mnt_usb/boot/grub/grub.cfg"
sudo bash -c "echo -n ${KERNEL} >> mnt_usb/boot/grub/grub.cfg"
sudo bash -c "echo ' iso-scan/filename=\${ISO_FILE} file=/cdrom/preseed/ubuntu.seed boot=casper quiet splash ---' >> mnt_usb/boot/grub/grub.cfg"
sudo bash -c "echo -ne '\tinitrd /boot/' >> mnt_usb/boot/grub/grub.cfg"
sudo bash -c "echo -n ${ISO} >> mnt_usb/boot/grub/grub.cfg"
sudo bash -c "echo -n '/' >> mnt_usb/boot/grub/grub.cfg"
KERNEL_VERSION=${KERNEL#vmlinuz-}
if [ "${KERNEL}" = "vmlinuz.efi" ]; then
INITRD=initrd.lz
else
INITRD=initrd.img-${KERNEL_VERSION}
fi
sudo bash -c "echo ${INITRD} >> mnt_usb/boot/grub/grub.cfg"
sudo bash -c "echo '}' >> mnt_usb/boot/grub/grub.cfg"
sudo bash -c "echo >> mnt_usb/boot/grub/grub.cfg"
done
done
# unmounting USB
echo -n "Syncing USB ... "
sudo sync
sudo sync
echo
sudo umount mnt_usb
rmdir mnt_usb
echo "Multiboot Ubuntu liveUSB created."