一、练习
1.新加一块硬盘,提供可直接单独运行的bash系统?
虚拟机添加一块新硬盘
不关机重启情况下进行新加硬盘的识别
[root@wybaron-host1005 ~]# ll /dev/sd*
brw-rw----. 1 root disk 8, 0 8月 12 22:15 /dev/sda
brw-rw----. 1 root disk 8, 1 8月 12 22:15 /dev/sda1
brw-rw----. 1 root disk 8, 2 8月 12 22:15 /dev/sda2
[root@wybaron-host1005 ~]# ls /sys/class/scsi_host/
host0 host1 host2
[root@wybaron-host1005 ~]# echo "- - -" > /sys/class/scsi_host/host0/scan
[root@wybaron-host1005 ~]# echo "- - -" > /sys/class/scsi_host/host1/scan
[root@wybaron-host1005 ~]# echo "- - -" > /sys/class/scsi_host/host2/scan
[root@wybaron-host1005 ~]# ls /sys/class/scsi_device/
1:0:0:0 2:0:0:0 2:0:1:0
[root@wybaron-host1005 ~]# echo 1 > /sys/class/scsi_device/1\:0\:0\:0/device/rescan
[root@wybaron-host1005 ~]# echo 1 > /sys/class/scsi_device/2\:0\:0\:0/device/rescan
[root@wybaron-host1005 ~]# echo 1 > /sys/class/scsi_device/2\:0\:1\:0/device/rescan
[root@wybaron-host1005 ~]# ll /dev/sd*
brw-rw----. 1 root disk 8, 0 8月 12 22:15 /dev/sda
brw-rw----. 1 root disk 8, 1 8月 12 22:15 /dev/sda1
brw-rw----. 1 root disk 8, 2 8月 12 22:15 /dev/sda2
brw-rw----. 1 root disk 8, 16 8月 13 02:11 /dev/sdb
对新加磁盘进行分区(一个作为/boot/,一个作为 /,一个作为swap分区)
[root@wybaron-host1005 ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xa2fc6379.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2610, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +5G
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (655-2610, default 655):
Using default value 655
Last cylinder, +cylinders or +size{K,M,G} (655-2610, default 2610): +10G
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (1961-2610, default 1961):
Using default value 1961
Last cylinder, +cylinders or +size{K,M,G} (1961-2610, default 2610): +4G
Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes): L
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 40 Venix 80286 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 <32M 41 PPC PReP Boot 85 Linux extended c7 Syrinx
5 Extended 42 SFS 86 NTFS volume set da Non-FS data
6 FAT16 4d QNX4.x 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS 4e QNX4.x 2nd part 88 Linux plaintext de Dell Utility
8 AIX 4f QNX4.x 3rd part 8e Linux LVM df BootIt
9 AIX bootable 50 OnTrack DM 93 Amoeba e1 DOS access
a OS/2 Boot Manag 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 52 CP/M 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 54 OnTrackDM6 a5 FreeBSD ee GPT
f W95 Ext d (LBA) 55 EZ-Drive a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 56 Golden Bow a7 NeXTSTEP f0 Linux/PA-RISC b
11 Hidden FAT12 5c Priam Edisk a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 61 SpeedStor a9 NetBSD f4 SpeedStor
14 Hidden FAT16 <3 63 GNU HURD or Sys ab Darwin boot f2 DOS secondary
16 Hidden FAT16 64 Novell Netware af HFS / HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 65 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 70 DiskSecure Mult b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 75 PC/IX bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 80 Old Minix be Solaris boot ff BBT
1e Hidden W95 FAT1
Hex code (type L to list codes): 82
Changed system type of partition 3 to 82 (Linux swap / Solaris)
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xa2fc6379
Device Boot Start End Blocks Id System
/dev/sdb1 1 654 5253223+ 83 Linux
/dev/sdb2 655 1960 10490445 83 Linux
/dev/sdb3 1961 2483 4200997+ 82 Linux swap / Solaris
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
格式化分区
[root@wybaron-host1005 ~]# mkfs.ext4 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
328656 inodes, 1313305 blocks
65665 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=1346371584
41 block groups
32768 blocks per group, 32768 fragments per group
8016 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
This filesystem will be automatically checked every 33 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@wybaron-host1005 ~]# mkfs.ext4 /dev/sdb2
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655776 inodes, 2622611 blocks
131130 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=2688548864
81 block groups
32768 blocks per group, 32768 fragments per group
8096 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
This filesystem will be automatically checked every 21 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@wybaron-host1005 ~]# mkswap /dev/sdb3
Setting up swapspace version 1, size = 4200992 KiB
no label, UUID=5833e835-b1b0-4df4-962f-3302354e61ba
分别挂载相应分区并创建所需环境
# 安装grub
[root@wybaron-host1005 ~]# mount /dev/sdb1 /mnt/
[root@wybaron-host1005 ~]# cd /mnt/
[root@wybaron-host1005 ~]# grub-install --root-directory=/mnt/ /dev/sdb
Probing devices to guess BIOS drives. This may take a long time.
Installation finished. No error reported.
This is the contents of the device map /mnt//boot/grub/device.map.
Check if this is correct or not. If any of the lines is incorrect,
fix it and re-run the script `grub-install`.
(fd0) /dev/fd0
(hd0) /dev/sda
(hd1) /dev/sdb
[root@wybaron-host1005 ~]# sync
[root@wybaron-host1005 ~]# cat /mnt/boot/grub/device.map
(fd0) /dev/fd0
(hd0) /dev/sda
(hd1) /dev/sdb
########################
## 注意:
这个文件指定了hd0是sda,如果不打算把硬盘移植到其他虚拟机上,要使用新安装的grub,在配置文件中,应该把root指定为hd1,因为sdb是第二块硬盘linux识别顺序会把它识别为sdb;如果打算把硬盘移植到新的虚拟机上,hd0就是第一块硬盘,只要linux识别这块硬盘为sda,那么这里就用不用修改了,现在的实验是打算移植到新的虚拟机上,所以在配置文件中,root应该指定为hd0。
########################
# 复制内核文件与initrd文件到grub目录
[root@wybaron-host1005 ~]# cp /boot/vmlinuz-2.6.32-642.el6.x86_64 /mnt/
[root@wybaron-host1005 ~]# cp /boot/initramfs-2.6.32-642.el6.x86_64.img /mnt/
[root@wybaron-host1005 ~]# sync
[root@wybaron-host1005 ~]# sync
# 创建新的grub配置文件
[root@wybaron-host1005 ~]# cp /boot/grub/grub.conf /mnt/boot/grub/
[root@wybaron-host1005 ~]# vim /mnt/boot/grub/grub.conf
[root@wybaron-host1005 ~]# cat /mnt/boot/grub/grub.conf
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE: You have a /boot partition. This means that
# all kernel and initrd paths are relative to /boot/, eg.
# root (hd0,0)
# kernel /vmlinuz-version ro root=/dev/mapper/vg_wybaronhost1005-lv_root
# initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS 6 (wybaron)
root (hd0,0)
kernel /vmlinuz-2.6.32-642.el6.x86_64 ro selinux=0 root=/dev/sda2 quiet
initrd /initramfs-2.6.32-642.el6.x86_64.img
[root@wybaron-host1005 ~]# sync
[root@wybaron-host1005 ~]# sync
# 构建“根”环境
[root@wybaron-host1005 ~]# umount /mnt/
[root@wybaron-host1005 ~]# mount /dev/sdb2 /mnt/
[root@wybaron-host1005 ~]# mkdir /mnt/{bin,boot,dev,etc,home,lib,lib64,mnt,opt,proc,root,sbin,srv,sys,tmp,usr,var}
[root@wybaron-host1005 ~]# ls /mnt/
bin etc lib64 opt sbin tmp
boot home lost+found proc srv usr
dev lib mnt root sys var
[root@wybaron-host1005 ~]# ldd /bin/bash # 查看bash依赖的库文件
linux-vdso.so.1 => (0x00007ffd1f7d4000)
libtinfo.so.5 => /lib64/libtinfo.so.5 (0x00007f5703fab000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f5703da7000)
libc.so.6 => /lib64/libc.so.6 (0x00007f5703a12000)
/lib64/ld-linux-x86-64.so.2 (0x00007f57041d8000)
[root@wybaron-host1005 ~]# cp /lib64/libtinfo.so.5 /mnt/lib64/
[root@wybaron-host1005 ~]# cp /lib64/libdl.so.2 /mnt/lib64/
[root@wybaron-host1005 ~]# cp /lib64/libc.so.6 /mnt/lib64/
[root@wybaron-host1005 ~]# cp /lib64/ld-linux-x86-64.so.2 /mnt/lib64/
[root@wybaron-host1005 ~]# cp /bin/bash /mnt/sbin/init # 复制bash作为init程序
[root@wybaron-host1005 ~]# sync
[root@wybaron-host1005 ~]# umount /mnt/
虚拟机关闭当前主机,另外新建一个虚拟机,引用上面的硬盘设备,测试结果
此时说明此练习成功完成!
2.人为破坏本机grub stage1,然后通过救援模式进行修复?
备份现有MBR
注意:MBR的范围是0-446字节位置,不要损坏了磁盘的分区表。
[root@wybaron-host1005 ~]# dd if=/dev/sda of=/root/stage1 count=1 bs=446
记录了1+0 的读入
记录了1+0 的写出
446字节(446 B)已复制,0.000168649 秒,2.6 MB/秒
[root@wybaron-host1005 ~]# sync
[root@wybaron-host1005 ~]# sync
[root@wybaron-host1005 ~]# sync
损坏现有的MBR
[root@wybaron-host1005 ~]# dd if=/dev/zero of=/dev/sda count=1 bs=446
记录了1+0 的读入
记录了1+0 的写出
446字节(446 B)已复制,0.0106515 秒,41.9 kB/秒
[root@wybaron-host1005 ~]# sync
[root@wybaron-host1005 ~]# sync
重启系统验证是否能够正常引导
挂载CentOS原版光盘,进入救援模式
A)救援模式下使用根的方式修复引导(直接选中“Rescue installed system”然后回车确认)
语言选择
键盘布局选择
提示是否通过网络进行修复,这里假设网络不可修复
选择“Continue”
... ...
进入bash环境进行操作
Statting shell...
bash-4.1# chroot /mnt/sysimage/
sh-4.1# ls /root/sta* # 之前备份的时候将mbr备份至此处的
stage1
sh-4.1# grub-install --root-directory=/ /dev/sda
...
sh-4.1# exit
exit
bash-4.1# reboot
...
B)救援模式的GRUB命令行救援
此界面直接“ESC”即可
直接按照如下输入和上面是等效的
boot:linux rescue
重启验证修复结果(建议弹出CentOS光盘,防止再次进入光盘系统)
![999d2230fed13248e76a693526cc46394d7.jpg](https://oscimg.oschina.net/oscnet/999d2230fed13248e76a693526cc46394d7.jpg)
3.为grub设备提供保护功能?
建立grub的菜单编辑认证,密码使用“wybaron”
[root@wybaron-host1005 ~]# grub-md5-crypt
Password:
Retype password:
$1$hAG050$VQMk..WvnVgidwG20o0QI1
[root@wybaron-host1005 ~]# cat /boot/grub/grub.conf
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE: You have a /boot partition. This means that
# all kernel and initrd paths are relative to /boot/, eg.
# root (hd0,0)
# kernel /vmlinuz-version ro root=/dev/mapper/vg_wybaronhost1005-lv_root
# initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
password --md5 $1$Tr805^CPa0htdzznM5XlnbrzSOKZ/
title CentOS 6 (2.6.32-642.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-642.el6.x86_64 ro root=/dev/mapper/vg_wybaronhost1005-lv_root rd_LVM_LV=vg_wybaronhost1005/lv_root rd_NO_MD rd_LVM_LV=vg_wybaronhost1005/lv_swap crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LUKS KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-642.el6.x86_64.img
[root@wybaron-host1005 ~]# sync
[root@wybaron-host1005 ~]# sync
引导按键“e”
输入任意密码测试结果
输入正确密码测试结果
二、补充
dd命令
转换和拷贝文件
dd [if=file] [of=file] [bs=bytes] [count=blocks]
if=file # 从 file 中读而不是标准输入
of=file # 写到 file 里去而不是标准输出。除非指定 conv=notrunc,否则,dd 将把file截为O字节(或由seek= 选项指定的大小)
bs=bytes # 一次读和写 bytes 字节。这将覆盖 ibs 和 obs 设定的值(并且,设定 bs不等于同时将 ibs 和 obs 设为同一个值,至少在 只使用 sync, noerror或 notrunc 转换时是这样的。因为bs规定,每个输入块都应作为单独的数据块拷贝到输出,而不把较短的块组合到一起)
count=blocks # 只拷贝输入文件的前 blocks 块(每块的大小为 ibs-byte 字节),而不是全部内容,直到文件末尾。
# 常用格式
[root@wybaron_host1015 ~]# dd if=/dev/[zero] of=[/dev/DEVICE | FILE_NAME] count=# bs=#
sync命令
强制将内存中的数据写入磁盘中。
sync [OPTION]
# 常用格式(2-3次使用)
[root@wybaron_host1015 ~]# sync
[root@wybaron_host1015 ~]# sync
忘记grub密码的修复办法
插入对应版本的CentOS系统盘,进入救援模式(设置从光盘启动,BIOS中设置光盘启动为第一启动项)
按照上文中的步骤进入bash界面(切换到对应目录下,删除或修改/boot/grub/grub.conf中password的值)
此时发现grub密码被取消