Skip to main content

System Configuration

1. Shell Script

Shell is an application that provides an interface for interaction between the user and the operating system kernel. Through a shell, users can send commands to the operating system, manipulate the file system, run programs, and perform various system tasks. Shells are typically command-line interfaces where users enter text commands to perform various tasks.

Common shells include sh, bash, csh, tcsh, ash, and more.

  • sh (Bourne Shell): Sh is one of the most common command-line interpreters (shells) in Unix and Linux operating systems. It was named after its creator, Stephen Bourne, and is one of the earliest Unix shells.
  • Bash (Bourne-Again Shell): Bash is the most commonly used shell in Unix and Linux systems. It is an improved version of the Bourne Shell, offering powerful scripting capabilities and a rich set of command-line tools.
  • Dash: Dash stands for Debian Almquist Shell and is often used as the default shell for system initialization scripts. It is known for its fast startup and is suitable for system bootstrapping and script tasks.
  • Csh (C Shell) and Tcsh (Tenex C Shell): These are variants of the C Shell and are primarily used in Unix systems. They have different syntax and features compared to Bash.
  • Ash (Almquist Shell): Ash is another lightweight shell commonly used in embedded systems and some lightweight Linux distributions.

2. Creating Your First Script

Open a text editor, create a new text file, and name it luckfox.sh. The file extension .sh is commonly used to indicate a shell script, but the extension does not affect script execution.

  1. Create the script and input the following code into luckfox.sh:

    #!/bin/sh
    echo "Hello Luckfox !"
    • The #!/ (shebang) line is a convention that tells the system which interpreter to use to execute the script, specifying which shell to use. In this case, it specifies /bin/sh as the interpreter.
    • The second line uses the echo command to output the text "Hello Luckfox!" to the standard output (usually the screen). Running a command in a .sh file is equivalent to running it directly in the terminal.
  2. Add execute permission to the script and run it:

    # vi test.sh
    # chmod 775 test.sh
    # ./test.sh
    Hello Luckfox !

3. Setting Up Autostart

3.1 Buildroot Environment

In the /etc/init.d/ directory, you can find system scripts related to startup and shutdown processes.

When the system starts, it first executes rcS, which loops through and executes the start branch of scripts beginning with S. When the system shuts down, it executes rcK, which similarly loops through and executes the stop branch of such scripts.To add a script that runs during system startup, follow these steps:

  1. Create a new script with a name starting with S??* in the /etc/init.d/ directory.

    • S: Indicates that the script is for starting.
    • ??: Two digits that represent the priority. Lower numbers indicate higher priority.
    • *: A descriptive string that describes the script's purpose.
  2. Implement start and stop branches in the script.

  3. Provide execute permission for the script:

    chmod +x script_name
    #!/bin/sh

    case $1 in
    start)
    echo "start"
    ;;
    stop)
    echo "stop"
    ;;
    *)
    exit 1
    ;;
    esac
  4. Adjust the priority of system scripts:

    mv S99usb0config S90usb0config 
    mv S99_auto_reboot S90_auto_reboot
  5. For example, to set a static IP address, you can name the file S99staticip (make sure this naming is after the system scripts).

    • Ensure the development board is connected to a router or switch, and the static IP address matches the router's subnet.

      Do not conflict with IP addresses assigned by the router.

    #!/bin/sh


    MAX_TRIES=10
    TRIES=0


    check_ip_address() {

    if ifconfig eth0 | grep -q "inet "; then
    return 0
    else
    return 1
    fi
    }

    static_ip()
    {

    while [ $TRIES -lt $MAX_TRIES ]; do
    if check_ip_address; then
    echo "DHCP succeed!"
    ifconfig eth0 192.168.10.66 netmask 255.255.252.0
    route add default gw 192.168.11.1
    echo "nameserver 114.114.114.114" > /etc/resolv.conf
    ifconfig eth0 up
    break
    else
    echo "wait DHCP IP..."
    TRIES=$((TRIES + 1))
    sleep 5
    fi
    done


    if [ $TRIES -eq $MAX_TRIES ]; then
    echo "error"

    fi
    }
    case $1 in
    start)
    echo "start"
    static_ip
    ;;
    stop)
    echo "stop"
    ;;
    *)
    exit 1
    ;;
    esac
    • In the command ifconfig eth0 192.168.10.66 netmask 255.255.252.0, replace with your own IP address and subnet mask.

      For the command route add default gw 192.168.11.1, change the gateway address to your own configuration. If uncertain, use commands like route -n to check.

3.2 Ubuntu Environment

In the Ubuntu environment, you can use systemd to create a service. The steps are as follows:

  • Ensure the development board is connected to a router or switch, and the static IP address matches the router's subnet.
  • Do not conflict with IP addresses assigned by the router.
  1. Create the service file.

    sudo nano /etc/systemd/system/static-ip.service
  2. Add the service content.

    [Unit]
    Description=Set Static IP Address
    After=network.target

    [Service]
    Type=oneshot
    ExecStart=/usr/local/bin/set-static-ip.sh
    RemainAfterExit=yes

    [Install]
    WantedBy=multi-user.target
  3. Create the script file.

    sudo nano /usr/local/bin/set-static-ip.sh
  4. Add the script content.

    #!/bin/bash

    ifconfig eth0 192.168.10.66 netmask 255.255.252.0
    ip link set eth0 up
    route add default gw 192.168.11.1
    echo "nameserver 114.114.114.114" > /etc/resolv.conf
    ifconfig eth0 up
  5. Set executable permissions.

    sudo chmod +x /usr/local/bin/set-static-ip.sh
  6. Reload systemd.

    sudo systemctl daemon-reload
  7. Enable the service.

    sudo systemctl enable static-ip.service
  8. Test the service.

    sudo systemctl start static-ip.service
  9. Check the status.

    sudo systemctl status static-ip.service
  10. After rebooting, test the network.

    sudo reboot
    ping www.baidu.com