Skip to content

Commit

Permalink
Automatic pipe creation using scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasbakken committed Jun 6, 2024
1 parent 3f48023 commit 77aaad4
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 12 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ install_bins:
chmod +x /usr/local/bin/get-free-space
chmod +x /usr/local/bin/mount-unmount-usb
chmod +x /usr/local/bin/get-reflash-version
chmod +x /usr/local/bin/flash-cleanup
chmod +x /usr/local/bin/flash-mkfifo

upload_bins:
scp bin/prod/* [email protected]:/usr/local/bin
Expand Down
20 changes: 20 additions & 0 deletions bin/dev/flash-cleanup
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
set -euo pipefail

info() {
echo "[info] $1" >> /var/log/reflash.log
echo "$1"
}

info "-----------------------"
info "Starting cleanup script"
info "-----------------------"

REVISION=$1
OUTFILE="/opt/reflash/mmcblk2"

info "Using Recore hardware revision $REVISION"
info "Overwriting block device $OUTFILE"
info "Checking filesystems"
info "Creating new UUIDs"
info "Cleanup script done!"
3 changes: 2 additions & 1 deletion bin/dev/flash-direct
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ info() {
echo "$1"
}

info "----------------------------"
info "Starting flash direct script"
info "------------------"
info "----------------------------"

URL=$1
OUTFILE="/dev/null"
Expand Down
22 changes: 22 additions & 0 deletions bin/dev/flash-mkfifo
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
set -euo pipefail

info() {
echo "[info] $1" >> /var/log/reflash.log
echo "$1"
}

info "-----------------------"
info "Starting mkfifo script"
info "-----------------------"

OUTFILE="/opt/reflash/mmcblk2"
FIFO="/tmp/mypipe"

info "Overwriting block device $OUTFILE"

rm -f "$FIFO"
mkfifo "$FIFO"
xz -d < "$FIFO" > "$OUTFILE" &

info "Mkfifo script done!"
85 changes: 85 additions & 0 deletions bin/prod/flash-cleanup
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/bin/bash
set -euo pipefail

info() {
echo "[info] $1" >> /var/log/reflash.log
echo "$1"
}

info "-----------------------"
info "Starting cleanup script"
info "-----------------------"

REVISION=$1
OUTFILE="/dev/mmcblk2"

info "Using Recore hardware revision $REVISION"
info "Overwriting block device $OUTFILE"

sync

# Re-read partition table
partprobe ${OUTFILE}

info "Checking filesystems"
e2fsck -y -f ${OUTFILE}p1
e2fsck -y -f ${OUTFILE}p2

# Create new uuids so they are different than the USB drive
info "Creating new UUIDs"
tune2fs -U random ${OUTFILE}p1
tune2fs -U random ${OUTFILE}p2

UUID1=$(blkid -s UUID -o value ${OUTFILE}p1)
UUID2=$(blkid -s UUID -o value ${OUTFILE}p2)

mkdir -p /mnt/emmc
# mount and exchange UUID
mount ${OUTFILE}p2 /mnt/emmc
info "Updating fstab with new UUIDs"
sed -i "s:UUID=.* \/ :UUID=${UUID2} / :" /mnt/emmc/etc/fstab
sed -i "s:UUID=.* \/boot :UUID=${UUID1} /boot :" /mnt/emmc/etc/fstab

# Copy config file, if one is not already present
if test -d /mnt/emmc/home/debian; then
cd /mnt/emmc/home/debian/ || exit
if ! test -f printer_data/config/printer.cfg; then
if test -f klipper/config/generic-recore-"$REVISION".cfg; then
info "Installing Klipper config file"
cp klipper/config/generic-recore-"$REVISION".cfg printer_data/config/printer.cfg
chown 1000:1000 printer_data/config/printer.cfg
fi
# Enable mainsail on mainsail distros
if test -f printer_data/config/mainsail.cfg; then
info "Including Mainsail in config file, above the first section"
# Older distros has the include fiel as a comment
sed -i 's/\#\[include mainsail.cfg\]//' printer_data/config/printer.cfg
sed -i 's:\(\[recore.*\]\):\[include mainsail.cfg\]\n\n\1:' printer_data/config/printer.cfg
fi
# Enable fluidd on fluidd distros
if test -f printer_data/config/fluidd.cfg; then
info "Including Fluidd in config file, above the first section"
# Older distros has the include fiel as a comment
sed -i 's/\#\[include mainsail.cfg\]//' printer_data/config/printer.cfg
sed -i 's:\(\[recore.*\]\):\[include fluidd.cfg\]\n\n\1:' printer_data/config/printer.cfg
fi
fi
fi
cd /
umount ${OUTFILE}p2

mount ${OUTFILE}p1 /mnt/emmc
sed -i "/^rootdev=.*/d" /mnt/emmc/armbianEnv.txt
sed -i '/^$/d' /mnt/emmc/armbianEnv.txt
echo "rootdev=UUID=${UUID2}" >> /mnt/emmc/armbianEnv.txt

cd /mnt/emmc/dtb/allwinner/ || exit
if test -f sun50i-a64-recore-"$REVISION".dtb; then
info "Symlinking device tree"
ln -sf sun50i-a64-recore-"$REVISION".dtb sun50i-a64-recore.dtb
fi
cd /
umount ${OUTFILE}p1

sync
info "Cleanup script done!"
3 changes: 2 additions & 1 deletion bin/prod/flash-direct
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ info() {
echo "$1"
}

info "---------------------"
info "Starting magic script"
info "------------------"
info "---------------------"

URL=$1
OUTFILE="/dev/mmcblk2"
Expand Down
22 changes: 22 additions & 0 deletions bin/prod/flash-mkfifo
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
set -euo pipefail

info() {
echo "[info] $1" >> /var/log/reflash.log
echo "$1"
}

info "-----------------------"
info "Starting mkfifo script"
info "-----------------------"

OUTFILE="/dev/mmcblk2"
FIFO="/tmp/mypipe"

info "Overwriting block device $OUTFILE"

rm -f "$FIFO"
mkfifo "$FIFO"
xz -d < "$FIFO" > "$OUTFILE" &

info "Mkfifo script done!"
9 changes: 5 additions & 4 deletions client/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -390,16 +390,13 @@ export default {
}
},
async magicUploadLocalFile() {
console.log("magig upload local file");
const CHUNK_SIZE = 3 * 1024 * 1024;
let self = this;
var reader = new FileReader();
var offset = 0;
var filesize = this.file.size;
console.log(filesize);
reader.onload = function () {
console.log("onload");
var result = reader.result;
var chunk = result;
axios
Expand Down Expand Up @@ -531,7 +528,7 @@ export default {
this.backupFile = data.filename;
}
// This method is called on page load. If a refresh happens during upload, we can not continue.
} else if (data.state == "UPLOADING") {
} else if (data.state == "UPLOADING" || data.state == "UPLOADING_MAGIC") {
this.selectedMethod = this.availableMethods[2];
}
this.previousState = this.state;
Expand Down Expand Up @@ -579,6 +576,10 @@ export default {
this.selectedRefactorImage = null;
await axios.get(`/api/run_install_finished_commands`);
this.installFinished = true;
} else if (this.previousState == "UPLOADING_MAGIC") {
this.selectedUploadImage = [];
await axios.get(`/api/run_install_finished_commands`);
this.installFinished = true;
}
} else if (data.state == "CANCELLED") {
this.selectedGithubImage = null;
Expand Down
34 changes: 28 additions & 6 deletions reflash/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,17 +357,22 @@ func uploadMagicStart(w http.ResponseWriter, r *http.Request) {
state.BytesTotal = data.Size
state.State = UPLOADING_MAGIC

go goUploadMagic()
time.Sleep(1 * time.Second)

sendResponse(w, nil)
}

func goUploadMagic() {
timeStart = time.Now()
logInfo("Starting magic upload at " + timeStart.Format("15:04:05"))
logInfo("Filename: " + state.Filename)

path := "/tmp/mypipe"
var err error
state.File, err = os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0644)
stdout, _, err := runCommand2("/usr/local/bin/flash-mkfifo")
if err != nil {
log.Fatal(err)
logError("Error encountered when setting up pipe: \n" + stdout)
}
sendResponse(w, nil)
state.State = FINISHED
}

func uploadMagicChunk(w http.ResponseWriter, r *http.Request) {
Expand All @@ -378,9 +383,18 @@ func uploadMagicChunk(w http.ResponseWriter, r *http.Request) {
}

var chunk *Chunk = &Chunk{}
var err error
reqBody, _ := io.ReadAll(r.Body)
json.Unmarshal(reqBody, &chunk)

path := "/tmp/mypipe"
if state.File == nil {
state.File, err = os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
}

decoded, err := base64.StdEncoding.DecodeString(chunk.Encoded[37:])
if err != nil {
http.Error(w, "Failed to decode base64", http.StatusBadRequest)
Expand All @@ -405,7 +419,15 @@ func uploadMagicFinish(w http.ResponseWriter, r *http.Request) {
}
duration := time.Since(timeStart)
logInfo(fmt.Sprintf("Upload magic finished in %d minutes and %d seconds", int(duration.Minutes()), int(duration.Seconds())%60))
state.State = FINISHED
revision := runCommandReturnString("get-recore-revision")
stdout, _, err := runCommand2("/usr/local/bin/flash-cleanup", revision)
if err != nil {
logError("Error encountered during cleanup: \n" + stdout)
state.State = ERROR
state.Error = "An error was encountered during magic. Check log for details"
} else {
state.State = FINISHED
}
if saveOptionsWhenIdle {
saveOptions()
}
Expand Down

0 comments on commit 77aaad4

Please sign in to comment.