How to start debian bootstrap on boot with freetz?

robertaaron

Neuer User
Mitglied seit
16 Apr 2014
Beiträge
7
Punkte für Reaktionen
0
Punkte
0
Hallo,

Danke für das mod! Freetz is awesome, and I am sorry but that is all the German I know (and ich lieber petersilie, because a friend told me this over dinner one night /uselessinfo). So I have to write in English I am sorry.

I have setup my 7390 with freetz, and also setup a debian bootstrap so that I can run a local nginx server from it.

To do this, I followed this guide for setting up a LAMP on fritz and made some minor changes.

Unfortunately, I'm not sure how to setup fritz so that on reboot it automatically starts up the Chroot environment mounted on my usb, and I'm hoping somebody here might be able to help.

When I turn on my fritz, freetz loads up, and I have to telnet in, then issue these commands:
Code:
export MY_CHROOT="/var/media/ftp/uStor02/debian"
mount -t proc proc $MY_CHROOT/proc
mount -t sysfs sysfs $MY_CHROOT/sys
cp /proc/mounts $MY_CHROOT/etc/mtab
chroot $MY_CHROOT /bin/bash

Then I can start nginx, dropbear etc.

Is there a way I can tell freetz to run these commands once it is finished loading?

Many thanks,
Robert
 
Zuletzt bearbeitet:
There is a file rc.custom that is executed at startup, but the USB storage is probably not yet mounted at the time it is executed.

If external is enabled, then a script is executed after the USB storage is mounted.
 
Thank you Ralf. That is good to know it is possible!

Sorry for not knowing, but how do I check if external is enabled?

When building the freetz image, I see there is a checkbox 'Enable external processing' - I didn't check this - but if I do, will this mean rc.custom would be executed after the usb storage is mounted?
 
Zuletzt bearbeitet:
rc,custom will always be executed. You can edit the file through the web interface. In general rc.custom is executed too early. The problem with the USB storage is that depending on the size, speed and file system of the USB storage it may take some time till the files are available.

External processing is mainly used to move parts of the firmware (usually parts of Freetz) from the internal Flash to an external USB storage. As the external files are not available when the box boots, Freetz provides a way to start services after the USB storage became available. This is typically used for services that need files that are on the USB storage. You can also write your own commands into /tmp/flash/mod/rc.external (probably also through the web interface) that are executed after the external storage becomes available.

Another, less clean solution is to use rc.custom with a loop in the background to wait for the USB storage to become available.

By the way, your commands about chroot are fine, but "chroot $MY_CHROOT /bin/bash" will just start a shell that you can't use. This shell will either be connected to the console, which is of no use to you, or it will be connected to /dev/null and exit immediately.
 
rc,custom will always be executed. You can edit the file through the web interface. In general rc.custom is executed too early. The problem with the USB storage is that depending on the size, speed and file system of the USB storage it may take some time till the files are available.

External processing is mainly used to move parts of the firmware (usually parts of Freetz) from the internal Flash to an external USB storage. As the external files are not available when the box boots, Freetz provides a way to start services after the USB storage became available. This is typically used for services that need files that are on the USB storage. You can also write your own commands into /tmp/flash/mod/rc.external (probably also through the web interface) that are executed after the external storage becomes available.

Another, less clean solution is to use rc.custom with a loop in the background to wait for the USB storage to become available.

By the way, your commands about chroot are fine, but "chroot $MY_CHROOT /bin/bash" will just start a shell that you can't use. This shell will either be connected to the console, which is of no use to you, or it will be connected to /dev/null and exit immediately.

Thanks for explaining all this Ralf.

I've been trying a few things and ran into a couple of issues.

If I enable external processing, the USB stick is no longer recognised and I can't mount it. Is that the expected functionality and is there another way to mount / access the USB, or have I configured my image incorrectly?

In regard to the "chroot $MY_CHROOT /bin/bash" command - If I don't run this, I can't access the daemons I've installed on the debian bootstrap. e.g. before this command, if I do /etc/init.d/nginx etc it won't work. To be honest however, I don't know what most of these commands do.

I will see if I can work out how to do this loop which waits to see until the USB is available and then execute the script I posted earlier. It does sound messy and add to an already quite slow boot of the Fritz, but if it works I would be happy.

Thanks again.
 
The USB storage should be mounted automatically. You may have to configure external storage in the web interface.

As I mentioned, the other possibility is a loop in rc.custom, something like
Code:
MY_CHROOT="/var/media/ftp/uStor02/debian"
while true; do
    # omit mount if mounted automatically
    mount /dev/sda1 /var/media/ftp/uStor02 2> /dev/null
    if test -d $MY_CHROOT; then
        chroot $MY_CHROOT ...
        break
    fi
    sleep 1
done &
As I wrote, "chroot $MY_CHROOT /bin/bash" will start a shell that either terminates immediately because it's input is /dev/null, or it will try to read from /dev/console, but you can't normally type on /dev/console.

What you need is "chroot $MY_CHROOT command", where command is what you want to execute. You don't really want a shell, you want to execute some commands.
 
The USB storage should be mounted automatically. You may have to configure external storage in the web interface.

As I mentioned, the other possibility is a loop in rc.custom, something like
Code:
MY_CHROOT="/var/media/ftp/uStor02/debian"
while true; do
    # omit mount if mounted automatically
    mount /dev/sda1 /var/media/ftp/uStor02 2> /dev/null
    if test -d $MY_CHROOT; then
        chroot $MY_CHROOT ...
        break
    fi
    sleep 1
done &
As I wrote, "chroot $MY_CHROOT /bin/bash" will start a shell that either terminates immediately because it's input is /dev/null, or it will try to read from /dev/console, but you can't normally type on /dev/console.

What you need is "chroot $MY_CHROOT command", where command is what you want to execute. You don't really want a shell, you want to execute some commands.

I added the above script with some changes:

Code:
echo "Beginning rc.custom..." > /var/log/mycustom.log

OUTPUT = $(export MY_CHROOT="/var/media/ftp/uStor02/debian")
echo "exporting MY_CHROOT... $OUTPUT" >> /var/log/mycustom.log

while true; do
    # omit mount if mounted automatically

    OUTPUT = $(mount /dev/sda1 /var/media/ftp/uStor02 2> /dev/null)
    echo "Mounting USB... $OUTPUT" >> /var/log/mycustom.log

    if test -d $MY_CHROOT; then
        
        echo "USB is now ready" >> /var/log/mycustom.log

        OUTPUT = $(mount -t proc proc $MY_CHROOT/proc)
        echo "Mounting proc... $OUTPUT" >> /var/log/mycustom.log
        
        OUTPUT = $(mount -t sysfs sysfs $MY_CHROOT/sys)
        echo "Moutning sysfs... $OUTPUT" >> /var/log/mycustom.log

        OUTPUT = $(cp /proc/mounts $MY_CHROOT/etc/mtab)
        echo "Copying /proc/mounts to /etc/mtab... $OUTPUT" >> /var/log/mycustom.log

        break
    fi
    sleep 1
done &

but it doesn't seem to be mounting the USB. e.g. after I've telnet in, I can't access /dev/sda1 or the files on my debootstrap.

/var/log/mycustom.log shows:

Beginning rc.custom...
exporting MY_CHROOT...
Mounting USB...
USB is now ready
Mounting proc...
Moutning sysfs...
Copying /proc/mounts to /etc/mtab...



No errors that I can see.

If I telnet in and paste the exact same script it all works as expected. So rc.custom is running, since it generates this log.. .but the commands don't seem to do anything. Can you see what I might be doing wrong?
 
Zuletzt bearbeitet:
You shouldn't quote the full text of post above your new post, it's already there in the original post. If applicable, quote the parts that you refer to.

I added the above script with some changes
The code above was meant as a template, so I thought you would need some changes.
but it doesn't seem to be mounting the USB. e.g. after I've telnet in, I can't access /dev/sda1 or the files on my debootstrap.
You tried /var/media/ftp/uStor02/debian and not /dev/sda1, I assume.
/var/log/mycustom.log shows ...
No errors that I can see.
The commands see to do exactly what they are told to do, and they generate the mycustom.log.
Your mycustom.log shows no errors for the mount, but you should not redirect errors to /dev/null if you want to see error messages.
When you set MY_CHROOT, is there a reason to use export? And why do use "OUTPUT = $(export MY_CHROOT=...)"? Do you expect any output from the assignment?
Use "MY_CHROOT=...", or "export MY_CHROOT=..." if you think you need the export, but not "OUTPUT = ...",
If I telnet in and paste the exact same script it all works as expected. So rc.custom is running, since it generates this log.. .but the commands don't seem to do anything. Can you see what I might be doing wrong?
Really? I'm quite sure that script as such would not work, unless you did "export MY_CHROOT=..." in the shell that called the script.
Calling the script also likely produced some output. Did you investigate what this output means?
Or what exactly does it mean when you say you "paste the exact same script"? A script is a file and normally not pasted, so did you just connect with telnet and paste some of the lines of the script into the shell?
 
Calling the script also likely produced some output. Did you investigate what this output means?

Do you mean running rc.custom? I had not run it yet, but I removed the output stuff and the whole rc.custom now looks like this:

Code:
MY_CHROOT="/var/media/ftp/uStor02/debian"

while true; do
    # omit mount if mounted automatically
    mount /dev/sda1 /var/media/ftp/uStor02 2> /dev/null
    
    if test -d $MY_CHROOT; then
        
        mount -t proc proc $MY_CHROOT/proc
        mount -t sysfs sysfs $MY_CHROOT/sys
        cp /proc/mounts $MY_CHROOT/etc/mtab

        break
    fi
    sleep 1
done &

Executing this returned the following:

Code:
root@fritz:/var/tmp/flash/mod# ./rc.custom
root@fritz:/var/tmp/flash/mod# mount: mounting proc on /var/media/ftp/uStor02/debian/proc failed: Device or resource busy
mount: mounting sysfs on /var/media/ftp/uStor02/debian/sys failed: Device or resource busy

You were right - I can now access /var/media/ftpuStor02/debian on boot. :)

But the whole script doesn't appear to execute correctly. As in, I can't access a user I created on the debootstrap.

Now if I telnet in on a fresh boot, I still have to run the following commands to get access to my user account and daemons:

Code:
root@fritz:/var/mod/root# /etc/init.d/nginx
-sh: /etc/init.d/nginx: not found
root@fritz:/var/mod/root# MY_CHROOT="/var/media/ftp/uStor02/debian"
root@fritz:/var/mod/root# chroot $MY_CHROOT /bin/bash
root@fritz:# /etc/init.d/nginx start
Starting nginx: nginx.

How can I make it so that when I boot up the fritz I can access /home/robert from the debootstrap without having to telnet in to execute these commands? Or run /etc/init.d/nginx etc?
 
Zuletzt bearbeitet:
Do you mean running rc.custom? I had not run it yet
It is a good idea to test the script from a shell so you can see what it does any what messages it produces. That it works when invoked from the interactive shell doesn't mean that it will work at startup, but it may help catch some errors.

The comment "omit mount if mounted automatically" means that if the partition is mounted automatically, you can (and should) remove the mount command in the following line, otherwise there would be two placed that try to mount the file system, possibly with different options. Normally the firmware would mount a FAT partition without and intervention, and Freetz would mount an EXT file system if the automount is patched for the file system is enabled.

Executing this returned the following:
Obviously you already had proc and sys mounted in the debian directory. It doesn't make sense to test whether a script could mount the directories when the preconditions for doing so are not met.

But the whole script doesn't appear to execute correctly. As in, I can't access a user I created on the debootstrap.
The script correctly executes the instructions you gave. What exactly is the meaning of "access a user I created on the debootstrap"? A "user" is just a line in /etc/passwd.
You will note that I also wrote that you need "chroot $MY_CHROOT command", where command is what you want to execute. You didn't include that in your script, and therefor it is not execute that. So if you want to start nginx then the command you want to run seems to be "/etc/init.d/nginx start", so you should add a line "chroot $MY_CHROOT /etc/init.d/nginx start". If you want to also execute other commands, just add more lines.

By the way, instead of "cp /proc/mounts $MY_CHROOT/etc/mtab" you can create a symbolic link from /etc/mtab to /proc/mounts once instead of copying the current contents at each boot.
 
The script correctly executes the instructions you gave. What exactly is the meaning of "access a user I created on the debootstrap"? A "user" is just a line in /etc/passwd.
I just meant I wasn't able to access the folder of the user as though I was logged in and was used to.

e.g. from telnet, when I ran the command 'chroot $MY_CHROOT /bin/bash' I could then go into my home directory of the user like '/home/robert' or run commands as that user. With my limited knowledge of linux and debootstrapping I wasn't sure how to run daemon's.

You will note that I also wrote that you need "chroot $MY_CHROOT command", where command is what you want to execute. You didn't include that in your script, and therefor it is not execute that. So if you want to start nginx then the command you want to run seems to be "/etc/init.d/nginx start", so you should add a line "chroot $MY_CHROOT /etc/init.d/nginx start".

Until now I did not understand I could add chroot $MY_CHROOT command in the rc.custom script like you said earlier. Sorry for not understanding that! I've only just realised now what the chroot command was even doing.

I am happy to say now that on boot everything I wish is loading up without typing a thing. And I can turn off my 250watt local webserver. Fritz box with Freetz is only using about 12watts! Truly awesome modification for this device. I am very happy about this.

By the way, instead of "cp /proc/mounts $MY_CHROOT/etc/mtab" you can create a symbolic link from /etc/mtab to /proc/mounts once instead of copying the current contents at each boot.

Thank you for this tip. It takes quite a bit each time to copy this each time so that should help alot.

Again, thanks for your help.
 
e.g. from telnet, when I ran the command 'chroot $MY_CHROOT /bin/bash' I could then go into my home directory of the user like '/home/robert' or run commands as that user.
...
Until now I did not understand I could add chroot $MY_CHROOT command in the rc.custom script like you said earlier.

You should also understand the the root directory, like te current directory, is not a property of the system, but property of each process. The chroot command changes the root directory of its own process and then executes the given commands. but it can't change the root directory of any other processes. So all the processes already started, including telnet and the shells created by telnet, keep their root directory. If you want to connect to the box and already have the changed root directory, the easiest way is to connect to a server that is already running with the changed root. That means that start either a telnet server or a SSH server inside the debian chroot. If you use a telnet server, you must either first stop the normal telnet server to free the telnet port 23, or use a different port for telnet inside the chroot. Turning of the normal telnet server means that you won't have access to anything outside the chroot, is it's usually not a good idea. So either use a different port for the telnet server inside the chroot, or use the SSH server.

Note also that chroot doesn't mean that you can run commands as another user, to do that you need 'su'.
 
If you want to connect to the box and already have the changed root directory, the easiest way is to connect to a server that is already running with the changed root. That means that start either a telnet server or a SSH server inside the debian chroot.

This is perfect. Now I can dropbear in for the chroot, and have telnet in for accessing the regular Freetz root. Thank you for explaining all of that about how the chroot works.
 
Holen Sie sich 3CX - völlig kostenlos!
Verbinden Sie Ihr Team und Ihre Kunden Telefonie Livechat Videokonferenzen

Gehostet oder selbst-verwaltet. Für bis zu 10 Nutzer dauerhaft kostenlos. Keine Kreditkartendetails erforderlich. Ohne Risiko testen.

3CX
Für diese E-Mail-Adresse besteht bereits ein 3CX-Konto. Sie werden zum Kundenportal weitergeleitet, wo Sie sich anmelden oder Ihr Passwort zurücksetzen können, falls Sie dieses vergessen haben.