% Unix/Linux Boot Process, GRUB, and run levels % Ian! D. Allen - idallen@idallen.ca - www.idallen.com % Winter 2012 - January to April 2012 Topics and Readings (Fedora Textbook) ===================================== * Chapter 11 (pages 417 – 424, 430-1, 435) * Chapter 15 (pages 543, 551) * Boot Process * Boot Loaders: Specifically Grub - see also the LInux LOader: `lilo` * Run Levels: display, change, etc. * Startup Files * Wikipedia: [Booting](http://wikipedia.org/wiki/Booting) * [GRUB Reference](http://www.gnu.org/software/grub/) - We are using [GRUB Legacy](http://www.gnu.org/software/grub/grub-legacy.html) - [GRUB Legacy Tutorial](http://www.dedoimedo.com/computers/grub.html) - [GRUB Legacy Manual](http://www.gnu.org/software/grub/manual/legacy/grub.html) * Wikipedia Upstart: * Wikipedia Systemd: The Boot Process ================ Definitions: boot and boot loader --------------------------------- * To **boot** or **bootstrap**: load an operating system kernel into memory and start it running * A **boot loader**: a small program used in the bootstrap process - Usually resides on the starting sector(s) of a hard disk called the Boot Sector or the Master Boot Record [MBR] - Responsible for locating a secondary boot loader or the actual O/S kernel What happens at power on ------------------------ * The firmware BIOS program in the hardware executes first - BIOS is stored in some form of persistent firmware that keeps its information even when the power is off * The BIOS usually performs a Power On System Test [POST] - checks memory, itemizes disks, etc. * After the POST, the BIOS searches for "bootable media" - some disk partitions can be marked "bootable" - bootable media store boot information in the MBR * If a valid MBR is found, the BIOS uses or executes it to find an O/S - MBR may contain actual code that passes control to a secondary boot record stored inside a partition - partition boot record may then locate the system kernel and load it The Linux /boot directory ========================= * Linux usually keeps its kernels under directory **`/boot`**, which may be a separately mounted partition at the start of the disk * Usually **`/boot`** is located near the beginning of the disk because older BIOSes can't address disk blocks at the middle or end of larger disks! * Once Linux is running, Linux has no problem addressing the whole disk. - The /boot problem is a BIOS limitation, not a Linux limitation. - Modern BIOSes can find and boot an O/S anywhere on the disk. The Grand Unified Boot Loader - GRUB ==================================== * Replacement for the older LInux LOader (LILO) boot loader. - More flexible; more options; easier to configure and update - We are using the [GRUB Legacy Version 0.9x](http://www.gnu.org/software/grub/grub-legacy.html) in this course - More modern Linux systems (since about 2007) are using the more complex [GRUB 2 (version 1.xx)](http://www.gnu.org/software/grub/) * Allows loading of any free operating system directly - Recognizes many types of file systems and kernel executable formats * Allows chain loading (loading another boot loader) of proprietary operating systems (e.g. Windows) * Automatic boot can be configured using a text file named (depending on the version) **`menu.lst`**, **`grub.conf`**, or **`grub.cfg`** under **`/boot/grub/`** - Modern versions of GRUB create this file from pieces stored under **`/etc/grub.d/`** and you should edit the pieces, not the main file - Some systems create a symlink from **`/etc/grub.conf`** to the real location under **`/boot/grub/`** - On other systems (e.g. SuSE), **`/etc/grub.conf`** has a completely different function - to configure GRUB installation! * GRUB configuration files are installed and then maintained by system updates - Fedora's [Anaconda](https://fedoraproject.org/wiki/Anaconda) installer sets up and installs GRUB for you; other distributions do the same - You don't have much to do unless you need to add a special boot entry * GRUB can also be run in interactive command mode, where you type GRUB commands directly into GRUB to find and load operating systems A sample GRUB Legacy configuration file --------------------------------------- GRUB Legacy uses a configuration file under **`/boot/grub`** to display a menu of alternative operating systems to boot. Without a configuration file, GRUB starts in interactive mode and you have to specify and type everything yourself. With a configuration file, you can select from a menu list of choices. Each choice in the configuration file starts with a "title" keyword followed by a descriptive name: # Sample GRUB menu.lst or grub.conf file (GRUB Legacy version 0.9x) timeout=30 splashimage=(hd0,0)/grub/splash.xpm.gz hiddenmenu default=0 # this sdb disk has partition sdb1 for /boot and sdb2 for / (ROOT) title Fedora 2.6.31 on second disk root (hd1,0) kernel /vmlinuz-2.6.31.5-127.fc12.i686.PAE ro root=/dev/sdb2 initrd /initramfs-2.6.31.5-127.fc12.i686.PAE.img # this sdc disk has partition sdc1 for / (ROOT) and uses a /boot directory title Old Fedora 2.6.23 on third disk password sesame root (hd2,0) kernel /boot/vmlinuz-2.6.23.15.137.fc8 ro root=/dev/sdc1 initrd /boot/initrd-2.6.23.15.137.fc8.img # this first disk partition has a Windows boot loader in the first sector title Windows on first disk rootnoverify (hd0,0) makeactive chainloader +1 Notes: * The part of the file before the first "title" section is for general GRUB configuration options - see below for the keywords - **`timeout`**: how long the menu waits before selecting the default - **`default`**: which "title" section to boot when the time-out happens - **`splashimage`**: a pretty picture - **`hiddenmenu`**: hide the GRUB menu (and show the picture instead) * The "title" sections for Linux kernels have these keywords: - **`root`**: set this prefix for all GRUB pathnames, similar to "cd" in the shell - this is NOT the Linux ROOT; this is the GRUB ROOT - this is prepended to all incomplete pathname references, e.g. incomplete /foo becomes complete path (hd1,0)/foo - **`kernel`**: the Linux kernel image followed by optional kernel options - **`initrd`**: the initial RAM disk to be used by the kernel at boot time - **`password`**: require the given password to boot this menu entry * Windows may not be able to boot if it isn't the first partition of the first disk! See the GRUB documentation for ways to work around this. * **Do not move or remove the GRUB configuration files!** GRUB expects the files to be in a particular location on disk, and moving them may cause GRUB to fail to find them, requiring a re-install of GRUB. - you can edit the files **in-place** as long as you do not delete or move them ### grub.conf keywords ### * Global options: - **`default`** n [opt.]: The nth "title" boot menu item, starting at 0, will be booted. Otherwise, the first (zeroth) "title" item is booted. - **`timeout`** 20 [opt.]: Number of seconds before booting into default. - **`password`** sesame [opt.]: Disable interactive editing and protect all boot menus that include the **`lock`** option * Individual menu options (used with "title"): - **`title`**: Menu title of this boot menu - **`kernel`**: Location of Linux kernel file, followed by options - **`lock`** [opt.]: Requires global password to boot this menu entry - **`password`** [opt.]: require the given password to boot this menu entry Note: To find out about additional GRUB commands, type **`help`** at the GRUB shell prompt. ### GRUB Device and file Naming Convention - (hd0,0)/grub/menu.lst ### A full GRUB path name specification is a device name (often a partition name) followed by a file system pathname inside that partition (relative to the start of the partition), e.g. **`(hd0,0)/grub/menu.lst`** where **`/grub/menu.lst`** is relative to the *start of the partition* **`(hd0,0)`**. * Device names are identified by enclosing parenthesis () * Hard disks are named hd, e.g (hd0), (hd1) * Floppy disks are named fd, e.g. (fd0), (fd1) * Disks and Floppies are numbered sequentially, starting at 0 (ZERO!) * Partition numbers are given after commas, e.g. (hd0,0), (hd0,1) * Partitions are also numbered sequentially, starting at 0 (ZERO!) - WARNING! Linux numbers partitions starting at 1 (ONE!) - Linux sda1 is GRUB (hd0,0); Linux sdc2 is GRUB (hd2,1) * Examples: - **`(hd0)`** – First recognized hard disk (any type: SCSI, ATA, USB, etc.); usually corresponds to **`/dev/sda`** - **`(fd0)`** – First recognized floppy diskette; usually corresponds to **`/dev/fd0`** - **`(hd0,0)`** – First partition on first recognized hard disk; usually corresponds to **`/dev/sda1`** - **`(hd1,4)`** – First *logical* drive on second recognized hard disk; usually corresponds to **`/dev/sdb5`** * The GRUB "root" command can pre-set the device name for all following pathnames - e.g. "root (hd0,2)" followed by "kernel /linux" is the same as "kernel (hd0,2)/linux" ### Effect of separate /boot partition on GRUB pathnames ### GRUB configuration files are stored under directory **`/boot/grub`** in Linux. **`/boot/grub`** is the Linux pathname, not necessarily the GRUB pathname, because GRUB pathnames are composed of a partition name followed by a pathname inside the partition, e.g. **`(hd0,0)/some/path/name`** where **`/some/path/name`** is relative to the *start of the partition*. Since partitions are mounted on directories in Linux, and those directory pathnames prefix the names inside the partition for Linux pathnames, a GRUB pathname and a Linux absolute pathname will always differ, except for the one partition that is mounted on ROOT (because prefixing an absolute pathname with ROOT doesn't change it). If **`/boot`** is not its own partition, then it is a directory inside the ROOT partition. GRUB shell command pathnames referring to the kernel and to GRUB configuration files will also be inside the ROOT partition and will therefore all start with the ROOT partition name followed by the subdirectory name **`/boot`** inside the partition, e.g. **`(hd0,0)/boot/grub/grub.conf`** refers to the Linux pathname **`/boot/grub/grub.conf`** (because **`/boot`** is a directory inside the ROOT partition **`(hd0,0)`**). If **`/boot`** is its own mounted partition, then the GRUB files will be on this separate BOOT partition (not on the ROOT partition) that gets mounted on **`/boot`** and the GRUB pathnames (which are relative to partitions, not to file systems) will therefore *not* start with **`/boot`**, they will start directly under the BOOT partition device name e.g. **`(hd0,0)/grub/grub.conf`** refers to the Linux pathname **`/boot/grub/grub.conf`** (because **`(hd0,0)`** is mounted on **`/boot`** and so **`/boot`** is prefixed to the part of the pathname contained inside the partition). * Separate /boot partition: **`(hd0,0)/grub/grub.conf`** * /boot is under ROOT: **`(hd0,0)/boot/grub/grub.conf`** Fedora 12 uses a separate BOOT partition. Do not use "/boot" at the start of GRUB pathnames because the pathnames are inside their own BOOT partition (that will be mounted on /boot) and GRUB addresses pathnames by partition name. Installing GRUB --------------- Your Linux installation already installed GRUB for you. If you have to re-install GRUB, you may find (or be able to install) the "grub-install" script that will do it for you. You can also try it manually, if the GRUB setup files are already stored under /boot/grub: * As root, type **`grub`** at the command line: this will load GRUB and display the GRUB shell prompt "grub>" * Type **`setup`** with two GRUB pathname arguments: the boot sector installation device and the device containing the GRUB setup files - the GRUB setup files (e.g. grub.conf) must be on the given partition * To put GRUB into the MBR of the first disk: \ ` setup (hd0) (hd0,0) ` * Exit GRUB with the **`quit`** command when done Most versions of the GRUB shell have TAB command completion, where you can type part of a device or pathname and GRUB will give you all the possible completions. The command-line GRUB in Fedora 12 is broken, and does not have this feature. The GRUB that runs at boot time is not broken and does have the TAB completion feature. BE CAREFUL. GRUB does not ask you for confirmation! If you type the wrong thing, you will overwrite your boot sector with garbage and be unable to boot your system. You will need to boot a "live" CD and repair. Booting without a GRUB menu - interactive GRUB commands ------------------------------------------------------- If **`grub.conf`** is not present or has been moved, GRUB cannot display a menu. In this case it will display the GRUB interactive shell, similar to what you get by typing **`grub`** at a Linux command line. The interactive GRUB shell has its own prompt ("grub>") and its own built-in commands, some with the same names as BASH shell commands. Be clear on when you are typing into the GRUB shell and when you are typing into BASH. The same command name may do different things, depending on whether GRUB executes it or BASH executes it. Most versions of the GRUB shell have TAB command completion, where you can type part of a device or pathname and GRUB will give you all the possible completions. The command-line GRUB in Fedora 12 is broken, and does not have this feature enabled. The GRUB that runs stand-alone at boot time is not broken and does have the TAB completion feature. Type "help" for a partial list of commands you can use. Some examples: * cat - display a text file * geometry - display information about disk partitions * find - find which device (partition) contains a pathname * md5crypt - prompt for and encrypt a password At the GRUB shell prompt, GRUB commands can be entered to query the system, see text files, and load and then boot a kernel image. Example: * ` grub> find /grub/grub.conf ` * ` grub> cat (hd0,0)/grub/grub.conf ` * ` grub> kernel (hd0,0)/vmlinuz ro root=/dev/sda2 ` * ` grub> initrd (hd0,0)/initrd ` * ` grub> boot ` Note: The GRUB shell can also be accessed by following directions (usually by pressing **`c`**) when the GRUB menu is presented. **`[Esc]`** brings you from the GRUB shell back to the menu. Pressing **`e`** allows you to edit boot menu entries of grub.conf. Pressing **`b`** boots the edited menu item. Some versions of GRUB allow typing **`a`** to go directly to a GRUB "kernel" line and edit the options. Kernel options: single user mode, run levels, confirmation, kernel ------------------------------------------------------------------ Kernel options, given after the kernel image name on a "kernel" line, allow you to boot your system with different features enabled. Some versions of GRUB allow you to type **`a`** while viewing the GRUB menu, to go directly to a GRUB "kernel" line and edit the kernel options. When you edit these lines, you are only changing the in-memory copy of the configuration, you are *not* changing the actual configuration on disk. To make any edit permanent, you must boot the system (single-user is fine) and actually edit the configuration file and save it. ### Booting Single User Mode ### Single-user mode is a "half-up" state used for system repair and maintenance (especially for resetting the ROOT password!). The system only brings up a minimal number of services. The GUI is not started. Networking may not be enabled. Not all disks may be mounted. The console terminal gets a shell running as the ROOT user. To boot into single-user mode, add the word "single" as an option to a "kernel" line in GRUB and then boot that entry, e.g.: * ` kernel (hd0,0)/vmlinuz ro root=/dev/sda3 single ` ### Booting into a different Run Level ### To start Linux with a different run level than the default, add the run level digit to the kernel options, e.g. to boot Run Level 3 (multi-user without the X11 GUI window system): * ` kernel (hd0,0)/vmlinuz ro root=/dev/sda3 3 ` ### Confirming the startup of individual services ### To be prompted at boot time before each system service starts up: * ` kernel (hd0,0)/vmlinuz ro root=/dev/sda3 confirm ` ### Fixing a kernel panic (missing ROOT file system) ### If you moved your ROOT file system to a different partition but forgot to change the GRUB configuration file, your kernel will "panic" and tell you it can't find the root. You can reboot into the GRUB shell and edit the **`root=`** option on the "kernel" line to point to the correct ROOT partition: * ` kernel (hd0,0)/vmlinuz ro root=/dev/sdb2 ` Legacy Run Levels ================= The old Run Levels system was a crude way to specify groups of services that were to be started and stopped together. Run Levels expect that a system boots into a fixed state, with a fixed set of disks and fixed set of services. They do not work well with systems where devices come and go after booting, e.g. USB drives, printers, cameras, hotplug disks, etc. * Red Hat/Fedora Linux uses seven Run Levels, numbered 0 through 6: - 0 - halt (Do NOT set initdefault to this) - 1 - Single user mode (for maintenance) - 2 - Multiuser mode, without NFS (Networking File System) (The same as 3, if you do not have networking) - 3 - Full multiuser mode (console only - no X11 or GUI) - 4 - unused - 5 - X11 (full multiuser mode with the GUI window system) - 6 - reboot (Do NOT set initdefault to this) * Each Run Level had a set of services (daemons and processes) that were started for that level * Higher numbered Run Levels generally meant more services started, but the highest Run Level ("6") was used to reboot the system * The default Run Level to use at boot time is set by the **`initdefault`** option of the **`/etc/inittab`** file * The system can be in only one Run Level but can be moved to any other Run Level; changing Run Levels cause services to be started and stopped to match what is supposed to be available in that Run Level Run Level Commands ------------------ * To display previous and current system Run Levels (separated by a space) use the **`runlevel`** command: - "N" will be printed as the previous Run Level after booting * To change Run Levels use the **`telinit`** command: - ` telinit 1 ` \ # Switch to single-user mode (but use "shutdown now" instead) - ` telinit 3 ` \ # Switch to multi-user console mode (no GUI) - ` telinit 5 ` \ # Switch to full multi-user mode with X11 GUI - ` telinit 6 ` \ # Reboot (but use "shutdown -r now" instead) Run Level Services and chkconfig -------------------------------- You can see which services are started and stopped in each Run Level using the **`chkconfig`** command. If that command is not available, look at the file symlinks under the Run Level directories **`/etc/rc?.d`**. Each symlink starts with either an "S" (start this service) or a "K" (kill this service) followed by a two-digit sequence number followed by the name of the service, e.g. * ` /etc/rc3.d/K15httpd ` \ # kill the HTTP daemon * ` /etc/rc3.d/S55sshd ` \ # start the SSH daemon Each of those symlinks points to a script file responsible for killing or starting the indicated service. You can change what services get killed or started using the **`chkconfig`** command that adds or removes symlinks from the Run Level directories (or you can add/remove symlinks manually). Upstart and Systemd =================== Run Levels expect that a system boots into a fixed state, with a fixed set of disks and fixed set of services. They do not work well with systems where devices come and go after booting, e.g. USB drives, printers, cameras, hotplug disks, etc. Modern Unix/Linux systems moved away from static Run Levels to use the more dynamic and complex **`upstart`** system of starting/stopping services. Run Levels were "emulated" by **`upstart`**, but **`upstart`** could handle devices coming and going dynamically much better. When devices were added/removed, "events" were generated that could start and stop related system services. The complexity and limitations of **`upstart`** prompted Lennart Poettering to write a new Linux-only **`systemd`** session manager. Fedora 15 then moved from **`upstart`** to use the new **`systemd`**. Debian is reluctant to use a Linux-only start-up system, and so Debian and Ubuntu still use **`upstart`**. There is still controversy as to which is the better system. See the Wikipedia references for details and read up on [systemd](http://0pointer.de/blog/projects/systemd.html). -- | Ian! D. Allen - idallen@idallen.ca - Ottawa, Ontario, Canada | Home Page: http://idallen.com/ Contact Improv: http://contactimprov.ca/ | College professor (Free/Libre GNU+Linux) at: http://teaching.idallen.com/ | Defend digital freedom: http://eff.org/ and have fun: http://fools.ca/ [Plain Text] - plain text version of this page in [Pandoc Markdown](http://johnmacfarlane.net/pandoc/) format