Showing posts with label systemd. Show all posts
Showing posts with label systemd. Show all posts

Monday, October 17, 2022

Auto mount an SD card upon insert on Ubuntu Server

I have a headless Raspberry Pi board running Ubuntu 20.04.x server with a USB SD card reader. I wanted the system to automatically mount an SD card to a fixed mount point, e.g. /media/ubuntu/sdcard/ upon card insertion. By default, Ubuntu Server doesn't mount the SD card so I had to do some setup and configuration, as illustrate in the steps below. I had to install some prerequisite software package (udevil) and create a systemd service.

Install udevil package and create mount directory

  1. Open up a Terminal and type in the following command to install udevil.

    $ sudo apt install udevil

  2. Then, create a SD card directory mount point, e.g. /media/ubuntu/sdcard/ with the following command:

    $ mkdir -p /media/ubuntu/sdcard

 

Add a mount rule to the fstab file

  1. Using a text editor, e.g. vi, open up the system file /etc/fstab.

    $ sudo vi /etc/fstab

  2. Append the following line:

    /dev/sda1 /media/ubuntu/sdcard auto rw,user,exec,umask=000  0  2

    An example fstab file is shown below:
LABEL=writable  /        ext4   defaults        0 1
LABEL=system-boot       /boot/firmware  vfat    defaults        0       1
/dev/sda1       /media/ubuntu/sdcard auto rw,user,exec,umask=000        0       2

Create the devmon Systemd service

  1. In the Terminal, change directory to /etc/systemd/system/.

    $ cd /etc/systemd/system

  2. Using a text editor, e.g. vi, create a file e.g. devmon.service. Enter the following and save the file.

    [Unit]
    Description=Systemd service for running devmon
    [Service]
    Type=simple
    User=ubuntu
    Group=ubuntu
    ExecStart=/usr/bin/devmon
    [Install]
    WantedBy=multi-user.target
    


  3. In the Terminal, type in the following to generate the service:

    $ sudo systemctl daemon-reload
    $ sudo systemctl enable devmon

  4. Then either reboot the board or run the following command to start the service:

    $ sudo systemctl start devmon

 Monitor the service

  1. In a Terminal, type in the following to monitor the newly create devmon service when an SD card is inserted or unmounted.

    $ journalctl -u devmon -f

 Note: The SD card should be unmounted properly using the following command(s):

$ udevil umount /media/ubuntu/sdcard

- or -

$ sudo umount /media/ubuntu/sdcard

Monday, September 19, 2022

Run Quectel's Connection Manager as a systemd service on Ubuntu

I found myself in possession of a modem by Quectel (https://www.quectel.com) to hook up to a Raspberry Pi running Ubuntu 20.04. I wanted to auto run Quectel's connection manager executable quectel-CM on system start up but found limited information available online from Quectel. So I had to roll up my sleeves and make my own systemd service for that purpose.

This post outlines the steps I took:

Download and compile the quectel-CM executable

  1. Using an Internet browser, download the latest Quectel LTE, 5G Linux USB driver, e.g. Quectel_LTE5G_Linux_USB_Driver_V1.0.zip from https://www.quectel.com/download-zone.
     
  2.  Extract QConnectManager_Linux_V1.0.zip into a folder e.g. /home/ubuntu/Downloads/quectel-CM/.

  3. In a Terminal, change directory to the extracted folder.

    $ cd ~/Downloads/quectel-CM/

  4. Use the make command to compile the connection manager executable.

    $ make

    The quectel-CM executable is compiled.

Place the quectel-CM exe to the run time directory

  1. In a Terminal, type in the following commands to change directory to the extracted directory.

    $ cd /home/ubuntu/Downloads/quectel-CM/

  2. Copy the executable to /usr/local/bin/.

    $ sudo cp quectel-CM /usr/local/bin

Create a systemd service file

  1.  In the directory /etc/systemd/system, use a text editor to create a service file e.g. quectelcm.service.

    $ sudo vi /etc/systemd/system/quectelcm.service

  2. Type in the following lines, save and exit the file:
    [Unit]
    Description=Systemd service for running Quectel's Connection Manager quectel-CM executable
    
    [Service]
    ExecStart=/usr/local/bin/quectel-CM 
    Restart=always
    RestartSec=5
    
    [Install]
    WantedBy=multi-user.target
    

  3. Give the service file the following executable permission:

    $ sudo chmod 664 /etc/systemd/system/quectelcm.service

 Create the service

  1. In the Terminal, type in the following commands:

    $ sudo systemctl daemon-reload
    $ sudo systemctl enable quectelcm

    The quectelcm service is created.

 

Running and monitoring the service

  1. You can either reboot the Raspberry Pi or type in the following command to start the service in a Terminal.

    $ sudo systemctl start quectelcm

  2. To monitor the quectelcm service, use the journalctl command:

    $ journalctl -u quectelcm -f


Note: In order for quectel-CM to request and set the machine's network address, the software prerequisites net-tools and udhcpc must be installed on the Raspberry Pi.