-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·229 lines (179 loc) · 5.52 KB
/
install.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
#!/usr/bin/env bash
setup() {
if [ `whoami` != "root" ]; then
echo "Please run this as root!"
exit 1
fi
read -p "Enter device to install to: " -e -i /dev/mmcblk0 drive
local boot_dev="$drive"p1
local main_dev="$drive"p2
read -p "Raspberry Pi generation: " -e -i 3 pi_generation
echo 'Creating partitions'
partition_drive "$drive"
echo 'Formatting filesystems'
format_filesystems "$boot_dev" "$main_dev"
echo 'Mounting filesystems'
mount_filesystems "$boot_dev" "$main_dev"
echo 'Installing base system'
install_base "$pi_generation"
echo 'Chrooting into installed system to continue setup...'
local setup_file="setup.sh"
chroot_into_dev "$setup_file"
if [ -f "/mnt/$setup_file" ]
then
echo 'ERROR: Something failed inside the chroot, not unmounting filesystems so you can investigate.'
echo 'Make sure you unmount everything before you try to run this script again.'
else
echo 'Unmounting filesystems'
unmount_filesystems
echo 'Done! Unplug SD card.'
fi
}
configure() {
echo 'Installing additional packages'
install_packages
echo 'Clearing package tarballs'
clean_packages
echo 'Configuring sudo'
set_sudoers
echo 'Setting up udiskie'
setup_udiskie
echo 'Installing music player app'
install_music_app
rm /setup.sh
}
partition_drive() {
local drive="$1"; shift
# First partition, 100M in size, for the boot files and the second one for the rest of the system
parted --script "$drive" mklabel msdos
parted --script "$drive" mkpart primary fat32 0% 100M
parted --script "$drive" mkpart primary ext4 100M 100%
}
format_filesystems() {
local boot_dev="$1"; shift
local main_dev="$1"; shift
mkfs.vfat -F32 "$boot_dev"
mkfs.ext4 -F "$main_dev"
}
mount_filesystems() {
local boot_dev="$1"; shift
local main_dev="$1"; shift
mount "$main_dev" /mnt
mkdir /mnt/boot
mount "$boot_dev" /mnt/boot
}
install_base() {
local pi_generation="$1"; shift
if [ "$pi_generation" == "1" ]
then
wget http://os.archlinuxarm.org/os/ArchLinuxARM-rpi-latest.tar.gz
else
wget http://os.archlinuxarm.org/os/ArchLinuxARM-rpi-2-latest.tar.gz
fi
tar -xpf ArchLinuxARM-rpi-*latest.tar.gz -C /mnt
}
chroot_into_dev() {
local setup_file="$1"; shift
# Mount the temporary api filesystems
mount -t proc none /mnt/proc
mount -t sysfs none /mnt/sys
mount -o bind /dev /mnt/dev
# To get a working network inside the chroot we need to fix the resolv.conf file
mv /mnt/etc/resolv.conf /mnt/etc/resolv.conf.bak
cp /etc/resolv.conf /mnt/etc/resolv.conf
# Allow us to execute arm executables on a x86 or x86_64 system
cp /usr/bin/qemu-arm-static /mnt/usr/bin/
cp $0 "/mnt/$setup_file"
chroot /mnt "./$setup_file" chroot
}
unmount_filesystems() {
umount /mnt/boot
umount /mnt
}
install_packages() {
local packages=''
# General utilities/libraries
packages+=' base-devel python2 flite'
# Automatic mounting of devices is achieved with udisks2
packages+=' udiskie'
pacman -Sy --noconfirm --needed $packages
}
clean_packages() {
yes | pacman -Scc
}
set_sudoers() {
# Allows people in group wheel to run all commands, without a password
echo '%wheel ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers
}
setup_udiskie() {
# Allow all members of the storage group to run udiskie
cat > /etc/polkit-1/rules.d/50-udiskie.rules <<EOF
polkit.addRule(function(action, subject) {
var YES = polkit.Result.YES;
// NOTE: there must be a comma at the end of each line except for the last:
var permission = {
// required for udisks1:
"org.freedesktop.udisks.filesystem-mount": YES,
"org.freedesktop.udisks.luks-unlock": YES,
"org.freedesktop.udisks.drive-eject": YES,
"org.freedesktop.udisks.drive-detach": YES,
// required for udisks2:
"org.freedesktop.udisks2.filesystem-mount": YES,
"org.freedesktop.udisks2.encrypted-unlock": YES,
"org.freedesktop.udisks2.eject-media": YES,
"org.freedesktop.udisks2.power-off-drive": YES,
// required for udisks2 if using udiskie from another seat (e.g. systemd):
"org.freedesktop.udisks2.filesystem-mount-other-seat": YES,
"org.freedesktop.udisks2.filesystem-unmount-others": YES,
"org.freedesktop.udisks2.encrypted-unlock-other-seat": YES,
"org.freedesktop.udisks2.eject-media-other-seat": YES,
"org.freedesktop.udisks2.power-off-drive-other-seat": YES
};
if (subject.isInGroup("storage")) {
return permission[action.id];
}
});
EOF
# Add user 'alarm' to the group 'storage'
gpasswd -a alarm storage
# Create a systemd unit file that runs udiskie
cat > /etc/systemd/system/udiskie.service <<EOF
[Unit]
Description=Udiskie
[Service]
ExecStart=/usr/bin/udiskie
User=alarm
[Install]
WantedBy=multi-user.target
EOF
# Enable the udiskie unit so it starts at boot time
systemctl enable udiskie.service
}
install_music_app() {
# Add user 'alarm' to the group 'gpio'
gpasswd -a alarm gpio
# Retrieve source code for the music player
if [ ! -d "/home/alarm/pi-music-player" ]; then
git clone https://github.com/seymour7/pi-music-player.git /home/alarm/pi-music-player
fi
# Create a systemd unit file that runs the music player
cat > /etc/systemd/system/pi-music-player.service <<EOF
[Unit]
Description=Pi Music Player
[Service]
WorkingDirectory=/home/alarm/pi-music-player
ExecStart=/home/alarm/pi-music-player/player.py
User=alarm
[Install]
WantedBy=multi-user.target
EOF
# Enable the unit so the music player starts at boot time
systemctl enable pi-music-player.service
}
set -ex
if [ "$1" == "chroot" ]
then
configure
else
setup
fi