2.1. 2017-12-01-练习-磁盘管理

2.1.1. 练习1-故障修复

1、 mbr损坏的修复

[root@centos7 ~]$ dd if=/dev/sda of=/app/mbr bs=1 count=512
512+0 records in
512+0 records out
512 bytes (512 B) copied, 0.000847418 s, 604 kB/s
[root@centos7 ~]$ scp /app/mbr 172.18.1.159:
root@172.18.1.159's password:
mbr                                                                                 100%  512   428.1KB/s   00:00
[root@centos7 ~]$ dd if=/dev/zero of=/dev/sda bs=1 count=512
512+0 records in
512+0 records out
512 bytes (512 B) copied, 0.000467582 s, 1.1 MB/s
#进入救援模式
[root@centos7 ~]$ ifconfig ens33 172.18.1.250
[root@centos7 ~]$ scp root@172.18.1.159:/root/mbr .
[root@centos7 ~]$ dd if=mbr of=/dev/sda bs=1 count=512
[root@centos7 ~]$ reboot

2、文件系统出现故障的修复

模拟破坏环境

[root@centos7 mnt]$ mkfs.ext4 /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes, 5242880 blocks
262144 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2153775104
160 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
    4096000

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

[root@centos7 mnt]$ mount /dev/sdb1 /mnt/1
[root@centos7 mnt]$ touch 1.txt >/mnt/1/
-bash: /mnt/1/: Is a directory
[root@centos7 mnt]$ touch "f1 " >> /mnt/1/f1.txt
[root@centos7 mnt]$ dd if=/dev/zero of=/dev/sdb1 bs=1M count=10
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.00539083 s, 1.9 GB/s

修复文件系统问题

[root@centos7 mnt]$ umount /dev/sdb1
[root@centos7 mnt]$ fsck /dev/sdb1
fsck from util-linux 2.23.2
e2fsck 1.42.9 (28-Dec-2013)
ext2fs_open2: Bad magic number in super-block
fsck.ext2: Superblock invalid, trying backup blocks...
Superblock has an invalid journal (inode 8).
Clear<y>? yes
*** ext3 journal has been deleted - filesystem is now ext2 only ***

/dev/sdb1 was not cleanly unmounted, check forced.
Resize inode not valid.  Recreate<y>? yes
Pass 1: Checking inodes, blocks, and sizes
Root inode is not a directory.  Clear<y>? yes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Root inode not allocated.  Allocate<y>? yes
/lost+found not found.  Create<y>? yes
Pass 4: Checking reference counts
Pass 5: Checking group summary information
Block bitmap differences:  +(0--9252) +(32768--33795) +(98304--99331) +(163840--164867) +(229376--230403) +(294912--295939) -(2655236--2688003)
Fix<y>? yes
Free blocks count wrong for group #0 (23508, counted=23513).
Fix<y>? yes
Free blocks count wrong for group #81 (0, counted=31740).
Fix<y>? yes
Free blocks count wrong for group #82 (31740, counted=32768).
Fix<y>? yes
Free blocks count wrong (5116556, counted=5149329).
Fix<y>? yes
Inode bitmap differences:  +1 +(3--10)
Fix<y>? yes
Free inodes count wrong for group #0 (8180, counted=8181).
Fix<y>? yes
Directories count wrong for group #0 (3, counted=2).
Fix<y>? yes
Free inodes count wrong (1310708, counted=1310709).
Fix<y>? yes
Recreate journal<y>? yes
Creating journal (32768 blocks):

Done.

*** journal has been re-created - filesystem is now ext3 again ***

/dev/sdb1: ***** FILE SYSTEM WAS MODIFIED *****
/dev/sdb1: 11/1310720 files (0.0% non-contiguous), 126319/5242880 blocks

2、写一个脚本,完成如下功能:

  1. 列出当前系统识别到的所有磁盘设备

  2. 如磁盘数量为1,则显示其空间使用信息

否则,则显示最后一个磁盘上的空间使用信息

lsblk  |grep '^s[dh][a-z]\+' -o
df  /dev/`lsblk  |grep '^s[dh][a-z]\+' -o | tail -n 1 `

3、将CentOS6的CentOS-6.8-x86_64-bin-DVD1.iso和CentOS-6.8-x86_64-bin-DVD2.iso两个文件,合并成一个CentOS-6.8-x86_64-Everything.iso文件,并将其配置为yum源

mount /dev/sr2 /mnt/cdrom1
mount /dev/sr3 /mnt/cdrom2

cp -a /mnt/cdrom2/*  /app/test/
\cp -a /mnt/cdrom1/* /app/tset/
cp /mnt/cdrom1/.discinfo /app/test
./mkdvdiso.sh  /app/test /root/centos.iso

mkdvdiso.sh

2.1.2. 练习3-交换分区

1 、制作交换分区,并启用

[root@centos7 ~]$ fdisk /dev/sdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition number (3-128, default 3):
First sector (34-209715166, default 62787584):
Last sector, +sectors or +size{K,M,G,T,P} (62787584-209715166, default 209715166): +1G
Created partition 3


Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: gpt
Disk identifier: CF8E8E9A-2E50-4DD8-AC6F-A621DDD9B8ED


#         Start          End    Size  Type            Name
1      1953792     58593279     27G  Linux LVM       primary
2     58593280     62787583      2G  Linux filesyste
3     62787584     64884735      1G  Linux filesyste

Command (m for help): t
Partition number (1-3, default 3):
Partition type (type L to list all types): L
1 EFI System                     C12A7328-F81F-11D2-BA4B-00A0C93EC93B
2 MBR partition scheme           024DEE41-33E7-11D3-9D69-0008C781F39F
3 BIOS boot partition            21686148-6449-6E6F-744E-656564454649
4 Microsoft reserved             E3C9E316-0B5C-4DB8-817D-F92DF00215AE
5 Microsoft basic data           EBD0A0A2-B9E5-4433-87C0-68B6B72699C7
6 Microsoft LDM metadata         5808C8AA-7E8F-42E0-85D2-E1E90434CFB3
7 Microsoft LDM data             AF9B60A0-1431-4F62-BC68-3311714A69AD
8 Windows recovery evironmnet    DE94BBA4-06D1-4D40-A16A-BFD50179D6AC
9 IBM General Parallel Fs        37AFFC90-EF7D-4E96-91C3-2D7AE055B174
10 HP-UX data partition           75894C1E-3AEB-11D3-B7C1-7B03A0000000
11 HP-UX service partition        E2A1E728-32E3-11D6-A682-7B03A0000000
12 Linux filesystem               0FC63DAF-8483-4772-8E79-3D69D8477DE4
13 Linux RAID                     A19D880F-05FC-4D3B-A006-743F0F84911E
14 Linux swap                     0657FD6D-A4AB-43C4-84E5-0933C84B4F4F
15 Linux LVM                      E6D6D379-F507-44C2-A23C-238F2A3DF928
16 Linux reserved                 8DA63339-0007-60C0-C436-083AC8230908
17 FreeBSD data                   516E7CB4-6ECF-11D6-8FF8-00022D09712B
18 FreeBSD boot                   83BD6B9D-7F41-11DC-BE0B-001560B84F0F
19 FreeBSD swap                   516E7CB5-6ECF-11D6-8FF8-00022D09712B
20 FreeBSD UFS                    516E7CB6-6ECF-11D6-8FF8-00022D09712B
21 FreeBSD ZFS                    516E7CBA-6ECF-11D6-8FF8-00022D09712B
22 FreeBSD Vinum                  516E7CB8-6ECF-11D6-8FF8-00022D09712B
23 Apple HFS/HFS+                 48465300-0000-11AA-AA11-00306543ECAC
24 Apple UFS                      55465300-0000-11AA-AA11-00306543ECAC
25 Apple RAID                     52414944-0000-11AA-AA11-00306543ECAC
26 Apple RAID offline             52414944-5F4F-11AA-AA11-00306543ECAC
27 Apple boot                     426F6F74-0000-11AA-AA11-00306543ECAC
28 Apple label                    4C616265-6C00-11AA-AA11-00306543ECAC
29 Apple TV recovery              5265636F-7665-11AA-AA11-00306543ECAC
30 Apple Core storage             53746F72-6167-11AA-AA11-00306543ECAC
31 Solaris boot                   6A82CB45-1DD2-11B2-99A6-080020736631
32 Solaris root                   6A85CF4D-1DD2-11B2-99A6-080020736631
33 Solaris /usr & Apple ZFS       6A898CC3-1DD2-11B2-99A6-080020736631
34 Solaris swap                   6A87C46F-1DD2-11B2-99A6-080020736631
35 Solaris backup                 6A8B642B-1DD2-11B2-99A6-080020736631
36 Solaris /var                   6A8EF2E9-1DD2-11B2-99A6-080020736631
37 Solaris /home                  6A90BA39-1DD2-11B2-99A6-080020736631
38 Solaris alternate sector       6A9283A5-1DD2-11B2-99A6-080020736631
39 Solaris reserved 1             6A945A3B-1DD2-11B2-99A6-080020736631
40 Solaris reserved 2             6A9630D1-1DD2-11B2-99A6-080020736631
41 Solaris reserved 3             6A980767-1DD2-11B2-99A6-080020736631
42 Solaris reserved 4             6A96237F-1DD2-11B2-99A6-080020736631
43 Solaris reserved 5             6A8D2AC7-1DD2-11B2-99A6-080020736631
44 NetBSD swap                    49F48D32-B10E-11DC-B99B-0019D1879648
45 NetBSD FFS                     49F48D5A-B10E-11DC-B99B-0019D1879648
46 NetBSD LFS                     49F48D82-B10E-11DC-B99B-0019D1879648
47 NetBSD concatenated            2DB519C4-B10E-11DC-B99B-0019D1879648
48 NetBSD encrypted               2DB519EC-B10E-11DC-B99B-0019D1879648
49 NetBSD RAID                    49F48DAA-B10E-11DC-B99B-0019D1879648
50 ChromeOS kernel                FE3A2A5D-4F32-41A7-B725-ACCC3285A309
51 ChromeOS root fs               3CB8E202-3B7E-47DD-8A3C-7FF2A13CFCEC
52 ChromeOS reserved              2E0A753D-9E48-43B0-8337-B15192CB1B5E
53 MidnightBSD data               85D5E45A-237C-11E1-B4B3-E89A8F7FC3A7
54 MidnightBSD boot               85D5E45E-237C-11E1-B4B3-E89A8F7FC3A7
55 MidnightBSD swap               85D5E45B-237C-11E1-B4B3-E89A8F7FC3A7
56 MidnightBSD UFS                0394Ef8B-237C-11E1-B4B3-E89A8F7FC3A7
57 MidnightBSD ZFS                85D5E45D-237C-11E1-B4B3-E89A8F7FC3A7
58 MidnightBSD Vinum              85D5E45C-237C-11E1-B4B3-E89A8F7FC3A7

Partition type (type L to list all types): 14
Changed type of partition 'Linux filesystem' to 'Linux swap'

Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: gpt
Disk identifier: CF8E8E9A-2E50-4DD8-AC6F-A621DDD9B8ED


#         Start          End    Size  Type            Name
1      1953792     58593279     27G  Linux LVM       primary
2     58593280     62787583      2G  Linux filesyste
3     62787584     64884735      1G  Linux swap

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@centos7 ~]$ lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0  128G  0 part [SWAP]
├─sda3   8:3    0 48.8G  0 part /
├─sda4   8:4    0  512B  0 part
└─sda5   8:5    0 19.5G  0 part /app
sdb      8:16   0  100G  0 disk
├─sdb1   8:17   0   27G  0 part
└─sdb2   8:18   0    2G  0 part /test
sr0     11:0    1  8.1G  0 rom  /run/media/root/CentOS 7 x86_64
loop0    7:0    0    1G  0 loop /mnt/c1
[root@centos7 ~]$ partx -a /dev/sdb
partx: /dev/sdb: error adding partitions 1-2
[root@centos7 ~]$ lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0  128G  0 part [SWAP]
├─sda3   8:3    0 48.8G  0 part /
├─sda4   8:4    0  512B  0 part
└─sda5   8:5    0 19.5G  0 part /app
sdb      8:16   0  100G  0 disk
├─sdb1   8:17   0   27G  0 part
├─sdb2   8:18   0    2G  0 part /test
└─sdb3   8:19   0    1G  0 part
sr0     11:0    1  8.1G  0 rom  /run/media/root/CentOS 7 x86_64
loop0    7:0    0    1G  0 loop /mnt/c1

[root@centos7 ~]$ mkswap /dev/sdb3
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=a0516c4f-40e6-4919-905a-8b44db12ff7b
[root@centos7 ~]$ free
            total        used        free      shared  buff/cache   available
Mem:        2031912      845264      377640        9488      809008      955040
Swap:     134217724         296   134217428
[root@centos7 ~]$ swapon /dev/sdb3
[root@centos7 ~]$ free
            total        used        free      shared  buff/cache   available
Mem:        2031912      846432      376416        9488      809064      953872
Swap:     135266296         296   135266000