Handling Unstable Networks in VPS Reinstall Scripts

Preface

In the DD script group

some group members encountered the issue of unstable network, which easily causes interruptions when the script downloads resources.


Idea

I have an idea, 
1) Make this script output the links of all resources that need to be downloaded
2) The user downloads the resources by themselves, and uploads them to the VPS

Practice

Let's analyze this DD script
https://github.com/bin456789/reinstall/raw/refs/heads/main/reinstall.sh

At the beginning of the script, we can see that the author has already implemented a curl function to wrap the system's curl.

This is a very good approach.
Based on this, we can make the following modifications:
1. Output the current pwd directory
2. Output the full curl command and all its parameters
3. This curl wrapper does not actually download the file.
4. This curl wrapper always returns success.

Take these to ask GPT.
The final modification looks like this:


curl() {
    # Output the current working directory
    echo "[DEBUG] Current directory: $(pwd)" >&2

    # Output the full command (including all parameters)
    printf 'curl ' >&2
    printf '%q ' "$@" >&2
    printf '\n' >&2

    echo >&2

    # Do not perform the actual download, only print information
    # Always return success
    return 0
}

We upload the modified reinstall-edited.sh to the VPS and test it
bash reinstall-edited.sh debian 13
debian 13 is an example.

The log output is:
[DEBUG] Current directory: /root
curl -L http://www.qualcomm.cn/cdn-cgi/trace

We search the script for http://www.qualcomm.cn/cdn-cgi/trace and find the following code

We need to make the curl here call the system's real curl, not our curl wrapper.
After chatting with GPT, we learned that adding command will call the system's curl, like this:

Execute again
bash reinstall-edited.sh debian 13
The log output is:
[DEBUG] Current directory: /root
curl -Lo /reinstall-vmlinuz https://deb.debian.org/debian/dists/trixie/main/installer-amd64/current/images/netboot/debian-installer/amd64/linux

[DEBUG] Current directory: /root
curl -Lo /reinstall-initrd https://deb.debian.org/debian/dists/trixie/main/installer-amd64/current/images/netboot/debian-installer/amd64/initrd.gz


We take this error to ask GPT
The program outputs logs like this, and then fails. 
To remediate, what URLs of resources should I download, where on the VPS should I place them, and what should the filenames be?
We got this result.
Based on the program's log output, let me analyze the remediation plan:
First file:
Download URL: https://deb.debian.org/debian/dists/trixie/main/installer-amd64/current/images/netboot/debian-installer/amd64/linux
Location: /reinstall-vmlinuz
Filename: reinstall-vmlinuz

Second file:
Download URL: https://deb.debian.org/debian/dists/trixie/main/installer-amd64/current/images/netboot/debian-installer/amd64/initrd.gz
Location: /reinstall-initrd
Filename: reinstall-initrd

We are now assuming the VPS network is unstable, so we cannot let the script download files.
So we download files by our own method, then upload them to the specified location on the VPS. Note that the filenames may need to be modified.

Then try again bash reinstall-edited.sh debian 13
There is a new error

Repeating the same trick, we search the script for 脚本有更新,请重新下载 and find

These 2 curls are both download tool scripts, both are small files, I believe the VPS can download successfully. So we add command in front of curl
(omitted, no images attached)

Continue trying  bash reinstall-edited.sh debian 13
The error is as follows

The above 4
fix-eth-name.sh
fix-eth-name.service
get-xda.sh
ttys.sh
are all small files. Let's search in the script, add command in front of curl

For the later .gz, use our own method to download, then upload to the VPS.
But this curl -L is suspicious, this is not the parameter to save as a file.

Continue trying  bash reinstall-edited.sh debian 13
As expected,  Packages.gz still reports an error.

We search for Packages.gz in the script and find

Because we have already uploaded  Packages.gz to the VPS, we change the original curl to cat


Continue trying  bash reinstall-edited.sh debian 13
The error says it cannot find  Packages.gz 


After checking, this file is indeed gone, so  /reinstall-tmp/initrd is a temporary directory, it gets cleared every time.
So we put the file into /root/ and modify the script to cat /root/Packages.gz

Continue trying  bash reinstall-edited.sh debian 13
This time it's about saving the file to  /reinstall-tmp/initrd/

Based on the experience just now, this directory cannot save files, it will be cleaned up.
So we still use our own method to download the file, then upload to the VPS's /root/
Then modify the script to cp /root/some file to  /reinstall-tmp/tmp.deb

Search for  tmp.deb in the script and find

We first need to download the openssh-server-udeb_10.0p1-7_amd64.udeb file, then upload it to /root/
Then modify the script to

Continue trying  bash reinstall-edited.sh debian 13
A strange error was reported.

Thought for a long time, couldn't figure out the reason. 
Can only start from the normal resinstall.sh.

Add the print of curl command and function to the log file in the original reinstall.sh's curl() function, but without affecting the actual function behavior.
Let's see what curl calls happen in the normal process.

Let's still use DD debian 13 as an example.
The result is like this:

Found that Packages.gz is not just 1. 
The .udeb file is also not just 1.

That is to say, the code involves  Packages.gz with only 1 line of code, but due to logical loops and other reasons, the code will be executed multiple times.
So the method we tried above to directly modify that 1 line of code definitely won't work.

To be continued.

========

See also


Comments