49
Linux Development 8.02.2016 Lecture 7

Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

Embed Size (px)

Citation preview

Page 1: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

Linux Development8.02.2016Lecture 7

Page 2: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

Schedule• Linux Root Filesystem

Page 3: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

Linux Root Filesystem

Page 4: Linux Development 8.02.2016 Lecture 7. Schedule 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

Page 5: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

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

Page 6: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

How to unmount device• # umount /mnt/usbstorage

Page 7: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

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

Page 8: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

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

Page 9: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

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

Page 10: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

Rootfs mouting locations• From harddisk• From USB drive• From SD card• From NAND• From network (using NFS)• From memory, uploaded through the bootloader

Page 11: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

Rootfs over network (just FYI)

Host NFS server NFS client build into the kernel

Page 12: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

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

Page 13: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

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

Page 14: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

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)

Page 15: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

Rootfs in memory: initramfs

Kernel image (zImage, bzImage, etc)

Kernel core and data Root file system stored as a compressed archive

Page 16: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

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

Page 17: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

Questions ?

Page 18: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

Filesystem content

Page 19: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

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

Page 20: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

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

Page 21: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

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

Page 22: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

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

Page 23: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

Questions ?

Page 24: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

Devices

Page 25: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

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)

Page 26: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

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

Page 27: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

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

Page 28: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

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);

Page 29: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

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

Page 30: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

Questions ?

Page 31: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

Pseudo file systems

Page 32: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

/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

Page 33: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

/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

Page 34: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

/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/

Page 35: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

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

Page 36: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

Questions ?

Page 37: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

Minimal file system

Page 38: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

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

Page 39: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

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

Page 40: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

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

Page 41: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

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

Page 42: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

Busybox

Page 43: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

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.

Page 44: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

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

Page 45: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

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

Page 46: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

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

Page 47: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

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

Page 48: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

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 …...........)

Page 49: Linux Development 8.02.2016 Lecture 7. Schedule Linux Root Filesystem

Homework task 2(optional)• Find a way to load rootfs over network.

• This task is optional