Upload
aileen-pamela-morris
View
230
Download
0
Embed Size (px)
Citation preview
Linux Development8.02.2016Lecture 7
Schedule• Linux Root Filesystem
Linux Root Filesystem
Principle and solution• Filesystems are organized as hierarchy• In Unix systems application and user see a single global
hierarchy of files and directories• Filesystems are mounted in a specific location in this
hierarchy of directories• The content of this directory reflects the content of a storage
device• The start point of a mounted directory is called start point
• The principle of work leads to easy access
How to mount a device• 1. Create directory in /mnt/ called usbstorage• 2. Plug a USB flash device in your system and check
where it is (using ls /dev or fdisk -l)• 3. Mount the device using # mount /dev/sdxXX
/mnt/usbstorage• 4. ls /mnt/usbstorage
How to unmount device• # umount /mnt/usbstorage
Deeper in mount command• # mount –t [type] [device] [mountpoint]• -t is argument for specifying the device type (also called
format e.g. FAT, FAT32, HFS+, NTFS)• [type] – the system type – vfat, ntfs, ext3, ext4, auto• [device] – specify the device (our device was /dev/sdb)• [mountpoint] – the folder that will be our mount folder
(in our case it was /mnt/usbstorage)• Check the –o option in the internet
Task 1• Format a flash drive to fat32 format on your host OS• Mount it to Linux• Make few directories/files• Unmount it• Connect to your host FS and check if everything is ok
Root filesystem• The filesystem mounted on / is called root filesystem• It is the core filesystem for linux and cannot be
unmounted• It is mounted directly to the kernel• When no root filesystem is available the kernel panics
(the problem from Lecture 4)• It is commonly called rootfs
Rootfs mouting locations• From harddisk• From USB drive• From SD card• From NAND• From network (using NFS)• From memory, uploaded through the bootloader
Rootfs over network (just FYI)
Host NFS server NFS client build into the kernel
NFS server installation• $ sudo apt-get install nfs-kernel-server• $ Add the export directory to your /etc/exports file:
~/rootfs 192.168.xxx.xxx(rw, no_root_sqash,no_subtree_check)• 192.168.xxx.xxx is the client IP address• rw, no_root_squash,no_subtree_check are the NFS server
options for this directory export• Start the NFS servier suing sudo/etc/init.d/nfs-kernel-
server start
NFS client• The kernel must be compiled with:• CONFIG_NFS_FS=y• CONFIG_IP_PNP=y• CONFIG_ROOT_NFS=y
• The kernel must be booted with the following parameters• root=/dev/nfs• Ip=192.168.xxx.xxx # target IP• Nfsroot=192.168.xxx.xxx:/home/user/rootfs # NFS server
details, where xxx.xxx are the numbers of the host IP address, and /user/ must be changed with your user name
Rootfs in memory: initramfs• The rootfs can be integrated into the kernel image• It is loaded to the system with the kernel• The mechanism is called initramfs • It can be make in one image with the kernel• Also it can be loaded separately by the bootloader
• Useful for:• Booting in small systems• As an intermediate step before switching to the real
root(installing a new system, network system install)
Rootfs in memory: initramfs
Kernel image (zImage, bzImage, etc)
Kernel core and data Root file system stored as a compressed archive
Rootfs in memory: initramfs• The content is defined in the kernel configuration using
the parameter CONFIG_INIT_RAMFS_SOURCE option . It can be the path to the filesystem located somewhere on your PC, cpio archive, ca be a text describing the content of initramfs (described in Documentation/filesystems/)• The kernel build will automatically integrate the fs in
your kernel image• Details can be fount at:• Documentation/fylesystems/ramfs-rootfs-initramfs.txt• Documentation/early-userspace/README
Questions ?
Filesystem content
Root fs organization• The Linux rootfs organization in well-defined bt
Filesystem Hierarchy Standart• http://linuxfoundation.org/collaborate/workgroups/lsb/fh
s• Most of the Linux systems use this specification• Application expect this organization• It is easier for developers to and users to have the same
organization in all systems
Important directories• /bin – basic programs• /boot – Kernel image (only when kernel is loaded from a
filesystem, non common on non-x86 architectures)• /dev – devices • /etc – system-wide configurations• /home – user directories• /lib – basic libraries• /media – mount points for removable media
Important directories• /mnt – mount point for static media• /proc – mount point for the proc virtual fs• /root – home directory for the root user• /sbin – basic system programs• /sys – mount point for sysfs virtual fs • /tmp – temporary files• /usr – user programs, libraries• /var – variable data files – logging data, temporary files
Separation of programs and libraries• Basic programs are installed in /bin and /sbin and basic
libraries in /lib• All other programs are installed under /usr/bin and
user/sbin, libraries /usr/lib
Questions ?
Devices
Devices• One of the most important kernel role is to allow
application to access hardware devices• In the most linux systems device types is divided to• Character device• Block device
• Internally the kernel identifies the device by three characteristics• Type (char or block)• Major number (Category of device)• Minor number (identifier of device)
Device types• Block device• HDD, SD card, SSD, Flash drive, etc.
• Char devices• Serial devices• The stream of information has no beginning and no end, no
size
Devices represented as files• The devices in unix are represented as files• This allows API protocols to be used for development (as
open, write, read, close, etc)• All device files are store in /dev directory
Devices represented as files• A simple C program that writes a message to a serial
device
int fd;fd = open(“dev/ttyS0”, O_RDWR);write(fd, “Hello it’s me”, 13);close(fd);
Creating device files• On a basic linux system the device files have to be
made manually using mknod command• mknod /dev/<device> [c|b] major minor
• Since kernel version 2.6.32 a lot of the Linux systems create the device files automatically• Devtmpfs – vitual file system• udev – daemon for creating and destroing device files
Questions ?
Pseudo file systems
/proc• /proc fs allows • The kernel to expose statictics about running processes in the
system• The user to adjust at runtime various system parameters
about process management, memory management etc.• /proc is needed by the mass of the userspace
application• More information – Documentation/filesystems/proc.txt
in the kernel sources
/proc• A directory holds information about each process that
runs on the linux machine• It contains information about the files opened by the
process, the CPU and memory usage• Can be mounted by making directory proc in root and
then # mount –t proc none /proc
/proc• /proc/interrupts, /proc/devices,
/proc/iomem,/proc/ioportts contain general device related information• /proc/cmdline contains the kernel command line• /proc/sys conations many files that can be written to
adjust kernel parameters – more information available at Documentation/sysctl/
sysfs• Integrated since kernel 2.6• Allows to represent in user space the cision that the
kernel has of the buses, devices and drivers in the system• Used in various user space applications• All application expect sysfs to be mounted in /sys
directory• Can be mounted by making directory sys in “/” and then
# mount –t sysfs none /sys
Questions ?
Minimal file system
Basic Applications• In order Linux to work, a Linux system needs at least a
few applications• init application – first userspace application that is
started by the kernel after mounting the rootfs• Kernel looks for it in /sbin/, /bin/, /etc/, /bin/sh• It initialize all other userspace applications and services
Basic applications• shell application – to allow user to interact with the
system• Basic unix apps like mv, cp, mkdir, cat, etc.• These commands must be integrated in the rootfs to
make it usable
Bootloader(loads kernel to ram
and stat it)
Rootfs
Kernel – initialize devices, mounts
rootfs
/sbin/init -starts other
userspace services and apps
shell Other apps
Overall booting process
Kernel – initialize devices, mounts
rootfs
Booting process with initramfsBootloader
(loads kernel to ram and stat it)
/sbin/init -starts other
userspace services and apps
/init – starts early userspace
commands, loads drivers to access the final rootfs,
mounts rootfs and start it
Busybox
Busybox• In order to work properly Linux system needs few basic
sets of programs:• An init program• A shell• Various basic utilities for file manipulation
• For easier setup and run of a rootfs with all the needed basic components there is Busybox. It is used for compiling all the needed basic programs.
Busybox• Provides simple implementation of init program• A single configuration file - /etc/inittab• Allows to run services at start and to make sure that
certain services are always running on the system• See examples/inittab in Busybox for details on the
configuration
Busybox setup• 1. Get busybox source - https://
www.busybox.net/downloads/busybox-1.24.1.tar.bz2
• 2. untar it• 3. Configure it with defconfig (do not forget the ARCH
and CROSS_COMPILE)• 4. Configure it with menuconfig and check Busybox
Settings Options -> Build as a static binary
Busybox setup• 5. make ARCH=….... CROSS_COMPILE=......... Install• 6. After make we need to compress the whole folder
_install to a rootfs.img, this happens using • $ cd _install• $ find . | cpio –o --format=newc > ../rootfs.img• xq
Final Task• 1. Build kernel for versatilepb• 2. Build busybox• 3. $ qemu-system-arm –M versatilepb –kernel zImage –
initrd rootfs.img –append “root=/dev/ram rdinit=/bin/sh console=ttyAMA0,115200” –nographic # (for people with GUI change –nographic with –serial stdio)• 4. mount /proc• 5. mount /sys
Homework task 1• 1. Modify rootfs directory by adding folders proc sys dev
etc etc/init.d• 2. Make a script inside etc/init.d/ to mount proc and sys
when the system starts • 3. Post the qemu command line start string (e.g. qemu-
system-arm –M versatilepb –kernel zImage …...........)
Homework task 2(optional)• Find a way to load rootfs over network.
• This task is optional