挂载、分区、grub
通过给一块新磁盘安装grub回顾磁盘挂载、分区文件系统创建等操作:
该实验基于(CtonOS6.8;kernel:2.6.32-642.15.1.el6.x86_64)
1.通过VMware Workstationg添加一块磁盘(SCSI);
2./sys下SCSI扫描,查看主机总线号,磁盘肯定是有总线连接着:
1 [root@cl Test]# ls /sys/class/scsi_host/2 host0 host1 host23 [root@cl Test]# echo "- - -" > /sys/class/scsi_host/host0/scan4 [root@cl Test]# echo "- - -" > /sys/class/scsi_host/host1/scan5 [root@cl Test]# echo "- - -" > /sys/class/scsi_host/host2/scan6 [root@cl Test]# fdisk -l
3.创建分区:
1 [root@cl Test]# fdisk /dev/sdb 2 Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel 3 Building a new DOS disklabel with disk identifier 0xa61749e4. 4 Changes will remain in memory only, until you decide to write them. 5 After that, of course, the previous content won't be recoverable. 6 7 Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) 8 9 WARNING: DOS-compatible mode is deprecated. It's strongly recommended to10 switch off the mode (command 'c') and change display units to11 sectors (command 'u').12 13 Command (m for help): n14 Command action15 e extended16 p primary partition (1-4)17 p18 Partition number (1-4): 119 First cylinder (1-2610, default 1): 20 Using default value 121 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +200M22 23 Command (m for help): n24 Command action25 e extended26 p primary partition (1-4)27 p28 Partition number (1-4): 229 First cylinder (27-2610, default 27): 30 Using default value 2731 Last cylinder, +cylinders or +size{K,M,G} (27-2610, default 2610): +2G32 33 Command (m for help): t34 Partition number (1-4): 235 Hex code (type L to list codes): 8236 Changed system type of partition 2 to 82 (Linux swap / Solaris)37 38 Command (m for help): n39 Command action40 e extended41 p primary partition (1-4)42 p43 Partition number (1-4): 344 First cylinder (289-2610, default 289): 45 Using default value 28946 Last cylinder, +cylinders or +size{K,M,G} (289-2610, default 2610): +5G47 48 Command (m for help): w49 The partition table has been altered!50 51 Calling ioctl() to re-read partition table.52 Syncing disks.53 [root@cl Test]# partx -a /dev/sdb54 BLKPG: Device or resource busy55 error adding partition 156 BLKPG: Device or resource busy57 error adding partition 258 BLKPG: Device or resource busy59 error adding partition 360 [root@cl Test]# cat /proc/partitions 61 major minor #blocks name62 63 8 0 125829120 sda64 8 1 204800 sda165 8 2 5242880 sda266 8 3 2097152 sda367 8 4 1 sda468 8 5 10487075 sda569 8 6 5253223 sda670 8 7 8393931 sda771 8 16 20971520 sdb72 8 17 208813 sdb173 8 18 2104515 sdb274 8 19 5253255 sdb3
4.创建文件系统:
1 [root@cl Test]# mke2fs -t ext4 /dev/sdb12 [root@cl Test]# mke2fs -t ext4 /dev/sdb33 [root@cl Test]# mkswap /dev/sdb2
5.挂载分区:
1 [root@cl ~]# mkdir /mnt/boot2 [root@cl ~]# mount /dev/sdb1 /mnt/boot/3 [root@cl ~]# ls /mnt/boot/4 lost+found
6.安装grub:
1 [root@cl ~]# grub-install --root-directory=/mnt /dev/sdb1 (指明/的位置在mnt下) 2 Probing devices to guess BIOS drives. This may take a long time. 3 Installation finished. No error reported. 4 This is the contents of the device map /mnt/boot/grub/device.map. 5 Check if this is correct or not. If any of the lines is incorrect, 6 fix it and re-run the script `grub-install'. 7 8 (fd0) /dev/fd0 9 (hd0) /dev/sda10 (hd1) /dev/sdb11 [root@cl ~]# ls /mnt/boot/12 grub lost+found13 [root@cl ~]# ls /mnt/boot/grub/14 device.map ffs_stage1_5 minix_stage1_5 stage2 xfs_stage1_515 e2fs_stage1_5 iso9660_stage1_5 reiserfs_stage1_5 ufs2_stage1_516 fat_stage1_5 jfs_stage1_5 stage1 vstafs_stage1_5
7.配置grub:
1 [root@cl ~]# cp /boot/vmlinuz-2.6.32-642.el6.x86_64 /mnt/boot/vmlinuz2 [root@cl ~]# cp /boot/initramfs-2.6.32-642.el6.x86_64.img /mnt/boot/initramfs.img3 [root@cl ~]# vim /mnt/boot/grub/grub.conf
default=0
timeout=5
title CentOS (Express)
root (hd0,0)
kernel /vmlinuz ro root=/dev/sda3
initrd /initramfs.img
8.创建根文件目录:
1 [root@cl ~]# mkdir /mnt/sysroot 2 [root@cl ~]# mount /dev/sdb3 /mnt/sysroot/ 3 [root@cl ~]# cd /mnt/sysroot/ 4 [root@cl sysroot]# mkdir -p etc bin sbin lib lib64 dev proc sys tmp var usr home mnt media 5 [root@cl sysroot]# ls 6 bin dev etc home lib lib64 lost+found media mnt proc sbin sys tmp usr var 7 [root@cl sysroot]# cp /bin/bash /mnt/sysroot/bin/ (复制程序bash) 8 [root@cl sysroot]# ldd /bin/bash (查看程序依赖库文件) 9 linux-vdso.so.1 => (0x00007fffd39f9000)10 libtinfo.so.5 => /lib64/libtinfo.so.5 (0x0000003cc3800000)11 libdl.so.2 => /lib64/libdl.so.2 (0x0000003cc1c00000)12 libc.so.6 => /lib64/libc.so.6 (0x0000003cc2000000)13 /lib64/ld-linux-x86-64.so.2 (0x0000003cc1800000)14 [root@cl sysroot]# cp /lib64/libtinfo.so.5 /mnt/sysroot/lib64/15 [root@cl sysroot]# cp /lib64/libdl.so.2 /mnt/sysroot/lib64/16 [root@cl sysroot]# cp /lib64/libc.so.6 /mnt/sysroot/lib64/17 [root@cl sysroot]# cp /lib64/ld-linux-x86-64.so.2 /mnt/sysroot/lib64/18 [root@cl sysroot]# cd19 [root@cl ~]# chroot /mnt/sysroot/ (切换根)20 bash-4.1# 21 bash-4.1# exit22 exit
9.编辑启动init为/bin/bash:
[root@cl ~]# vim /mnt/boot/grub/grub.conf default=0timeout=5title CentOS (Express) root (hd0,0) kernel /vmlinuz ro root=/dev/sda3 init=/bin/bash initrd /initramfs.img [root@cl ~]# sync
note:该操作把bash当做第一个用户空间运行进程启动;
note:sync命令的作用是,将有关文件系统的存储器常驻信息送入物理介质内。在暂停系统之前,比如要重新启动机器,一定要去执行sync命令。
补充:模拟破坏和修复方法一:
1 [root@cl ~]# dd if=/dev/sda of=/root/mbr.bak count=1 bs=512 2 1+0 records in 3 1+0 records out 4 512 bytes (512 B) copied, 0.000155983 s, 3.3 MB/ 5 [root@cl ~]# dd if=/dev/zero of=/dev/sda bs=200 count=1 6 1+0 records in 7 1+0 records out 8 200 bytes (200 B) copied, 0.000307597 s, 650 kB/s 9 [root@cl ~]# sync10 [root@cl ~]# grub-install --root-directory=/ /dev/sda11 Installation finished. No error reported.12 This is the contents of the device map //boot/grub/device.map.13 Check if this is correct or not. If any of the lines is incorrect,14 fix it and re-run the script `grub-install'.15 16 # this device map was generated by anaconda17 (hd0) /dev/sda
模拟破坏和修复方法二:
1 [root@cl ~]# dd if=/dev/zero of=/dev/sda bs=200 count=1 2 1+0 records in 3 1+0 records out 4 200 bytes (200 B) copied, 0.0170366 s, 11.7 kB/s 5 [root@cl ~]# sync 6 [root@cl ~]# grub 7 Probing devices to guess BIOS drives. This may take a long time. 8 9 10 GNU GRUB version 0.97 (640K lower / 3072K upper memory)11 12 [ Minimal BASH-like line editing is supported. For the first word, TAB13 lists possible command completions. Anywhere else TAB lists the possible14 completions of a device/filename.]15 grub> root (hd0,0)16 root (hd0,0)17 Filesystem type is ext2fs, partition type 0x8318 grub> setup (hd0)19 setup (hd0)20 Checking if "/boot/grub/stage1" exists... no21 Checking if "/grub/stage1" exists... yes22 Checking if "/grub/stage2" exists... yes23 Checking if "/grub/e2fs_stage1_5" exists... yes24 Running "embed /grub/e2fs_stage1_5 (hd0)"... 27 sectors are embedded.25 succeeded26 Running "install /grub/stage1 (hd0) (hd0)1+27 p (hd0,0)/grub/stage2 /grub/grub.conf"... succeeded27 Done.28 grub> quit29 quit30 [root@cl ~]# sync
模拟破坏和修复方法三:(重启后救援模式修复)
重启:
[root@cl ~]# dd if=/dev/zero of=/dev/sda bs=200 count=11+0 records in1+0 records out200 bytes (200 B) copied, 0.000283877 s, 705 kB/s [root@cl ~]# sync[root@cl ~]# reboot Broadcast message from root@cl.y.dba (/dev/pts/0) at 15:21 ... The system is going down for reboot NOW!
修复:1.选择镜像光盘:
2.重启系统选择救援模式:
3.进入shell:
4.修复并重启: