Installation de la configuration du NAS sur le Robot - vaul-ulaval/vaul-wiki GitHub Wiki

English version follow


Prérequis

  1. Assurez-vous d'avoir un accès root ou sudo à la machine.
  2. Assurez-vous que le paquet cifs-utils est installé pour monter les partages CIFS/SMB. Sinon, vous pouvez l'installer en utilisant :
    sudo apt update
    sudo apt install cifs-utils
    
  3. Assurez-vous d'avoir un centre multimédia SAMBA prêt sur le réseau.

Étapes

1. Créer le fichier de script

  1. Ouvrez un terminal et naviguez vers le répertoire où vous souhaitez enregistrer le script, par exemple /home/vaul-f1tenth/setup_scripts/.

  2. Créez un nouveau fichier nommé mount_nas.sh en utilisant un éditeur de texte :

    nano /home/vaul-f1tenth/setup_scripts/mount_nas.sh
    
  3. Collez le contenu du script mount_nas.sh dans le fichier.

    #!/bin/bash
    
    # Check if NAS is already mounted
    if mountpoint -q /mnt/nas; then
      echo "Warning: NAS is already mounted. Exiting."
      exit 0
    fi
    
    retries=0
    max_retries=10
    
    while [ $retries -lt $max_retries ]; do
      ping -c 1 -I wlan0 -w 1 192.168.1.10 >/dev/null 2>&1
      if [ "$?" == "0" ]; then
        mkdir -p /mnt/nas
        mount -t cifs //192.168.1.10/f1tenth /mnt/nas -o user=vaul,password=macaroni,uid=1000,gid=1000
        exit 0 
      fi
      sleep 10
      ((retries++))
    done
    
    # If the script reaches this point, it means it failed to connect after 10 tries.
    echo "Warning: Max retries reached. NAS could not be mounted. Please start manually later." >&2
    exit 1
    
  4. Sauvegardez et quittez l'éditeur de texte.

  5. Rendez le script exécutable :

    chmod +x /home/vaul-f1tenth/setup_scripts/mount_nas.sh
    

2. Créer le fichier de service

  1. Naviguez vers le répertoire /etc/systemd/system/ :
    cd /etc/systemd/system/
    
  2. Créez un nouveau fichier nommé mount_nas.service en utilisant un éditeur de texte avec des permissions root :
    sudo nano mount_nas.service
    
  3. Collez le contenu du fichier mount_nas.service dans le fichier.
    [Unit]
    Description=Mount NAS at boot
    After=network-online.target
    Wants=network-online.target
    
    [Service]
    ExecStart=/home/vaul-f1tenth/setup_scripts/mount_nas.sh
    Type=oneshot
    RemainAfterExit=yes
    
    [Install]
    WantedBy=multi-user.target
    
  4. Sauvegardez et quittez l'éditeur de texte.

3. Recharger Systemd et activer le service

  1. Rechargez le démon systemd pour reconnaître votre nouveau service :
    sudo systemctl daemon-reload
    
  2. Activez le service pour qu'il démarre au démarrage :
    sudo systemctl enable mount_nas.service
    
  3. Optionnellement, vous pouvez démarrer le service immédiatement sans redémarrer :
    sudo systemctl start mount_nas.service
    

4. Tester le service

  1. Redémarrez votre machine et vérifiez si le NAS est monté comme prévu :
    df -h
    
    ou
    mount | grep nas
    
  2. Vous pouvez également vérifier le statut du service :
    sudo systemctl status mount_nas.service
    

Que faire si le NAS n'est pas en ligne lorsque le robot démarre

Si votre robot démarre avant que le NAS ne soit en ligne (par exemple, après une coupure de courant ou un redémarrage du routeur), le script tentera de monter le NAS 10 fois, en attendant 60 secondes entre chaque tentative. S'il ne parvient toujours pas à monter le NAS, il se terminera avec un message d'erreur.

Lorsque le routeur et le NAS sont de nouveau en ligne, vous avez deux options selon que le service est déjà en cours d'exécution :

  1. Vérifiez si le service est en cours d'exécution :

    sudo systemctl status mount_nas.service
    
    • Si le statut du service indique "active (exited)", cela signifie que le service a terminé son exécution mais n'a pas réussi à monter le NAS.
  2. Si le service n'est pas en cours d'exécution :

    • Démarrez manuellement le service en utilisant :
      sudo systemctl start mount_nas.service
      
  3. Si le service est déjà en cours d'exécution :

    • Redémarrez le service pour tenter de monter le NAS à nouveau :
      sudo systemctl restart mount_nas.service
      

Chacune de ces options tentera de monter le NAS à nouveau.

De cette manière, vous pouvez d'abord vérifier si le service est en cours d'exécution. S'il ne l'est pas, vous pouvez le démarrer manuellement. S'il est en cours d'exécution mais n'a pas réussi à monter le NAS, vous pouvez le redémarrer pour essayer à nouveau.

Dépannage

  • Si le service échoue, vous pouvez vérifier ses journaux en utilisant :
    sudo journalctl -u mount_nas.service
    
  • Assurez-vous que le NAS est accessible et que les identifiants sont corrects dans le script.
  • Assurez-vous que le réseau est opérationnel avant que le service tente de monter le NAS. Le fichier de service inclut déjà After=network-online.target pour gérer cela, mais vérifiez deux fois si votre système respecte cette dépendance.

Prerequisites

  1. Make sure you have root or sudo access to the machine.
  2. Ensure that the cifs-utils package is installed for mounting CIFS/SMB shares. If not, you can install it using:
    sudo apt update
    sudo apt install cifs-utils
    
  3. Make sure you have a SAMBA media center ready on the network.

Steps

1. Create the Script File

  1. Open a terminal and navigate to the directory where you want to save the script, for example /home/vaul-f1tenth/setup_scripts/.
  2. Create a new file named mount_nas.sh using a text editor:
    nano /home/vaul-f1tenth/setup_scripts/mount_nas.sh
    
  3. Paste the content of the mount_nas.sh script into the file.
    #!/bin/bash
    
    # Check if NAS is already mounted
    if mountpoint -q /mnt/nas; then
      echo "Warning: NAS is already mounted. Exiting."
      exit 0
    fi
    
    retries=0
    max_retries=10
    
    while [ $retries -lt $max_retries ]; do
      ping -c 1 -I wlan0 -w 1 192.168.1.10 >/dev/null 2>&1
      if [ "$?" == "0" ]; then
        mkdir mnt/nas
        mount -t cifs //192.168.1.10/f1tenth /mnt/nas -o user=vaul,password=macaroni,uid=1000,gid=1000
        exit 0 
      fi
      sleep 60
      ((retries++))
    done
    
    # If the script reaches this point, it means it failed to connect after 10 tries.
    echo "Warning: Max retries reached. NAS could not be mounted. Please start manually later." >&2
    exit 1
    
  4. Save and exit the text editor.
  5. Make the script executable:
    chmod +x /home/vaul-f1tenth/setup_scripts/mount_nas.sh
    

2. Create the Service File

  1. Navigate to the /etc/systemd/system/ directory:
    cd /etc/systemd/system/
    
  2. Create a new file named mount_nas.service using a text editor with root permissions:
    sudo nano mount_nas.service
    
  3. Paste the content of the mount_nas.service into the file.
    [Unit]
    Description=Mount NAS at boot
    After=network-online.target
    Wants=network-online.target
    
    [Service]
    ExecStart=/home/vaul-f1tenth/setup_scripts/mount_nas.sh
    Type=oneshot
    RemainAfterExit=yes
    
    [Install]
    WantedBy=multi-user.target
    
  4. Save and exit the text editor.

3. Reload Systemd and Enable the Service

  1. Reload the systemd daemon to recognize your new service:
    sudo systemctl daemon-reload
    
  2. Enable the service so that it starts on boot:
    sudo systemctl enable mount_nas.service
    
  3. Optionally, you can start the service immediately without rebooting:
    sudo systemctl start mount_nas.service
    

4. Test the Service

  1. Reboot your machine and check if the NAS is mounted as expected:
    df -h
    
    or
    mount | grep nas
    
  2. You can also check the service status:
    sudo systemctl status mount_nas.service
    

What to Do If the NAS Is Not Online When the Robot Starts

If your robot starts up before the NAS is online (e.g., after a power outage or router restart), the script will attempt to mount the NAS 10 times, waiting 60 seconds between each attempt. If it still can't mount the NAS, it will exit with an error message.

When the router and NAS are back online, you have two options depending on whether the service is already running:

  1. Check if the Service is Running:

    sudo systemctl status mount_nas.service
    
    • If the service status shows "active (exited)", it means the service has completed its run but didn't manage to mount the NAS.
  2. If the Service is Not Running:

    • Manually start the service using:
      sudo systemctl start mount_nas.service
      
  3. If the Service is Already Running:

    • Restart the service to attempt mounting the NAS again:
      sudo systemctl restart mount_nas.service
      

Either of these options will attempt to mount the NAS again.

This way, you can first check if the service is running. If it's not, you can start it manually. If it is running but didn't manage to mount the NAS, you can restart it to try again.

Troubleshooting

  • If the service fails, you can check its logs using:
    sudo journalctl -u mount_nas.service
    
  • Make sure that the NAS is reachable and the credentials are correct in the script.
  • Ensure that the network is up and running before the service tries to mount the NAS. The service file already includes After=network-online.target to handle this, but double-check if your system respects this dependency.