Lesson 6: Boot and the Filesystem Hierarchy

Note

  • grub, filesystem stuff based roughly on Frostsnow's talk
  • basics of kernel and differences between virtualization/physical (the picture that kevin draws)

The Linux Filesystem Hierarchy

What's a filesystem?

In computing, a file system is used to control how information is stored and retrieved. Without a file system, information placed in a storage area would be one large body of information with no way to tell where one piece of information stops and the next begins.

Filesystem can mean:

Note

Moving from Windows?

  • Binaries, not executables.
  • Directories, not folders.
  • Read, not load.
  • Symbolic links, not shortcuts.
  • Write, not save.

The File System


_images/you_are_here.jpg
$ ls
bin   etc         initrd.img.old  lost+found  opt   run      srv  usr
boot  home        lib             media       proc  sbin     sys  var
dev   initrd.img  lib64           mnt         root  selinux  tmp  vmlinuz

Installed programs and utilities

/bin                /usr/sbin
/sbin               /usr/local/bin
/usr/bin            /usr/local/sbin
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
$ which bash
/bin/bash

User-Specific Data & Configuration

Where are drives mounted?

$ dmesg | tail
[260930.208715]  sdb: sdb1
[260930.320756] sd 6:0:0:0: >[sdb] Asking for cache data failed
[260930.320765] sd 6:0:0:0: >[sdb] Assuming drive cache: write through
[260930.320771] sd 6:0:0:0: >[sdb] Attached SCSI removable disk

Space on drives

$ df -h /
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda8        73G   29G   41G  42% /
$ du -sh /home/
21G /home/

Three Tiers of Filesystem Hierarchy

_images/hierarchy.jpg

Common Directories

Directory Contents
/bin Binary files
/include Header files for C/C++ programs
/lib Libraries
/sbin Binary files for root (superuser)
/boot Files essential for booting kernel, initramfs
/dev Virtual filesystem, exports hardware devices
/etc System-wide configurations
/home Individual users' data
/media Removable storage devices
/mnt Like media -- place to mount disks and things

Common Directories

Directory Contents
/opt "Add-on application software packages"
/proc Virtual filesystem exporting system data
/root homedir for root
/run Volatile information accumulated since boot
/sys Virtual filesystem exporting kernel objects
/tmp Temporary files
/var Data which varies -- logs, mail, etc.
/usr/share Architecture-independent, read-only data
/usr/src Kernel source code

/proc has lots of useful system information

Which Linux kernel version are you running?

$ cat /proc/version
Linux version 3.5.0-17-generic (buildd@allspice) (gcc version 4.7.2
(Ubuntu/Linaro 4.7.2-2ubuntu1) ) #28-Ubuntu SMP Tue Oct 9 19:31:23 UTC 2012

Learn about system's hardware

$ less /proc/cpuinfo
$ less /proc/meminfo

Some parts of /proc can be written as well as read...

$ echo 3 > /proc/sys/vm/drop_caches # drop caches

Commands for working with filesystems

Creating filesystems

$ mkfs

Mounting filesystems

$ mount
# -t for type
# -o for options
# requires device path and mount point

Loopback devices

$ losetup
$ /dev/loop*
# makes it look like a device instead of a file

devfs

sd*
sr*
/dev/null
/dev/random
/dev/urandom
/dev/zero

Blocks and dd

$ dd if=/dev/random of=/dev/sda
# What will this do?

Filesystem Consistency

More about Journaling

The Boot Process

_images/xkcd-fight.png

Bootstrapping

Note

kernel loaded into memory, initialization tasks, and available to users

Init
  • kernel spawns init which is always PID 1
  • controls the boot process
  • can be a simple script to a binary

Steps in boot process

Note

Kernel
  • 1st stage – bootloader, 2nd, boot the kernel
  • boot from boot loader
  • load into memory
  • located in /boot/ on Linux
Hardware config
  • locate & initialize hardware
  • print out what it does
System processes
  • init, kswapd, pdflush, etc
  • init only real process
  • Others look like processes for scheduling (appear as [kswapd] with ps)

_images/booting.png
  1. Kernel initialization
  2. Hardware configuration
  3. System processes
  4. Operator intervention (single-user)
  5. Execution of start-up scripts
  6. Multi-user operation

Booting

Note

On hardware specific to UNIX (i.e. Sun)
  • firmware knows how to use devices
  • talk to the network
  • understand filesystems
  • all accessible via the commandline
BIOS smarter than they used to be
  • Not standardized
  • Most servers support PXE

Boot Loaders (Grub)

Note

Grub
  • next generation PC boot loader
  • no need to “re-run grub” config updates
  • Grub config
  • disks are index based from zero
  • grub install commands
  • netboot, pretty, serial
  • device.map, grub.conf
  • robust with weird disk geometry
grub> root (hd0,0)    (Specify where your /boot partition resides)
grub> setup (hd0)     (Install GRUB in the MBR)
grub> quit            (Exit the GRUB shell)

grub-install

Single User Mode

Note

Show on VM
  • enter grub, hit ESC, pick kernel, hit “e” for edit
  • use arrows

Typically ask for root password

_images/single-user-mode.png
  • Troubleshoot problems
  • Manual Filesystem Checks
  • Booting with bare services
  • Fix boot problems
  • Add “single” to kernel option

Startup Script Tasks

Note

Verbose and print out description of what its doing.

Old days were to manually adjust scripts, not anymore. Most are configurable now.


_images/fsck.jpg

System-V Boot Style

Note

  • System-V Most common today
  • Show system changing between different run levels.
  • Slightly different between Distros

Run levels:

level 0 sys is completely down (halt)
level 1 or S single-user mode
level 2 through 5 multi-user levels
level 6 reboot level

/etc/inittab

Note

Look at inittab

# The default runlevel.
id:2:initdefault:

# What to do in single-user mode.
~~:S:wait:/sbin/sulogin

# What to do when CTRL-ALT-DEL is pressed.
ca:12345:ctrlaltdel:/sbin/shutdown -t1 -a -r now

# terminals
1:2345:respawn:/sbin/getty 38400 tty1
T0:23:respawn:/sbin/getty -L ttyS0 9600 vt100

init.d Scripts

Note

sshd init script
  • case statement
  • functions
  • chkconfig
$ service sshd status
openssh-daemon (pid  1186) is running...

$ service sshd restart
Stopping sshd:                                             [  OK  ]
Starting sshd:                                             [  OK  ]

Starting services on boot

Note

Show sshd script show list, adding, removing, enabling, disabling

$ chkconfig --list sshd
sshd            0:off 1:off 2:on  3:on  4:on  5:on  6:off

$ chkconfig sshd off

$ chkconfig --list sshd
sshd            0:off 1:off 2:off 3:off 4:off 5:off 6:off

Configuring init.d Scripts

Note

show sendmail & network config examples for CentOS

/etc/defaults seems to be more common between UNIX's

$ cat /etc/sysconfig/ntpd
# Drop root to id 'ntp:ntp' by default.
OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"

Shutting Down

Note

Modern systems are less touchy with hard resets, but still need to be careful. Only for emergencies.

Shutdown -h

$ wall hello world
Broadcast message from root@devops-bootcamp (pts/0) (Fri Jan 31 00:40:29 2014):

hello world

Homework