# Create Your Own Root File System
[toc]
## 1. Preface
This how-to is taking AM335x series as example to build the root file system
- ARM Architecture: ARMv7-A
- CPU Family(ARM Cores): ARM Cortex-A8(Cortex-A is 32 bit processor)
- Floating point strategy: VFPv3-D16
- Endianness: Little endian(ARM could set as big and little endian, little as the default setting)
### 1.1 Source code to know your system endianness
Build the source code to test it on your target system
```C
#include <stdio.h>
typedef union {
unsigned long l;
unsigned char c[4];
} EndianTest;
int main() {
EndianTest et;
et.l = 0x12345678;
printf("System endianness would be:");
if (et.c[0] == 0x78 && et.c[1] == 0x56 && et.c[2] == 0x34 && et.c[3] == 0x12) {
printf("Little Endiann");
} else if (et.c[0] == 0x12 && et.c[1] == 0x34 && et.c[2] == 0x56 && et.c[3] == 0x78) {
printf("Big Endiann");
} else {
printf("Unknown Endiann");
}
printf("0x%lX Memory order:\n", et.l);
for (int i = 0; i < 4; i++) {
printf("%p : 0x%02X\n", &et.c[i], et.c[i]);
}
return 0;
}
```
## 2. Build your basic system and packages
Buildroot would be the tool to build the root file system
In this document, take the TI AM335x as the target device to build the root file system. The hardware feature and support could go [the ti webpage](https://www.ti.com/processors/sitara-arm/am335x-cortex-a8/overview.html)
buildroot could be run on linux platform
### 2.1 What is Buildroot
Buildroot is a set of Makefiles and patches that simplifies and automates the process of building a complete and bootable Linux environment for an embedded system(ref: [wiki definition](https://en.wikipedia.org/wiki/Buildroot))
The official web site is [here](https://buildroot.org/),choose the long term support version to build the root file system
The version used here is buildroot-2020.02.6, which is released on Sep 5th, 2020
### 2.2 Settings of buildroot
Before starting to build the buildroot, make sure you got everything ready, please refer [the document page](https://buildroot.org/downloads/manual/manual.html#requirement) for the system requirement
Here, buildroot would take the systemd as the init system service
#### 2.2.1 Kconfig-like setting features
Please enter the directory of buildroot folder and type the command to enter the buildroot configuration setting
> cd buildroot-2020.02.6
> make menuconfig
## 3. Porting the service and settings
### 3.1 Buildroot file system tools must-be
- dosfstools
- e2fsprogs
### 3.2 Buildroot root file system configuration
For AM335x platform, several things need to be done(on my developing device)
- copy /lib/firmware, /lib/modules from SDK's rootfs
- copy /lib/libtinfo.so* from SDK's rootfs(systemctl needs)
- modify file /etc/profile, set PS1='\u@\h \w# '
- create symbolic link for init:ln -s /lib/systemd/systemd init
- disable mysql service(if exists) in /etc/systemd/system or fix it next step
> ln -s /dev/null mysqld.service
- fix mysql service start: check folder /var/log/mysql/mysqld.log exist or not, and owner:group should be mysql:root, and mode is 0755
- disable postgresql service(if exists) in /etc/systemd/system or fix it next step
> ln -s /dev/null postgresql.service
- fix postgresql service start: make sure the locale with 'no locale':
> /usr/bin/pg_ctl initdb -D /var/lib/pgsql -o '--no-locale --encoding=UTF8'
- edit sshd config for ssh root login(etc/ssh/sshd_config)
> PermitRootLogin yes
- edit lighttpd's config (/etc/lighttpd) if need
- edit lighttpd's config with the location of sever root
- edit /etc/issue for shell login and copy it to /etc/issue.net for ssh login
- [optiotnal] copy qt library to your rootfs
## 4. Network in Buildroot built rootfs
### 4.1 systemd's networkd
Without building network manager(NetworkManager) or connection manager(ConnMan), systemd's networkd is the default network setting configuration.
It's using '/etc/network/interfaces' as the config file.
### 4.2 Network Manager
- Buildroot's NetworkManager use freedesktop setting with dbus controlling
- Config Path: /etc/dbus-1/system.d/org.freedesktop.NetworkManager.conf
- Service definition paths:
- /etc/systemd/system/dbus-org.freedesktop.NetworkManager.service
- /etc/systemd/system/dbus-org.freedesktop.network1.service
- /etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service
- /etc/systemd/system/dbus-org.freedesktop.resolve1.service
- /etc/systemd/system/dbus-org.freedesktop.timesync1.service
Please refer to [NetworkManager](https://wiki.gnome.org/Projects/NetworkManager) Project for more information
### 4.3 ConnMan
Please refer to [ConnMan wiki](https://wiki.archlinux.org/index.php/ConnMan)for more information
## 5. Some Tips in system run time
### 5.1 Export GPIO and control
1. Enter /sys/class/gpio
2. write the gpio number to 'export' file: echo NUMBER > export
3. a gpioNUMBER folder will show in the gpio folder
4. Enter that folder to control direction(in or out) and value(0 for low, 1 for high)
### 5.2 Check tty driver and serial status
cat /proc/tty/driver/serial for more detail
###### tags: `buildroot` `root file system` `rootfs`