Skip to content

Instantly share code, notes, and snippets.

@maditnerd
Last active August 31, 2016 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maditnerd/af9ab33fd137e02e43c671dda49475df to your computer and use it in GitHub Desktop.
Save maditnerd/af9ab33fd137e02e43c671dda49475df to your computer and use it in GitHub Desktop.
temporary script for samplerbox
#!/bin/bash -v
# CREATE A RASPBIAN JESSIE IMAGE
# SAMPLERBOX
#
# _______ __ __
#| __|.---.-.--------.-----.| |.-----.----.| |--.-----.--.--.
#|__ || _ | | _ || || -__| _|| _ | _ |_ _|
#|_______||___._|__|__|__| __||__||_____|__| |_____|_____|__.__|
# |__|
# needed: sudo apt-get update && sudo apt-get install -y cdebootstrap kpartx parted sshpass zip
# This script must be run on a Raspberry Pi
#
# nohup sudo ./samplerbox_maker.sh &
set -e
#############################
# 0 - Settings #
#############################
image_name=samplerbox_20160831.img
image_size=1200
hostname=samplerbox
root_password=root
http=http://mirrordirector.raspbian.org/raspbian/
#################################
# 1 - Partitions #
#################################
# Format partition
dd if=/dev/zero of=$image_name bs=1M count=$image_size
fdisk $image_name <<EOF
o
n
+64M
t
c
n
w
EOF
#Create the image file (with verbose)
kpartx -av $image_name
#Re-Read partition table
partprobe /dev/loop0
bootpart=/dev/mapper/loop0p1
rootpart=/dev/mapper/loop0p2
#Generate Boot (fat32) /Root (ext4) partitions on loop interface
mkdosfs -n BOOT $bootpart
mkfs.ext4 -L ROOT $rootpart
sync
#Display partition information (.img)
fdisk -l $image_name
##############################################################
# 2 - Generate Raspbian #
# This will takes some times... #
# #
# This will copied raspbian inside the folder: sdcard #
##############################################################
#Create directory ./sdcard
mkdir -v sdcard
#Mount Root partition as ./sdcard
mount -v -t ext4 -o sync $rootpart sdcard
#Create Raspbian Jessie system from scratch on ./sdcard
#(based on http://mirrordirector.raspbian.org/raspbian/)
cdebootstrap --arch=armhf jessie sdcard $http --include=locales --allow-unauthenticated
#Resync partitions
sync
######################
# 3 - Boot Partition #
######################
# Mount boot partition to ./sdcard/boot
mount -v -t vfat -o sync $bootpart sdcard/boot
###########################################
# 4 - APT-GET / System configuration #
###########################################
# Add root and root password
echo root:$root_password | chroot sdcard chpasswd
# APT-GET gpg key
wget -O sdcard/raspberrypi.gpg.key http://archive.raspberrypi.org/debian/raspberrypi.gpg.key
chroot sdcard apt-key add raspberrypi.gpg.key
rm -v sdcard/raspberrypi.gpg.key
wget -O sdcard/raspbian.public.key http://mirrordirector.raspbian.org/raspbian.public.key
chroot sdcard apt-key add raspbian.public.key
rm -v sdcard/raspbian.public.key
chroot sdcard apt-key list
# APT-GET sources.list
sed -i sdcard/etc/apt/sources.list -e "s/main/main contrib non-free firmware/"
echo "deb http://archive.raspberrypi.org/debian/ jessie main" >> sdcard/etc/apt/sources.list
# Time zone
echo Etc/UTC > sdcard/etc/timezone
# Keyboard layout / locales
echo en_GB.UTF-8 UTF-8 > sdcard/etc/locale.gen
cp -v /etc/default/keyboard sdcard/etc/default/keyboard
chroot sdcard locale-gen LANG="en_GB.UTF-8"
chroot sdcard dpkg-reconfigure -f noninteractive locales
# Hostname
echo $hostname > sdcard/etc/hostname
echo "127.0.1.1 $hostname" >> sdcard/etc/hosts
########################
# 5 - Boot #
########################
# Cmdline (disable console on TX/RX to use it for midi)
cat <<EOF > sdcard/boot/cmdline.txt
root=/dev/mmcblk0p2 ro rootwait console=tty1 selinux=0 plymouth.enable=0 smsc95xx.turbo_mode=N dwc_otg.lpm_enable=0 elevator=noop bcm2708.uart_clock=3000000
EOF
# Boot kernel configuration
cat <<EOF > sdcard/boot/config.txt
device_tree_param=i2c_arm=on
init_uart_clock=2441406
init_uart_baud=38400
gpu_mem=64
boot_delay=0
disable_splash=1
disable_audio_dither=1
EOF
# Automount usb
cat <<EOF > sdcard/etc/fstab
/dev/sda1 /media auto nofail 0 0
EOF
#/dev/sdb1 /media auto nofail 0 0
#######################
# 6- Network #
#######################
# Network configuration (eth0 dhcp)
# "allow-hotplug" instead of "auto" very important to prevent blocking on boot if no network present
cat <<EOF > sdcard/etc/network/interfaces
auto lo
iface lo inet loopback
allow-hotplug eth0
iface eth0 inet dhcp
EOF
#echo "timeout 10;" >> sdcard/etc/dhcp/dhclient.conf
#echo "retry 1;" >> sdcard/etc/dhcp/dhclient.conf
###########################
# 7 - Dependencies/Update #
###########################
# APT-GET update/upgrade
chroot sdcard apt-get update
chroot sdcard apt-get -y upgrade
chroot sdcard apt-get -y dist-upgrade
# Raspbian
chroot sdcard apt-get -y install libraspberrypi0 libraspberrypi-bin libraspberrypi-dev raspberrypi-bootloader ssh wireless-tools wpasupplicant usbutils
chroot sdcard apt-get clean
# Python / Audio Dependencies
chroot sdcard apt-get -y install build-essential python-dev python-pip cython python-smbus python-numpy python-rpi.gpio python-serial portaudio19-dev alsa-utils git libportaudio2 libffi-dev
chroot sdcard apt-get clean
chroot sdcard apt-get autoremove -y
# SSH - Allowing root to log into $release with password... "
sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' sdcard/etc/ssh/sshd_config
#############################
# 8 - Samplerbox #
#############################
# cffi / python-sounddevice / rt-midi / pyaudio / samplerbox
chroot sdcard sh -c "pip install cffi ; pip install sounddevice"
chroot sdcard sh -c "cd /root ; git clone https://github.com/superquadratic/rtmidi-python.git ; cd rtmidi-python ; python setup.py install ; cd .. ; git clone http://people.csail.mit.edu/hubert/git/pyaudio.git ; cd pyaudio ; python setup.py install ; cd .. ; git clone https://github.com/josephernest/SamplerBox.git ; cd SamplerBox ; python setup.py build_ext --inplace"
# Startup script
cat <<EOF > sdcard/root/SamplerBox/samplerbox.sh
#!/bin/sh
python /root/SamplerBox/samplerbox.py
EOF
chmod 777 sdcard/root/SamplerBox/samplerbox.sh
# Systemd startup configuration
cat <<EOF > sdcard/etc/systemd/system/samplerbox.service
[Unit]
Description=Starts SamplerBox
DefaultDependencies=false
[Service]
Type=simple
ExecStart=/root/SamplerBox/samplerbox.sh
WorkingDirectory=/root/SamplerBox/
[Install]
WantedBy=local-fs.target
EOF
# Enable systemd service
chroot sdcard systemctl enable /etc/systemd/system/samplerbox.service
# Terminal banner
cat <<EOF > sdcard/etc/motd
Welcome to SamplerBox!
######################
* The filesystem is read-only, see http://www.samplerbox.org/faq#readonly
Here is how to remount as read-write: mount -o remount,rw /
* The SamplerBox program (/root/SamplerBox/samplerbox.py) should be
up and running. If not, try: systemctl status samplerbox
######################
EOF
# Setup Alsa volume
sed -i 's/ENV{pvolume}:="-20dB"/ENV{pvolume}:="-10dB"/' sdcard/usr/share/alsa/init/default
# Samplerbox configuration modification
sed -i 's/USE_SERIALPORT_MIDI = False/USE_SERIALPORT_MIDI = True/' sdcard/root/SamplerBox/samplerbox.py
sed -i 's/USE_I2C_7SEGMENTDISPLAY = False/USE_I2C_7SEGMENTDISPLAY = True/' sdcard/root/SamplerBox/samplerbox.py
sed -i 's/USE_BUTTONS = False/USE_BUTTONS = True/' sdcard/root/SamplerBox/samplerbox.py
sed -i 's,SAMPLES_DIR = ".",SAMPLES_DIR = "/media/",' sdcard/root/SamplerBox/samplerbox.py
#######################
# 9 - /etc/modules #
#######################
# Enable modules in /etc/modules
echo 'i2c-bcm2708' >> sdcard/etc/modules
echo 'i2c-dev' >> sdcard/etc/modules
echo 'snd_bcm2835' >> sdcard/etc/modules
# Sync partitions
sync
# Unmounting mount points
umount -v sdcard/boot
umount -v sdcard
# Delete partition mapping
kpartx -dv $image_name
#dd if=$image_name of=/dev/sda bs=1M
# Sync partitions
sync
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment