Create the backing files for the USB drives on a separate partition.

This commit is contained in:
cimryan
2018-10-14 19:37:24 -07:00
parent 98cecb4cbf
commit 4970fea833
4 changed files with 172 additions and 81 deletions

View File

@@ -0,0 +1,15 @@
#!/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 ))
parted -m /dev/mmcblk0 u s mkpart primary ext4 "$FIRST_BACKINGFILES_PARTITION_SECTOR" 100%
mkfs.ext4 /dev/mmcblk0p3
echo "/dev/mmcblk0p3 $BACKINGFILES_MOUNTPOINT ext4 auto,rw,noatime 0 2" >> /etc/fstab

View File

@@ -0,0 +1,37 @@
#!/bin/bash -eu
SPACE_TO_USE="$1"
CAM_PERCENT="$2"
BACKINGFILES_MOUNTPOINT="$3"
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"
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
}
CAM_DISK_SIZE="$(( $SPACE_TO_USE * $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_PERCENT="$(( 100 - $CAM_PERCENT ))"
MUSIC_DISK_SIZE="$(( $SPACE_TO_USE * $MUSIC_PERCENT / 100 ))"
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

View File

@@ -23,7 +23,7 @@ Write-Verbose "Updating $configPath ..."
Write-Verbose "Updating $cmdlinePath ..." Write-Verbose "Updating $cmdlinePath ..."
$cmdlinetxtContent = gc -Raw $cmdlinePath $cmdlinetxtContent = gc -Raw $cmdlinePath
$cmdlinetxtContent.Replace("rootwait", "rootwait modules-load=dwc2,g_ether") | Out-File -FilePath $cmdlinePath -Encoding utf8 $cmdlinetxtContent.Replace("rootwait", "rootwait modules-load=dwc2,g_ether").Replace(" init=/usr/lib/raspi-config/init_resize.sh", "") | Out-File -FilePath $cmdlinePath -Encoding utf8
Write-Verbose "Enabling SSH ..." Write-Verbose "Enabling SSH ..."
[System.IO.File]::CreateText($sshPath).Dispose() [System.IO.File]::CreateText($sshPath).Dispose()

View File

@@ -1,6 +1,8 @@
#!/bin/bash -eu #!/bin/bash -eu
if [ "$(whoami)" != "root" ] BRANCH=u/cimryan/readonlyrootfs
if ! [ $(id -u) = 0 ]
then then
echo "STOP: Run sudo -i." echo "STOP: Run sudo -i."
exit 1 exit 1
@@ -15,74 +17,88 @@ function check_variable () {
fi fi
} }
check_variable "archiveserver" function check_archive_server_reachable () {
check_variable "sharename" echo "Verifying that the archive server $archiveserver is reachable..."
check_variable "shareuser" local serverunreachable=false
check_variable "sharepassword" ping -c 1 -w 1 "$archiveserver" 1>/dev/null 2>&1 || serverunreachable=true
check_variable "campercent"
serverunreachable=false if [ "$serverunreachable" = true ]
ping -c 1 -w 1 "$archiveserver" 1>/dev/null 2>&1 || serverunreachable=true then
if [ "$serverunreachable" = true ]
then
echo "STOP: The archive server $archiveserver is unreachable. Try specifying its IP address instead." echo "STOP: The archive server $archiveserver is unreachable. Try specifying its IP address instead."
exit 1 exit 1
fi fi
archiveserverip="$(getent hosts $archiveserver | cut -d' ' -f1)" echo "The archive server is reachable."
available_space="$(($(df --output=avail / | tail -1) - 1000000))"
if [ "$available_space" -lt 0 ]
then
echo "STOP: The MicroSD card is too small."
exit 1
fi
function add_drive () {
local name="$1"
local label="$2"
local size="$3"
local filename="$4"
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
} }
pushd ~ function check_available_space () {
echo "Verifying that there is sufficient space available on the MicroSD card..."
local available_space="$1"
if [ "$available_space" -lt 4294967296 ]
then
echo "STOP: The MicroSD card is too small."
exit 1
fi
cp /boot/cmdline.txt ~ echo "There is sufficient space available."
cat ~/cmdline.txt | sed 's/[[:space:]]\+modules-load=[^ [:space:]]\+//' | sed 's/rootwait/rootwait modules-load=dwc2/' > /boot/cmdline.txt }
rm ~/cmdline.txt
mkdir /mnt/archive 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/[[:space:]]\+modules-load=[^ [:space:]]\+//' | sed 's/rootwait/rootwait modules-load=dwc2/' > /boot/cmdline.txt
rm ~/cmdline.txt
echo "Fixed cmdline.txt."
}
echo "" >> /etc/fstab BACKINGFILES_MOUNTPOINT=/backingfiles
echo "//$archiveserverip/$sharename /mnt/archive cifs vers=3,credentials=/root/.teslaCamArchiveCredentials,iocharset=utf8,file_mode=0777,dir_mode=0777 0" >> /etc/fstab
echo "username=$shareuser" > /root/.teslaCamArchiveCredentials function create_usb_drive_backing_files () {
echo "password=$sharepassword" >> /root/.teslaCamArchiveCredentials wget https://raw.githubusercontent.com/cimryan/teslausb/"$BRANCH"/windows_archive/create-backingfiles-partition.sh
chmod +x ./create-backingfiles-partition.sh
./create-backingfiles-partition.sh "$BACKINGFILES_MOUNTPOINT"
mkdir /root/bin echo "Mounting the partition for the backing files..."
mount /backingfiles
echo "Mounted the partition for the backing files."
wget https://raw.githubusercontent.com/cimryan/teslausb/master/windows_archive/archiveloop wget https://raw.githubusercontent.com/cimryan/teslausb/"$BRANCH"/windows_archive/create-backingfiles.sh
sed s/ARCHIVE_HOST_NAME=archiveserver/ARCHIVE_HOST_NAME=$archiveserver/ ~/archiveloop > /root/bin/archiveloop chmod +x ./create-backingfiles.sh
rm ~/archiveloop ./create-backingfiles.sh "$AVAILABLE_SPACE" "$campercent" "$BACKINGFILES_MOUNTPOINT"
chmod +x /root/bin/archiveloop }
pushd /root/bin function configure_archive () {
wget https://raw.githubusercontent.com/cimryan/teslausb/master/windows_archive/archive-teslacam-clips echo "Configuring the archive..."
chmod +x archive-teslacam-clips mkdir /mnt/archive
popd local archive_server_ip_address="$(getent hosts $archiveserver | cut -d' ' -f1)"
echo "//$archive_server_ip_address/$sharename /mnt/archive cifs vers=3,credentials=/root/.teslaCamArchiveCredentials,iocharset=utf8,file_mode=0777,dir_mode=0777 0" >> /etc/fstab
echo "#!/bin/bash -eu" > ~/rc.local echo "username=$shareuser" > /root/.teslaCamArchiveCredentials
tail -n +2 /etc/rc.local | sed '$d' >> ~/rc.local echo "password=$sharepassword" >> /root/.teslaCamArchiveCredentials
echo "Configured the archive."
}
function configure_archive_scripts () {
echo "Configuring the archive scripts..."
mkdir /root/bin
wget https://raw.githubusercontent.com/cimryan/teslausb/"$BRANCH"/windows_archive/archiveloop
sed s/ARCHIVE_HOST_NAME=archiveserver/ARCHIVE_HOST_NAME=$archiveserver/ ~/archiveloop > /root/bin/archiveloop
rm ~/archiveloop
chmod +x /root/bin/archiveloop
pushd /root/bin
wget https://raw.githubusercontent.com/cimryan/teslausb/"$BRANCH"/windows_archive/archive-teslacam-clips
chmod +x archive-teslacam-clips
popd
echo "Configured the archive scripts."
}
function configure rc_local () {
echo "Configuring /etc/rc.local to run the archive scripts at startup..."
echo "#!/bin/bash -eu" > ~/rc.local
tail -n +2 /etc/rc.local | sed '$d' >> ~/rc.local
cat << 'EOF' >> ~/rc.local cat << 'EOF' >> ~/rc.local
LOGFILE=/tmp/rc.local.log LOGFILE=/tmp/rc.local.log
@@ -97,26 +113,49 @@ log "All done"
exit 0 exit 0
EOF EOF
cat ~/rc.local > /etc/rc.local cat ~/rc.local > /etc/rc.local
rm ~/rc.local rm ~/rc.local
echo "Configured rc.local."
}
cam_disk_size="$(( $available_space * $campercent / 100 ))" function configure_hostname () {
cam_disk_file_name="/cam_disk.bin" echo "Configuring the hostname..."
add_drive "cam" "CAM" "$cam_disk_size" "$cam_disk_file_name"
if [ "$campercent" -lt 100 ] local new_host_name="teslausb"
then cp /etc/hosts ~
musicpercent="$(( 100 - $campercent ))" sed 's/raspberrypi/$new_host_name/g' ~/hosts > /etc/hosts
music_disk_size="$(( $available_space * $musicpercent / 100 ))"
music_disk_file_name="/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" > /etc/modprobe.d/g_mass_storage.conf
else
echo "options g_mass_storage file=$cam_disk_file_name removable=1 ro=0 stall=0 iSerialNumber=123456" > /etc/modprobe.d/g_mass_storage.conf
fi
cp /etc/hosts ~ cp /etc/hostname ~
sed s/raspberrypi/teslausb/g ~/hosts > /etc/hosts sed 's/raspberrypi/$new_host_name/g' ~/hostname > /etc/hostname
echo "Configured the hostname."
}
cp /etc/hostname ~ echo "Verifying environment variables..."
sed s/raspberrypi/teslausb/g ~/hostname > /etc/hostname
check_variable "archiveserver"
check_variable "sharename"
check_variable "shareuser"
check_variable "sharepassword"
check_variable "campercent"
check_archive_server_reachable
AVAILABLE_SPACE="$( parted -m /dev/mmcblk0 u b print free | tail -1 | cut -d ":" -f 4 | sed 's/B//g' )"
check_available_space "$AVAILABLE_SPACE"
pushd ~
fix_cmdline_txt_modules_load
echo "" >> /etc/fstab
create_usb_drive_backing_files
configure_archive
configure_archive_scripts
configure rc_local
configure_hostname