NASA Lab
Partition/Filesystem/Bootloader
TinRay, Yu-Chuan
➔ Conceptual
◆ Storage Structure
◆ Bootloader
➔ Practical & \Exercise/
◆ Linux Device File
◆ Storage Related Commands
◆ Boot Related Commands
Agenda
Before we start...
Download the image we will use first.
➔ VirtualBox .ova file
➔ CentOS Installation Minimal CD (.iso)
Storage Structure
Levels of Storage
➔ Application
◆ Read/Write file on filesystem.
➔ Filesystem
◆ Handle lock, permission, journal, index…
◆ EXT 2/3/4, ZFS, FAT32, NFS...
➔ Block Device
◆ Disk (?)
◆ What really stores things
Filesystem Level
Block Device/Level Application
Network Filesystem (NFS)
➔ Client use remote filesystem.
➔ Host provides a filesystem.
➔ Single host for multiple client.
➔ Multiple host for multiple client.
◆ high performance (scalability)
◆ high availability (redundancy)
➔ Ex. NFS, CephFS, GlusterFS Filesystem Level Application
Host Client
Simulated Block Device
➔ OS views as block devices (disks).
➔ May not really be disks.
➔ ★ can be fiber, SATA cable, network
Filesystem Level
Block Device/Level Application
Software
Example: Hardware RAID (Pic)
Example: Hardware RAID
➔ Multiple disks form a RAID and constitute a storage pool (or whatever).
➔ Then on that “storage pool”, allocate space for “virtual disks (image)”.
➔ OS can only see those “virtual disks” not those physical.
Disk Disk
Virtual Disk
Question
➔ Will hardware RAID know how many partitions are there on disks?
➔ So does partition belongs to block device level?
Example
➔ SAN (https://en.wikipedia.org/wiki/Storage_area_network)
➔ iSCSI (https://en.wikipedia.org/wiki/ISCSI)
➔ Ceph (http://ceph.com/)
Bootloader
Everything is mysterious…
If I am wrong please tell me.
How does W1nd0ws boot?
➔ Not necessary.
Your PC ran into a problem and need to restart. We’re just collecting some error info, and there we will restart for you.
(99% complete)
:(
:O
Solution: Install Linux!!
:D
How does Linux Boot? (Legacy/MBR)
(Roughly, if anything incorrect please tell me.) 1. BIOS self test
2. Read and test MBR in the order of bootable devices configured in BIOS.
3. Load bootstrap in MBR.
4. Bootstrap load bootloader.
5. Then bootloader load initrd ramdisk image, as well as kernel.
6. Then kernel magically bring up whole system :D
Boot Step 2, 3, 4
Step 2. Read and test MBR in the order of bootable devices configured in BIOS.
➔ Stupid BIOS can only see block level.
➔ MBR = Master Boot Record
◆ 0 ~ 446 Bytes: Bootstrap (program) code
◆ 446 ~ 510 Bytes: Partition table
◆ 510 ~ 512 Bytes: Boot signature
Step 3, 4
➔ BIOS load bootstrap -> bootstrap load bootloader
Boot Step 5
Then bootloader load initrd ramdisk image, as well as kernel.
➔ For GRUB2, GRUB menu shows up.
➔ It can handle some basic filesystem.
➔ Pass some parameters to kernel.
➔ Then its kernel’s term.
For more, see Jserv DA DA der blog.
Who is Jserv?
Question
Why MBR can only support 4 “primary partition”?
What is load?
➔ Do you remember From NAND to Tetris ?
➔ Load ≒ Put into memory.
➔ Pass control ≒ Set proper PC.
➔ So first hard-coded BIOS loads bootstrap.
➔ Then bootstrap loads bootloader.
➔ Then bootloader loads ramdisk.
➔ Then...
EFI mode
➔ Different from MBR/Legacy mode.
➔ Only works with GPT partition table. (HW bonus)
Story of bootstrap
“It is conjectured that Mr. Murphee will now be enabled to hand
himself over the Cumberland river or a barn yard fence by the straps of his boots.”
From wikipedia
Practical
Unix device files
➔ Block devices
◆ /dev/sdX, /dev/vdX (virtualized)
➔ Partition
◆ /dev/sdX1, etc…
◆ /dev/mapper/cl-root (LVM)
◆ /dev/cl/root (same as above)
➔ Pseudo devices
◆ /dev/null
Dangerous Command
➔ dd if=[device] of=[file]
➔ ex.
dd if=/dev/zero of=/dev/sda when you want to reinstall OS :D
\Exercise/
➔ You get a PC (VM image) that can not boot.
➔ Fix it with CentOS installation CD!
➔ Download VM from Google Drive
So why not just reinstall it?
Since we took NASA!!
(And it is not Windows)
How was the exercise created?
su root
rm -rf /boot/*
dd if=/dev/zero of=/dev/sda1 bs=1 count=446 Then delete /boot partition.
Step 0.
Try to boot it.
No grub ->
How does Linux Boot? (Legacy/MBR)
1. BIOS self test
2. Read and test MBR in the order of bootable devices configured in BIOS. MBR destroyed!
3. Load bootstrap in MBR.
4. Bootstrap load bootloader. /boot vanished!
5. Then bootloader load initrd ramdisk image, as well as kernel.
/boot/initramfs, vmlinuz deleted!
6. Then kernel magically bring up whole system :(
Overview of what we will do
➔ Boot with Installation CD.
➔ Fix broken stuffs with the broken system.
◆ So will use /usr/bin/ in broken system.
Step 1.
Inject CentOS CD and boot from it.
Then select
“Troubleshooting” > “Rescue a CentOS Linux system”
Then choose “3) Skip to shell”
Inject CD in
VirtualBox
Step 2:
Gather some information about the PC (VM).
Commands to Get Information
➔ lsblk -f
◆ See block devices as well as partitions even not mounted.
◆ -f to see partition UUID.
➔ df -h
◆ See usage of mounted partitions.
◆ -h to show in human friendly way.
Step 3:
In step2, find that /boot disappeared.
So create a partition for /boot.
How does Linux Boot? (Legacy/MBR)
1. BIOS self test
2. Read and test MBR in the order of bootable devices configured in BIOS. MBR destroyed!
3. Load bootstrap in MBR.
4. Bootstrap load bootloader. /boot vanished!
5. Then bootloader load initrd ramdisk image, as well as kernel.
/boot/initramfs, vmlinuz deleted!
6. Then kernel magically bring up whole system :(
parted
Used to partition disk
➔ help tell you everything
➔ mktable/mklabel: GPT/msdos
➔ mkpart
Note it will apply changes as soon as you press enter!!
mkfs
mkfs -t [fs_type] [device]
or
mkfs.ext4 mkfs.xfs
➔ dosfstools required for fat family.
Step 3: Solution
parted print
mkpart \n primary \n ext4 \n 2048s \n 1024MB quit
mkfs.ext4 /dev/sda1
Step 4.
mount and chroot into the broken system.
➔ mount the broken system on /mnt
➔ Then go into the broken system.
(command chroot)
mount
➔ mount /dev/sda1 /mnt
◆ then files in /dev/sda1 are under /mnt.
◆ -o to specify options
ex. -o ro to mount read only
➔ Mount special devices
You need to mount...
➔ root partition to /mnt
➔ boot partition to /mnt/boot
➔ And some special partitions as follow
mount -t proc /mnt/proc
mount -t sysfs sys /mnt/sys mount -B dev /mnt/dev
Step 4: Solution
mount /dev/mapper/cl-root /mnt mount /dev/sda1 /mnt/boot
mount -t proc proc /mnt/proc mount -t sysfs sys /mnt/sys mount -B dev /mnt/dev
mount -B /run/ /mnt/run/
chroot /mnt
Step 5.
Edit /etc/fstab for new /boot
/etc/fstab
Looks like
UUID=953-sdsd /boot xfs defaults 0 0
/dev/sda2 / xfs rw 0 0
/dev/sda3 /home xfs rw 0 0
➔ mount automatically when booting
➔ mount with less arguments.
➔ UUID is more robust!
From something like
UUID=9016f079-6b2f-4d59-9420-07e97e997767 /boot xfs defaults 0 0
to
/dev/sda1 /boot ext4 defaults 0 0
Step 5: Solution
How does Linux Boot? (Legacy/MBR)
1. BIOS self test
2. Read and test MBR in the order of bootable devices configured in BIOS. MBR destroyed!
3. Load bootstrap in MBR.
4. Bootstrap load bootloader. /boot vanished!
5. Then bootloader load initrd ramdisk image, as well as kernel.
/boot/initramfs, vmlinuz deleted!
6. Then kernel magically bring up whole system :(
Step 6.
Get back removed /boot/vmlinuz
Originally there are at least two files that looks like vmlinuz-linux
initramfs-linux.img
Making Initrd ramdisk
➔ Arch: mkinitcpio
➔ CentOS: mkinitrd
◆ mkinitrd and it will tell what to type.
Where can I get vmlinuz?
From installation CD!!
mount /dev/sr0 /mnt
rpm -i --replacepkgs \
/mnt/Packages/kernel-3.10.0-514.el7.x86_64.rpm
Step 7.
Install GRUB
How does Linux Boot? (Legacy/MBR)
1. BIOS self test
2. Read and test MBR in the order of bootable devices configured in BIOS. MBR destroyed!
3. Load bootstrap in MBR.
4. Bootstrap load bootloader. /boot vanished!
5. Then bootloader load initrd ramdisk image, as well as kernel.
/boot/initramfs, vmlinuz deleted!
6. Then kernel magically bring up whole system :(
GRUB-Related commands
Arch CentOS
➔ grub-install, grub2-install
◆ Install MBR on device
◆ grub2-install --target i386_pc [device]
➔ grub-mkconfig, grub2-mkconfig
◆ Detect existence of OSs and generate configuration file (/boot/grub/grub.cfg /boot/grub2/grub.cfg).
◆ grub2-mkconfig > /boot/grub2/grub.cfg
◆ Best practice:
Step 7: Solution
grub2-install /dev/sda
grub2-mkconfig > /boot/grub2/grub.cfg
Finish!!
Remove the installation CD and reboot.
Boot from local drive option on CD menu will not work.
➔ Order of those steps is important!!
a. boot
b. gather information c. create /boot partition
d. mount and chroot the broken system require /boot created
e. edit fstab
f. get back vmlinuz
Wrap up
How does Linux Boot? (Legacy/MBR)
1. BIOS self test
2. Read and test MBR in the order of bootable devices configured in BIOS. MBR destroyed!
3. Load bootstrap in MBR.
4. Bootstrap load bootloader. /boot vanished!
5. Then bootloader load initrd ramdisk image, as well as kernel.
/boot/initramfs, vmlinuz deleted!
6. Then kernel magically bring up whole system :(
Misc: ext family commands
➔ e2label
◆ Change/See partition label.
➔ tune2fs
◆ Adjust tunable filesystem parameters on ext2/ext3/ext4 filesystems. (from its man page)
License
This slide is under Creative Commons
Attribution-NonCommercial-ShareAlike 4.0 International License.