LPIC-1

[101-400]

Objectives

Important Commands

1. System Architecture

System Boot Process

Firmware

BIOS

MBR (Master Boot Record - DOS Partition Table )

The MBR consists of a primary boot loader (first 446 bytes)  and a partition table. 

sh# dd if=/dev/sda bs=1 count=512 2>/dev/null | hexdump -Cv

00000000  eb 63 90 00 00 00 00 00  00 00 00 00 00 00 00 00  |.c..............|      // ==begin

00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|      // * The

00000020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|      // * first

00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|      // * 446 bytes

00000040  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|      // * are

00000050  00 00 00 00 00 00 00 00  00 00 00 80 01 00 00 00  |................|      // * the

00000060  00 00 00 00 ff fa 90 90  f6 c2 80 74 05 f6 c2 70  |...........t...p|      // * bootstrap

00000070  74 02 b2 80 ea 79 7c 00  00 31 c0 8e d8 8e d0 bc  |t....y|..1......|      // * part,

00000080  00 20 fb a0 64 7c 3c ff  74 02 88 c2 52 be 80 7d  |. ..d|<.t...R..}|      // * which

00000090  e8 17 01 be 05 7c b4 41  bb aa 55 cd 13 5a 52 72  |.....|.A..U..ZRr|      // * contains

000000a0  3d 81 fb 55 aa 75 37 83  e1 01 74 32 31 c0 89 44  |=..U.u7...t21..D|      // * the

000000b0  04 40 88 44 ff 89 44 02  c7 04 10 00 66 8b 1e 5c  |.@.D..D.....f..\|      // * primary

000000c0  7c 66 89 5c 08 66 8b 1e  60 7c 66 89 5c 0c c7 44  ||f.\.f..`|f.\..D|      // * boot loader

000000d0  06 00 70 b4 42 cd 13 72  05 bb 00 70 eb 76 b4 08  |..p.B..r...p.v..|      // * code

000000e0  cd 13 73 0d 5a 84 d2 0f  83 d8 00 be 8b 7d e9 82  |..s.Z........}..|      // * 

000000f0  00 66 0f b6 c6 88 64 ff  40 66 89 44 04 0f b6 d1  |.f....d.@f.D....|      // *

00000100  c1 e2 02 88 e8 88 f4 40  89 44 08 0f b6 c2 c0 e8  |.......@.D......|      // *

00000110  02 66 89 04 66 a1 60 7c  66 09 c0 75 4e 66 a1 5c  |.f..f.`|f..uNf.\|      // *

00000120  7c 66 31 d2 66 f7 34 88  d1 31 d2 66 f7 74 04 3b  ||f1.f.4..1.f.t.;|      // *

00000130  44 08 7d 37 fe c1 88 c5  30 c0 c1 e8 02 08 c1 88  |D.}7....0.......|      // *

00000140  d0 5a 88 c6 bb 00 70 8e  c3 31 db b8 01 02 cd 13  |.Z....p..1......|      // *

00000150  72 1e 8c c3 60 1e b9 00  01 8e db 31 f6 bf 00 80  |r...`......1....|      // *

00000160  8e c6 fc f3 a5 1f 61 ff  26 5a 7c be 86 7d eb 03  |......a.&Z|..}..|      // *

00000170  be 95 7d e8 34 00 be 9a  7d e8 2e 00 cd 18 eb fe  |..}.4...}.......|      // *

00000180  47 52 55 42 20 00 47 65  6f 6d 00 48 61 72 64 20  |GRUB .Geom.Hard |      // *

00000190  44 69 73 6b 00 52 65 61  64 00 20 45 72 72 6f 72  |Disk.Read. Error|      // *

000001a0  0d 0a 00 bb 01 00 b4 0e  cd 10 ac 3c 00 75 f4 c3  |...........<.u..|      // *

000001b0  00 00 00 00 00 00 00 00  df 8a 21 7e 00 00 00 20  |..........!~... |      // ==end 000001bd

000001c0  21 00 82 aa 28 82 00 08  00 00 00 00 20 00 80 aa  |!...(....... ...|      // 4 partition entries (each 16 bytes)

000001d0  29 82 83 d4 a2 32 00 08  20 00 00 f8 5f 02 00 00  |)....2.. ..._...|      // 

000001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|      //

000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|      // Boot Signature 0xaa55

00000200

Partition Table

The 'fdisk' command shows information about the partitions on the command line:

sh# fdisk -l /dev/sda

Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors

Units: sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disklabel type: dos

Disk identifier: 0x7e218adf


Device     Boot   Start      End  Sectors Size Id Type

/dev/sda1          2048  2099199  2097152   1G 82 Linux swap / Solaris

/dev/sda2  *    2099200 41943039 39843840  19G 83 Linux

Partition Entry Details

A partition entry consists of 16 bytes, that are layed out in the following order:

Example

UEFI (Unified Extensible Firmware Interface)



GPT (GUID Partition Table)

Protective MBR

To ensure Compatibility with older tools an MBR is part of GPT's specification and is called protective MBR. The MBR consists of only one partition entry with partition type '0xee' to indicate, that GTP is actually used. The MBR is called 'protective MBR', because it protects the GPT to get wiped out by old tools which don't know anything about GPT. This is how a protective MBR looks like:

sh# dd if=/dev/sda bs=512 count=4 status=none | hexdump -C

00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

*

000001c0  01 00 ee fe ff ff 01 00  00 00 af b7 cb 02 00 00  |................| 

000001d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

*

000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|    Boot Signature 0x55aa

00000200

...

GPT Header

...

00000200  45 46 49 20 50 41 52 54  00 00 01 00 5c 00 00 00  |EFI PART....\...Signature(8B), Revision(4B), Header size(4B)

00000210  d4 c5 30 c2 00 00 00 00  01 00 00 00 00 00 00 00  |..0.............Header Chksum(4B), 4B reserved, cur. LBA(8B)

00000220  af b7 cb 02 00 00 00 00  00 08 00 00 00 00 00 00  |................|  Backup LBA(8B), 1st usable LBA(8B)

00000230  8e b7 cb 02 00 00 00 00  7a 84 e2 b1 f6 79 b2 4b  |........z....y.K|  Last usable LBA(8B), Disk GUID (8B)

00000240  a7 03 a8 f3 b9 11 7f 08  02 00 00 00 00 00 00 00  |................|  Disk GUID(8B), Starting LBA(8B)

00000250  80 00 00 00 80 00 00 00  36 43 71 96 00 00 00 00  |........6Cq.....|  Num of parts(4B), PEsize(4B), crc32 part

00000260  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|  reserved (rest of the 512B block must be 

*                                                                               zeroes

00000400

...

GPT Partition Entries

...

00000400  28 73 2a c1 1f f8 d2 11  ba 4b 00 a0 c9 3e c9 3b  |(s*......K...>.;|    C12A7328-F81F-11D2-BA4B-00A0C93EC93B

00000410  a4 58 12 d7 70 23 b0 43  bd 1f 7c e3 e5 5b 49 17  |.X..p#.C..|..[I.|    Partition GUID

00000420  00 08 00 00 00 00 00 00  ff 3f 0f 00 00 00 00 00  |.........?......|    First LBA (8 bytes), Last LBA (8 bytes)

00000430  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|    Attribute Flags (8 bytes), partition name

*                                                                                 (72 bytes)

00000480  af 3d c6 0f 83 84 72 47  8e 79 3d 69 d8 47 7d e4  |.=....rG.y=i.G}.|    0FC63DAF-8483-4772-8E79-3D69D8477DE4

00000490  e3 97 0c 48 38 ac 2b 4e  86 b8 ee 13 7c 20 1d a3  |...H8.+N....| ..|    Partition GUID

000004a0  00 40 0f 00 00 00 00 00  ff c7 2d 00 00 00 00 00  |.@........-.....|    First LBA (8 bytes), Last LBA (8 bytes)

000004b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|    Attribute Flags (8 bytes), partition name

*                                                                                 (72 bytes)

00000500  af 3d c6 0f 83 84 72 47  8e 79 3d 69 d8 47 7d e4  |.=....rG.y=i.G}.|    0FC63DAF-8483-4772-8E79-3D69D8477DE4

00000510  ee 3c 93 0f c0 8d 7d 44  9d 34 99 6d 4d cd db 99  |.<....}D.4.mM...|    Partition GUID

00000520  00 c8 2d 00 00 00 00 00  ff af cb 02 00 00 00 00  |..-.............|    First LBA (8 bytes), Last LBA (8 bytes)

00000530  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|    Attribute Flags (8 bytes), partition name

*                                                                                 (72 bytes)

00000800

There are 3 Partition Entries in the above output: 


GTP Partition Table

sh# fdisk -l /dev/sda

Disk /dev/sda: 22.4 GiB, 24015495168 bytes, 46905264 sectors

Units: sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disklabel type: gpt

Disk identifier: B1E2847A-79F6-4BB2-A703-A8F3B9117F08


Device       Start      End  Sectors  Size Type

/dev/sda1     2048   999423   997376  487M EFI System

/dev/sda2   999424  3000319  2000896  977M Linux filesystem

/dev/sda3  3000320 46903295 43902976   21G Linux filesystem



Boot Loader / Boot Manager

GRUB



Grub Installation

UEFI

sh# grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=grub

# generates a boot option named 'grub' and the required files in the directory /boot/efi/EFI/grub/

BIOS

sh# grub-install --target=i386-pc /dev/sda

# writes the first-stage bootloader code into the Boot Sector (MBR) of the defined disk

Grub Configuration

sh# grub-mkconfig -o /boot/grub/grub.cfg

# generates the configuration in /boot/grub/grub.cfg by parsing the /etc/default/grub configuration file.

Hardware Integration

BUSES

Peripherals (Input/Output Devices)


Keywords

USB

Hosts

Ports

Connectors

Transfer Modes

Keywords

Hard Disks


Facts and Keywords

List Partition Table Type

sh# gdisk -l /dev/sda

GPT fdisk (gdisk) version 1.0.1


Partition table scan:

  MBR: protective

  BSD: not present

  APM: not present

  GPT: present


Found valid GPT with protective MBR; using GPT.

Disk /dev/sda: 41943040 sectors, 20.0 GiB

Logical sector size: 512 bytes

Disk identifier (GUID): 127BE6C7-E5E4-465B-85EC-62A094DA09D1

Partition table holds up to 128 entries

First usable sector is 2048, last usable sector is 41943006

Partitions will be aligned on 2048-sector boundaries

Total free space is 0 sectors (0 bytes)


Number  Start (sector)    End (sector)  Size       Code  Name

   1            2048            4095   1024.0 KiB  EF02  

   2            4096         2101247   1024.0 MiB  8200  

   3         2101248        41943006   19.0 GiB    8300 

File System

Swap Space

sh# mkswap /dev/sda2 

Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)

no label, UUID=9b1da30b-962a-451f-99b7-1fa2895a1ede

File System EXT4

sh# mkfs.ext4 /dev/sda3

mke2fs 1.43.3 (04-Sep-2016)

Discarding device blocks: done                            

Creating filesystem with 4980219 4k blocks and 1245184 inodes

Filesystem UUID: 401f2ba6-0e95-46ee-9a44-ddebd1c588cd

Superblock backups stored on blocks: 

  32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 

 4096000


Allocating group tables: done                            

Writing inode tables: done                            

Creating journal (32768 blocks): done

Writing superblocks and filesystem accounting information: done  

2. System Installation and Package Management

Installing Linux (GPT/EFI)

Partition Layout ( 1GB RAM / 10G Disk assumed )

sh# cfdisk -z /dev/sda

...

512M : /boot/efi - EFI System   # /dev/sda1

1G   : Swap                     # /dev/sda2

1G   : /boot                    # /dev/sda3

3.5G : /var                     # /dev/sda4

2G   : /home                    # /dev/sda5

2G   : /                        # /dev/sda6 


sh# mkfs.vfat /dev/sda1

sh# mkswap    /dev/sda2

sh# mkfs.ext4 /dev/sda3

sh# mkfs.ext4 /dev/sda4

sh# mkfs.ext4 /dev/sda5

sh# mkfs.ext4 /dev/sda6

Installing Linux (GPT/BIOS)

Partition Layout ( 1GB RAM  / 10G Disk assumed )

sh# cfdisk -z /dev/sda

...

1M   : BIOS boot - no mount point  # /dev/sda1

1G   : Swap                        # /dev/sda2

1G   : /boot                       # /dev/sda3

3.5G : /var                        # /dev/sda4

2G   : /home                       # /dev/sda5

2G   : /                           # /dev/sda6


Manual Partitioning

sh# mkswap    /dev/sda2

sh# mkfs.ext4 /dev/sda3

sh# mkfs.ext4 /dev/sda4

sh# mkfs.ext4 /dev/sda5

sh# mkfs.ext4 /dev/sda6


Manual Partition Formatting

sh# mount /dev/sda6 /mnt

sh# mkdir /mnt/{boot,var,home}

sh# mount /dev/sda3 /mnt/boot

sh# mount /dev/sda4 /mnt/var

sh# mount /dev/sda5 /mnt/home


Package Installation (e.g. archlinux)

sh# pacstrap /mnt base

sh# genfstab -U /mnt >> /mnt/etc/fstab


System Configuration (e.g. archlinux)

sh# arch-chroot /mnt

sh@chroot# 

sh@chroot# pacman -S grub

sh@chroot# ln -sv /usr/share/zoneinfo/Canada/Eastern /etc/localtime

sh@chroot# echo "Canada/Eastern" > /etc/timezone

sh@chroot# vi /etc/locale.gen

...

en_US.UTF-8


sh@chroot# locale-gen

sh@chroot# echo "LANG=en_US.UTF-8" > /etc/locale.conf

sh@chroot# echo "archlinux" > /etc/hostname

sh@chroot# echo "127.0.0.1    archlinux.localdomain    archlinux" >> /etc/hosts


Boot Loader Installation (grub 2 and BIOS)

sh@chroot# pacman -S grub

sh@chroot# grub-install --target=i386-pc /dev/sda

sh@chroot# grub-mkconfig -o /boot/grub/grub.cfg


sh@chroot# exit


Boot Loader Installation (grub 2 and UEFI)

sh@chroot# pacman -S grub efibootmgr

sh@chroot# grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=archlinux

sh@chroot# grup-mkconfig -o /boot/grub/grub.cfg


sh@chroot# exit


sh# umount /mnt/{boot,home,var} /mnt

Package Manager

Centos

sh# rpm -qa

sh# rpm -qi openssh

Name        : openssh

Version     : 6.6.1p1

Release     : 35.el7_3

Architecture: x86_64

Install Date: Tue 05 Sep 2017 10:54:14 AM CDT

Group       : Applications/Internet

Size        : 1450186

License     : BSD

Signature   : RSA/SHA256, Wed 12 Apr 2017 08:31:03 PM CDT, Key ID 24c6a8a7f4a80eb5

Source RPM  : openssh-6.6.1p1-35.el7_3.src.rpm

Build Date  : Wed 12 Apr 2017 09:05:51 AM CDT

Build Host  : c1bm.rdu2.centos.org

Relocations : (not relocatable)

Packager    : CentOS BuildSystem <http://bugs.centos.org>

Vendor      : CentOS

URL         : http://www.openssh.com/portable.html

Summary     : An open source implementation of SSH protocol versions 1 and 2

Description :

SSH (Secure SHell) is a program for logging into and executing

commands on a remote machine. SSH is intended to replace rlogin and

rsh, and to provide secure encrypted communications between two

untrusted hosts over an insecure network. X11 connections and

arbitrary TCP/IP ports can also be forwarded over the secure channel.


OpenSSH is OpenBSD's version of the last free version of SSH, bringing

it up to date in terms of security and features.


This package includes the core files necessary for both the OpenSSH

client and server. To make this package useful, you should also

install openssh-clients, openssh-server, or both.

3. GNU and Unix Commands

Process Management

A process is an executing program/application, i.e.  a process is created whenever a program/application gets executed. A process is running in memory. A process is assigned a unique process id. Per default the max pid is 2^15, but can be increased up to 2^20 ( /proc/sys/kernel/pid_max).

A program is a file ( usually stored on the filesystem ), which holds all needed information for the kernel to construct a process, i.e. a file gets loaded and 'processed' by the kernel 

Process Attributes

Process Identifier

A process gets assigned a unique identifier ( special integer type 'pid_t' ). The first process ( typically init ) gets assigend 1 and for each following process the pid gets increased by 1. The max value of pid is per default 2^15 = 32768, but can be increased up to the value 2^22 through the procfs:

bash# cat /proc/sys/kernel/pid_max

bash# echo $(( 2**16 )) > /proc/sys/kernel/pid_max

Maximum Process ID's

Process File System


Virtual Memory


Keywords

Process Memory


Process Memory Layout


Examples

list processes

bash# ps 

  PID TTY          TIME CMD

  514 ttyS0    00:00:00 sudo

  516 ttyS0    00:00:00 bash

  521 ttyS0    00:00:00 ps

show process tree

bash# pstree

systemd─┬─agetty

        ├─dbus-daemon

        ├─dhcpcd

        ├─httpd─┬─PassengerAgent─┬─PassengerAgent───6*[{PassengerAgent}]

        │       │                ├─PassengerAgent───5*[{PassengerAgent}]

        │       │                └─5*[{PassengerAgent}]

        │       └─3*[httpd───26*[{httpd}]]

        ├─login───bash───sudo───bash───pstree

        ├─mysqld───26*[{mysqld}]

        ├─sshd

        ├─systemd───(sd-pam)

        ├─systemd-journal

        ├─systemd-logind

        └─systemd-udevd

find processes with regular expressions

bash# pgrep ".?ash"

9835

9858

10313

10403

output more details

bash# ps -l $( pgrep bash )

F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY        TIME CMD

4 S  1000  9835   617  0  80   0 - 28879 wait   tty1       0:00 -bash

4 S     0  9858  9854  0  80   0 - 28844 n_tty_ tty1       0:00 -bash

4 S  1000 10313   618  0  80   0 - 28878 wait   ttyS0      0:00 -bash

4 S     0 10403 10399  0  80   0 - 28844 wait   ttyS0      0:04 -bash


Important Commands


Important Flags for 'ps'

list all processes ( bsd style )

bash# ps aux

list all processes ( standard syntax )

bash# ps -ef

list security info ( used by MAC, e.g. SELinux )

bash# ps -eM


Important Flags for 'pstree'

tree includes pids

bash# pstree -p

highlight current process and ancestors

bash# pstree -h

don't truncate long lines

bash# pstree -l

Job Control


Important Commands

Kernel (core operating system)

The kernel is the core software that manages the computer's resources. It's responsible for process scheduling, memory access, input/output, etc.

Customized Kernel

sh# wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.9.51.tar.xz

sh# tar xvf linux-4.9.51.tar.xz

sh# cd linux-4.9.51

sh# make mrproper

sh# make menuconfig

  #=> now you can edit the configuration

  #=> when you're finished editing, the configuration gets safed in the file '.config' in the same directory

sh# make && make modules

Init System

systemv

sh# cat /etc/inittab

...

id:3:initdefault

...


Upstart

Defining the default runlevel

sh# cat /etc/rc-sysinit.conf

...

env DEFAULT_RUNLEVEL=3

...

Sample Start/Stop configuration (openssh-server)

start on runlevel [2345]

stop  on runlevel [!2345]

respawn

respawn limit 10 5

umask 022



systemd

unit

target

Default Runlevel

Defining the default runlevel is done by creating a link default.target, which points to the desired runlevel target configuration.

sh# pwd

/usr/lib/systemd/system


sh# ls -l default.target 

lrwxrwxrwx 1 root root 16 Jul 18 03:05 default.target -> graphical.target


sh# cat graphical.target 

#  This file is part of systemd.

#

#  systemd is free software; you can redistribute it and/or modify it

#  under the terms of the GNU Lesser General Public License as published by

#  the Free Software Foundation; either version 2.1 of the License, or

#  (at your option) any later version.


[Unit]

Description=Graphical Interface

Documentation=man:systemd.special(7)

Requires=multi-user.target

Wants=display-manager.service

Conflicts=rescue.service rescue.target

After=multi-user.target rescue.service rescue.target display-manager.service

AllowIsolate=yes

Signals