9/10/12

Topics CT 320: Network and System 2 Administration 1. Booting 2. Bootstrap loaders 3. Run levels CHAPTER 3: BOOTING AND SHUTTING DOWN 4. Startup scripts

COLORADO STATE UNIVERSITY 5. Shutdown and reboot COMPUTER SCIENCE DEPARTMENT CHRIS WILCOX FALL 2012

Original slides from Dr. James Walden at Northern Kentucky University.

CT 320, Fall Semester 2012 9/11/12

Booting BIOS 3 4 1. ROM boot code (BIOS in PCs)‏ 1. Load CMOS settings. 2. Master Boot Record (MBR)‏ 2. Initialize registers and power management. 3. POST (Power On Self Test.)‏ 3. Bootloader 4. Display system settings (if key pressed.)‏ 4. Kernel 5. Activate other BIOSes 5. Hardware detection and configuration ¡ Disk interfaces 6. Creation of system processes ¡ Graphics cards ¡ Network interfaces 7. Multiuser operation 6. Find bootable device. 7. Load MBR. 8. Run MBR program.

CT 320, Fall Semester 2012 9/11/12 CT 320, Fall Semester 2012 9/11/12

1 9/10/12

MBR Bootloader: GRUB 5 6 — Up to four primary Stage 1 (stored in MBR or boot sector)‏ partitions in MBR: ¡ Initialize the system briefly. ¡ Has CHS (Cyl/Head/Sector) ¡ Detect the geometry and access mode of the loading drive. addresses or partition ¡ Load the first sector of Stage 2. ¡ LBA (Logical Block Addressing) for newer disk drives ¡ Jump to the starting address of the Stage 2. — One can be used as an Stage 1.5 (stored in sectors of MBR track)‏ extended partition ¡ One version for each filesystem. ¡ Links to an Extended Boot ¡ Loads stage 2 from filesystem. Record (EBR) on first sector Stage 2 (stored in filesystem)‏ ¡ Each logical partition is described by its own EBR ¡ Displays menu of OS choices. ¡ Includes link to the next EBR, ¡ Often displays boot splash screen. to allow any # of partitions ¡ Loads selected OS kernel.

CT 320, Fall Semester 2012 9/11/12 CT 320, Fall Semester 2012 9/11/12

GRUB Installation GRUB Boot 7 8 At OS Install Time — Automatically boots default OS after timeout. Most distributions install GRUB. — Key sequences bring up Linux Install ¡ Menu of boot options (described in menu.lst)‏ grub-install /dev/hda ¡ Edit kernel options (runlevel, other features)‏ Native install ¡ GRUB command shell Boot with CD and run grub. — A boot prompt password may be required before #>> root (hd0,0)‏ edit privileges or shell access granted. #>> setup (hd0)‏ ¡ Can bypass OS security by specifying kernel options or #>> quit accessing files from GRUB shell.

CT 320, Fall Semester 2012 9/11/12 CT 320, Fall Semester 2012 9/11/12

2 9/10/12

Booting with GRUB Config: /boot/grub/menu.lst 9 10 Direct Boot # timeout (sec) before booting default timeout 10 Specify OS kernel image to load. Chainloading # entry title Ubuntu, kernel 2.6.10-5-386 Specify another bootloader to load. root (hd1,0)‏ Chainload NTLDR to boot MS Windows. kernel /boot/vmlinuz-2.6.10-5-386 root=/dev/hde1 ro quiet splash initrd /boot/initrd.img-2.6.10-5-386 boot

# Windows entry title Windows 2000 root hd0,0)‏ makeactive chainloader +1

CT 320, Fall Semester 2012 9/11/12 CT 320, Fall Semester 2012 9/11/12

GRUB Naming Convention Kernel 11 12 GRUB uses BIOS hard disk numbers 1. GRUB loads kernel file into memory OS may not number disks identically to BIOS. ¡ /boot/vmlinuz-VERSION Examples 2. Kernel uncompresses self and begins running. 3. Sets up interrupts and virtual memory. (hd0,0) First partition of first hard disk 4. Loads initial RAMdisk into memory (hd1,2) Third partition of second hard disk. ¡ /boot/initrd.img-VERSION 5. Kernel uncompresses initrd and mounts as /sysroot. 6. Kernel loads drivers and modules from /sysroot. ¡ SCSI and RAID drivers, ext3 filesystem module, etc. 7. Switches from real mode to protected mode. 8. Creates kernel threads (ksoftirq, khelper, kblockd)‏ 9. Starts or process.

CT 320, Fall Semester 2012 9/11/12 CT 320, Fall Semester 2012 9/11/12

3 9/10/12

Kernel Arguments Run Levels 13 14 Numeric arguments 0 – Shutdown ¡ Specify runlevel. 1 – Single user ¡ single also specifies single user mode 2 – Multi-user, no networking Root device options 3 – Multi-user, with networking – specifies which root device to use root= 4 – Unused – ro, rw specify access type 5 – Multi-user, networking, and GUI Console options 6 – Reboot – console=ttyS1,9600 will use serial console Hardware options ¡ Enable, disable specific hardware devices/features.

CT 320, Fall Semester 2012 9/11/12 CT 320, Fall Semester 2012 9/11/12

/etc/inittab rc 15 16 # The default runlevel. — Executed by init with runlevel as argument. id:2:initdefault: — # Boot-time system config/initialization script. Starts and stops services for each runlevel. si::sysinit:/etc/init.d/rcS — /etc/rc#.d scripts # What to do in single-user mode. ~~:S:wait:/sbin/sulogin ¡ Symlinks to actual scripts in /etc/init.d

# /etc/init.d executes the start and kill scripts ¡ Kills services beginning with K* l0:0:wait:/etc/init.d/rc 0 ¡ Starts services beginning with S* l1:1:wait:/etc/init.d/rc 1 l2:2:wait:/etc/init.d/rc 2 ¡ Executes in ASCII order. l3:3:wait:/etc/init.d/rc 3 l4:4:wait:/etc/init.d/rc 4 l5:5:wait:/etc/init.d/rc 5 l6:6:wait:/etc/init.d/rc 6

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

CT 320, Fall Semester 2012 9/11/12 CT 320, Fall Semester 2012 9/11/12

4 9/10/12

Scripts in runlevel 2 Example init.d script 17 18 #! /bin/sh $ ls /etc/rc2.d case "$1" in S05vbesave S13gdm S20openntpd S89cron 'start') S08iptables S18hplip S20postfix S91apache2 if [ -f /usr/local/sbin/sshd ]; then S10acpid S18portmap S20powernowd S98usplash echo "starting SSHD daemon" S10powernowd.early S19cupsys S20rsync S99acpi-support /usr/local/sbin/sshd & fi S10sysklogd S20apmd S20ssh S99rc.local ;; S10syslog-ng S20argus-server S20xinetd S99rmnologin 'stop')‏ S11klogd S20hotkey-setup S21nfs-common S99stop-readahead PID=`/usr/bin/ps -e -u 0 | /usr/bin/fgrep sshd | S12dbus S20makedev S89anacron /usr/bin/awk '{print $1}'` if [ ! -z "$PID" ] ; then /usr/bin/kill ${PID} >/dev/null 2>&1 fi ;; *) echo "usage: /etc/init.d/sshd {start|stop}“ ;; esac CT 320, Fall Semester 2012 9/11/12 CT 320, Fall Semester 2012 9/11/12

Turning Services On/Off Red Hat Service Commands 19 20 Add a new service Starting or stopping a service Install startup script in /etc/init.d service NAME [start,restart,stop] Create S symlinks in appropriate runlevels is equivalent to Create K symlinks in appropriate runlevels /etc/init.d/NAME [start,restart,stop] Enabling or disabling a service at boot time Prevent a service from starting on boot chkconfig NAME on –level Remove S links from /etc/rc?.d/* is equivalent to Remove K links from /etc/rc?.d/* ln –s /etc/init.d/NAME /etc/init.d/rc.d/ rc#.d/S??name

CT 320, Fall Semester 2012 9/11/12 CT 320, Fall Semester 2012 9/11/12

5 9/10/12

Single User Mode What if single user mode doesn’t work? 21 22 — Kernel boots (arg 1 or “single”)‏ — Bypass init by booting into shell — System asks for root password ¡ Pass init=/bin/bash argument to kernel. — Root shell starts ¡ Probably want to specify rw option as well. ¡ No system processes ¡ No need for root password. ¡ No networking ¡ No housekeeping performed. — Only root filesystem mounted (read-only). — Boot from CD ¡ Remount for RW: mount –o remount,rw / ¡ RedHat CD with linux rescue — Why? ¡ Fix without user interference ¡ For example: disks, backups, security

CT 320, Fall Semester 2012 9/11/12 CT 320, Fall Semester 2012 9/11/12

Shutdown What's wrong with init? 23 24 — Performance — shutdown, init 0, halt ¡ Starts services sequentially. ¡ Offers time-delay option ¡ Shell scripts spawn lots of processes. ¡ Runs server/daemon stop scripts — Manual configuration ¡ Kill remaining processes ¡ Order to start services determined manually. ¡ Flushes writes to disk (sync) ¡ Network before authorization before NFS. — reboot, init 6 — Reliability ¡ Init does NOT monitor or restart services after boot. ¡ restarts system — Hotplug hardware — Ctrl-Alt-Del ¡ Drives mounted at boot. ¡ as defined in /etc/inittab ¡ What about USB or network drives? ¡ ca:ctrlaltdel:/sbin/shutdown -t1 -a -r now

CT 320, Fall Semester 2012 9/11/12 CT 320, Fall Semester 2012 9/11/12

6 9/10/12

Init Alternatives Systemd 25 26 — systemd — Systemd is the system and service manager for ¡ Full replacement for init, used by Fedora Core Fedora releases ¡ Parallel starting of services, reduce shell overhead — More information can be found on the Fedora — website at http:/fedoraproject.org/wiki/Systemd ¡ Mac replacement for init, rc, cron, inetd — Uses units to encapsulate services, sockets, ¡ Monitors services, starts on demand devices, mounts, automounts, and targets — initng — Has advanced capabilities that make it better than ¡ Service configuration specifies dependencies previous initd mechanism: ¡ Starts services in parallel, once dependencies met ¡ Aggressive parallelization when launching processes ¡ Bus activation starts services only when accessed ¡ Supports save and rollback of service states ¡ Automatically maintains mount and automount points

CT 320, Fall Semester 2012 9/11/12 CT 320, Fall Semester 2012 9/11/12

Systemd (cont’d) 27 28 — systemadm is a graphical front-end for systemd that allows inspection and control of services — Event-based init replacement. — systemctl is a command front-end for systemd, — Supports wider range of events than launchd. identical in functionality to systemadm ¡ Hardware device has been added. — Became default in Fedora 15, replacing Upstart, init ¡ Filesystem has been mounted. not used since at least Fedora 9 ¡ Time-based (can replace cron as well as init.) — Still supports the chkconfig command described previously in these slides ¡ File has been modified. — Files reside in /etc/systemd/system, for example ¡ Another job has begun or finished running. multi-user.target.wants for run level 5. — Backwards compatible with /etc/init.d scripts. — Links set of service files, for example sshd.service: — Respawns services that are down. ¡ ExecStart=/usr/bin/sshd –D $OPTIONS ¡ ExecReload=/bin/kill –HUP $MAINPID — Used in Ubuntu since version 6.10. ¡ WantedBy=multi-user.target

CT 320, Fall Semester 2012 9/11/12 CT 320, Fall Semester 2012 9/11/12

7 9/10/12

References 29 1. M. Tim Jones, “Inside the Linux boot process,” http://www.ibm.com/developerworks/library/l-linuxboot/ index.html, 2006. 2. M. Tim Jones, “Parallelize applications for faster Linux booting,” http://www.ibm.com/developerworks/linux/library/l-boot- faster/index.html, 2007. 3. Scott James Remnant, “Upstart in Universe,” http://www.netsplit.com/2006/08/26/upstart-in-universe/, 2006. 4. Starman, “Boot records revealed: MBR details,” http://mirror.href.com/thestarman/asm/mbr/index.html 5. “Extended boot record,” http://en.wikipedia.org/wiki/Extended_Boot_Record. 6. GRUB manual http://www.gnu.org/software/grub/manual/grub.html 7. “How it works: Master Boot Record (MBR),” http://www.ata-atapi.com/hiwmbr.htm

CT 320, Fall Semester 2012 9/11/12

8