Separate files into those used for setup and those used at runtime.

Files used for both will go in the run tree.
This commit is contained in:
cimryan
2018-10-24 20:55:29 -07:00
parent ef10b2ec67
commit e875763e58
27 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
#!/bin/bash -eu
BACKINGFILES_MOUNTPOINT="$1"
PARTITION_TABLE=$(parted -m /dev/mmcblk0 unit s print)
ROOT_PARTITION_LINE=$(echo "$PARTITION_TABLE" | grep -e "^2:")
LAST_ROOT_PARTITION_SECTOR=$(echo "$ROOT_PARTITION_LINE" | sed 's/s//g' | cut -d ":" -f 3)
FIRST_BACKINGFILES_PARTITION_SECTOR=$(( $LAST_ROOT_PARTITION_SECTOR + 1 ))
ORIGINAL_DISK_IDENTIFIER=$( fdisk -l /dev/mmcblk0 | grep -e "^Disk identifier" | sed "s/Disk identifier: 0x//" )
parted -m /dev/mmcblk0 u s mkpart primary ext4 "$FIRST_BACKINGFILES_PARTITION_SECTOR" 100%
NEW_DISK_IDENTIFIER=$( fdisk -l /dev/mmcblk0 | grep -e "^Disk identifier" | sed "s/Disk identifier: 0x//" )
sed -i "s/${ORIGINAL_DISK_IDENTIFIER}/${NEW_DISK_IDENTIFIER}/g" /etc/fstab
sed -i "s/${ORIGINAL_DISK_IDENTIFIER}/${NEW_DISK_IDENTIFIER}/" /boot/cmdline.txt
mkfs.ext4 -F /dev/mmcblk0p3
echo "/dev/mmcblk0p3 $BACKINGFILES_MOUNTPOINT ext4 auto,rw,noatime 0 2" >> /etc/fstab

View File

@@ -0,0 +1,46 @@
#!/bin/bash -eu
CAM_PERCENT="$1"
BACKINGFILES_MOUNTPOINT="$2"
G_MASS_STORAGE_CONF_FILE_NAME=/etc/modprobe.d/g_mass_storage.conf
function add_drive () {
local name="$1"
local label="$2"
local size="$3"
local filename="$4"
echo "Allocating ${size}K for $filename..."
fallocate -l "$size"K "$filename"
mkfs.vfat "$filename" -F 32 -n "$label"
local mountpoint=/mnt/"$name"
mkdir "$mountpoint"
echo "$filename $mountpoint vfat noauto,users,umask=000 0 0" >> /etc/fstab
}
function create_teslacam_directory () {
mount /mnt/cam
mkdir /mnt/cam/TeslaCam
umount /mnt/cam
}
FREE_1K_BLOCKS="$(df --output=avail --block-size=1K /backingfiles/ | tail -n 1)"
CAM_DISK_SIZE="$(( $FREE_1K_BLOCKS * $CAM_PERCENT / 100 ))"
CAM_DISK_FILE_NAME="$BACKINGFILES_MOUNTPOINT/cam_disk.bin"
add_drive "cam" "CAM" "$CAM_DISK_SIZE" "$CAM_DISK_FILE_NAME"
if [ "$CAM_PERCENT" -lt 100 ]
then
MUSIC_DISK_SIZE="$(df --output=avail --block-size=1K /backingfiles/ | tail -n 1)"
MUSIC_DISK_FILE_NAME="$BACKINGFILES_MOUNTPOINT/music_disk.bin"
add_drive "music" "MUSIC" "$MUSIC_DISK_SIZE" "$MUSIC_DISK_FILE_NAME"
echo "options g_mass_storage file=$CAM_DISK_FILE_NAME,$MUSIC_DISK_FILE_NAME removable=1,1 ro=0,0 stall=0 iSerialNumber=123456" > "$G_MASS_STORAGE_CONF_FILE_NAME"
else
echo "options g_mass_storage file=$CAM_DISK_FILE_NAME removable=1 ro=0 stall=0 iSerialNumber=123456" > "$G_MASS_STORAGE_CONF_FILE_NAME"
fi
create_teslacam_directory

View File

@@ -0,0 +1,49 @@
#!/bin/bash
# Adapted from https://github.com/adafruit/Raspberry-Pi-Installer-Scripts/blob/master/read-only-fs.sh
function append_cmdline_txt_param() {
local toAppend="$1"
sed -i "s/\'/ ${toAppend}/g" /boot/cmdline.txt >/dev/null
}
echo "Updating package index files..."
apt-get update
echo "Removing unwanted packages..."
apt-get remove -y --force-yes --purge triggerhappy logrotate dphys-swapfile fake-hwclock
apt-get -y --force-yes autoremove --purge
# Replace log management with busybox (use logread if needed)
echo "Installing ntp and busybox-syslogd..."
apt-get -y --force-yes install ntp busybox-syslogd; dpkg --purge rsyslog
echo "Configuring system..."
# Add fastboot, noswap and/or ro to end of /boot/cmdline.txt
append_cmdline_txt_param fastboot
append_cmdline_txt_param noswap
append_cmdline_txt_param ro
# Move /var/spool to /tmp
rm -rf /var/spool
ln -s /tmp /var/spool
# Change spool permissions in var.conf (rondie/Margaret fix)
sed -i "s/spool\s*0755/spool 1777/g" /usr/lib/tmpfiles.d/var.conf >/dev/null
# Move dhcpd.resolv.conf to tmpfs
touch /tmp/dhcpcd.resolv.conf
rm /etc/resolv.conf
ln -s /tmp/dhcpcd.resolv.conf /etc/resolv.conf
# Update /etc/fstab
# make /boot read-only
# make / read-only
# tmpfs /var/log tmpfs nodev,nosuid 0 0
# tmpfs /var/tmp tmpfs nodev,nosuid 0 0
# tmpfs /tmp tmpfs nodev,nosuid 0 0
sed -i -r "s@(/boot\s+vfat\s+\S+)@\1,ro@" /etc/fstab
sed -i -r "s@(/\s+ext4\s+\S+)@\1,ro@" /etc/fstab
echo "" >> /etc/fstab
echo "tmpfs /var/log tmpfs nodev,nosuid 0 0" >> /etc/fstab
echo "tmpfs /var/tmp tmpfs nodev,nosuid 0 0" >> /etc/fstab
echo "tmpfs /tmp tmpfs nodev,nosuid 0 0" >> /etc/fstab

247
setup/pi/setup-teslausb Normal file
View File

@@ -0,0 +1,247 @@
#!/bin/bash -eu
USER_ENABLED_PUSHOVER=false
REPO=${REPO:-cimryan}
BRANCH=${BRANCH:-master}
if ! [ $(id -u) = 0 ]
then
echo "STOP: Run sudo -i."
exit 1
fi
function check_variable () {
local var_name="$1"
if [ -z "${!var_name+x}" ]
then
echo "STOP: Define the variable $var_name like this: export $var_name=value"
exit 1
fi
}
function check_pushover_enabled () {
if [ ! -z "${pushover_enabled+x}" ]
then
if [ ! -n "${pushover_user_key+x}" ] || [ ! -n "${pushover_app_key+x}" ]
then
echo "STOP: You're trying to setup Pushover but didn't provide your User and/or App key."
echo "Define the variables like this:"
echo "export pushover_user_key=put_your_userkey_here"
echo "export pushover_app_key=put_your_appkey_here"
exit 1
elif [ "${pushover_user_key}" = "put_your_userkey_here" ] || [ "${pushover_app_key}" = "put_your_appkey_here" ]
then
echo "STOP: You're trying to setup Pushover, but didn't replace the default User and App key values."
exit 1
else
USER_ENABLED_PUSHOVER=true
echo "export pushover_enabled=true" > /root/.teslaCamPushoverCredentials
echo "export pushover_user_key=$pushover_user_key" >> /root/.teslaCamPushoverCredentials
echo "export pushover_app_key=$pushover_app_key" >> /root/.teslaCamPushoverCredentials
fi
fi
}
function check_available_space () {
echo "Verifying that there is sufficient space available on the MicroSD card..."
local available_space="$( parted -m /dev/mmcblk0 u b print free | tail -1 | cut -d ":" -f 4 | sed 's/B//g' )"
if [ "$available_space" -lt 4294967296 ]
then
echo "STOP: The MicroSD card is too small."
exit 1
fi
echo "There is sufficient space available."
}
function get_script () {
local local_path="$1"
local name="$2"
local remote_path="${3:-}"
wget -O "$local_path/$name" https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/"$remote_path"/"$name"
chmod +x "$local_path/$name"
}
function get_ancillary_setup_scripts () {
get_script /tmp create-backingfiles-partition.sh
get_script /tmp create-backingfiles.sh
get_script /tmp make-root-fs-readonly.sh
}
function fix_cmdline_txt_modules_load ()
{
echo "Fixing the modules-load parameter in /boot/cmdline.txt..."
cp /boot/cmdline.txt ~
cat ~/cmdline.txt | sed 's/ modules-load=dwc2,g_ether/ modules-load=dwc2/' > /boot/cmdline.txt
rm ~/cmdline.txt
echo "Fixed cmdline.txt."
}
BACKINGFILES_MOUNTPOINT=/backingfiles
function create_usb_drive_backing_files () {
if [ ! -e "$BACKINGFILES_MOUNTPOINT" ]
then
mkdir "$BACKINGFILES_MOUNTPOINT"
fi
if [ ! -e /dev/mmcblk0p3 ]
then
/tmp/create-backingfiles-partition.sh "$BACKINGFILES_MOUNTPOINT"
fi
if ! findmnt --mountpoint /backingfiles
then
echo "Mounting the partition for the backing files..."
mount /backingfiles
echo "Mounted the partition for the backing files."
fi
if [ ! -e /backingfiles/*.bin ]
then
/tmp/create-backingfiles.sh "$campercent" "$BACKINGFILES_MOUNTPOINT"
fi
}
function configure_archive_scripts () {
echo "Configuring the archive scripts..."
get_script /root/bin archiveloop
get_script /root/bin archive-teslacam-clips
if [ $RSYNC_ENABLE = true ]
then
get_script /root/bin archive-clips.sh rsync_archive
get_script /root/bin connect-archive.sh rsync_archive
get_script /root/bin disconnect-archive.sh rsync_archive
else
get_script /root/bin archive-clips.sh cifs_archive
get_script /root/bin connect-archive.sh cifs_archive
get_script /root/bin disconnect-archive.sh cifs_archive
fi
get_script /root/bin remountfs_rw
echo "Configured the archive scripts."
}
function configure_pushover_scripts() {
get_script /root/bin send-pushover
}
function configure_rc_local () {
if grep -q archiveloop /etc/rc.local
then
return
fi
echo "Configuring /etc/rc.local to run the archive scripts at startup..."
echo "#!/bin/bash -eu" > ~/rc.local
echo "archiveserver=\"${archiveserver}\"" >> ~/rc.local
cat << 'EOF' >> ~/rc.local
LOGFILE=/tmp/rc.local.log
function log () {
echo "$( date )" >> "$LOGFILE"
echo "$1" >> "$LOGFILE"
}
log "Launching archival script..."
/root/bin/archiveloop "$archiveserver" &
log "All done"
exit 0
EOF
cat ~/rc.local > /etc/rc.local
rm ~/rc.local
echo "Configured rc.local."
}
function configure_hostname () {
echo "Configuring the hostname..."
local new_host_name="teslausb"
cp /etc/hosts ~
sed "s/raspberrypi/$new_host_name/g" ~/hosts > /etc/hosts
cp /etc/hostname ~
sed "s/raspberrypi/$new_host_name/g" ~/hostname > /etc/hostname
echo "Configured the hostname."
}
function make_root_fs_readonly () {
/tmp/make-root-fs-readonly.sh
}
echo "Verifying environment variables..."
RSYNC_ENABLE="${RSYNC_ENABLE:-false}"
if [ "$RSYNC_ENABLE" = true ]
then
check_variable "RSYNC_USER"
check_variable "RSYNC_SERVER"
export archiveserver="$RSYNC_SERVER"
check_variable "RSYNC_PATH"
else # Else for now, TODO allow both for more redundancy?
check_variable "sharename"
check_variable "shareuser"
check_variable "sharepassword"
export cifs_version="${cifs_version:-3}"
fi
check_variable "archiveserver"
check_variable "campercent"
check_pushover_enabled
if [ ! -e /root/bin ]
then
mkdir /root/bin
fi
if [ "$RSYNC_ENABLE" = true ]
then
get_script /root/bin verify-archive-configuration.sh rsync_archive
get_script /root/bin configure-archive.sh rsync_archive
else
get_script /root/bin verify-archive-configuration.sh cifs_archive
get_script /root/bin configure-archive.sh cifs_archive
get_script /root/bin write-archive-credentials-to.sh cifs_archive
fi
get_script /root/bin get-archiveserver-ip-address.sh
/root/bin/verify-archive-configuration.sh
check_available_space
get_ancillary_setup_scripts
pushd ~
configure_archive_scripts
configure_pushover_scripts
fix_cmdline_txt_modules_load
echo "" >> /etc/fstab
create_usb_drive_backing_files
/root/bin/configure-archive.sh
configure_rc_local
configure_hostname
make_root_fs_readonly
echo "All done."