-
Notifications
You must be signed in to change notification settings - Fork 3
Build: Advanced Settings
All buttons on this page use php/ajax to process commands that result in a systemd service running. I'll talk through this first one step-by-step as this was a learning curve for me and I may not have approached the problems that were presented to me in the most conventional of ways. There were two main advantages to using systemd to manage monerod. First is that systemd monitors the process and attempts re-start if it fails. On this PiNode-XMR it attempts restart of a service after 30 seconds of failing. The second reason for systemd use was that it allowed the 'bridging' of users from the apache web side of the node as user 'www-data' on button click, to user 'pinodexmr' for running monerod processes.
On button click the html processes a script function via ajax to run "monerod-start.php"
All php files are held local to user 'www-data' at /var/www/html/
The php contains the following to start the systemd service:
<?php
exec("sudo systemctl start monerod-start.service");
echo "Start Command Sent";
?>
Due to permission limits, exceptions are made in the 'sudoers' file (/etc/sudoers) to allow user 'www-data' to execute the 'sudo' command without a password:
(/etc/sudoers) example www-data ALL=(ALL) NOPASSWD: /bin/systemctl start monerod-start.service
With permission granted the systemd service is started. From here in the systemd config the user under which to run the service can be specified:
[Service]
ExecStart=/bin/bash monerod-start.sh
WorkingDirectory=/home/pinodexmr/
StandardOutput=inherit
StandardError=inherit
Type=forking
PIDFile=/home/pinodexmr/monero/monerod.pid
Restart=always
RestartSec=30
User=pinodexmr
The service and monerod are then started as user 'pinodexmr' giving access to the /home/pinodexmr/ directory where monerod and the blockchain is held.
With access to the /home/pinodexmr/ directory, monerod is started with the monerod-start.sh script, this script first loads all variables required such as IP address for binding, port number, RPC username:password, bandwidth limits and PID (a unique process number for systemd to manage).
This follows a similar method as outlined above:
On button click the html processes a script function via ajax to run "monerod-stop.php"
All php files are held local to user 'www-data' at /var/www/html/
The php contains the following to stop the systemd service:
<?php
exec("sudo systemctl stop monerod-start.service");
echo "Stop Command Sent";
?>
Due to permission limits, exceptions are made in the 'sudoers' file (/etc/sudoers) to allow user 'www-data' to execute the 'sudo' command without a password:
(/etc/sudoers) example www-data ALL=(ALL) NOPASSWD: /bin/systemctl stop monerod-start.service
Giving systemd the command to stop monerod as user 'pinodexmr'
Once any settings changes (bandwidth/peers/mining address) are made a restart of monerod is required for them to be active. This button is for that function. The button works in the same manner as those already described above but the systemd config and resulting script works very differently.
On button click the html processes a script function via ajax to run "monerod-reboot.php"
All php files are held local to user 'www-data' at /var/www/html/
The php contains the following to start the systemd service:
<?php
exec("sudo systemctl start monerod-reboot.service");
echo "Reboot Command Sent - allow 1m for process";
?>
Due to permission limits, exceptions are made in the 'sudoers' file (/etc/sudoers) to allow user 'www-data' to execute the 'sudo' command without a password:
(/etc/sudoers) example www-data ALL=(ALL) NOPASSWD: /bin/systemctl start monerod-reboot.service
With permission granted the systemd reboot service is started. However note in this config the 'Type' is no longer specified as 'Forking' to background, it is now a 'oneshot'. Commanding systemd to run the /home/pinodexmr/monerod-reboot.sh script only once as user 'pinodexmr'
(example)
[Service]
ExecStart=/bin/bash monerod-reboot.sh
WorkingDirectory=/home/pinodexmr/
StandardOutput=inherit
StandardError=inherit
Type=oneshot
RemainAfterExit=true
Restart=no
User=pinodexmr
The /home/pinodexmr/monerod-reboot.sh script contains a link to a safe shutdown script for monerod called monerod-stop.sh
./monerod exit
This safely exits monerod. This produces the desired effect of rebooting monerod because systemd is still monitoring the 'monerod-start.service' PID which has now unexpectedly stopped. Therefore as intended systemd will restart monerod after a 30 second delay. The 30 second delay is intended to allow and block read/write cache processes to finish before the process restarts.
Although the format of the UI page doesn't make it clear the 'restart' button functions as intended for the tor & mining node too by exiting monerod for systemd to recover/restart 30 seconds later.
These drop down boxes output their selections into bash scripts for the monerod-start.sh scripts (inc tor & mining) to pick up as start flag variables in the following manner:
(speed_up snippit/example from html dropdown, value='###' where ### is exported)
...
<option value='1536'>1536</option>
<option value='-1'>2048 [DEFAULT]</option>
<option value='2560'>2560</option>
...
(script/ajax)
function speedup(value){
$.ajax({
url:"speed_up.php", //the page containing php script
type: "POST", //request type
data: ({value}),
value=### is then sent to /var/www/html/speed_up.php as user 'www-data'
(example speed_up.php)
<?php
$VALUE = $_POST["value"];
$fp = fopen('/home/pinodexmr/limit-rate-up.sh', 'w');
fwrite($fp, "#!/bin/bash\nLIMIT_RATE_UP=$VALUE
");
fclose($fp);
echo "Upload Speed limit set to $VALUE kB/s";
?>
Using function 'fopen' to open the bash script and 'fwrite' to input it's new contents where $VALUE is the value ### selected from the drop down list.
Note value '-1' is default and this process is repeated for all drop down boxes.
Is a free text box where any text can be entered and on pressing the 'return' key or clicking outside the text box, any changes made inside the box are outputted in the same same fashion as the drop-down boxes described above. There is no immediate address verification check so a user must ensure it is correct. An incorrect address will be rejected by monerod and logged in the log file. The address text is processed via ajax to the mining-address.php file where 'fopen' and 'fwrite' are used to put the address entered into the script file /home/pinodexmr/mining-address.sh for monerod-start-mining.sh to pickup as a variable on start.
The button works in exactly the same way as the clearnet start button. The difference occurs in the /home/pinodexmr/monerod-start-tor.sh start script to wrap monerod traffic in tor SOCKS so I'll explain this in better detail:
(/home/pindoexmr/monerod-start-tor.sh example)
DNS_PUBLIC=tcp TORSOCKS_ALLOW_INBOUND=1 ./monero/monerod --p2p-bind-ip 127.0.0.1 --no-igd --non-interactive
- DNS_PUBLIC=tcp
- It is possible to specify tcp however this has only ever caused me problems (example = DNS_PUBLIC=tcp://1.1.1.1) Obtaining connections in this way was much slower and unreliable. When IP is not specified, monerod uses the default list of servers defined in src/common/dns_utils.cpp.
- TORSOCKS_ALLOW_INBOUND=1
- Allows connection from a wallet to be able to broadcast transactions through the tor network.
- p2p-bind-ip 127.0.0.1
- tells monerod that ".onion" p2p addresses can be forwarded to a socks proxy at IP 127.0.0.1
- no-igd
- disable IGD (UPnP port forwarding negotiation), which is pointless with Tor.
- non-interactive
- For clearnet PiNode-XMR passes the flag --detach, this is it's tor replacement for a number of reasons. We cannot pass --detach because stderr/stdout is not available when detached, but torsocks attempts to write to it, and fails with 'invalid argument', causing monerod to fail.
- non-interactive = Do not require tty in a foreground mode. Helpful when running in a container. By default monerod runs in a foreground and opens stdin for reading (not possible for reason above). This breaks containerization because no tty gets assigned and monerod process crashes.
- So in conjunction with the above point for tor systemd configuration the service 'Type=' must be set to 'simple' not 'forking'.
Therefore the systemd service for tor on the PiNode-XMR is configured as such:
(/etc/systemd/system/monerod-start-tor.service)
[Unit]
Description=Monero Full Node Tor
After=network.target
[Service]
User=pinodexmr
Group=pinodexmr
WorkingDirectory=/home/pinodexmr/
RuntimeDirectory=monero
Type=simple
PIDFile=/home/pinodexmr/monero/monerod.pid
ExecStart=/usr/bin/torsocks /home/pinodexmr/monerod-start-tor.sh
RestartSec=30
Restart=always
Sources:
- https://github.com/monero-project/monero/blob/master/ANONYMITY_NETWORKS.md#behavior
- https://github.com/monero-project/monero#using-tor
- https://monerodocs.org/interacting/monerod-reference/#p2p-network
Calls the same stop php,
exec("sudo systemctl stop monerod-start-tor.service");
as clearnet function, file names and order should be intuitive.
Is again the same as the clearnet monerod start/stop buttons, (ajax/php -> systemd with sudoers permission). I'm trying not to bloat this wiki with too much repeating code.
The only changes to the mining start command are found in the /home/pinodexmr/monerod-start-mining.sh script:
./monerod --start-mining=$MINING_ADDRESS --bg-mining-miner-target=$MINING_INTENSITY
Where $MINING_ADDRESS is the user set variable collected from the free text field on the UI.
$MINING_INTENSITY is set at a default value of 50% and can be user set via web-terminal.
Mining is only available once node is 100% sync'd.
There is a file located in the /home/pinodexmr/ directory called 'current_ver.sh' containing the current version number expressed without decimal points:
(example)
#!/bin/bash
CURRENT_VERSION=01402
On update button click the html processes a script function via ajax to run "monerod-update.php"
All php files are held local to user 'www-data' at /var/www/html/
The php contains the following to start the systemd service:
<?php
exec("sudo systemctl start monerod-update.service");
echo "Your PiNode-XMR has begun it's Update Script\n\nIf a new version is found to be available then update takes approx 5 mins\n\nCheck for completed update at 'Node Version' under 'Node Status' tab";
?>
Due to permission limits, exceptions are made in the 'sudoers' file (/etc/sudoers) to allow user 'www-data' to execute the 'sudo' command without a password:
(/etc/sudoers) example www-data ALL=(ALL) NOPASSWD: /bin/systemctl start monerod-update.service
With permission granted the systemd monerod-update service is started. However note in this config the 'Type' is a 'oneshot'. Commanding systemd to run the /home/pinodexmr/monerod-update.sh script only once as user 'pinodexmr'
(example)
[Service]
ExecStart=/bin/bash Updater.sh
WorkingDirectory=/home/pinodexmr/
StandardOutput=inherit
StandardError=inherit
Type=oneshot
RemainAfterExit=true
Restart=no
User=pinodexmr
Running script Updater.sh
:
The General process of the updater script is as follows:
- Is there a new version available?
- Download a file called
xmr-new-ver.sh
from this repository (contains monerod version number without decimals) wget -q https://raw.githubusercontent.com/shermand100/pinode-xmr/master/xmr-new-ver.sh -O /home/pinodexmr/xmr-new-ver.sh
- Download a file called
- Compare the Monero version number from
xmr-new-ver.sh
withcurrent-ver.sh
- If
current-ver.sh
=xmr-new.ver.sh
then node is up to date. Do nothing. - If
current-ver.sh
is less thanxmr-new-ver.sh
- Run all three systemd stop commands
- Wait 30 seconds for processes to stop
- Delete Monerod directory /home/pinodexmr/monero/
- Make a new empty Monero directory at /home/pinodexmr/monero/
- Download new Monero cli from https://downloads.getmonero.org/cli/linuxarm7
- Unpack the directory into /home/pinodexmr/monero/ with
tar -xvf ./linuxarm7 -C /home/pinodexmr/monero --strip $STRIP
NOTE The STRIP variable is a new requirement to remove subdirectories when unpacking and is dependant on how the Monero dev's release and package new versions. Recently STRIP 2 has instead become STRIP 1, this variable copes with this change. - Restart monerod & systemd service
-
current-ver.sh
is updated with new version number - Remove now un-needed
xmr-new-ver.sh
&linuxarm7
files
- If
This button ajax/php calls the systemd service shutdown.service
.
The systemd shutdown service oneshot calls the script /home/pinodexmr/shutdown.sh as user 'pinodexmr'
sudo shutdown
This starts a 60 second shutdown
This kill process button calls the systemd service kill.service
The service calls a oneshot script /home/pinodexmr/kill.sh
kill.sh
contains sudo killall -9 monerod
stopping any process called 'monerod'