-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild_ubuntu_sdcard.sh
executable file
·172 lines (155 loc) · 4.55 KB
/
build_ubuntu_sdcard.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
#!/usr/bin/env bash
# Script to build Ubuntu bootable SD card from file
DEBUGMODE=0
# Functions
# Check required commands
function check_command {
type -P $1 &>/dev/null || fail "Unable to find $1, please install it and run this script again."
}
# Fail
function fail(){
tput setaf 1; echo "Failure: $*" && tput sgr0
exit 1
}
# Success
function success(){
tput setaf 2; echo "$*" && tput sgr0
}
# Pause
function pause(){
read -p "Press any key to continue..."
echo
}
# Select Ubuntu Image
function image(){
# clear
echo "======================================================================================="
success "This tool will build an Ubuntu bootable SD card from img file in your Downloads folder."
echo "======================================================================================="
pause
Ubuntu=$(find ~/Downloads -type f -name "*ubuntu*.img" | sort -r)
if [ -z "$Ubuntu" ]; then
ZIPPED=$(find ~/Downloads -type f -name "*ubuntu*.zip")
if [ -z "$ZIPPED" ]; then
fail "Could not find any Ubuntu images in your Downloads folder."
else
echo "List of compressed Ubuntu images"
echo "$ZIPPED"
fail "Must uncompress image first."
fi
else
NUMIMAGES=$(echo "$Ubuntu" | wc -l)
success Found $NUMIMAGES Ubuntu images found in Downloads.
echo
echo "$Ubuntu"
# echo "List of Ubuntu images found in Downloads:"
success "$Ubuntu"
echo
if [ "$NUMIMAGES" -gt "1" ]; then
START=1
for (( COUNT=$START; COUNT<=$NUMIMAGES; COUNT++ ))
do
# echo "====================================================="
# echo \#$COUNT
IMAGE=$(echo "$Ubuntu" | nl | grep -w $COUNT | cut -f2)
echo "Image: "$IMAGE
read -rp "Use this Ubuntu image? (y/n) " REPLY
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
SELECTED="Y"
Ubuntu=$IMAGE
if [[ $DEBUGMODE = "1" ]]; then
echo Image: $IMAGE
echo Ubuntu: $Ubuntu
fi
break
fi
# if [[ $REPLY =~ ^[Nn]$ ]]; then
# echo
# read -rp "Enter full path to Ubuntu image: " REPLY
# Ubuntu=$REPLY
# fi
done
if ! [[ $SELECTED =~ "Y" ]]; then
fail "Must select at least one Ubuntu image!"
fi
fi
fi
}
# Select Mounted Disk
function disk(){
echo
echo "================================================================================================="
success "List of mounted disks:"
echo
df -lH
success "(The Filesystem column is the path to the disk.)"
echo "================================================================================================="
# ls -fl /Volumes/
echo
echo "Type the full Filesystem path to disk for your Raspberry Pi MicroSD Card:"
echo "Example: /dev/disk5"
read DISK
if [ -z "$DISK" ]; then
fail "Must enter full path to disk to format!"
fi
echo "================================================================================================="
tput setaf 1; echo "WARNING THE NEXT STEP WILL ERASE THE DISK!!!"
echo "================================================================================================="
read -rp "Proceed to erase and format \"$DISK\" with Ubuntu image? (y/n) " REPLY && tput sgr0
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "Unmounting" $DISK
UNMOUNT=$(diskutil unmountDisk $DISK 2>&1)
if echo "$UNMOUNT" | grep -q "fail"; then
fail "$UNMOUNT"
exit 1
fi
echo "Formatting with FAT32"
FORMAT=$(sudo diskutil eraseDisk FAT32 RASPBERRYPI MBRFormat $DISK 2>&1)
if echo "$FORMAT" | grep -q "fail"; then
fail "$FORMAT"
exit 1
fi
UNMOUNT=$(diskutil unmountDisk $DISK 2>&1)
if echo "$UNMOUNT" | grep -q "fail"; then
fail "$UNMOUNT"
exit 1
fi
if [[ $UNMOUNT = "dd: $DISK: Resource busy" ]]; then
UNMOUNT=$(diskutil unmountDisk $DISK)
fi
if [[ $DEBUGMODE = "1" ]]; then
echo
echo "Ubuntu Image: "; success "$Ubuntu"
echo
echo "Filesystem path: "; success "$DISK"
fi
echo
# tput setaf 1; echo "CONFIRM AND FORMAT DISK?"
# pause
echo "================================================================================================="
echo "Installing the Raspberry Pi image... (this may take some time)"
echo "================================================================================================="
start=$(date +%s)
pause
DD=$(eval sudo dd bs=1m if="$Ubuntu" of="$DISK" 2>&1)
if [[ $DEBUGMODE = "1" ]]; then
echo "DD is done..."
fi
if echo "$DD" | grep -q "unknown"; then
fail "$DD"
exit 1
fi
dur=$(echo "$(date +%s) - $start" | bc)
printf "Execution time: %.2f seconds\n" $dur
echo "$DD"
else
fail "Cancelled."
fi
echo "Done!"
}
check_command "dd"
check_command "diskutil"
image
disk