From 4970fea8332a1d4adfa4aa21cc1e0e495627135f Mon Sep 17 00:00:00 2001 From: cimryan Date: Sun, 14 Oct 2018 19:37:24 -0700 Subject: [PATCH 01/16] Create the backing files for the USB drives on a separate partition. --- .../create-backingfiles-partition.sh | 15 ++ windows_archive/create-backingfiles.sh | 37 ++++ windows_archive/setup-piForHeadlessConfig.ps1 | 2 +- windows_archive/setup-teslausb | 199 +++++++++++------- 4 files changed, 172 insertions(+), 81 deletions(-) create mode 100644 windows_archive/create-backingfiles-partition.sh create mode 100644 windows_archive/create-backingfiles.sh diff --git a/windows_archive/create-backingfiles-partition.sh b/windows_archive/create-backingfiles-partition.sh new file mode 100644 index 0000000..df41f4f --- /dev/null +++ b/windows_archive/create-backingfiles-partition.sh @@ -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 \ No newline at end of file diff --git a/windows_archive/create-backingfiles.sh b/windows_archive/create-backingfiles.sh new file mode 100644 index 0000000..a5376b7 --- /dev/null +++ b/windows_archive/create-backingfiles.sh @@ -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 diff --git a/windows_archive/setup-piForHeadlessConfig.ps1 b/windows_archive/setup-piForHeadlessConfig.ps1 index d5c682f..ff951ea 100644 --- a/windows_archive/setup-piForHeadlessConfig.ps1 +++ b/windows_archive/setup-piForHeadlessConfig.ps1 @@ -23,7 +23,7 @@ Write-Verbose "Updating $configPath ..." Write-Verbose "Updating $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 ..." [System.IO.File]::CreateText($sshPath).Dispose() diff --git a/windows_archive/setup-teslausb b/windows_archive/setup-teslausb index 4e40a1b..bf844f1 100644 --- a/windows_archive/setup-teslausb +++ b/windows_archive/setup-teslausb @@ -1,6 +1,8 @@ #!/bin/bash -eu -if [ "$(whoami)" != "root" ] +BRANCH=u/cimryan/readonlyrootfs + +if ! [ $(id -u) = 0 ] then echo "STOP: Run sudo -i." exit 1 @@ -15,74 +17,88 @@ function check_variable () { fi } -check_variable "archiveserver" -check_variable "sharename" -check_variable "shareuser" -check_variable "sharepassword" -check_variable "campercent" - -serverunreachable=false -ping -c 1 -w 1 "$archiveserver" 1>/dev/null 2>&1 || serverunreachable=true - -if [ "$serverunreachable" = true ] -then - echo "STOP: The archive server $archiveserver is unreachable. Try specifying its IP address instead." - exit 1 -fi - -archiveserverip="$(getent hosts $archiveserver | cut -d' ' -f1)" - -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 +function check_archive_server_reachable () { + echo "Verifying that the archive server $archiveserver is reachable..." + local serverunreachable=false + ping -c 1 -w 1 "$archiveserver" 1>/dev/null 2>&1 || serverunreachable=true + + if [ "$serverunreachable" = true ] + then + echo "STOP: The archive server $archiveserver is unreachable. Try specifying its IP address instead." + exit 1 + fi + + echo "The archive server is reachable." } -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 + + echo "There is sufficient space available." +} -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 +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." +} -mkdir /mnt/archive +BACKINGFILES_MOUNTPOINT=/backingfiles -echo "" >> /etc/fstab -echo "//$archiveserverip/$sharename /mnt/archive cifs vers=3,credentials=/root/.teslaCamArchiveCredentials,iocharset=utf8,file_mode=0777,dir_mode=0777 0" >> /etc/fstab +function create_usb_drive_backing_files () { + 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" + + 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/"$BRANCH"/windows_archive/create-backingfiles.sh + chmod +x ./create-backingfiles.sh + ./create-backingfiles.sh "$AVAILABLE_SPACE" "$campercent" "$BACKINGFILES_MOUNTPOINT" +} -echo "username=$shareuser" > /root/.teslaCamArchiveCredentials -echo "password=$sharepassword" >> /root/.teslaCamArchiveCredentials +function configure_archive () { + echo "Configuring the archive..." + mkdir /mnt/archive + 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 "username=$shareuser" > /root/.teslaCamArchiveCredentials + echo "password=$sharepassword" >> /root/.teslaCamArchiveCredentials + echo "Configured the archive." +} -mkdir /root/bin +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." +} -wget https://raw.githubusercontent.com/cimryan/teslausb/master/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/master/windows_archive/archive-teslacam-clips -chmod +x archive-teslacam-clips -popd - -echo "#!/bin/bash -eu" > ~/rc.local -tail -n +2 /etc/rc.local | sed '$d' >> ~/rc.local +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 LOGFILE=/tmp/rc.local.log @@ -97,26 +113,49 @@ log "All done" exit 0 EOF -cat ~/rc.local > /etc/rc.local -rm ~/rc.local + cat ~/rc.local > /etc/rc.local + rm ~/rc.local + echo "Configured rc.local." +} -cam_disk_size="$(( $available_space * $campercent / 100 ))" -cam_disk_file_name="/cam_disk.bin" -add_drive "cam" "CAM" "$cam_disk_size" "$cam_disk_file_name" +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." +} -if [ "$campercent" -lt 100 ] -then - musicpercent="$(( 100 - $campercent ))" - 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 +echo "Verifying environment variables..." -cp /etc/hosts ~ -sed s/raspberrypi/teslausb/g ~/hosts > /etc/hosts +check_variable "archiveserver" +check_variable "sharename" +check_variable "shareuser" +check_variable "sharepassword" +check_variable "campercent" -cp /etc/hostname ~ -sed s/raspberrypi/teslausb/g ~/hostname > /etc/hostname \ No newline at end of file +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 \ No newline at end of file From aedff01c72a852a1ccd5e690692461e942f441c5 Mon Sep 17 00:00:00 2001 From: cimryan Date: Sun, 14 Oct 2018 20:01:18 -0700 Subject: [PATCH 02/16] Typo in configure_rc_local name. --- windows_archive/setup-teslausb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/windows_archive/setup-teslausb b/windows_archive/setup-teslausb index bf844f1..a083c2e 100644 --- a/windows_archive/setup-teslausb +++ b/windows_archive/setup-teslausb @@ -55,6 +55,7 @@ function fix_cmdline_txt_modules_load () BACKINGFILES_MOUNTPOINT=/backingfiles function create_usb_drive_backing_files () { + mkdir "$BACKINGFILES_MOUNTPOINT" 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" @@ -95,7 +96,7 @@ function configure_archive_scripts () { echo "Configured the archive scripts." } -function configure rc_local () { +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 @@ -156,6 +157,6 @@ configure_archive configure_archive_scripts -configure rc_local +configure_rc_local configure_hostname \ No newline at end of file From 9c3daa3d9148cd18fb22fc06d1c154f03169353f Mon Sep 17 00:00:00 2001 From: cimryan Date: Sun, 14 Oct 2018 20:02:42 -0700 Subject: [PATCH 03/16] Remove unnecessary recreation of filesystem on the backing files partition. --- windows_archive/create-backingfiles-partition.sh | 2 -- 1 file changed, 2 deletions(-) diff --git a/windows_archive/create-backingfiles-partition.sh b/windows_archive/create-backingfiles-partition.sh index df41f4f..ebdb198 100644 --- a/windows_archive/create-backingfiles-partition.sh +++ b/windows_archive/create-backingfiles-partition.sh @@ -10,6 +10,4 @@ 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 \ No newline at end of file From c0cab4d6246931cc1ff8e9297d46bff76ad26784 Mon Sep 17 00:00:00 2001 From: cimryan Date: Sun, 14 Oct 2018 20:15:46 -0700 Subject: [PATCH 04/16] Correct the space allocation for the baking files. --- windows_archive/create-backingfiles.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/windows_archive/create-backingfiles.sh b/windows_archive/create-backingfiles.sh index a5376b7..7c29783 100644 --- a/windows_archive/create-backingfiles.sh +++ b/windows_archive/create-backingfiles.sh @@ -12,7 +12,7 @@ function add_drive () { local size="$3" local filename="$4" - fallocate -l "$size"K "$filename" + fallocate -l "$size" "$filename" mkfs.vfat "$filename" -F 32 -n "$label" local mountpoint=/mnt/"$name" From c1e840fa9f7053fc52285878148e48ee3b5418f7 Mon Sep 17 00:00:00 2001 From: cimryan Date: Sun, 14 Oct 2018 20:39:12 -0700 Subject: [PATCH 05/16] Make calculations of backng file size in terms of free 1k blocks --- windows_archive/create-backingfiles.sh | 14 +++++++------- windows_archive/setup-teslausb | 9 +++++---- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/windows_archive/create-backingfiles.sh b/windows_archive/create-backingfiles.sh index 7c29783..df6cbd2 100644 --- a/windows_archive/create-backingfiles.sh +++ b/windows_archive/create-backingfiles.sh @@ -1,8 +1,7 @@ #!/bin/bash -eu -SPACE_TO_USE="$1" -CAM_PERCENT="$2" -BACKINGFILES_MOUNTPOINT="$3" +CAM_PERCENT="$1" +BACKINGFILES_MOUNTPOINT="$2" G_MASS_STORAGE_CONF_FILE_NAME=/etc/modprobe.d/g_mass_storage.conf @@ -12,7 +11,7 @@ function add_drive () { local size="$3" local filename="$4" - fallocate -l "$size" "$filename" + fallocate -l "$size"K "$filename" mkfs.vfat "$filename" -F 32 -n "$label" local mountpoint=/mnt/"$name" @@ -21,14 +20,15 @@ function add_drive () { echo "$filename $mountpoint vfat noauto,users,umask=000 0 0" >> /etc/fstab } -CAM_DISK_SIZE="$(( $SPACE_TO_USE * $CAM_PERCENT / 100 ))" +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_PERCENT="$(( 100 - $CAM_PERCENT ))" - MUSIC_DISK_SIZE="$(( $SPACE_TO_USE * $MUSIC_PERCENT / 100 ))" + 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 diff --git a/windows_archive/setup-teslausb b/windows_archive/setup-teslausb index a083c2e..8f6bc0f 100644 --- a/windows_archive/setup-teslausb +++ b/windows_archive/setup-teslausb @@ -33,7 +33,9 @@ function check_archive_server_reachable () { function check_available_space () { echo "Verifying that there is sufficient space available on the MicroSD card..." - local available_space="$1" + + 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." @@ -66,7 +68,7 @@ function create_usb_drive_backing_files () { wget https://raw.githubusercontent.com/cimryan/teslausb/"$BRANCH"/windows_archive/create-backingfiles.sh chmod +x ./create-backingfiles.sh - ./create-backingfiles.sh "$AVAILABLE_SPACE" "$campercent" "$BACKINGFILES_MOUNTPOINT" + ./create-backingfiles.sh "$campercent" "$BACKINGFILES_MOUNTPOINT" } function configure_archive () { @@ -141,8 +143,7 @@ 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" +check_available_space pushd ~ From fc2a436c7ec38469d263746ca58d8e5453be749f Mon Sep 17 00:00:00 2001 From: cimryan Date: Sun, 14 Oct 2018 20:47:32 -0700 Subject: [PATCH 06/16] Fix the setting of the host name. --- windows_archive/setup-teslausb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/windows_archive/setup-teslausb b/windows_archive/setup-teslausb index 8f6bc0f..63962ff 100644 --- a/windows_archive/setup-teslausb +++ b/windows_archive/setup-teslausb @@ -126,10 +126,10 @@ function configure_hostname () { local new_host_name="teslausb" cp /etc/hosts ~ - sed 's/raspberrypi/$new_host_name/g' ~/hosts > /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 + sed "s/raspberrypi/$new_host_name/g" ~/hostname > /etc/hostname echo "Configured the hostname." } From 03aa06ddb62d8a2e92e8deb9492104aec5b11553 Mon Sep 17 00:00:00 2001 From: cimryan Date: Sun, 14 Oct 2018 21:07:02 -0700 Subject: [PATCH 07/16] Reference u/cimryan/readonlyrootfs --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c105a54..ee8361a 100644 --- a/README.md +++ b/README.md @@ -136,7 +136,7 @@ Now that you have a shell on the Pi you can turn the Pi into a smart USB drive. ``` 1. Run these commands: ``` - wget https://raw.githubusercontent.com/cimryan/teslausb/master/windows_archive/setup-teslausb + wget https://raw.githubusercontent.com/cimryan/teslausb/u/cimryan/readonlyrootfs/windows_archive/setup-teslausb chmod +x setup-teslausb ./setup-teslausb ``` From ed72f4d8441a7c2c6940cb4089600e01189e5561 Mon Sep 17 00:00:00 2001 From: cimryan Date: Sun, 14 Oct 2018 21:15:56 -0700 Subject: [PATCH 08/16] Reference u/cimryan/readonlyrootfs --- GetShellWithoutMonitorOnWindows.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GetShellWithoutMonitorOnWindows.md b/GetShellWithoutMonitorOnWindows.md index d696876..dfc896e 100644 --- a/GetShellWithoutMonitorOnWindows.md +++ b/GetShellWithoutMonitorOnWindows.md @@ -14,7 +14,7 @@ 1. Run the following commands: ``` cd ~ - wget https://raw.githubusercontent.com/cimryan/teslausb/master/windows_archive/setup-piForHeadlessConfig.ps1 -OutFile setup-piForHeadlessConfig.ps1 + wget https://raw.githubusercontent.com/cimryan/teslausb/u/cimryan/readonlyrootfs/windows_archive/setup-piForHeadlessConfig.ps1 -OutFile setup-piForHeadlessConfig.ps1 ./setup-piForHeadlessConfig.ps1 -Verbose ``` 1. Enter the single letter of the "boot" drive and press Enter. From d2aa8357aa1f7726a1762824bfa9968ec4c25f47 Mon Sep 17 00:00:00 2001 From: cimryan Date: Sun, 14 Oct 2018 21:49:18 -0700 Subject: [PATCH 09/16] Add logging for fallocate call --- windows_archive/create-backingfiles.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/windows_archive/create-backingfiles.sh b/windows_archive/create-backingfiles.sh index df6cbd2..67f8366 100644 --- a/windows_archive/create-backingfiles.sh +++ b/windows_archive/create-backingfiles.sh @@ -11,6 +11,7 @@ function add_drive () { local size="$3" local filename="$4" + echo "Allocating ${size}K for $filename..." fallocate -l "$size"K "$filename" mkfs.vfat "$filename" -F 32 -n "$label" From 012c4e225cf13b3014abebd57a1e8fe36695a118 Mon Sep 17 00:00:00 2001 From: cimryan Date: Sun, 14 Oct 2018 22:21:16 -0700 Subject: [PATCH 10/16] Create the file system after creating the partition for the backing files If there already happened to be a filesystem there is must be overwritten. --- windows_archive/create-backingfiles-partition.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/windows_archive/create-backingfiles-partition.sh b/windows_archive/create-backingfiles-partition.sh index ebdb198..0fa45c8 100644 --- a/windows_archive/create-backingfiles-partition.sh +++ b/windows_archive/create-backingfiles-partition.sh @@ -10,4 +10,6 @@ FIRST_BACKINGFILES_PARTITION_SECTOR=$(( $LAST_ROOT_PARTITION_SECTOR + 1 )) parted -m /dev/mmcblk0 u s mkpart primary ext4 "$FIRST_BACKINGFILES_PARTITION_SECTOR" 100% -echo "/dev/mmcblk0p3 $BACKINGFILES_MOUNTPOINT ext4 auto,rw,noatime 0 2" >> /etc/fstab \ No newline at end of file +mkfs.ext4 -F /dev/mmcblk0p3 + +echo "/dev/mmcblk0p3 $BACKINGFILES_MOUNTPOINT ext4 auto,rw,noatime 0 2" >> /etc/fstab From 610613d7b3f9fc175448863a462e5cec8c0fecfc Mon Sep 17 00:00:00 2001 From: cimryan Date: Mon, 15 Oct 2018 07:20:44 -0700 Subject: [PATCH 11/16] Update the partition Ids in /etc/fstab and /boot/cmdline.txt after modifying partitions with parted Parted changes the disk Ids, rendering the Pi unbootable without this step. --- windows_archive/create-backingfiles-partition.sh | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/windows_archive/create-backingfiles-partition.sh b/windows_archive/create-backingfiles-partition.sh index 0fa45c8..e8b7013 100644 --- a/windows_archive/create-backingfiles-partition.sh +++ b/windows_archive/create-backingfiles-partition.sh @@ -8,8 +8,15 @@ LAST_ROOT_PARTITION_SECTOR=$(echo "$ROOT_PARTITION_LINE" | sed 's/s//g' | cut -d 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 From 657efc9f178d26bebc4607f4fd5df0aea983e0f7 Mon Sep 17 00:00:00 2001 From: cimryan Date: Mon, 15 Oct 2018 14:50:52 -0700 Subject: [PATCH 12/16] Fix creation of g_mass_storage.conf --- windows_archive/create-backingfiles.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/windows_archive/create-backingfiles.sh b/windows_archive/create-backingfiles.sh index 67f8366..a406df4 100644 --- a/windows_archive/create-backingfiles.sh +++ b/windows_archive/create-backingfiles.sh @@ -32,7 +32,7 @@ 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 + 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 + 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 From a70b752a162369a40c5ffec6fc2d6be7d57eb8bc Mon Sep 17 00:00:00 2001 From: cimryan Date: Mon, 15 Oct 2018 22:32:36 -0700 Subject: [PATCH 13/16] Make the root fs readonly during setup. --- windows_archive/make-root-fs-readonly.sh | 49 ++++++++++++++++++++++++ windows_archive/setup-teslausb | 36 ++++++++++++----- 2 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 windows_archive/make-root-fs-readonly.sh diff --git a/windows_archive/make-root-fs-readonly.sh b/windows_archive/make-root-fs-readonly.sh new file mode 100644 index 0000000..1d7882c --- /dev/null +++ b/windows_archive/make-root-fs-readonly.sh @@ -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 + diff --git a/windows_archive/setup-teslausb b/windows_archive/setup-teslausb index 63962ff..e7906ab 100644 --- a/windows_archive/setup-teslausb +++ b/windows_archive/setup-teslausb @@ -45,6 +45,17 @@ function check_available_space () { echo "There is sufficient space available." } +function get_ancillary_setup_scripts () { + pushd /tmp + wget https://raw.githubusercontent.com/cimryan/teslausb/"$BRANCH"/windows_archive/create-backingfiles-partition.sh + chmod +x ./create-backingfiles-partition.sh + wget https://raw.githubusercontent.com/cimryan/teslausb/"$BRANCH"/windows_archive/create-backingfiles.sh + chmod +x ./create-backingfiles.sh + wget https://raw.githubusercontent.com/cimryan/teslausb/"$BRANCH"/windows_archive/make-root-fs-readonly.sh + chmod +x ./make-root-fs-readonly.sh + popd +} + function fix_cmdline_txt_modules_load () { echo "Fixing the modules-load parameter in /boot/cmdline.txt..." @@ -58,17 +69,13 @@ BACKINGFILES_MOUNTPOINT=/backingfiles function create_usb_drive_backing_files () { mkdir "$BACKINGFILES_MOUNTPOINT" - 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" + /tmp/create-backingfiles-partition.sh "$BACKINGFILES_MOUNTPOINT" 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/"$BRANCH"/windows_archive/create-backingfiles.sh - chmod +x ./create-backingfiles.sh - ./create-backingfiles.sh "$campercent" "$BACKINGFILES_MOUNTPOINT" + /tmp/create-backingfiles.sh "$campercent" "$BACKINGFILES_MOUNTPOINT" } function configure_archive () { @@ -85,11 +92,13 @@ function configure_archive () { function configure_archive_scripts () { echo "Configuring the archive scripts..." mkdir /root/bin - + + pushd ~ 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 + popd pushd /root/bin wget https://raw.githubusercontent.com/cimryan/teslausb/"$BRANCH"/windows_archive/archive-teslacam-clips @@ -133,6 +142,10 @@ function configure_hostname () { echo "Configured the hostname." } +function make_root_fs_readonly () { + /tmp/make-root-fs-readonly.sh +} + echo "Verifying environment variables..." check_variable "archiveserver" @@ -145,9 +158,12 @@ check_archive_server_reachable check_available_space +get_ancillary_setup_scripts pushd ~ +configure_archive_scripts + fix_cmdline_txt_modules_load echo "" >> /etc/fstab @@ -156,8 +172,8 @@ create_usb_drive_backing_files configure_archive -configure_archive_scripts - configure_rc_local -configure_hostname \ No newline at end of file +configure_hostname + +make_root_fs_readonly \ No newline at end of file From e608ce5c7370017dac0031b1f9d9ba594f85b180 Mon Sep 17 00:00:00 2001 From: cimryan Date: Tue, 16 Oct 2018 07:21:37 -0700 Subject: [PATCH 14/16] Report success at the end of setup --- windows_archive/setup-teslausb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/windows_archive/setup-teslausb b/windows_archive/setup-teslausb index e7906ab..b20eee1 100644 --- a/windows_archive/setup-teslausb +++ b/windows_archive/setup-teslausb @@ -176,4 +176,6 @@ configure_rc_local configure_hostname -make_root_fs_readonly \ No newline at end of file +make_root_fs_readonly + +echo "All done." From 6a3d6f34aed80861ce77bb6a91cc345a370171ef Mon Sep 17 00:00:00 2001 From: cimryan Date: Tue, 16 Oct 2018 07:39:05 -0700 Subject: [PATCH 15/16] Instructions for remounting rw if necessary. --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index 85288af..09e1cb0 100644 --- a/README.md +++ b/README.md @@ -146,3 +146,17 @@ If you set up the Pi with a keyboard and a monitor disconnect it and connect it 1. Eject the drives. 1. Unplug the Pi from the PC. 1. Plug the Pi into your Tesla. + +## Making changes to the system after setup +The setup process configures the Pi with read-only file systems for the operating system but with read-write access through the USB + interface. This means that you'll be able to record dashcam video and add and remove music files but you won't be able to make changes + to files on / or on /boot. This is to protect against corruption of the operating system when the Tesla cuts power to the Pi. + +To make changes to the system partitions: +``` +ssh pi@teslausb. +sudo -i +mount / -o remount,rw +mount /boot -o remount,rw +``` +Then make whatever changes you need to. The next time the system boots the partitions will once again be read-only. \ No newline at end of file From 7dfc9de59cc22649876dfb141bf46953a0c27cef Mon Sep 17 00:00:00 2001 From: cimryan Date: Tue, 16 Oct 2018 07:44:53 -0700 Subject: [PATCH 16/16] Update branch references to master after merge. --- GetShellWithoutMonitorOnWindows.md | 2 +- README.md | 2 +- windows_archive/setup-teslausb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/GetShellWithoutMonitorOnWindows.md b/GetShellWithoutMonitorOnWindows.md index dfc896e..d696876 100644 --- a/GetShellWithoutMonitorOnWindows.md +++ b/GetShellWithoutMonitorOnWindows.md @@ -14,7 +14,7 @@ 1. Run the following commands: ``` cd ~ - wget https://raw.githubusercontent.com/cimryan/teslausb/u/cimryan/readonlyrootfs/windows_archive/setup-piForHeadlessConfig.ps1 -OutFile setup-piForHeadlessConfig.ps1 + wget https://raw.githubusercontent.com/cimryan/teslausb/master/windows_archive/setup-piForHeadlessConfig.ps1 -OutFile setup-piForHeadlessConfig.ps1 ./setup-piForHeadlessConfig.ps1 -Verbose ``` 1. Enter the single letter of the "boot" drive and press Enter. diff --git a/README.md b/README.md index 09e1cb0..3d14615 100644 --- a/README.md +++ b/README.md @@ -130,7 +130,7 @@ Now that you have a shell on the Pi you can turn the Pi into a smart USB drive. ``` 1. Run these commands: ``` - wget https://raw.githubusercontent.com/cimryan/teslausb/u/cimryan/readonlyrootfs/windows_archive/setup-teslausb + wget https://raw.githubusercontent.com/cimryan/teslausb/master/windows_archive/setup-teslausb chmod +x setup-teslausb ./setup-teslausb ``` diff --git a/windows_archive/setup-teslausb b/windows_archive/setup-teslausb index b20eee1..25466a2 100644 --- a/windows_archive/setup-teslausb +++ b/windows_archive/setup-teslausb @@ -1,6 +1,6 @@ #!/bin/bash -eu -BRANCH=u/cimryan/readonlyrootfs +BRANCH=master if ! [ $(id -u) = 0 ] then