-
Notifications
You must be signed in to change notification settings - Fork 145
/
glim.sh
executable file
·172 lines (150 loc) · 4.72 KB
/
glim.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
#!/bin/bash
#
# BASH. It's what I know best, sorry.
#
# Check that we are *NOT* running as root
if [[ `id -u` -eq 0 ]]; then
echo "ERROR: Don't run as root, use a user with full sudo access."
exit 1
fi
# Sanity check : GRUB2
if which grub2-install &>/dev/null; then
GRUB2_INSTALL="grub2-install"
GRUB2_DIR="grub2"
elif which grub-install &>/dev/null; then
GRUB2_INSTALL="grub-install"
GRUB2_DIR="grub"
fi
if [[ -z "$GRUB2_INSTALL" ]]; then
echo "ERROR: grub2-install or grub-install commands not found."
exit 1
fi
# Sanity check : Our GRUB2 configuration
GRUB2_CONF="`dirname $0`/grub2"
if [[ ! -f ${GRUB2_CONF}/grub.cfg ]]; then
echo "ERROR: grub2/grub.cfg to use not found."
exit 1
fi
#
# Find GLIM device (use the first if multiple found, you've asked for trouble!)
#
# Sanity check : blkid command
if ! which blkid &>/dev/null; then
echo "ERROR: blkid command not found."
exit 1
fi
USBDEV1=`blkid -L GLIM | head -n 1`
# Sanity check : we found one partition to use with matching label
if [[ -z "$USBDEV1" ]]; then
echo "ERROR: no partition found with label 'GLIM', please create one."
exit 1
fi
echo "Found partition with label 'GLIM' : ${USBDEV1}"
# Sanity check : our partition is the first and only one on the block device
USBDEV=${USBDEV1%1}
if [[ ! -b "$USBDEV" ]]; then
echo "ERROR: ${USBDEV} block device not found."
exit 1
fi
echo "Found block device where to install GRUB2 : ${USBDEV}"
if [[ `ls -1 ${USBDEV}* | wc -l` -ne 2 ]]; then
echo "ERROR: ${USBDEV1} isn't the only partition on ${USBDEV}"
exit 1
fi
# Sanity check : our partition is mounted
if ! grep -q -w ${USBDEV1} /proc/mounts; then
echo "ERROR: ${USBDEV1} isn't mounted"
exit 1
fi
USBMNT=`grep -w ${USBDEV1} /proc/mounts | cut -d ' ' -f 2`
if [[ -z "$USBMNT" ]]; then
echo "ERROR: Couldn't find mount point for ${USBDEV1}"
exit 1
fi
echo "Found mount point for filesystem : ${USBMNT}"
BIOS=true
# Check BIOS support
if [[ -d /usr/lib/grub/i386-pc ]]; then
BIOS=true
else
echo "WARNING: no /usr/lib/grub/i386-pc dir. Skipping Grub BIOS support"
BIOS=false
EFI=true
fi
#
# EFI or regular?
#
if [[ $BIOS == true ]]; then
# Set the target
read -n 1 -s -p "Install for EFI in addition to standard BIOS? (Y/n) " EFI
if [[ "$EFI" == "n" ]]; then
EFI=false
echo "n"
else
EFI=true
echo "y"
fi
fi
# Sanity check : for EFI, an additional package might be missing
if [[ $EFI == true && ! -d /usr/lib/grub/x86_64-efi ]]; then
if [[ $BIOS == false ]]; then
echo "ERROR: neither support for BIOS or EFI was found"
exit 1
else
echo "WARNING: no /usr/lib/grub/x86_64-efi dir (grub2-efi-x64-modules rpm or grub-efi-amd64-bin deb missing?)"
fi
fi
#
# Get serious. If we get here, things are looking sane
#
# Sanity check : human will read the info and confirm
read -n 1 -s -p "Ready to install GLIM. Continue? (Y/n) " PROCEED
if [[ "$PROCEED" == "n" ]]; then
echo "n"
exit 2
else
echo "y"
fi
# Install GRUB2
if [[ $BIOS == true ]]; then
GRUB_TARGET="--target=i386-pc"
echo "Running ${GRUB2_INSTALL} ${GRUB_TARGET} --boot-directory=${USBMNT}/boot ${USBDEV} (with sudo) ..."
sudo ${GRUB2_INSTALL} ${GRUB_TARGET} --boot-directory=${USBMNT}/boot ${USBDEV}
if [[ $? -ne 0 ]]; then
echo "ERROR: ${GRUB2_INSTALL} returned with an error exit status."
exit 1
fi
fi
if [[ $EFI == true ]]; then
GRUB_TARGET="--target=x86_64-efi --efi-directory=${USBMNT} --removable"
echo "Running ${GRUB2_INSTALL} ${GRUB_TARGET} --boot-directory=${USBMNT}/boot ${USBDEV} (with sudo) ..."
sudo ${GRUB2_INSTALL} ${GRUB_TARGET} --boot-directory=${USBMNT}/boot ${USBDEV}
if [[ $? -ne 0 ]]; then
echo "ERROR: ${GRUB2_INSTALL} returned with an error exit status."
exit 1
fi
fi
# Check USB mount dir write permission, to use sudo if missing
if [[ -w "${USBMNT}" ]]; then
CMD_PREFIX=""
else
CMD_PREFIX="sudo"
fi
# Copy GRUB2 configuration
echo "Running rsync -rt --delete --exclude=i386-pc --exclude=x86_64-efi --exclude=fonts ${GRUB2_CONF}/ ${USBMNT}/boot/${GRUB2_DIR} ..."
${CMD_PREFIX} rsync -rt --delete --exclude=i386-pc --exclude=x86_64-efi --exclude=fonts ${GRUB2_CONF}/ ${USBMNT}/boot/${GRUB2_DIR}
if [[ $? -ne 0 ]]; then
echo "ERROR: the rsync copy returned with an error exit status."
exit 1
fi
# Be nice and pre-create the directory, and mention it
[[ -d ${USBMNT}/boot/iso ]] || ${CMD_PREFIX} mkdir ${USBMNT}/boot/iso
echo "GLIM installed! Time to populate the boot/iso/ sub-directories."
# Now also pre-create all supported sub-directories since empty are ignored
args=(
-E -n
'/\(distro-list-start\)/,/\(distro-list-end\)/{s,^\* \[`([a-z0-9]+)`\].*$,\1,p}'
)
for DIR in $(sed "${args[@]}" "$(dirname "$0")"/README.md); do
[[ -d ${USBMNT}/boot/iso/${DIR} ]] || ${CMD_PREFIX} mkdir ${USBMNT}/boot/iso/${DIR}
done