第5周

整理

在脚本中使用fdisk

[root@CENTOS7 ~]#echo -e “n\n\n\n\n+1G\nw\n” | fdisk /dev/sdc
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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x4c45a428.

Command (m for help): Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): Using default response p
Partition number (1-4, default 1): First sector (2048-167772159, default 2048): Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-167772159, default 167772159): Partition 1 of type Linux and of size 1 GiB is set

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

Calling ioctl() to re-read partition table.
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 50G 0 part /
├─sda3 8:3 0 30G 0 part /data
├─sda4 8:4 0 1K 0 part
├─sda5 8:5 0 2G 0 part [SWAP]
└─sda6 8:6 0 1G 0 part
sdb 8:16 0 100G 0 disk
├─sdb1 8:17 0 1G 0 part
├─sdb2 8:18 0 1K 0 part
├─sdb3 8:19 0 2G 0 part
└─sdb5 8:21 0 200M 0 part
sdc 8:32 0 80G 0 disk
└─sdc1 8:33 0 1G 0 part
sdd 8:48 0 60G 0 disk
sr0 11:0 1 8.1G 0 rom /run/media/root/CentOS 7 x86_64

[root@CENTOS7 ~]#echo -e “n\n\n\n\n+1G\nw\n” | fdisk /dev/sdc 从定向方式
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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x4c45a428.

Command (m for help): Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): Using default response p
Partition number (1-4, default 1): First sector (2048-167772159, default 2048): Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-167772159, default 167772159): Partition 1 of type Linux and of size 1 GiB is set

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

Calling ioctl() to re-read partition table.
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 50G 0 part /
├─sda3 8:3 0 30G 0 part /data
├─sda4 8:4 0 1K 0 part
├─sda5 8:5 0 2G 0 part [SWAP]
└─sda6 8:6 0 1G 0 part
sdb 8:16 0 100G 0 disk
├─sdb1 8:17 0 1G 0 part
├─sdb2 8:18 0 1K 0 part
├─sdb3 8:19 0 2G 0 part
└─sdb5 8:21 0 200M 0 part
sdc 8:32 0 80G 0 disk
└─sdc1 8:33 0 1G 0 part
sdd 8:48 0 60G 0 disk
sr0 11:0 1 8.1G 0 rom /run/media/root/CentOS 7 x86_64

查看centos6上sda有多少个块组,一个块组就多少个块组成
dumpe2fs /dev/sda1

[root@centos ~]#dumpe2fs /dev/sda1
dumpe2fs 1.41.12 (17-May-2010)
Filesystem volume name: <none>
Last mounted on: /boot
Filesystem UUID: 414ab9ee-af40-44f1-b300-d29fc674eb8e
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: has_journal ext_attr resize_inode dir_index filetype needs_recovery extent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
Filesystem flags: signed_directory_hash
Default mount options: user_xattr acl
Filesystem state: clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 65536
Block count: 262144
Reserved block count: 13107
Free blocks: 241094
Free inodes: 65498
First block: 0
Block size: 4096
Fragment size: 4096
Reserved GDT blocks: 63
Blocks per group: 32768
Fragments per group: 32768
Inodes per group: 8192
Inode blocks per group: 512
Flex block group size: 16
Filesystem created: Tue Mar 27 16:56:37 2018
Last mount time: Mon Apr 23 08:47:13 2018
Last write time: Mon Apr 23 08:47:13 2018
Mount count: 52
Maximum mount count: -1
Last checked: Tue Mar 27 16:56:37 2018
Check interval: 0 (<none>)
Lifetime writes: 82 MB
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)
First inode: 11
Inode size: 256
Required extra isize: 28
Desired extra isize: 28
Journal inode: 8
Default directory hash: half_md4
Directory Hash Seed: 1ae4dcb2-74ba-4214-955b-ae990ad39f42
Journal backup: inode blocks
Journal features: (none)
Journal size: 32M
Journal length: 8192
Journal sequence: 0x00000038
Journal start: 0

Group 0: (Blocks 0-32767) [ITABLE_ZEROED]
Checksum 0xf60c, unused inodes 8155
Primary superblock at 0, Group descriptors at 1-1
Reserved GDT blocks at 2-64
Block bitmap at 65 (+65), Inode bitmap at 81 (+81)
Inode table at 97-608 (+97)
28581 free blocks, 8155 free inodes, 5 directories, 8155 unused inodes
Free blocks: 79, 90-95, 4194-32767
Free inodes: 38-8192
Group 1: (Blocks 32768-65535) [ITABLE_ZEROED]
Checksum 0x2122, unused inodes 8191
Backup superblock at 32768, Group descriptors at 32769-32769
Reserved GDT blocks at 32770-32832
Block bitmap at 66 (+4294934594), Inode bitmap at 82 (+4294934610)
Inode table at 609-1120 (+4294935137)
24292 free blocks, 8191 free inodes, 1 directories, 8191 unused inodes
Free blocks: 34666-34667, 34682-34683, 34688-34815, 41362-41363, 41374-41375, 41380-65535
Free inodes: 8194-16384
Group 2: (Blocks 65536-98303) [INODE_UNINIT, BLOCK_UNINIT, ITABLE_ZEROED]
Checksum 0x4789, unused inodes 8192
Block bitmap at 67 (+4294901827), Inode bitmap at 83 (+4294901843)
Inode table at 1121-1632 (+4294902881)
32768 free blocks, 8192 free inodes, 0 directories, 8192 unused inodes
Free blocks: 65536-98303
Free inodes: 16385-24576
Group 3: (Blocks 98304-131071) [INODE_UNINIT, ITABLE_ZEROED]
Checksum 0x30dc, unused inodes 8192
Backup superblock at 98304, Group descriptors at 98305-98305
Reserved GDT blocks at 98306-98368
Block bitmap at 68 (+4294869060), Inode bitmap at 84 (+4294869076)
Inode table at 1633-2144 (+4294870625)
32703 free blocks, 8192 free inodes, 0 directories, 8192 unused inodes
Free blocks: 98369-131071
Free inodes: 24577-32768
Group 4: (Blocks 131072-163839) [INODE_UNINIT, ITABLE_ZEROED]
Checksum 0x74e6, unused inodes 8192
Block bitmap at 69 (+4294836293), Inode bitmap at 85 (+4294836309)
Inode table at 2145-2656 (+4294838369)
24576 free blocks, 8192 free inodes, 0 directories, 8192 unused inodes
Free blocks: 139264-163839
Free inodes: 32769-40960
Group 5: (Blocks 163840-196607) [INODE_UNINIT, ITABLE_ZEROED]
Checksum 0x0de1, unused inodes 8192
Backup superblock at 163840, Group descriptors at 163841-163841
Reserved GDT blocks at 163842-163904
Block bitmap at 70 (+4294803526), Inode bitmap at 86 (+4294803542)
Inode table at 2657-3168 (+4294806113)
32703 free blocks, 8192 free inodes, 0 directories, 8192 unused inodes
Free blocks: 163905-196607
Free inodes: 40961-49152
Group 6: (Blocks 196608-229375) [INODE_UNINIT, BLOCK_UNINIT, ITABLE_ZEROED]
Checksum 0x44e3, unused inodes 8192
Block bitmap at 71 (+4294770759), Inode bitmap at 87 (+4294770775)
Inode table at 3169-3680 (+4294773857)
32768 free blocks, 8192 free inodes, 0 directories, 8192 unused inodes
Free blocks: 196608-229375
Free inodes: 49153-57344
Group 7: (Blocks 229376-262143) [INODE_UNINIT, ITABLE_ZEROED]
Checksum 0x4c72, unused inodes 8192
Backup superblock at 229376, Group descriptors at 229377-229377
Reserved GDT blocks at 229378-229440
Block bitmap at 72 (+4294737992), Inode bitmap at 88 (+4294738008)
Inode table at 3681-4192 (+4294741601)
32703 free blocks, 8192 free inodes, 0 directories, 8192 unused inodes
Free blocks: 229441-262143
Free inodes: 57345-65536

superblock 很重要其中存放着分区的元数据和块组的划分,所以在奇数1,3..9….都有备份

[root@CENTOS7 ~]#mkfs.ext4 /dev/sdc1 -b 1024 创建一个文件系统指定这个文件系统的块大小是1k,块的大小只有在创建文件系统的时候可以指定,一但创建完毕则无法修改。默认的是4k
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
65536 inodes, 1048576 blocks
52428 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=34603008
128 block groups
8192 blocks per group, 8192 fragments per group
512 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409, 663553,
1024001

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

[root@CENTOS7 ~]#tune2fs -l /dev/sdc1
tune2fs 1.42.9 (28-Dec-2013)
Filesystem volume name: <none>
Last mounted on: <not available>
Filesystem UUID: e3982c14-ce5c-4285-8fb0-26816d691b6e
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: has_journal ext_attr resize_inode dir_index filetype extent 64bit flex_bg sparse_super huge_file uninit_bg dir_nlink extra_isize
Filesystem flags: signed_directory_hash
Default mount options: user_xattr acl
Filesystem state: clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 65536
Block count: 1048576
Reserved block count: 52428
Free blocks: 996238
Free inodes: 65525
First block: 1
Block size: 1024
Fragment size: 1024
Group descriptor size: 64
Reserved GDT blocks: 256
Blocks per group: 8192
Fragments per group: 8192
Inodes per group: 512
Inode blocks per group: 128
Flex block group size: 16
Filesystem created: Mon Apr 23 19:35:25 2018
Last mount time: n/a
Last write time: Mon Apr 23 19:35:25 2018
Mount count: 0
Maximum mount count: -1
Last checked: Mon Apr 23 19:35:25 2018
Check interval: 0 (<none>)
Lifetime writes: 32 MB
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)
First inode: 11
Inode size: 256
Required extra isize: 28
Desired extra isize: 28
Journal inode: 8
Default directory hash: half_md4
Directory Hash Seed: 74c87523-34f1-4be2-864c-d515d4425a22
Journal backup: inode blocks

Inode count: 65536 节点编号
Block count: 1048576 块数量
Reserved block count: 52428 默认百分之5,保留的块数量留给id为0的用户使用
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)

mkfs.ext4 /dev/sdc1 -b 1024 -m 1
mke2fs:ext系列文件系统专用管理工具
-t {ext2|ext3|ext4}
-b {1024|2048|4096}
-L ‘LABEL’
-j: 相当于 -t ext3
mkfs.ext3 = mkfs -t ext3 = mke2fs -j = mke2fs -t ext3
-i #: 为数据空间中每多少个字节创建一个inode;此大小
不应该小于block的大小
-N #:指定分区中创建多少个inode
-I 一个inode记录占用的磁盘空间大小,128—4096
-m #: 默认5%,为管理人员预留空间占总空间的百分比
-O FEATURE[,…]:启用指定特性
-O ^FEATURE:关闭指定特性

[root@CENTOS7 ~]#mkfs.ext4 /dev/sdc1 -b 1024 -m 0.1 创建一个块是1k,为id0预留千分之一个块的文件系统
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
65536 inodes, 1048576 blocks
1048 blocks (0.10%) reserved for the super user
First data block=1
Maximum filesystem blocks=34603008
128 block groups
8192 blocks per group, 8192 fragments per group
512 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409, 663553,
1024001

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

[root@CENTOS7 ~]#tune2fs -l /dev/sdc1
tune2fs 1.42.9 (28-Dec-2013)
Filesystem volume name: <none>
Last mounted on: <not available>
Filesystem UUID: 884dac1d-3d83-4685-8b98-53bcb1619452
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: has_journal ext_attr resize_inode dir_index filetype extent 64bit flex_bg sparse_super huge_file uninit_bg dir_nlink extra_isize
Filesystem flags: signed_directory_hash
Default mount options: user_xattr acl
Filesystem state: clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 65536
Block count: 1048576
Reserved block count: 1048
Free blocks: 996238
Free inodes: 65525
First block: 1
Block size: 1024
Fragment size: 1024
Group descriptor size: 64
Reserved GDT blocks: 256
Blocks per group: 8192
Fragments per group: 8192
Inodes per group: 512
Inode blocks per group: 128
Flex block group size: 16
Filesystem created: Mon Apr 23 19:42:10 2018
Last mount time: n/a
Last write time: Mon Apr 23 19:42:11 2018
Mount count: 0
Maximum mount count: -1
Last checked: Mon Apr 23 19:42:10 2018
Check interval: 0 (<none>)
Lifetime writes: 32 MB
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)
First inode: 11
Inode size: 256
Required extra isize: 28
Desired extra isize: 28
Journal inode: 8
Default directory hash: half_md4
Directory Hash Seed: 0940f0ee-880e-40a1-bc8c-21bb6677114a
Journal backup: inode blocks

[root@CENTOS7 ~]#mkfs.ext4 /dev/sdc2 -b 1024 -i 1024 -m 1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
2097152 inodes, 2097152 blocks
20971 blocks (1.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=35651584
256 block groups
8192 blocks per group, 8192 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409, 663553,
1024001, 1990657

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

[root@CENTOS7 ~]#tune2fs -l /dev/sdc2
tune2fs 1.42.9 (28-Dec-2013)
Filesystem volume name: <none>
Last mounted on: <not available>
Filesystem UUID: 381e389c-a0a7-4b8d-bc7e-5c29152162ce
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: has_journal ext_attr resize_inode dir_index filetype extent 64bit flex_bg sparse_super huge_file uninit_bg dir_nlink extra_isize
Filesystem flags: signed_directory_hash
Default mount options: user_xattr acl
Filesystem state: clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 2097152
Block count: 2097152
Reserved block count: 20971
Free blocks: 1536293
Free inodes: 2097141
First block: 1
Block size: 1024
Fragment size: 1024
Group descriptor size: 64
Reserved GDT blocks: 256
Blocks per group: 8192
Fragments per group: 8192
Inodes per group: 8192
Inode blocks per group: 2048
Flex block group size: 16
Filesystem created: Mon Apr 23 19:48:03 2018
Last mount time: n/a
Last write time: Mon Apr 23 19:48:04 2018
Mount count: 0
Maximum mount count: -1
Last checked: Mon Apr 23 19:48:03 2018
Check interval: 0 (<none>)
Lifetime writes: 32 MB
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)
First inode: 11
Inode size: 256
Required extra isize: 28
Desired extra isize: 28
Journal inode: 8
Default directory hash: half_md4
Directory Hash Seed: f5054342-a203-4d51-b668-67f26b90ae0e
Journal backup: inode blocks

[root@CENTOS7 ~]#mkfs.ext4 /dev/sdc2 -N 209715 指定多少个inodes,实际要多一些可能是因为取整.
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
209920 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
13120 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912

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

[root@CENTOS7 ~]#tune2fs -l /dev/sdc2
tune2fs 1.42.9 (28-Dec-2013)
Filesystem volume name: <none>
Last mounted on: <not available>
Filesystem UUID: 60be96b2-617c-4a8d-90f4-4f6fe194333e
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: has_journal ext_attr resize_inode dir_index filetype extent 64bit flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
Filesystem flags: signed_directory_hash
Default mount options: user_xattr acl
Filesystem state: clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 209920
Block count: 524288
Reserved block count: 26214
Free blocks: 493204
Free inodes: 209909
First block: 0
Block size: 4096
Fragment size: 4096
Group descriptor size: 64
Reserved GDT blocks: 255
Blocks per group: 32768
Fragments per group: 32768
Inodes per group: 13120
Inode blocks per group: 820
Flex block group size: 16
Filesystem created: Mon Apr 23 19:55:26 2018
Last mount time: n/a
Last write time: Mon Apr 23 19:55:27 2018
Mount count: 0
Maximum mount count: -1
Last checked: Mon Apr 23 19:55:26 2018
Check interval: 0 (<none>)
Lifetime writes: 65 MB
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)
First inode: 11
Inode size: 256
Required extra isize: 28
Desired extra isize: 28
Journal inode: 8
Default directory hash: half_md4
Directory Hash Seed: e3845910-09be-4b48-a8e4-aef809454480
Journal backup: inode blocks

[root@CENTOS7 ~]#mkfs.ext2 /dev/sdc1 -O has_journal 添加日志功能
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
65536 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376

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

[root@CENTOS7 ~]#blkid /dev/sdc1
/dev/sdc1: UUID=”34cacf85-0038-42c5-9ce5-0d77bba36d43″ SEC_TYPE=”ext2″ TYPE=”ext3″

[root@CENTOS7 ~]#tune2fs -O ^has_journal /dev/sdc1 取消日志功能
tune2fs 1.42.9 (28-Dec-2013)
[root@CENTOS7 ~]#blkid /dev/sdc1
/dev/sdc1: UUID=”8980fe3c-2c14-4db8-b623-b4f0bfcc3cba” TYPE=”ext2″

[root@CENTOS7 ~]#mkfs.ext4 /dev/sdc1 -L data 创建文件系统时起(卷标),最好用挂载点的名字
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=data
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65536 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376

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

[root@CENTOS7 ~]#tune2fs -l /dev/sdc1
tune2fs 1.42.9 (28-Dec-2013)
Filesystem volume name: data

[root@CENTOS7 ~]#blkid /dev/sdc1
/dev/sdc1: LABEL=”data” UUID=”70496471-2329-49d7-8524-5ba956fbb040″ TYPE=”ext4″

文件系统标签
指向设备的另一种方法
与设备无关
blkid:块设备属性信息查看
blkid [OPTION]… [DEVICE]
-U UUID: 根据指定的UUID来查找对应的设备 通用

-L LABEL:根据指定的LABEL来查找对应的设备
e2label:管理ext系列文件系统的LABEL
e2label DEVICE [LABEL] 只适合ext系列
findfs :查找分区
findfs [options] LABEL=<label> 通用
findfs [options] UUID=<uuid>

[root@CENTOS7 ~]#blkid -U c837ab44-edcb-43af-b4fa-c26832a7af7e 用uuid查看设备名
/dev/sdb1

[root@CENTOS7 ~]#blkid -L “data” 用卷标查看设备名
/dev/sdc1

[root@centos ~]#cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Tue Mar 27 16:59:39 2018
#
# Accessible filesystems, by reference, are maintained under ‘/dev/disk’
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=408b65d7-7551-480b-8916-5eff4cca1b00 / ext4 defaults 1 1
UUID=414ab9ee-af40-44f1-b300-d29fc674eb8e /boot ext4 defaults 1 2
UUID=37d5cf25-ead4-427b-9655-40fc1602b5df /data ext4 defaults 1 2
UUID=9fc26932-7094-4948-b395-24ab05d8ea73 swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
[root@centos ~]#cat /etc/fstab | grep -o “data”
data
[root@centos ~]#cat /etc/fstab | grep “data”
UUID=37d5cf25-ead4-427b-9655-40fc1602b5df /data ext4 defaults 1 2
[root@centos ~]#cat /etc/fstab | grep “data” | sed -r ‘s@.*=(.*) /.*@\1@’
37d5cf25-ead4-427b-9655-40fc1602b5df
[root@centos ~]#blkid -U cat /etc/fstab | grep "data" | sed -r 's@.*=(.*) /.*@\1@' 查看data对应的设备名
/dev/sda3

root@CENTOS7 ~]#e2label /dev/sdc1 /data/sdc1 修改卷标名字 只识别ext4
[root@CENTOS7 ~]#e2label /dev/sdc1
/data/sdc1

[root@CENTOS7 ~]#findfs LABEL=/data/sdc1
/dev/sdc1

[root@CENTOS7 ~]#findfs UUID=”4d8d9214-eeed-4758-8c34-f05492b9ea73″
/dev/sda1

tune2fs
tune2fs:重新设定ext系列文件系统可调整参数的值
-l:查看指定文件系统超级块信息;super block
-L ‘LABEL’:修改卷标
-m #:修预留给管理员的空间百分比
-j: 将ext2升级为ext3
-O: 文件系统属性启用或禁用, –O ^has_journal
-o: 调整文件系统的默认挂载选项,–o ^acl
-U UUID: 修改UUID号
dumpe2fs:
块分组管理,32768块
-h:查看超级块信息,不显示分组信息

/dev/sdc1: LABEL=”/data/sdc1″ UUID=”c2188236-fd12-4348-b372-7ee9003ae378″ TYPE=”ext4″
/dev/sdc2: UUID=”60be96b2-617c-4a8d-90f4-4f6fe194333e” TYPE=”ext4″
[root@CENTOS7 ~]#tune2fs -U uuidgen /dev/sdc1 修改UUID,一般不要修改
tune2fs 1.42.9 (28-Dec-2013)
[root@CENTOS7 ~]#blkid
/dev/sdb1: UUID=”c837ab44-edcb-43af-b4fa-c26832a7af7e” TYPE=”ext4″
/dev/sdb3: UUID=”8eeacb99-2062-4604-89c5-6518d832bec8″ TYPE=”ext2″
/dev/sda1: UUID=”4d8d9214-eeed-4758-8c34-f05492b9ea73″ TYPE=”xfs”
/dev/sda2: UUID=”0a1bc23e-06e5-4210-9b32-0edbff09ca1a” TYPE=”xfs”
/dev/sda3: UUID=”fb669d84-551f-4a70-a11e-f61deec0fd86″ TYPE=”xfs”
/dev/sda5: UUID=”906330a5-2af1-4bf4-8b2d-9337eaf92250″ TYPE=”swap”
/dev/sr0: UUID=”2017-09-06-10-53-42-00″ LABEL=”CentOS 7 x86_64″ TYPE=”iso9660″ PTTYPE=”dos”
/dev/sdc1: LABEL=”/data/sdc1″ UUID=”ad468b62-a818-493d-911f-4f8a871986b9″ TYPE=”ext4″

文件系统检测和修复
常发生于死机或者非正常关机之后
挂载为文件系统标记为“no clean”
注意:一定不要在挂载状态下修复
fsck: File System Check
fsck.FS_TYPE
fsck -t FS_TYPE
-p: 自动修复错误
-r: 交互式修复错误
FS_TYPE一定要与分区上已经文件类型相同
e2fsck:ext系列文件专用的检测修复工具
-y:自动回答为yes
-f:强制修复

[root@CENTOS7 ~]#dd if=/dev/zero of=/dev/sdc1 bs=1M count=20 破坏一个文件系统
20+0 records in
20+0 records out
20971520 bytes (21 MB) copied, 0.186818 s, 112 MB/s
[root@CENTOS7 ~]#tune2fs -l /dev/sdc1 已经破坏无法查看
tune2fs 1.42.9 (28-Dec-2013)
tune2fs: Bad magic number in super-block while trying to open /dev/sdc1
Couldn’t find valid filesystem superblock.
[root@CENTOS7 ~]#fsck /dev/sdc1 -y 修复
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? yes

*** ext3 journal has been deleted – filesystem is now ext2 only ***

/data/sdc1 was not cleanly unmounted, check forced.
Resize inode not valid. Recreate? yes

Pass 1: Checking inodes, blocks, and sizes
Root inode is not a directory. Clear? yes

Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Root inode not allocated. Allocate? yes

/lost+found not found. Create? yes

Pass 4: Checking reference counts
Pass 5: Checking group summary information
Block bitmap differences: +(0–136) +(145–152) +(161–4257) +(32768–32896) +(98304–98432) +(163840–163968) +(229376–229504)
Fix? yes

Free blocks count wrong for group #0 (28519, counted=28524).
Fix? yes

Free blocks count wrong for group #4 (24576, counted=32768).
Fix? yes

Free blocks count wrong (249187, counted=257384).
Fix? yes

Inode bitmap differences: +1 +(3–10)
Fix? yes

Free inodes count wrong for group #0 (8180, counted=8181).
Fix? yes

Directories count wrong for group #0 (3, counted=2).
Fix? yes

Free inodes count wrong (65524, counted=65525).
Fix? yes

Recreate journal? yes

Creating journal (8192 blocks): Done.

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

/data/sdc1: ***** FILE SYSTEM WAS MODIFIED *****
/data/sdc1: 11/65536 files (0.0% non-contiguous), 12952/262144 blocks
[root@CENTOS7 ~]#tune2fs -l /dev/sdc1 修复成功显示为clean
tune2fs 1.42.9 (28-Dec-2013)
Filesystem volume name: /data/sdc1
Last mounted on: <not available>
Filesystem UUID: ad468b62-a818-493d-911f-4f8a871986b9
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: has_journal ext_attr resize_inode dir_index filetype extent 64bit flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
Filesystem flags: signed_directory_hash
Default mount options: user_xattr acl
Filesystem state: clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 65536
Block count: 262144
Reserved block count: 13107
Free blocks: 249192
Free inodes: 65525
First block: 0
Block size: 4096
Fragment size: 4096
Group descriptor size: 64
Reserved GDT blocks: 127
Blocks per group: 32768
Fragments per group: 32768
Inodes per group: 8192
Inode blocks per group: 512
Flex block group size: 16
Filesystem created: Mon Apr 23 20:11:09 2018
Last mount time: n/a
Last write time: Mon Apr 23 20:46:23 2018
Mount count: 0
Maximum mount count: -1
Last checked: Mon Apr 23 20:46:23 2018
Check interval: 0 (<none>)
Lifetime writes: 65 MB
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)
First inode: 11
Inode size: 256
Required extra isize: 28
Desired extra isize: 28
Journal inode: 8
Default directory hash: half_md4
Directory Hash Seed: bfa8a27e-378a-4cdc-8184-6a2dd5f92ac6
Journal backup: inode blocks

[root@CENTOS7 ~]#e2fsck -f /dev/sdc1 强制检查全盘文件系统
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/data/sdc1: 11/65536 files (0.0% non-contiguous), 12952/262144 blocks

在centos7上修复文件系统用的命令是

xfs_repair

[root@CENTOS7 ~]#xfs_ 双击Tan查看觉得其他功能
xfs_admin xfs_db xfs_fsr xfs_io xfs_metadump xfs_quota
xfs_bmap xfs_estimate xfs_growfs xfs_logprint xfs_mkfile xfs_repair
xfs_copy xfs_freeze xfs_info xfs_mdrestore xfs_ncheck xfs_rtcp

[root@CENTOS7 ~]#xfs_info /dev/sda3 显示信息 但是必须是挂载状态才能查询
meta-data=/dev/sda3 isize=512 agcount=4, agsize=1966080 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=7864320, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=3840, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0

挂载mount
挂载:将额外文件系统与根文件系统某现存的目录建立起关联关系,进而使得此
目录做为其它文件访问入口的行为
卸载:为解除此关联关系的过程
把设备关联挂载点:mount Point
mount
卸载时:可使用设备,也可以使用挂载点
umount
挂载点下原有文件在挂载完成后会被临时隐藏
挂载点目录一般为空
一个挂载点不能挂载多个设备。作为挂载点最好是空文件夹,如果不是空里面的文件会被挂载的设备遮盖导致无法查看,但是文件没有消失当取消挂载的时候还会显示出来。一个设备可以挂载在多个挂载点上,挂载点显示的文件内容是一样的(比如光盘挂载在/misc和/run),有点像硬链接,但是连接数并不增多

用mount命令挂载文件系统
挂载方法:mount DEVICE MOUNT_POINT
mount:通过查看/etc/mtab文件显示当前已挂载的所有设备
mount [-fnrsvw] [-t vfstype] [-o options] device dir
device:指明要挂载的设备;
(1) 设备文件:例如/dev/sda5
(2) 卷标:-L ‘LABEL’, 例如 -L ‘MYDATA’
(3) UUID, -U ‘UUID’:例如 -U ‘0c50523c-43f1-45e7-
85c0-a126711d406e’
(4) 伪文件系统名称:proc, sysfs, devtmpfs, configfs
dir:挂载点
事先存在;建议使用空目录
进程正在使用中的设备无法被卸载

[root@CENTOS7 data]#mkdir sdc1

[root@CENTOS7 ~]#mount /dev/sdc1 /data/sdc1

[root@CENTOS7 ~]#df

dev/sdc1 999320 2552 927956 1% /data/sdc1

[root@CENTOS7 ~]#mount

/dev/sdc1 on /data/sdc1 type ext4 (rw,relatime,seclabel,data=ordered)

[root@CENTOS7 ~]#ll /etc/mtab 挂载的信息存放的地方
lrwxrwxrwx. 1 root root 17 Apr 19 20:35 /etc/mtab -> /proc/self/mounts

[root@CENTOS7 ~]#umount /dev/sdc1 取消挂载,可以写设备名,卷标
[root@CENTOS7 ~]#df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 52403200 3898416 48504784 8% /
devtmpfs 535296 0 535296 0% /dev
tmpfs 550036 0 550036 0% /dev/shm
tmpfs 550036 7964 542072 2% /run
tmpfs 550036 0 550036 0% /sys/fs/cgroup
/dev/sda3 31441920 161500 31280420 1% /data
/dev/sda1 1038336 161620 876716 16% /boot
tmpfs 110008 24 109984 1% /run/user/0
/dev/sr0 8490330 8490330 0 100% /run/media/root/CentOS 7 x86_64
tmpfs 110008 0 110008 0% /run/user/1000

[root@CENTOS7 ~]#umount LABEL=/data/sdc1

[root@CENTOS7 ~]#mount LABEL=/data/sdc1 /data/sdc1

[root@CENTOS7 ~]#mkdir /data/sdc11
[root@CENTOS7 ~]#mount /dev/sdc1 /data/sdc11
[root@CENTOS7 ~]#cd /data/sdc1
[root@CENTOS7 sdc1]#touch f1
[root@CENTOS7 sdc1]#ls
f1 lost+found
[root@CENTOS7 sdc1]#cd
[root@CENTOS7 ~]#cd /data/sdc11
[root@CENTOS7 sdc11]#ls
f1 lost+found

[root@CENTOS7 sdc1]#mount /dev/sdc2 /data/sdc1 多个设备挂载到同一个文件夹

[root@CENTOS7 sdc1]#ls
f1 lost+found
[root@CENTOS7 sdc1]#cd ..
[root@CENTOS7 data]#ls ./sdc1
lost+found

普通用户没有权限挂载,也没有权限卸载挂载

[root@CENTOS7 data]#mount -r /dev/sdc2 /data/sdc2 只读
[root@CENTOS7 data]#mount

/dev/sdc2 on /data/sdc2 type ext4 (ro,

[root@CENTOS7 sdc2]#mount -t ext2 /dev/sdc2 /data/sdc2
已ext2文件系统去挂载,除非有必要,不然系统会自动识别文件系统类型,不需要指明

mount常用命令选项
-t vsftype:指定要挂载的设备上的文件系统类型
-r: readonly,只读挂载
-w: read and write, 读写挂载 默认就是可读可写
-n: 不更新/etc/mtab,mount不可见
-a:自动挂载所有支持自动挂载的设备(定义在了/etc/fstab文件中,且挂载选
项中有auto功能)
-L ‘LABEL’: 以卷标指定挂载设备
-U ‘UUID’: 以UUID指定要挂载的设备
-B, –bind: 绑定目录到另一个目录上
查看内核追踪到的已挂载的所有设备
cat /proc/mounts

mount -n /dev/sdc2 /data/sdc2 只在centos6上好用

df cat/dtc/mtab mount查看不到,但是cat/proc/mounts可以查看

[root@CENTOS7 dir]#mount -B /data/dir /data/sdc2
[root@CENTOS7 dir]#touch /data/sdc2/ff
[root@CENTOS7 dir]#ls /data/dir
ff

mount常用命令选项
-o options:(挂载文件系统的选项),多个选项使用逗号分隔
async:异步模式
sync:同步模式,内存更改时,同时写磁盘
atime/noatime:包含目录和文件
diratime/nodiratime:目录的访问时间戳
auto/noauto:是否支持自动挂载,是否支持-a选项
exec/noexec:是否支持将文件系统上运行应用程序
dev/nodev:是否支持在此文件系统上使用设备文件
suid/nosuid:是否支持suid和sgid权限
remount:重新挂载
ro:只读
rw:读写
user/nouser:是否允许普通用户挂载此设备,/etc/fstab使用
acl:启用此文件系统上的acl功能
loop: 使用loop设备
defaults:相当于rw, suid, dev, exec, auto, nouser, async

Mount count: 4 当达到20次时会检查
Maximum mount count: 20
Check interval: 0 (<none>) 到达指定时间检查

[root@CENTOS7 ~]#tune2fs -c 20 /dev/sdc2 调整最大挂载次数,-C调整当前挂载次数
tune2fs 1.42.9 (28-Dec-2013)
Setting maximal mount count to 20

tune2fs -o acl /dev/sdc2 添加 acl权限
tune2fs -o ^acl /dev/sdc2 取消acl权限都需要从新挂载才能生效

mount -o acl /dev/sdc2 /data/sdc2 也可以添加acl权限

mount -o noacl,remount /dev/sdc2 /data/sdc2 不用卸载挂载,直接修改acl权限

卸载命令
查看挂载情况
findmnt MOUNT_POINT|device
查看正在访问指定文件系统的进程
lsof MOUNT_POINT
fuser -v MOUNT_POINT
终止所有在正访问指定的文件系统的进程
fuser -km MOUNT_POINT
卸载
umount DEVICE
umount MOUNT_POINT

[root@centos ~]#fuser -v /data/sdb1 查看谁在使用
USER PID ACCESS COMMAND
/data/sdb1: root 3543 ..c.. bash

[root@centos ~]#fuser -km /data/sdb1 结束所有正在访问这个文件夹的进程
/data/sdb1: 3543c

[root@centos ~]#lsof /data/sdb1 查看谁在使用这个文件夹
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
bash 3574 root cwd DIR 8,17 4096 2 /data/sdb1

root@centos ~]#findmnt /dev/sdb1
TARGET SOURCE FSTYPE OPTIONS
/data/sdb1 /dev/sdb1 ext4 rw,relatime,seclabel,barrier=1,data=ordered

[root@centos ~]#findmnt /dev/sdb2 查看一个设备是否被挂载
[root@centos ~]#

[root@centos ~]#findmnt /data/sdb2 &>/dev/null || mount /dev/sdb2 /data/sdb2
[root@centos ~]#df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 50264772 5516120 42188652 12% /
tmpfs 502056 76 501980 1% /dev/shm
/dev/sda1 999320 34944 911948 4% /boot
/dev/sda3 30106576 99960 28470616 1% /data
/dev/sr0 3878870 3878870 0 100% /media/CentOS_6.9_Final
/dev/sdb1 1010780 1304 956464 1% /data/sdb1
/dev/sdb2 2005880 3096 1897560 1% /data/sdb2

[root@centos ~]#dd if=/dev/zero of=/root/p1 bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 4.87694 s, 21.5 MB/s
[root@centos ~]#mkfs.ext4 /root/p1
mke2fs 1.41.12 (17-May-2010)
/root/p1 is not a block special device.
Proceed anyway? (y,n) y
Filesystem label=
OS type: Linux

[root@centos ~]#mount /root/p1 /data/mm
mount: /root/p1 is not a block device (maybe try -o loop'?)
[root@centos ~]#mount -o loop /root/p1 /data/mm 将一个文件模拟一个分区使用
[root@centos ~]#touch /data/mm/f1 在这个目录下创建的文件都放在p1里面
想把p1里的所有文件移走直接卸载挂载,远程复制到别的机器上,然后在挂载就可以将里面的文件导出来

[root@centos ~]#losetup -a 查看所有loop设备
/dev/loop0: [0802]:3014775 (/root/p1)

[root@centos ~]#ls /dev/loop*
/dev/loop0 /dev/loop1 /dev/loop2 /dev/loop3 /dev/loop4 /dev/loop5 /dev/loop6 /dev/loop7

[root@centos ~]#mknod /dev/loop8 b 7 8
[root@centos ~]#ls /dev/loop*
/dev/loop0 /dev/loop2 /dev/loop4 /dev/loop6 /dev/loop8
/dev/loop1 /dev/loop3 /dev/loop5 /dev/loop7
[root@centos ~]#cp p1 p2
[root@centos ~]#blkid p1 p2
p1: UUID="7871347f-da18-4f67-9759-503c43a9cf76" TYPE="ext4"
p2: UUID="7871347f-da18-4f67-9759-503c43a9cf76" TYPE="ext4"

[root@centos ~]#losetup /dev/loop8 p2
[root@centos ~]#mkdir /data/loop8; mount /dev/loop8 /data/loop8
[root@centos ~]#df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 50264772 5720948 41983824 12% /
tmpfs 502056 76 501980 1% /dev/shm
/dev/sda1 999320 34944 911948 4% /boot
/dev/sda3 30106576 99968 28470608 1% /data
/dev/sr0 3878870 3878870 0 100% /media/CentOS_6.9_Final
/dev/sdb1 1010780 1304 956464 1% /data/sdb1
/dev/sdb2 2005880 3096 1897560 1% /data/sdb2
/root/p1 95054 1551 88383 2% /data/mm
/dev/loop8 95054 1551 88383 2% /data/loop8
[root@centos ~]#ls /data/loop8
f1 lost+found

[root@centos ~]#vim /boot/grub/grub.conf 从新启动生效

default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS 6 (2.6.32-696.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-696.el6.x86_64 ro root=UUID=408b65d7-7551-480b-8916-5eff4cca1b00 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet max_loop=100 使loop自动生成100个
initrd /initramfs-2.6.32-696.el6.x86_64.img

平时使用mount挂载,是在内存中挂载,exit不会丢失,重启会丢失

挂载点和/etc/fstab
配置文件系统体系
被mount、fsck和其它程序使用
系统重启时保留文件系统体系
可以在设备栏使用文件系统卷标
使用mount -a 命令挂载/etc/fstab中的所有文件系统

修改配置文件使启动后自动挂载
[root@centos ~]#vim /etc/fstab
UUID=49002741-38ea-4585-9ef9-3f85cb99ae2c /data/sdb2 ext4 acl,nosuid 0 3
uuid 挂载点 文件类型 默认权限 几天备份(需要配合备份命令) 启动时按顺序检查文件完整性
mount -a 不用重启直接生效

在配置文件中新加入的项可以用mount -a,已经生效的项想要修改不能直接使用mount -a
可以先卸载挂载然后在mount -a 或者 mount -o remount /data/sdb2

[root@centos ~]#tune2fs -U uuidgen /dev/sdb2 修改uuid,没有写入配置文件中,重启
tune2fs 1.41.12 (17-May-2010)
系统无法正常启动
输入root口令
vim /etc/fatab 发现是只读模式。
mount -o remount,rw / 将/重新挂载为rw模式
vim /etc/fatab 将错误行的最后一项3改成0.
重新启动
已经可以启动
连接xshell修改配置文件,改成正确的uuid,mount -a

想把/home分到一个独立的分区上
1.备份
2.找一个大的硬盘分区,这个分区的大小要提前规划好,一点分区完成无法修改
fdisk /dev/sdc
3.创建文件系统
mkfs.ext4 /dev/sdc1 -L /home
4.临时创建一个文件夹,将/dev/sdc1挂载上去
mkdir /mnt/home ;mount /dev/sdc1 /mnt/home
5.发通知告知所有用户不要登录,切换到单用户模式init 1
6.cp -a /home/* /mnt/home/中 确保所有文件都已经复制完成
7.rm -rf /home/*
8.vim /etc/fstab 添加/dev/sdc1的uuid 卷标,文件系统 默认项 0 0
9.mount -a
10.init 5
11.umount /mnt/home
12.rm -rf /mnt/home
根目录无法迁移到别的硬盘以为根一直在被访问

文件挂载配置文件
/etc/fstab每行定义一个要挂载的文件系统
要挂载的设备或伪文件系统
设备文件
LABEL:LABEL=""
UUID:UUID=""
伪文件系统名称:proc, sysfs
挂载点
文件系统类型
挂载选项:defaults
转储频率:0:不做备份 1:每天转储 2:每隔一天转储
自检次序: 0:不自检
1:首先自检;一般只有rootfs才用1

特殊挂载关系
1.用文件当分区用 /root/p1 /mnt/p1 ext4 loop 0 0
2.挂载光胖 /dev/sr0 /mnt/cdrom iso9660 0 0
文件夹挂载文件夹 /boot /mnt/boot none bind 0 0

挂载交换分区
启用:swapon
swapon [OPTION]... [DEVICE]
-a:激活所有的交换分区
-p PRIORITY:指定优先级
/etc/fstab:pri=value
禁用:swapoff [OPTION]... [DEVICE]

SWAP的优先级
可以指定swap分区0到32767的优先级,值越大优先级越高
如果用户没有指定,那么核心会自动给swap指定一个优先级,这个优先级从
-1开始,每加入一个新的没有用户指定优先级的swap,会给这个优先级减一
先添加的swap的缺省优先级比较高,除非用户自己指定一个优先级,而用户
指定的优先级(是正数)永远高于核心缺省指定的优先级(是负数)
优化性能:分布存放,高性能磁盘存放

free -m 查看内存和swap

root@centos ~]#free -m
total used free shared buffers cached
Mem: 980 338 641 2 8 44
-/+ buffers/cache: 285 694
Swap: 2047 0 2047

让swap使用一下 dd if=/dev/zero of=/dev/null bs=1024M count=1
现在新加入一个内存条,使内存变成2G,这是swap需要变成4G
想使swap变成4个G有两个方法
1.删除原来的swap在分化成4个G,但是如果swap正在被使用会使数据丢失
2.在添加一个2个G得swap,这个swap最好用一个快速的磁盘,或者用机械磁盘的外圈

[root@centos ~]#free -h
total used free shared buffers cached
Mem: 2.0G 361M 1.6G 2.4M 8.5M 53M
-/+ buffers/cache: 300M 1.7G
Swap: 2.0G 0B 2.0G

[root@centos ~]#fdisk /dev/sdc

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-10443, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-10443, default 10443): +2G

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 82
Changed system type of partition 1 to 82 (Linux swap / Solaris)

Command (m for help): p

Disk /dev/sdc: 85.9 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 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: 0xe72db920

Device Boot Start End Blocks Id System
/dev/sdc1 1 262 2104483+ 82 Linux swap / Solaris

[root@centos ~]#mkswap /dev/sdc1 -L swap_sdc1
Setting up swapspace version 1, size = 2104476 KiB
LABEL=swap_sdc1, UUID=ca1c9c9d-7ed6-45b3-89a4-e8ff36548d0b

[root@centos ~]#vim /etc/fstab

UUID=ca1c9c9d-7ed6-45b3-89a4-e8ff36548d0b swap swap defaults 0 0

[root@centos ~]#swapon -a

[root@centos ~]#free -h
total used free shared buffers cached
Mem: 2.0G 385M 1.6G 2.4M 9.2M 56M
-/+ buffers/cache: 319M 1.6G
Swap: 4.0G 0B 4.0G

[root@centos ~]#swapon -s 查看swap 优先级 数字越大优先越高
Filename Type Size Used Priority
/dev/sda5 partition 2097148 0 -1
/dev/sdc1 partition 2104476 0 -2

修改swap的优先级
[root@centos ~]#vim /etc/fstab

UUID=ca1c9c9d-7ed6-45b3-89a4-e8ff36548d0b swap swap defaults,pri=100 0 0

[root@centos ~]#swapon -s
Filename Type Size Used Priority
/dev/sda5 partition 2097148 0 -1
/dev/sdc1 partition 2104476 0 -2
[root@centos ~]#swapoff /dev/sdc1
[root@centos ~]#swapon -a
[root@centos ~]#swapon -s
Filename Type Size Used Priority
/dev/sda5 partition 2097148 0 -1
/dev/sdc1 partition 2104476 0 100

用文件做swap用
dd if=/dev/zero of=/swapfile bs=1024M count=2

mkswap /swapfile

vim /etc/fstab
/swapfile swap swap defaults 0 0

swapon -a

查看swap
swapon -s =cat /proc/swaps

删除swap
swapoff /dev/sdc1
swapoff /swapfile
删除vim /etc/fstab中相应的行
rm -rf /swapfile
fdisk /dev/sdc
d
1
w

使用光盘
在图形环境下自动启动挂载/run/media/<user>/<label>
否则就必须被手工挂载
mount /dev/cdrom /mnt/
eject命令卸载或弹出磁盘 弹入eject -t
创建ISO文件
cp /dev/cdrom /root/centos7.iso
mkisofs -r -o /root/etc.iso /etc
刻录光盘
wodim –v –eject centos.iso

cp /dev/cdrom /root/centos7.iso制作一个ios文件
dd if=/dev/sr0 of=/root/centos7.iso

也可以将文件夹制作成iso文件

mkisofs -r -o etc.ios /etc/

挂载USB介质
查看USB设备是否识别
lsusb
被内核探测为SCSI设备
/dev/sdaX、/dev/sdbX、或类似的设备文件
在图形环境中自动挂载
图标在[计算机]窗口中创建
挂载在/run/media/<user>/<label>
手动挂载
mount /dev/sdb1 /mnt

tail -f /var/log/messages插入U盘时跟中
挂载U盘的时候 往U盘中传输文件需要多敲几次 ,防止数据放在buffs中没有传过去
sync;sync;sync

常见工具
文件系统空间占用等信息的查看工具:
df [OPTION]... [FILE]...
-H 以1000为单位
-T 文件系统类型
-h: human-readable
-i:inodes instead of blocks
-P: 以Posix兼容的格式输出 当设备名过长时自动对齐
查看某目录总体空间占用状态:
du [OPTION]... DIR
-h: human-readable
-s: summary --max-depth

[root@centos ~]#du -sh /*
27M /app
7.7M /bin
33M /boot
54M /data
704K /dev
42M /etc
672K /home
209M /lib
28M /lib64
16K /lost+found
5.8G /media
0 /misc
4.0K /mnt
0 /net
8.0K /opt
du: cannot access
/proc/4149/task/4149/fd/4′: No such file or directory
du: cannot access /proc/4149/task/4149/fdinfo/4': No such file or directory
du: cannot access
/proc/4149/fd/4′: No such file or directory
du: cannot access /proc/4149/fdinfo/4': No such file or directory
0 /proc
286M /root
17M /sbin
0 /selinux
4.0K /srv
0 /sys
1.4M /tmp
4.1G /usr
823M /var

[root@centos ~]#du -h --max-depth 0 /etc
42M /etc

工具dd
dd命令:convert and copy a file
用法:
dd if=/PATH/FROM/SRC of=/PATH/TO/DEST
bs=#:block size, 复制单元大小
count=#:复制多少个bs
of=file 写到所命名的文件而不是到标准输出
if=file 从所命名文件读取而不是从标准输入
bs=size 指定块大小(既是是ibs也是obs)
ibs=size 一次读size个byte
obs=size 一次写size个byte
cbs=size 一次转化size个byte
skip=blocks 从开头忽略blocks个ibs大小的块
seek=blocks 从开头忽略blocks个obs大小的块
count=n 只拷贝n个记录

工具dd
conv=conversion[,conversion...] 用指定的参数转换文件
转换参数:
ascii 转换 EBCDIC 为 ASCII
ebcdic 转换 ASCII 为 EBCDIC
lcase 把大写字符转换为小写字符
ucase 把小写字符转换为大写字符
nocreat 不创建输出文件
noerror 出错时不停止
notrunc 不截短输出文件
sync 把每个输入块填充到ibs个字节,不足部分用空(NUL)字
符补齐

[root@centos data]#cat f2
abcdefgh

[root@centos data]#dd if=f1 of=f2
0+1 records in
0+1 records out
7 bytes (7 B) copied, 0.000258348 s, 27.1 kB/s
[root@centos data]#cat f2 截断模式,后面的统统干掉
123456
设备文件不截断

[root@centos data]#dd if=f1 of=f2 bs=1 count=2 skip=3 seek=4 conv=notrunc
2+0 records in
2+0 records out
2 bytes (2 B) copied, 0.000281923 s, 7.1 kB/s
[root@centos data]#cat f2
abcd45gh

[root@centos data]#dd if=f2 of=f3 conv=ucase
0+1 records in
0+1 records out
9 bytes (9 B) copied, 0.000288186 s, 31.2 kB/s
[root@centos data]#cat f3
ABCD45GH

工具dd
备份MBR:
dd if=/dev/sda of=/tmp/mbr.bak bs=512 count=1
破坏MBR中的bootloader:
dd if=/dev/zero of=/dev/sda bs=64 count=1 seek=446
有一个大与2K的二进制文件fileA。现在想从第64个字节位置开始读取,需要读
取的大小是128Byts。又有fileB, 想把上面读取到的128Bytes写到第32个字节开
始的位置,替换128Bytes,实现如下:
dd if=fileA of=fileB bs=1 count=128 skip=63 seek=31 conv=notrunc

工具dd
备份:
dd if=/dev/sdx of=/dev/sdy sdy硬盘一定比sdx大
将本地的/dev/sdx整盘备份到/dev/sdy
dd if=/dev/sdx of=/path/to/image
将/dev/sdx全盘数据备份到指定路径的image文件
dd if=/dev/sdx | gzip >/path/to/image.gz
备份/dev/sdx全盘数据,并利用gzip压缩,保存到指定路径
恢复:
dd if=/path/to/image of=/dev/sdx
将备份文件恢复到指定盘
gzip -dc /path/to/image.gz | dd of=/dev/sdx
将压缩的备份文件恢复到指定盘

工具dd
拷贝内存资料到硬盘
dd if=/dev/mem of=/root/mem.bin bs=1024
将内存里的数据拷贝到root目录下的mem.bin文件
从光盘拷贝iso镜像
dd if=/dev/cdrom of=/root/cd.iso
拷贝光盘数据到root文件夹下,并保存为cd.iso文件
销毁磁盘数据
dd if=/dev/urandom of=/dev/sda1
利用随机的数据填充硬盘,在某些必要的场合可以用来销毁数据,执行此操作以后,
/dev/sda1将无法挂载,创建和拷贝操作无法执行

工具dd
得到最恰当的block size
dd if=/dev/zero bs=1024 count=1000000 of=/root/1Gb.file
dd if=/dev/zero bs=2048 count=500000 of=/root/1Gb.file
dd if=/dev/zero bs=4096 count=250000 of=/root/1Gb.file
通过比较dd指令输出中命令的执行时间,即可确定系统最佳的block size大小

[root@CENTOS7 ~]#dd if=/dev/zero of=f1 bs=1024 count=1024000
1024000+0 records in
1024000+0 records out
1048576000 bytes (1.0 GB) copied, 27.7282 s, 37.8 MB/s
[root@CENTOS7 ~]#dd if=/dev/zero of=f1 bs=2048 count=512000
512000+0 records in
512000+0 records out
1048576000 bytes (1.0 GB) copied, 28.0448 s, 37.4 MB/s
[root@CENTOS7 ~]#dd if=/dev/zero of=f1 bs=4k count=256000
256000+0 records in
256000+0 records out
1048576000 bytes (1.0 GB) copied, 14.9327 s, 70.2 MB/s

工具dd
修复硬盘
dd if=/dev/sda of=/dev/sda
当硬盘较长时间(比如1,2年)放置不使用后,磁盘上会产生消磁点。当磁头读
到这些区域时会遇到困难,并可能导致I/O错误。当这种情况影响到硬盘的第一
个扇区时,可能导致硬盘报废。上边的命令有可能使这些数据起死回生,且这个
过程是安全高效的

什么是RAID
RAID:Redundant Arrays of Inexpensive(Independent) Disks
1988年由加利福尼亚大学伯克利分校(University of California-
Berkeley) “A Case for Redundant Arrays of Inexpensive Disks”
多个磁盘合成一个“阵列”来提供更好的性能、冗余,或者两者都提供

RAID
提高IO能力
磁盘并行读写
提高耐用性
磁盘冗余来实现
级别:多块磁盘组织在一起的工作方式有所不同
RAID实现的方式:
外接式磁盘阵列:通过扩展卡提供适配能力
内接式RAID:主板集成RAID控制器
安装OS前在BIOS里配置
软件RAID:通过OS实现

RAID级别
RAID-0:
读、写性能提升;
可用空间:N*min(S1,S2,...)
无容错能力
最少磁盘数:2, 2+

RAID0又叫条带集 假设有3块硬盘,已3块硬盘中最小的一个为准假设是100G,那么通过raid0就组成了一个300G的大硬盘,然后对这个300G的硬盘分区创建文件系统。
挂载。在挂载文件夹中创建一个100M的文件,将100M的文件分解成chunk,chunk的大小可以指定
假设是256k。
存储时是1 2 3
4 5 6
......
读写速度快,但是没有冗余,一但有一块硬盘坏了,数据就被破坏了

RAID4 最少3块硬盘才能实现
假设有3块硬盘,已3块硬盘中最小的一个为准假设是100G,那么通过raid0就组成了一个300G的大硬盘,然后对这个300G的硬盘分区创建文件系统。
挂载。在挂载文件夹中创建一个100M的文件,将100M的文件分解成chunk,chunk的大小可以指定
假设是256k。
存储时是1 2 x x是校验位,x1^2
3 4 x
.......
读写速度变快,一但有一块硬盘坏了,可以继续工作,但是由于校验位运算太频繁导致经常损坏

RAID5 最少3块硬盘才能实现

假设有3块硬盘,已3块硬盘中最小的一个为准假设是100G,那么通过raid0就组成了一个300G的大硬盘,然后对这个300G的硬盘分区创建文件系统。
挂载。在挂载文件夹中创建一个100M的文件,将100M的文件分解成chunk,chunk的大小可以指定
假设是256k。
存储时是1 2 3 x x是校验位,x1^2^3
4 5 x 6
7 x 8 9
x 10 11 12
.............
读写速度变快,一但有一块硬盘坏了,可以继续工作,一但有一块硬盘坏了,会导致服务器负载急剧增大,可能导致系统崩溃
所以一但有硬盘坏了要迅速更换.有一块备用硬盘(spare disk)平时监视raid,一但有硬盘损坏则自己替换。

RAID6 最少4块硬盘才能实现

假设有3块硬盘,已3块硬盘中最小的一个为准假设是100G,那么通过raid0就组成了一个300G的大硬盘,然后对这个300G的硬盘分区创建文件系统。
挂载。在挂载文件夹中创建一个100M的文件,将100M的文件分解成chunk,chunk的大小可以指定
假设是256k。

存储时是1 2 x x x是校验位,
3 x x 4 x的算法a+b=c
x x 5 6 na+mb=d
x 7 8 x

RAID-1:
读性能提升、写性能略有下降;
可用空间:1*min(S1,S2,...)
有冗余能力
最少磁盘数:2, 2N

就是两两互为镜像. 一但删除文件无法恢复

RAID级别
JBOD:Just a Bunch Of Disks
功能:将多块磁盘的空间合并一个大的连续空间使用
可用空间:sum(S1,S2,...)
就是把几块硬盘组合在一起当一个大的硬盘使用,假设 1 2 3个
硬盘,1存满在存2.

硬raid组合的硬盘在linux中只能看到一块sda,别的看不到
软raid可以看到有几块硬盘生成的设备名通常为/dev/md#

RAID级别
RAID7
可以理解为一个独立存储计算机,自身带有操作系统和管理工具,可以独立
运行,理论上性能最高的RAID模式
常用级别:
RAID-0, RAID-1, RAID-5, RAID-10, RAID-50, JBOD

软RAID
mdadm:为软RAID提供管理界面
为空余磁盘添加冗余
结合内核中的md(multi devices)
RAID设备可命名为/dev/md0、/dev/md1、/dev/md2、/dev/md3等
系统默认没有raid

软RAID
  mdadm:为软RAID提供管理界面
  为空余磁盘添加冗余
  结合内核中的md(multi devices)
  RAID设备可命名为/dev/md0、/dev/md1、/dev/md2、/dev/md3等
如果硬盘只分出了一个分区,可以用
dd if=/dev/sdb of=/dev/sdc bs=1 count=66 skip=446 seek=446
dd if=/dev/sdb of=/dev/sdd bs=1 count=66 skip=446 seek=446
dd if=/dev/sdb of=/dev/sde bs=1 count=66 skip=446 seek=446
raid5的实现
实验:raid5
1 分区(fd),磁盘 大小一致
2 mdadm -C /dev/md0 -a yes -l5 -n 3 -x1 -c32 /dev/sd{b,c,d,e}1
3 mkfs.ext4 /dev/md0
4 vim /etc/fstab
5 mdadm -Ds /dev/md0 > /etc/mdadm.conf
6 mdadm -S /dev/md0 停止(umount)
7 mdadm -A /dev/md0 激活
8 mdadm /dev/md0 -f /dev/sde1 损坏
9 mdadm /dev/md0 -r /dev/sde1 删除
10 mdadm /dev/md0 -a /dev/sde1 加
dd if=/dev/zero of=/dev/sdb1 清空文件系统
[root@centos ~]#mdadm -C /dev/md0 -a yes(自动初始化) -l5(raid5) -c(chunk大小) 32 -n(几块硬盘) 3 -x(空闲硬盘) 1 /dev/sdb4 /dev/sdc2 /dev/sdd1 /dev/sde1
创建md0时不能超过现在机器的物理磁盘的容量
[root@centos ~]#mdadm -D /dev/md0   查看raid  等同于 mdadm --detail /dev/md0
/dev/md0:
        Version : 1.2
  Creation Time : Wed Apr 25 18:42:03 2018
     Raid Level : raid5
     Array Size : 4204864 (4.01 GiB 4.31 GB)
  Used Dev Size : 2102432 (2.01 GiB 2.15 GB)
   Raid Devices : 3
  Total Devices : 4
    Persistence : Superblock is persistent
    Update Time : Wed Apr 25 18:42:14 2018
          State : clean
 Active Devices : 3
Working Devices : 4
 Failed Devices : 0
  Spare Devices : 1
         Layout : left-symmetric
     Chunk Size : 32K
           Name : centos.localdomain:0  (local to host centos.localdomain)
           UUID : 334cca96:877322f5:7cd0110c:afdf35bf
         Events : 18
    Number   Major   Minor   RaidDevice State
       0       8       20        0      active sync   /dev/sdb4
       1       8       34        1      active sync   /dev/sdc2
       4       8       49        2      active sync   /dev/sdd1
       3       8       65        -      spare   /dev/sde1
[root@centos ~]#blkid
/dev/sda1: UUID="414ab9ee-af40-44f1-b300-d29fc674eb8e" TYPE="ext4"
/dev/sda2: UUID="408b65d7-7551-480b-8916-5eff4cca1b00" TYPE="ext4"
/dev/sda3: UUID="37d5cf25-ead4-427b-9655-40fc1602b5df" TYPE="ext4"
/dev/sda5: UUID="9fc26932-7094-4948-b395-24ab05d8ea73" TYPE="swap"
/dev/sdb2: UUID="e4c92849-b5b6-4736-a027-25c99f9a9da1" TYPE="ext4"
/dev/sdb1: UUID="309c1b15-1d6d-475a-a077-0caf7634442a" TYPE="ext4"
/dev/sdc1: LABEL="swap_sdc1" UUID="ca1c9c9d-7ed6-45b3-89a4-e8ff36548d0b" TYPE="swap"
/dev/sdb3: LABEL="/data/sdb3" UUID="e7742e5e-f923-4c7e-b43e-ee0952a0045e" TYPE="ext4"
/dev/sdb4: UUID="334cca96-8773-22f5-7cd0-110cafdf35bf" UUID_SUB="396e1715-7f32-a631-46ab-6819b9a95f42" LABEL="centos.localdomain:0" TYPE="linux_raid_member"
/dev/sdc2: UUID="334cca96-8773-22f5-7cd0-110cafdf35bf" UUID_SUB="8534ca6d-ff60-618d-d973-13566069ebd4" LABEL="centos.localdomain:0" TYPE="linux_raid_member"
/dev/sdd1: UUID="334cca96-8773-22f5-7cd0-110cafdf35bf" UUID_SUB="755ca4f0-1a5d-ca6a-086e-866fc3164673" LABEL="centos.localdomain:0" TYPE="linux_raid_member"
/dev/sde1: UUID="334cca96-8773-22f5-7cd0-110cafdf35bf" UUID_SUB="b84b7b36-3b55-6a14-ae42-3e6f1dbf3af5" LABEL="centos.localdomain:0" TYPE="linux_raid_member"
[root@centos ~]#mkfs.ext4 /dev/md0 -L "raid5"   为raid5创建文件系统
mke2fs 1.41.12 (17-May-2010)
Filesystem label=raid5
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=8 blocks, Stripe width=16 blocks
262944 inodes, 1051216 blocks
52560 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1077936128
33 block groups
32768 blocks per group, 32768 fragments per group
7968 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 23 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@centos ~]#blkid /dev/md0
/dev/md0: LABEL="raid5" UUID="d9b419d6-ae30-4c1a-b447-deeb1b5a0111" TYPE="ext4"
[root@centos ~]#mkdir /data/raid
[root@centos ~]#vim /etc/fstab
UUID=d9b419d6-ae30-4c1a-b447-deeb1b5a0111  /data/raid ext4 defaults 0 0
[root@centos ~]#mount -a
[root@centos ~]#df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda2       50264772 5721952  41982820  12% /
tmpfs             953128      76    953052   1% /dev/shm
/dev/sda1         999320   34944    911948   4% /boot
/dev/sda3       30106576   99988  28470588   1% /data
/dev/sr0         6049912 6049912         0 100% /media/CDROM
/dev/md0         4007728    8216   3789272   1% /data/raid
[root@centos ~]#mdadm -Ds /dev/md0   显示raid0的必要信息
ARRAY /dev/md0 metadata=1.2 spares=1 name=centos.localdomain:0 UUID=334cca96:877322f5:7cd0110c:afdf35bf
[root@centos ~]#mdadm -Ds /dev/md0 > /etc/mdadm.conf   将信息写入配置文件中,否则会在raid禁用启用时找不到文件
[root@centos ~]#ll /etc/mdadm.conf
-rw-r--r--. 1 root root 104 Apr 25 18:59 /etc/mdadm.conf
[root@centos ~]#dd if=/dev/zero of=/data/raid/f1 bs=1M count=1024 conv=fdatasync   测试一下写入速度
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 51.6999 s, 20.8 MB/s
停用raid
1.卸载挂载
[root@centos ~]#umount /data/raid
[root@centos ~]#mdadm -S /dev/md0
mdadm: stopped /dev/md0
[root@centos ~]#ll /dev/md0
ls: cannot access /dev/md0: No such file or directory
启用
[root@centos ~]#mdadm -A /dev/md0
mdadm: /dev/md0 has been started with 3 drives and 1 spare.
[root@centos ~]#ll /dev/md0
brw-rw----. 1 root disk 9, 0 Apr 25 19:30 /dev/md0
模拟损坏硬盘
[root@centos ~]#mdadm -f /dev/md0 /dev/sdd1   损坏的是/dev/sdd1
mdadm: set /dev/sdd1 faulty in /dev/md0
[root@centos ~]#mdadm -D /dev/md0
 Number   Major   Minor   RaidDevice State
       0       8       20        0      active sync   /dev/sdb4
       1       8       34        1      active sync   /dev/sdc2
       3       8       65        2      spare rebuilding   /dev/sde1
       4       8       49        -      faulty   /dev/sdd1
移除损坏的硬盘
[root@centos ~]#mdadm -r /dev/md0 /dev/sdd1
mdadm: hot removed /dev/sdd1 from /dev/md0
[root@centos ~]#mdadm -D /dev/md0
/dev/md0:
        Version : 1.2
  Creation Time : Wed Apr 25 18:42:03 2018
     Raid Level : raid5
     Array Size : 4204864 (4.01 GiB 4.31 GB)
  Used Dev Size : 2102432 (2.01 GiB 2.15 GB)
   Raid Devices : 3
  Total Devices : 3
    Persistence : Superblock is persistent
    Update Time : Wed Apr 25 19:39:01 2018
          State : clean
 Active Devices : 3
Working Devices : 3
 Failed Devices : 0
  Spare Devices : 0
         Layout : left-symmetric
     Chunk Size : 32K
           Name : centos.localdomain:0  (local to host centos.localdomain)
           UUID : 334cca96:877322f5:7cd0110c:afdf35bf
         Events : 38
    Number   Major   Minor   RaidDevice State
       0       8       20        0      active sync   /dev/sdb4
       1       8       34        1      active sync   /dev/sdc2
       3       8       65        2      active sync   /dev/sde1
[root@centos ~]#blkid
EL="centos.localdomain:0" TYPE="linux_raid_member"
/dev/sdc2: UUID="334cca96-8773-22f5-7cd0-110cafdf35bf" UUID_SUB="8534ca6d-ff60-618d-d973-13566069ebd4" LABEL="centos.localdomain:0" TYPE="linux_raid_member"
/dev/sdd1: UUID="334cca96-8773-22f5-7cd0-110cafdf35bf" UUID_SUB="755ca4f0-1a5d-ca6a-086e-866fc3164673" LABEL="centos.localdomain:0" TYPE="linux_raid_member"
/dev/sde1: UUID="334cca96-8773-22f5-7cd0-110cafdf35bf" UUID_SUB="b84b7b36-3b55-6a14-ae42-3e6f1dbf3af5" LABEL="centos.localdomain:0" TYPE="linux_raid_member"
/dev/md0: LABEL="raid5" UUID="d9b419d6-ae30-4c1a-b447-deeb1b5a0111" TYPE="ext4"
移除后再加回去,加回去后变成备用硬盘
[root@centos ~]#mdadm -a /dev/md0 /dev/sdd1
mdadm: added /dev/sdd1
[root@centos ~]#mdadm -D /dev/md0
/dev/md0:
        Version : 1.2
  Creation Time : Wed Apr 25 18:42:03 2018
     Raid Level : raid5
     Array Size : 4204864 (4.01 GiB 4.31 GB)
  Used Dev Size : 2102432 (2.01 GiB 2.15 GB)
   Raid Devices : 3
  Total Devices : 4
    Persistence : Superblock is persistent
    Update Time : Wed Apr 25 19:41:42 2018
          State : clean
 Active Devices : 3
Working Devices : 4
 Failed Devices : 0
  Spare Devices : 1
         Layout : left-symmetric
     Chunk Size : 32K
           Name : centos.localdomain:0  (local to host centos.localdomain)
           UUID : 334cca96:877322f5:7cd0110c:afdf35bf
         Events : 39
    Number   Major   Minor   RaidDevice State
       0       8       20        0      active sync   /dev/sdb4
       1       8       34        1      active sync   /dev/sdc2
       3       8       65        2      active sync   /dev/sde1
       4       8       49        -      spare   /dev/sdd1
关机移除备用硬盘和raid中任意一块硬盘,启动后还是可以使用但是速度变慢
[root@centos ~]#df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        48G  5.5G   41G  12% /
tmpfs           931M   76K  931M   1% /dev/shm
/dev/sda1       976M   35M  891M   4% /boot
/dev/sda3        29G   98M   28G   1% /data
/dev/md0        3.9G  9.9M  3.7G   1% /data/raid
/dev/sr0        5.8G  5.8G     0 100% /media/CDROM
[root@centos ~]#cat /data/raid/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
[root@centos ~]#mdadm -D /dev/md0
/dev/md0:
        Version : 1.2
  Creation Time : Wed Apr 25 18:42:03 2018
     Raid Level : raid5
     Array Size : 4204864 (4.01 GiB 4.31 GB)
  Used Dev Size : 2102432 (2.01 GiB 2.15 GB)
   Raid Devices : 3
  Total Devices : 2
    Persistence : Superblock is persistent
    Update Time : Wed Apr 25 20:27:58 2018
          State : clean, degraded    降级使用是特别危险的,需要及时更换硬盘
 Active Devices : 2
Working Devices : 2
 Failed Devices : 0
  Spare Devices : 0
         Layout : left-symmetric
     Chunk Size : 32K
           Name : centos.localdomain:0  (local to host centos.localdomain)
           UUID : 334cca96:877322f5:7cd0110c:afdf35bf
         Events : 41
    Number   Major   Minor   RaidDevice State
       0       8       20        0      active sync   /dev/sdb4
       1       8       34        1      active sync   /dev/sdc2
       4       0        0        4      removed
更换硬盘
先分出一个分区(类型是fd)2G大小
[root@centos ~]#fdisk /dev/sdc
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)
3
Invalid partition number for type 3′
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (525-10443, default 525):
Using default value 525
Last cylinder, +cylinders or +size{K,M,G} (525-10443, default 10443): +2G
Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes): fd
Changed system type of partition 3 to fd (Linux raid autodetect)
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@centos ~]#mdadm -a /dev/md0 /dev/sdc3
mdadm: Cannot find /dev/sdc3: No such file or directory
[root@centos ~]#partx -a /dev/sdc
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
[root@centos ~]#mdadm -a /dev/md0 /dev/sdc3
mdadm: added /dev/sdc3
实现raid10
mdadm -C /dev/md0 -a yes -l1 -n2 /dev/sd{b,c}
mdadm -C /dev/md1 -a yes -l1 -n2 /dev/sd{d,e}
mdadm -C /dev/md2 -a yes -l0 -n2 /dev/md{0,1}
或者
mdadm -C /dev/md0 -a yes -l10 -n4 /dev/sd{b,c,d,e}
如果不够用了增加一个磁盘
[root@centos ~]#fdisk /dev/sdc
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
Selected partition 4
First cylinder (787-10443, default 787):
Using default value 787
Last cylinder, +cylinders or +size{K,M,G} (787-10443, default 10443): +2G
Command (m for help): t
Partition number (1-4): 4
Hex code (type L to list codes): fd
Changed system type of partition 4 to fd (Linux raid autodetect)
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@centos ~]#mdadm /dev/md0 -G -n4 -a /dev/sdc4
mdadm: added /dev/sdc4
[root@centos ~]#mdadm -D /dev/md0
/dev/md0:
        Version : 1.2
  Creation Time : Wed Apr 25 18:42:03 2018
     Raid Level : raid5
     Array Size : 4204864 (4.01 GiB 4.31 GB)
  Used Dev Size : 2102432 (2.01 GiB 2.15 GB)
   Raid Devices : 4
  Total Devices : 4
    Persistence : Superblock is persistent
    Update Time : Wed Apr 25 20:53:55 2018
          State : clean, reshaping
 Active Devices : 4
Working Devices : 4
 Failed Devices : 0
  Spare Devices : 0
         Layout : left-symmetric
     Chunk Size : 32K
 Reshape Status : 5% complete
  Delta Devices : 1, (3->4)
           Name : centos.localdomain:0  (local to host centos.localdomain)
           UUID : 334cca96:877322f5:7cd0110c:afdf35bf
         Events : 84
    Number   Major   Minor   RaidDevice State
       0       8       20        0      active sync   /dev/sdb4
       1       8       34        1      active sync   /dev/sdc2
       3       8       35        2      active sync   /dev/sdc3
       4       8       36        3      active sync   /dev/sdc4
[root@centos ~]#df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        48G  5.5G   41G  12% /
tmpfs           931M   76K  931M   1% /dev/shm
/dev/sda1       976M   35M  891M   4% /boot
/dev/sda3        29G   98M   28G   1% /data
/dev/md0        3.9G  9.9M  3.7G   1% /data/raid    增加了一块硬盘却没有增大,是因为没有文件系统。
/dev/sr0        5.8G  5.8G     0 100% /media/CDROM
[root@centos ~]#resize2fs /dev/md0    同步文件系统,但是这个命令只适用于ext4
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/md0 is mounted on /data/raid; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/md0 to 1576824 (4k) blocks.
The filesystem on /dev/md0 is now 1576824 blocks long.
[root@centos ~]#df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        48G  5.5G   41G  12% /
tmpfs           931M   76K  931M   1% /dev/shm
/dev/sda1       976M   35M  891M   4% /boot
/dev/sda3        29G   98M   28G   1% /data
/dev/md0        5.8G  9.9M  5.5G   1% /data/raid
/dev/sr0        5.8G  5.8G     0 100% /media/CDROM
centos7也就是xfs同步命令是 xfs_growfs /data/raid  不能写设备,只能写挂载点。
[root@centos ~]#cat /proc/mdstat   查看raid信息  有备用硬盘会显示S
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sdc4[4] sdc3[3] sdb4[0] sdc2[1]
      6307296 blocks super 1.2 level 5, 32k chunk, algorithm 2 [4/4] [UUUU]
unused devices: <none>
删除raid
1.卸载挂载
删除 /etc/fstab中相应的行
[root@centos ~]#ll /dev/md0
brw-rw—-. 1 root disk 9, 0 Apr 25 21:02 /dev/md0
[root@centos ~]#rm -f /dec/mdamd.conf
[root@centos ~]#mdadm -S /dev/md0
mdadm: stopped /dev/md0
[root@centos ~]#ll /dev/md0     有点延迟,正常不显示
brw-rw—-. 1 root disk 9, 0 Apr 25 21:17 /dev/md0
[root@centos ~]#mdadm -D /dev/md0
/dev/md0:
        Version :
     Raid Level : raid0
  Total Devices : 0
          State : inactive
    Number   Major   Minor   RaidDevice
[root@centos ~]#ll /dev/md0
ls: cannot access /dev/md0: No such file or directory
[root@centos ~]#blkid    设备还在
/dev/sda1: UUID=”414ab9ee-af40-44f1-b300-d29fc674eb8e” TYPE=”ext4″
/dev/sda2: UUID=”408b65d7-7551-480b-8916-5eff4cca1b00″ TYPE=”ext4″
/dev/sda3: UUID=”37d5cf25-ead4-427b-9655-40fc1602b5df” TYPE=”ext4″
/dev/sda5: UUID=”9fc26932-7094-4948-b395-24ab05d8ea73″ TYPE=”swap”
/dev/sdb2: UUID=”e4c92849-b5b6-4736-a027-25c99f9a9da1″ TYPE=”ext4″
/dev/sdb1: UUID=”309c1b15-1d6d-475a-a077-0caf7634442a” TYPE=”ext4″
/dev/sdc1: LABEL=”swap_sdc1″ UUID=”ca1c9c9d-7ed6-45b3-89a4-e8ff36548d0b” TYPE=”swap”
/dev/sdb3: LABEL=”/data/sdb3″ UUID=”e7742e5e-f923-4c7e-b43e-ee0952a0045e” TYPE=”ext4″
/dev/sdb4: UUID=”334cca96-8773-22f5-7cd0-110cafdf35bf” UUID_SUB=”396e1715-7f32-a631-46ab-6819b9a95f42″ LABEL=”centos.localdomain:0″ TYPE=”linux_raid_member”
/dev/sdc2: UUID=”334cca96-8773-22f5-7cd0-110cafdf35bf” UUID_SUB=”8534ca6d-ff60-618d-d973-13566069ebd4″ LABEL=”centos.localdomain:0″ TYPE=”linux_raid_member”
/dev/sdc3: UUID=”334cca96-8773-22f5-7cd0-110cafdf35bf” UUID_SUB=”d538892d-8c71-c215-a6ec-3b48bfcb3800″ LABEL=”centos.localdomain:0″ TYPE=”linux_raid_member”
/dev/sdc4: UUID=”334cca96-8773-22f5-7cd0-110cafdf35bf” UUID_SUB=”d1822b1e-7aea-8416-cd50-530db3b67727″ LABEL=”centos.localdomain:0″ TYPE=”linux_raid_member”
删除/dev/sdc{2,3,4}和/dev/sdb4
如果在创建相应的分区大小一样,还是会出现raid的信息
这步应在删除分区之前
#mdadm –zero-surpblock /dev/sdc234
也可以用这个命令
[root@centos ~]#dd if=/dev/zero of=/dec/sdc234
软件RAID的实现
   -C: 创建模式
-n #: 使用#个块设备来创建此RAID
-l #:指明要创建的RAID的级别
-a {yes|no}:自动创建目标RAID设备的设备文件
-c CHUNK_SIZE: 指明块大小,单位k
-x #: 指明空闲盘的个数
   -D:显示raid的详细信息
mdadm -D /dev/md#
   管理模式:
-f: 标记指定磁盘为损坏
-a: 添加磁盘
-r: 移除磁盘
   观察md的状态:
cat /proc/mdstat
软RAID配置示例
  使用mdadm创建并定义RAID设备
mdadm -C /dev/md0 -a yes -l 5 -n 3 -x 1 /dev/sdb1 /dev/sdc1 /dev/sdd1
/dev/sde1
  用文件系统对每个RAID设备进行格式化
mke2fs -j /dev/md0
  测试RAID设备
  使用mdadm检查RAID设备的状况
mdadm –detail|D /dev/md0
  增加新的成员
mdadm –G /dev/md0 –n4 -a /dev/sdf1
软RAID测试和修复
  模拟磁盘故障
mdadm /dev/md0 -f /dev/sda1
  移除磁盘
mdadm /dev/md0 –r /dev/sda1
  从软件RAID磁盘修复磁盘故障
? 替换出故障的磁盘然后开机
? 在备用驱动器上重建分区
? mdadm /dev/md0 -a /dev/sda1
  mdadm、/proc/mdstat及系统日志信息
软RAID管理
  生成配置文件:mdadm –D –s >> /etc/mdadm.conf
  停止设备:mdadm –S /dev/md0
  激活设备:mdadm –A –s /dev/md0 激活
  强制启动:mdadm –R /dev/md0
  删除raid信息:mdadm –zero-superblock /dev/sdb1
逻辑卷管理器(LVM)
  允许对卷进行方便操作的抽象层,包括重新设定文件系统的大小
  允许在多个物理设备间重新组织文件系统
  将设备指定为物理卷
  用一个或者多个物理卷来创建一个卷组
  物理卷是用固定大小的物理区域(Physical Extent,PE)来定义的
  在物理卷上创建的逻辑卷
是由物理区域(PE)组成
  可以在逻辑卷上创建文件系统
先将划分好的分区或者硬盘变成物理卷,再将物理卷组合成卷组,再把卷组划分成逻辑卷,
逻辑卷的好处是可以增加或减少(尽量别用)其空间大小,在线扩展,不影响用户的使用
PE相当于划分逻辑卷组的最小单位,所以逻辑卷的大小都是PE的整数倍
创建两个物理卷
分区将分区类型改为8e
[root@centos ~]#pvcreate /dev/sd{b1,c1}
  Physical volume “/dev/sdb1” successfully created
  Physical volume “/dev/sdc1” successfully created
[root@centos ~]#pvs    查看一下
  PV         VG   Fmt  Attr PSize PFree
  /dev/sdb1       lvm2 —- 1.01g 1.01g
  /dev/sdc1       lvm2 —- 2.01g 2.01g
[root@centos ~]#pvdisplay
  “/dev/sdb1” is a new physical volume of “1.01 GiB”
  — NEW Physical volume —
  PV Name               /dev/sdb1
  VG Name
  PV Size               1.01 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               kNF3li-xdQx-uWio-T8fq-V3es-U0Jh-vc8Gef
  “/dev/sdc1” is a new physical volume of “2.01 GiB”
  — NEW Physical volume —
  PV Name               /dev/sdc1
  VG Name
  PV Size               2.01 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               PEeTCR-zFF0-zK5q-2yrZ-hTH7-xwdt-918DBq
将连个物理卷加入卷组
[root@centos ~]#vgcreate -s 16M vg0 /dev/sd{b1,c1} -s指定PE大小
  Volume group “vg0” successfully created
[root@centos ~]#pvs
  PV         VG   Fmt  Attr PSize PFree
  /dev/sdb1  vg0  lvm2 a–u 1.00g 1.00g
  /dev/sdc1  vg0  lvm2 a–u 2.00g 2.00g
[root@centos ~]#pvdisplay
  — Physical volume —
  PV Name               /dev/sdb1
  VG Name               vg0
  PV Size               1.01 GiB / not usable 11.41 MiB   不够16M剩下的
  Allocatable           yes
  PE Size               16.00 MiB
  Total PE              64
  Free PE               64
  Allocated PE          0
  PV UUID               kNF3li-xdQx-uWio-T8fq-V3es-U0Jh-vc8Gef
  — Physical volume —
  PV Name               /dev/sdc1
  VG Name               vg0
  PV Size               2.01 GiB / not usable 7.16 MiB
  Allocatable           yes
  PE Size               16.00 MiB
  Total PE              128
  Free PE               128
  Allocated PE          0
  PV UUID               PEeTCR-zFF0-zK5q-2yrZ-hTH7-xwdt-918DBq
[root@centos ~]#vgs
  VG   #PV #LV #SN Attr   VSize VFree
  vg0    2   0   0 wz–n- 3.00g 3.00g
[root@centos ~]#vgdisplay
  — Volume group —
  VG Name               vg0
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               3.00 GiB
  PE Size               16.00 MiB
  Total PE              192
  Alloc PE / Size       0 / 0
  Free  PE / Size       192 / 3.00 GiB
  VG UUID               OJVY05-qjee-wGKK-cOAc-eY5m-aLPj-NvEgwA
创建逻辑卷
[root@centos ~]#lvcreate -n lv0 -l 100 vg0
  Logical volume “lv0” created.
[root@centos ~]#lvs
  LV   VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv0  vg0  -wi-a—– 1.56g
[root@centos ~]#lvdisplay
  — Logical volume —
  LV Path                /dev/vg0/lv0
  LV Name                lv0
  VG Name                vg0
  LV UUID                fdN6Zs-6MCJ-j9aD-g0R6-5LYj-df8F-IvW72J
  LV Write Access        read/write
  LV Creation host, time centos.localdomain, 2018-04-26 09:42:31 +0800
  LV Status              available
  # open                 0
  LV Size                1.56 GiB
  Current LE             100
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  – currently set to     256
  Block device           253:0
[root@centos ~]#lvcreate -n lv1 -l 100%FREE vg0
  Logical volume “lv1” created.
[root@centos ~]#lvs
  LV   VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv0  vg0  -wi-a—– 1.56g
  lv1  vg0  -wi-a—– 1.44g
[root@centos ~]#ll /dev/mapper/vg0-lv0    设备名
lrwxrwxrwx. 1 root root 7 Apr 26 09:42 /dev/mapper/vg0-lv0 -> ../dm-0
[root@centos ~]#ll /dev/vg0/lv1
lrwxrwxrwx. 1 root root 7 Apr 26 09:46 /dev/vg0/lv1 -> ../dm-1
创建文件系统
[root@centos ~]#mkfs.ext4 /dev/vg0/lv0
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
102544 inodes, 409600 blocks
20480 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=419430400
13 block groups
32768 blocks per group, 32768 fragments per group
7888 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 21 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
挂载
[root@centos ~]#vim /etc/fstab
UUID=18b81131-36ce-48ad-b819-e47d1088c5d3 /mnt/lv0 ext4 defaults 0 0
[root@centos ~]#mkdir /mnt/lv0
[root@centos ~]#mount -a
[root@centos ~]#df
Filesystem          1K-blocks    Used Available Use% Mounted on
/dev/sda2            50264772 5721644  41983128  12% /
tmpfs                  953128      76    953052   1% /dev/shm
/dev/sda1              999320   34944    911948   4% /boot
/dev/sda3            30106576   99988  28470588   1% /data
/dev/sr0              6049912 6049912         0 100% /media/CDROM
/dev/mapper/vg0-lv0   1579844    2400   1495524   1% /mnt/lv0
[root@centos ~]#pvcreate /dev/sdd1
  Physical volume “/dev/sdd1” successfully created
[root@centos ~]#vgextend vg0 /dev/sdd1
  Volume group “vg0” successfully extended
[root@centos ~]#vgs
  VG   #PV #LV #SN Attr   VSize  VFree
  vg0    3   2   0 wz–n- 23.00g 20.00g
[root@centos ~]#lvextend -L +5G /dev/vg0/lv0
  Size of logical volume vg0/lv0 changed from 1.56 GiB (100 extents) to 6.56 GiB (420 extents).
  Logical volume lv0 successfully resized.
[root@centos ~]#lvs
  LV   VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv0  vg0  -wi-ao—- 6.56g
  lv1  vg0  -wi-a—– 1.44g
[root@centos ~]#df -h
Filesystem           Size  Used Avail Use% Mounted on
/dev/sda2             48G  5.5G   41G  12% /
tmpfs                931M   76K  931M   1% /dev/shm
/dev/sda1            976M   35M  891M   4% /boot
/dev/sda3             29G   98M   28G   1% /data
/dev/sr0             5.8G  5.8G     0 100% /media/CDROM
/dev/mapper/vg0-lv0  1.6G  4.2M  1.5G   1% /mnt/lv0   没有创建文件系统所以大小没有增大
[root@centos ~]#resize2fs /dev/vg0/lv0     同步文件系统
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg0/lv0 is mounted on /mnt/lv0; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vg0/lv0 to 1720320 (4k) blocks.
The filesystem on /dev/vg0/lv0 is now 1720320 blocks long.
[root@centos ~]#df -h
Filesystem           Size  Used Avail Use% Mounted on
/dev/sda2             48G  5.5G   41G  12% /
tmpfs                931M   76K  931M   1% /dev/shm
/dev/sda1            976M   35M  891M   4% /boot
/dev/sda3             29G   98M   28G   1% /data
/dev/sr0             5.8G  5.8G     0 100% /media/CDROM
/dev/mapper/vg0-lv0  6.5G  5.4M  6.2G   1% /mnt/lv0
[root@centos ~]#lvextend -l +100%FREE /dev/vg0/lv0   加-r直接同步文件系统
  Size of logical volume vg0/lv0 changed from 6.56 GiB (420 extents) to 21.56 GiB (1380 extents).
  Logical volume lv0 successfully resized.
[root@centos ~]#resize2fs /dev/vg0/lv0
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg0/lv0 is mounted on /mnt/lv0; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/vg0/lv0 to 5652480 (4k) blocks.
The filesystem on /dev/vg0/lv0 is now 5652480 blocks long.
[root@centos ~]#df -h
Filesystem           Size  Used Avail Use% Mounted on
/dev/sda2             48G  5.5G   41G  12% /
tmpfs                931M   76K  931M   1% /dev/shm
/dev/sda1            976M   35M  891M   4% /boot
/dev/sda3             29G   98M   28G   1% /data
/dev/sr0             5.8G  5.8G     0 100% /media/CDROM
/dev/mapper/vg0-lv0   22G  6.1M   21G   1% /mnt/lv0
缩减逻辑卷:xfs无法缩减,ext4可以压缩
1.备份
2.取消挂载
[root@centos ~]#umount /mnt/lv0
[root@centos ~]#df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda2       50264772 5721664  41983108  12% /
tmpfs             953128      76    953052   1% /dev/shm
/dev/sda1         999320   34944    911948   4% /boot
/dev/sda3       30106576   99988  28470588   1% /data
/dev/sr0         6049912 6049912         0 100% /media/CDROM
3.先检查文件完整性
[root@centos ~]#fsck -f /dev/vg0/lv0
fsck from util-linux-ng 2.17.2
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/mapper/vg0-lv0: 146/1364624 files (0.7% non-contiguous), 95415/5652480 blocks
4.缩减文件系统
[root@centos ~]#resize2fs /dev/vg0/lv0 10G
resize2fs 1.41.12 (17-May-2010)
Please run ‘e2fsck -f /dev/vg0/lv0’ first.
[root@centos ~]#resize2fs /dev/vg0/lv0 10G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg0/lv0 to 2621440 (4k) blocks.
The filesystem on /dev/vg0/lv0 is now 2621440 blocks long.
5.缩减逻辑卷
[root@centos ~]#lvreduce -L 10G /dev/vg0/lv0
  WARNING: Reducing active logical volume to 10.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg0/lv0? [y/n]: y
  Size of logical volume vg0/lv0 changed from 21.56 GiB (1380 extents) to 10.00 GiB (640 extents).
  Logical volume lv0 successfully resized.
[root@centos ~]#mount -a
[root@centos ~]#lvs
  LV   VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv0  vg0  -wi-ao—- 10.00g
  lv1  vg0  -wi-a—–  1.44g
[root@centos ~]#df -h
Filesystem           Size  Used Avail Use% Mounted on
/dev/sda2             48G  5.5G   41G  12% /
tmpfs                931M   76K  931M   1% /dev/shm
/dev/sda1            976M   35M  891M   4% /boot
/dev/sda3             29G   98M   28G   1% /data
/dev/sr0             5.8G  5.8G     0 100% /media/CDROM
/dev/mapper/vg0-lv0  9.9G  5.4M  9.4G   1% /mnt/lv0
[root@centos ~]#vgdisplay
  — Volume group —
  VG Name               vg0
  System ID
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  7
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               1
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               23.00 GiB
  PE Size               16.00 MiB
  Total PE              1472
  Alloc PE / Size       732 / 11.44 GiB
  Free  PE / Size       740 / 11.56 GiB
  VG UUID               OJVY05-qjee-wGKK-cOAc-eY5m-aLPj-NvEgwA
移除逻辑卷中的一个磁盘
1.找到一个足够大的磁盘存放要移除的磁盘的内容
[root@centos ~]#fdisk /dev/sdc   分出一个分区
[root@centos ~]#partx -a /dev/sdc    同步
BLKPG: Device or resource busy
error adding partition 1
[root@centos ~]#pvcreate /dev/sdc3
  Physical volume “/dev/sdc3” successfully created
[root@centos ~]#vgextend vg0 /dev/sdc3
  Volume group “vg0” successfully extended
[root@centos ~]#pvdisplay
  — Physical volume —
  PV Name               /dev/sdb1
  VG Name               vg0
  PV Size               1.01 GiB / not usable 11.41 MiB
  Allocatable           yes (but full)
  PE Size               16.00 MiB
  Total PE              64
  Free PE               0
  Allocated PE          64
  PV UUID               kNF3li-xdQx-uWio-T8fq-V3es-U0Jh-vc8Gef
  — Physical volume —
  PV Name               /dev/sdc1
  VG Name               vg0
  PV Size               2.01 GiB / not usable 7.16 MiB
  Allocatable           yes (but full)
  PE Size               16.00 MiB
  Total PE              128
  Free PE               0
  Allocated PE          128
  PV UUID               PEeTCR-zFF0-zK5q-2yrZ-hTH7-xwdt-918DBq
  — Physical volume —
  PV Name               /dev/sdd1
  VG Name               vg0
  PV Size               20.01 GiB / not usable 9.12 MiB
  Allocatable           yes
  PE Size               16.00 MiB
  Total PE              1280
  Free PE               1280
  Allocated PE          0
  PV UUID               35xtf7-vvtE-ZGWx-vy0U-An6D-jdlW-Z1PQX2
  — Physical volume —
  PV Name               /dev/sdc3
  VG Name               vg0
  PV Size               20.01 GiB / not usable 9.15 MiB
  Allocatable           yes
  PE Size               16.00 MiB
  Total PE              1280
  Free PE               740
  Allocated PE          540
  PV UUID               bH2kul-NShg-DgPC-mRDr-Obv9-ntTT-tD4FFS
[root@centos ~]#vgreduce vg0 /dev/sdd1    移除sdd1
  Removed “/dev/sdd1” from volume group “vg0”
[root@centos ~]#pvdisplay
 “/dev/sdd1” is a new physical volume of “20.01 GiB”
  — NEW Physical volume —
  PV Name               /dev/sdd1
  VG Name
  PV Size               20.01 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               35xtf7-vvtE-ZGWx-vy0U-An6D-jdlW-Z1PQX2
[root@centos ~]#pvremove /dev/sdd1    变成孤立的磁盘
  Labels on physical volume “/dev/sdd1” successfully wiped
转移逻辑卷到别的电脑
[root@centos ~]#pvmove /dev/sdb1
[root@centos ~]#vgreduce vg0 /dev/sdb1
  Removed “/dev/sdb1” from volume group “vg0”
搬家之前需要看看搬家到的电脑上是否也有相同的vg0 lv0,如果有需要改名字
[root@centos ~]#vgrename vg0 centos6vg0
  Volume group “vg0” successfully renamed to “centos6vg0”
[root@centos ~]#lvrename /dev/centos6vg0/lv0 /dev/centos6vg0/centos6lv0
  Renamed “lv0” to “centos6lv0” in volume group “centos6vg0”
[root@centos ~]#lvrename /dev/centos6vg0/lv1 /dev/centos6vg0/centos6lv1
  Renamed “lv1” to “centos6lv1” in volume group “centos6vg0”
[root@centos ~]#ll /dev/centos6vg0/centos6lv0    修改的是软连接
lrwxrwxrwx. 1 root root 7 Apr 26 11:10 /dev/centos6vg0/centos6lv0 -> ../dm-0
[root@centos ~]#umount /mnt/lv0   卸载挂载
[root@centos ~]#df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda2       50264772 5721736  41983036  12% /
tmpfs             953128      76    953052   1% /dev/shm
/dev/sda1         999320   34944    911948   4% /boot
/dev/sda3       30106576   99988  28470588   1% /data
/dev/sr0         6049912 6049912         0 100% /media/CDROM
[root@centos ~]#vgchange -an centos6vg0     禁用vg0
  0 logical volume(s) in volume group “centos6vg0” now active
[root@centos ~]#vgexport centos6vg0
  Volume group “centos6vg0” successfully exported
[root@centos ~]#vgdisplay
  — Volume group —
  VG Name               centos6vg0
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  20
  VG Access             read/write
  VG Status             exported/resizable
  MAX LV                0
  Cur LV                2
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               22.00 GiB
  PE Size               16.00 MiB
  Total PE              1408
  Alloc PE / Size       732 / 11.44 GiB
  Free  PE / Size       676 / 10.56 GiB
  VG UUID               OJVY05-qjee-wGKK-cOAc-eY5m-aLPj-NvEgwA
拔出硬盘插入要搬家到的电脑
[root@CENTOS7 ~]#pvdisplay
  Physical volume “/dev/sdc1” of volume group “centos6vg0” is exported
  — Physical volume —
  PV Name               /dev/sdc1
  VG Name               centos6vg0 (exported)
  PV Size               <2.01 GiB / not usable <7.16 MiB
  Allocatable           yes (but full)
  PE Size               16.00 MiB
  Total PE              128
  Free PE               0
  Allocated PE          128
  PV UUID               PEeTCR-zFF0-zK5q-2yrZ-hTH7-xwdt-918DBq
  Physical volume “/dev/sdc3” of volume group “centos6vg0” is exported
  — Physical volume —
  PV Name               /dev/sdc3
  VG Name               centos6vg0 (exported)
  PV Size               <20.01 GiB / not usable 9.15 MiB
  Allocatable           yes
  PE Size               16.00 MiB
  Total PE              1280
  Free PE               676
  Allocated PE          604
  PV UUID               bH2kul-NShg-DgPC-mRDr-Obv9-ntTT-tD4FFS
[root@CENTOS7 ~]#vgimport centos6vg0    导入
  Volume group “centos6vg0” successfully imported
[root@CENTOS7 ~]#vgchange -ay centos6vg0    启用vg0
  2 logical volume(s) in volume group “centos6vg0” now active
[root@CENTOS7 ~]#vgdisplay
  — Volume group —
  VG Name               centos6vg0
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  21
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               22.00 GiB
  PE Size               16.00 MiB
  Total PE              1408
  Alloc PE / Size       732 / <11.44 GiB
  Free  PE / Size       676 / 10.56 GiB
  VG UUID               OJVY05-qjee-wGKK-cOAc-eY5m-aLPj-NvEgwA
[root@CENTOS7 ~]#mkdir /mnt/centos6lv0
[root@CENTOS7 ~]#mount /dev/centos6vg0/centos6lv0 /mnt/centos6lv0
[root@CENTOS7 ~]#cat /mnt/centos6lv0/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
LVM介绍
  LVM: Logical Volume Manager, Version: 2
  dm: device mapper:将一个或多个底层块设备组织成一个逻辑设备的模块
  设备名:/dev/dm-#
  软链接:
/dev/mapper/VG_NAME-LV_NAME
/dev/mapper/vol0-root
/dev/VG_NAME/LV_NAME
/dev/vol0/root
LVM更改文件系统的容量
  LVM可以弹性的更改LVM的容量
通过交换PE来进行资料的转换,将原来LV内的PE转移到其他的
设备中以降低LV的容量,或将其他设备中的PE加到LV中以加大
容量
pv管理工具
  显示pv信息
pvs:简要pv信息显示
pvdisplay
  创建pv
pvcreate /dev/DEVICE
vg管理工具
  显示卷组
vgs
vgdisplay
  创建卷组
vgcreate [-s #[kKmMgGtTpPeE]] VolumeGroupName PhysicalDevicePath
[PhysicalDevicePath…]
  管理卷组
vgextend VolumeGroupName PhysicalDevicePath [PhysicalDevicePath…]
vgreduce VolumeGroupName PhysicalDevicePath [PhysicalDevicePath…]
  删除卷组
先做pvmove,再做vgremove
lv管理工具
  显示逻辑卷
lvs
Lvdisplay
  创建逻辑卷
lvcreate -L #[mMgGtT] -n NAME VolumeGroup
lvcreate -l 60%VG -n mylv testvg
lvcreate -l 100%FREE -n yourlv testvg
  删除逻辑卷
lvremove /dev/VG_NAME/LV_NAME
  重设文件系统大小
fsadm [options] resize device [new_size[BKMGTEP]]
resize2fs [-f] [-F] [-M] [-P] [-p] device [new_size]
扩展和缩减逻辑卷
  扩展逻辑卷:
lvextend -L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME
resize2fs /dev/VG_NAME/LV_NAME
lvresize -r -l  +100%FREE  /dev/ VG_NAME/LV_NAME
  缩减逻辑卷:
umount /dev/VG_NAME/LV_NAME
e2fsck -f /dev/VG_NAME/LV_NAME
resize2fs /dev/VG_NAME/LV_NAME #[mMgGtT]
lvreduce -L [-]#[mMgGtT] /dev/VG_NAME/LV_NAME
mount
跨主机迁移卷组
  源计算机上
  1 在旧系统中,umount所有卷组上的逻辑卷
  2 vgchange –a n vg0
lvdisplay
  3 vgexport vg0
pvscan
vgdisplay
拆下旧硬盘
   在目标计算机上
  4 在新系统中安装旧硬盘,并vgimport vg0.
  5 vgchange –ay vg0
  6 mount所有卷组上的逻辑卷
创建逻辑卷示例
  创建物理卷
pvcreate /dev/sda3
  为卷组分配物理卷
vgcreate vg0 /dev/sda3
  从卷组创建逻辑卷
lvcreate -L 256M -n data vg0
mke2fs -j /dev/vg0/data
  mount /dev/vg0/data /mnt/data
逻辑卷管理器快照
   快照是特殊的逻辑卷,它是在生成快照时存在的逻辑卷的准确拷贝
   对于需要备份或者复制的现有数据临时拷贝以及其它操作来说,快照是最合适的选择
   快照只有在它们和原来的逻辑卷不同时才会消耗空间
   在生成快照时会分配给它一定的空间,但只有在原来的逻辑卷或者快照有所改变
才会使用这些空间
   当原来的逻辑卷中有所改变时,会将旧的数据复制到快照中。
   快照中只含有原来的逻辑卷中更改的数据或者自生成快照后的快照中更改的数据
   建立快照的卷大小只需要原始逻辑卷的15%~20%就够了,也可以使用lvextend放大
快照
创建快照   快照必须和逻辑卷在同一个卷组中,快照使用的空间就是这个逻辑卷卷组的空间
[root@CENTOS7 ~]#lvcreate -n centos6lv0-snapshot -s -L 1G /dev/centos6vg0/centos6lv0
  Using default stripesize 64.00 KiB.
  Logical volume “centos6lv0-snapshot” created.
[root@CENTOS7 ~]#lvdisplay    快照逻辑卷
 — Logical volume —
  LV Path                /dev/centos6vg0/centos6lv0-snapshot
  LV Name                centos6lv0-snapshot
  VG Name                centos6vg0
  LV UUID                Yf0CA3-eYFL-6MIt-V7tx-iHcW-r74z-bRcI1Y
  LV Write Access        read/write
  LV Creation host, time CENTOS7.localdomain, 2018-04-26 13:23:39 +0800
  LV snapshot status     active destination for centos6lv0
  LV Status              available
  # open                 0
  LV Size                10.00 GiB
  Current LE             640
  COW-table size         1.00 GiB
  COW-table LE           64
  Allocated to snapshot  0.00%
  Snapshot chunk size    4.00 KiB
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  – currently set to     8192
  Block device           253:4
[root@CENTOS7 ~]#ls /mnt/centos6lv0/
f1  f2  f3
[root@CENTOS7 ~]#ls /mnt/snap/    虽然快照中有内容但是实际上是没有的
f1  f2  f3
[root@CENTOS7 ~]#vim /mnt/centos6lv0/f1
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
~
[root@CENTOS7 ~]#ll /mnt/centos6lv0/ /mnt/snap/
/mnt/centos6lv0/:
total 12
-rw-r–r–. 1 root root   41 Apr 26 13:31 f1
-rw-r–r–. 1 root root 2430 Apr 26 13:18 f2
-rw-r–r–. 1 root root 2430 Apr 26 13:18 f3
/mnt/snap/:
total 12
-rw-r–r–. 1 root root 2430 Apr 26 13:18 f1
-rw-r–r–. 1 root root 2430 Apr 26 13:18 f2
-rw-r–r–. 1 root root 2430 Apr 26 13:18 f3
[root@CENTOS7 ~]#rm -f /mnt/centos6lv0/f2
[root@CENTOS7 ~]#ll /mnt/centos6lv0/ /mnt/snap/
/mnt/centos6lv0/:
total 8
-rw-r–r–. 1 root root   41 Apr 26 13:31 f1
-rw-r–r–. 1 root root 2430 Apr 26 13:18 f3
/mnt/snap/:
total 12
-rw-r–r–. 1 root root 2430 Apr 26 13:18 f1
-rw-r–r–. 1 root root 2430 Apr 26 13:18 f2
-rw-r–r–. 1 root root 2430 Apr 26 13:18 f3
当把快照挂载后,快照中的内容也是可以删除的。因此我们在创建或者挂载的时候应该将快照定义成只读
[root@CENTOS7 ~]#lvcreate -n centos6lv0-snapshot -s -L 1G -p r /dev/centos6vg0/centos6lv0
[root@CENTOS7 ~]#mount -o ro /dev/centos6vg0/centos6lv0-snapshot /mnt/snap
用快照恢复逻辑卷
1.取消挂载
[root@CENTOS7 ~]#umount /mnt/centos6lv0/
[root@CENTOS7 ~]#umount /mnt/snap/
2.合并
[root@CENTOS7 ~]#lvconvert –merge /dev/centos6vg0//centos6lv0
  centos6vg0/centos6lv0 is not read-only – refusing to merge.
[root@CENTOS7 ~]#pvcreate /dev/sdb1
WARNING: ext4 signature detected on /dev/sdb1 at offset 1080. Wipe it? [y/n]: y
  Wiping ext4 signature on /dev/sdb1.
  Physical volume “/dev/sdb1” successfully created.
[root@CENTOS7 ~]#vgcreate vg0 -s 8M /dev/sdb1
  Volume group “vg0” successfully created
[root@CENTOS7 ~]#lvcreate -n lv0 -l 200 vg0
  Logical volume “lv0” created.
[root@CENTOS7 ~]#mkfs.xfs /dev/vg0/lv0
[root@CENTOS7 ~]#mount /dev/vg0/lv0 /mnt/lv0
快照的uuid和逻辑卷的一样
/dev/mapper/centos6vg0-centos6lv0: UUID=”18b81131-36ce-48ad-b819-e47d1088c5d3″ TYPE=”ext4″
/dev/mapper/centos6vg0-centos6lv0–snapshot: UUID=”18b81131-36ce-48ad-b819-e47d1088c5d3″ TYPE=”ext4″
[root@CENTOS7 ~]#partprobe   同步
[root@CENTOS7 ~]#pvcreate /dev/sdb2
  Physical volume “/dev/sdb2” successfully created.
[root@CENTOS7 ~]#vgextend vg0 /dev/sdb2
  Volume group “vg0” successfully extended
[root@CENTOS7 ~]#lvcreate -n lv0-snapshot -s -L 1G -p r /dev/vg0/lv0
  Using default stripesize 64.00 KiB.
  Logical volume “lv0-snapshot” created.
在centos7上只读卷不能挂载
[root@CENTOS7 ~]#rm -f /mnt/lv0/f1
[root@CENTOS7 ~]#> /mnt/lv0/f2
[root@CENTOS7 ~]#umount /mnt/lv0/
[root@CENTOS7 ~]#lvconvert –merge /dev/vg0/lv0-snapshot
  Merging of volume vg0/lv0-snapshot started.
  lv0: Merged: 100.00%
[root@CENTOS7 ~]#mount /dev/vg0/lv0 /mnt/lv0/
[root@CENTOS7 ~]#ls /mnt/lv0/
f1  f2  f3
合并成功后快照自动删除
删除逻辑卷
root@CENTOS7 ~]#lvremove /dev/vg0/lv0
Do you really want to remove active logical volume vg0/lv0? [y/n]: y
  Logical volume “lv0” successfully removed
删除有快照的逻辑卷会先删除快照再删除逻辑卷
[root@CENTOS7 ~]#umount /mnt/mm
[root@CENTOS7 ~]#lvremove /dev/centos6vg0/centos6lv0
Do you really want to remove active origin logical volume centos6vg0/centos6lv0 with 1 snapshot(s)? [y/n]: y
  Logical volume “centos6lv0-snapshot” successfully removed
  Logical volume “centos6lv0” successfully removed
[root@CENTOS7 ~]#lvcreate -n lv0 -L 1G vg0
WARNING: xfs signature detected on /dev/vg0/lv0 at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/vg0/lv0.
  Logical volume “lv0” created.
[root@CENTOS7 ~]#mkfs.xfs /dev/vg0/lv0
meta-data=/dev/vg0/lv0           isize=512    agcount=4, agsize=65536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=262144, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@CENTOS7 ~]#mount /dev/vg0/lv0 /mnt/lv0
[root@CENTOS7 ~]#df
Filesystem          1K-blocks    Used Available Use% Mounted on
/dev/sda2            52403200 4926308  47476892  10% /
devtmpfs               535296       0    535296   0% /dev
tmpfs                  550036       0    550036   0% /dev/shm
tmpfs                  550036    7968    542068   2% /run
tmpfs                  550036       0    550036   0% /sys/fs/cgroup
/dev/sda3            31441920  161500  31280420   1% /data
/dev/sda1             1038336  161620    876716  16% /boot
tmpfs                  110008      20    109988   1% /run/user/0
/dev/sr0              8490330 8490330         0 100% /run/media/root/CentOS 7 x86_64
tmpfs                  110008       0    110008   0% /run/user/1000
/dev/mapper/vg0-lv0   1038336   32944   1005392   4% /mnt/lv0
[root@CENTOS7 ~]#cp /etc/passwd /mnt/lv0/f1
[root@CENTOS7 ~]#cp /etc/passwd /mnt/lv0/f2
[root@CENTOS7 ~]#cp /etc/passwd /mnt/lv0/f3
[root@CENTOS7 ~]#lvcreate -n lv0-snapshot -L 500M -s /dev/vg0/lv0
  Using default stripesize 64.00 KiB.
  Rounding up size to full physical extent 504.00 MiB
  Logical volume “lv0-snapshot” created.
[root@CENTOS7 ~]#mount -o nouuid /dev/vg0/lv0-snapshot /mnt/mm
[root@CENTOS7 ~]#df
Filesystem                    1K-blocks    Used Available Use% Mounted on
/dev/sda2                      52403200 4926088  47477112  10% /
devtmpfs                         535296       0    535296   0% /dev
tmpfs                            550036       0    550036   0% /dev/shm
tmpfs                            550036    7980    542056   2% /run
tmpfs                            550036       0    550036   0% /sys/fs/cgroup
/dev/sda3                      31441920  161500  31280420   1% /data
/dev/sda1                       1038336  161620    876716  16% /boot
tmpfs                            110008      20    109988   1% /run/user/0
/dev/sr0                        8490330 8490330         0 100% /run/media/root/CentOS 7 x86_64
tmpfs                            110008       0    110008   0% /run/user/1000
/dev/mapper/vg0-lv0             1038336   32956   1005380   4% /mnt/lv0
/dev/mapper/vg0-lv0–snapshot   1038336   32956   1005380   4% /mnt/mm
[root@CENTOS7 ~]#rm -f /mnt/lv0/f1
[root@CENTOS7 ~]#ll /mnt/lv0
total 8
-rw-r–r–. 1 root root 2430 Apr 26 14:39 f2
-rw-r–r–. 1 root root 2430 Apr 26 14:39 f3
[root@CENTOS7 ~]#ll /mnt/lv0
total 4
-rw-r–r–. 1 root root    0 Apr 26 14:47 f2
-rw-r–r–. 1 root root 2430 Apr 26 14:39 f3
[root@CENTOS7 ~]#
[root@CENTOS7 ~]#lvconvert –merge /dev/vg0/lv0-snapshot
  Merging of volume vg0/lv0-snapshot started.
  lv0: Merged: 100.00%
[root@CENTOS7 ~]#mount /dev/vg0/lv0 /mnt/lv0/
[root@CENTOS7 ~]#ls /mnt/lv0/
f1  f2  f3
逻辑卷管理器快照
  快照就是将当时的系统信息记录下来,就好像照相一般,若将来有任何数据
改动了,则原始数据会被移动到快照区,没有改动的区域则由快照区和文件
系统共享
  由于快照区与原本的LV共用很多PE的区块,因此快照与被快照的LV必须在同
一个VG中.系统恢复的时候的文件数量不能高于快照区的实际容量
使用LVM快照
  为现有逻辑卷创建快照
lvcreate -l 64 -s -n data-snapshot -p r /dev/vg0/data
  挂载快照
mkdir -p /mnt/snap
mount -o ro /dev/vg0/data-snapshot /mnt/snap
  恢复快照
umount /dev/vg0/data-snapshot
umount /dev/vg0/data
lvconvert –merge /dev/vg0/data-snapshot
  删除快照
umount /mnt/databackup
lvremove /dev/vg0/databackup
网络管理
什么是计算机网络
一组网络设备,通过有形的或者无形的媒介,把他们连接起来,按照一定得通讯规则来通讯
资源共享的功能和优点
  数据和应用程序
  资源
  网络存储
  备份设备
常见的网络物理组件
网卡,交换机,路由器,网线,水晶头
网络应用程序
  Web 浏览器(Chrome、IE、Firefox等)
  即时消息(QQ、微信、钉钉等)
  电子邮件(Outlook、foxmail 等)
  协作(视频会议、VNC、Netmeeting、WebEx 等)
  web网络服务(apache,nginx,IIS)
  文件网络服务(ftp,nfs,samba)
  数据库服务( MySQL,MariaDB, MongoDB)
  中间件服务(Tomcat,JBoss)
  安全服务( Netfilter)
用户应用程序对网络的影响
   批处理应用程序
   FTP、TFTP、库存更新
   无需直接人工交互
   带宽很重要,但并非关键性因素
   交互式应用程序    淘宝购物
   库存查询、数据库更新
   人机交互
   因为用户需等待响应,所以响应
时间很重要,但并非关键性因素,
除非要等待很长时间
   实时应用程序  微信
   VoIP、视频
   人与人的交互
   端到端的延时至关重要
网络的特征
  速度   带宽
  成本
  安全性
  可用性     正常运行时间占总时长的百分比。集群(cluster)可以快速的替换坏掉的网络实现高可用性
  可扩展性   支持100用户,加一些插件或者扩展槽支持200人使用.所以现在好多的硬件软件都是模块化的.
  可靠性     网络不断线
  拓扑
带宽:通常所说的100M是100Mps 而资源是1k所以100M的带宽理论上是12.8M/s
物理拓扑分类
   物理拓扑描述了物理设备的布线方式
总线型:铜轴电缆
环状
星型
逻辑拓扑
  逻辑拓扑描述了信息在网络中流动的方式
总线拓扑
  所有设备均可接收信号
总线型:每次只能有一个信息发送,如果两个信息同时发送就会互相干扰碰撞造成损坏。如果中间发生断裂,会使网络瘫痪,信号会在断裂的地方反弹.
星型拓扑
  通过中心点传输
  单一故障点      如果中间交换机坏点网络瘫痪
简单地说:交换机用来共享一根网线,路由器用来共享一个IP。
也就是说,如果一根网线上想接几个电脑,就必须用,交换机;
如果你只有一个IP,想几个电脑上网,就要用路由器来解决。比如宽带路由器就可以让家里的几台电脑共享同一个账号上网;
在已经共享上网的情况下,某房间只有一根网线,有多台电脑要上网,用交换机。
扩展星型拓扑
  比星型拓扑的复原能力更强
环拓扑
  信号绕环传输     信号只能按一个方向传输  不会产生冲突
  单一故障点
双环拓扑
  信号沿相反方向传输    主环逆时针,辅环顺时针,主环失败辅环顶上
  比单环的复原能力更强
全网状拓扑
  容错能力强
  实施成本高
部分网状拓扑
  在容错能力与成本之间寻求平衡
了解主机到主机通信
  旧模型
  专有产品
  由一个厂商控制应用程序和嵌入的软件
  基于标准的模型
  多厂商软件
  分层方法
网络模型分层
  降低复杂性
  标准化接口
  简化模块化设计
  确保技术的互操作性
  加快发展速度
  简化教学
OSI 开放系统互联
ISO 国际标准化组织
IOS 炒作系统
ATM 异步传输模式
OSI 模型的七层结构
7.应用层:网络进程访问应用层,为应用程序进程(列如电子邮件文件传输和终端仿真)
提供网络服务,提供用户身份验证
6.表示层:数据表示,确保接受系统可以读出数据,格式化数据,建立数据
协商用于应用层的数据传输语法,提供加密
5.会话层:主机间通信,建立管理和终止在应用程序之间的会话,PDU是 message 即消息。
4.传输层:数据传输单位叫段segment 即数据段,传输问题,确保数据传输可靠性,
一但发现错误会让用户从新再传,建立维护和终止虚拟电路,通过错误检查和恢复,信息流控制来保证可靠性
3.网络层:单位叫做packet,其中有地址是逻辑地址就是IP地址。路由:到哪去选择一条最佳路径。
2.数据链路层:将2进制格式化,那些表示原地址,那些表示目标地址等。
访问介质定义如何格式化数据以便进行传输以及如何控制对网络的访问,支持错误检查。
 将多个0101组合起来形成的单位交帧(frame)CRC校验位
在帧中有物理地:MAC地址
1.物理层:2进制传输,为启动维护以及关闭物理链路定义了电器规范·机械规范·过程规范和功能规范 以bit为单位
数据封装
从7层开始,逐步添加各层HDR=报头。在数据链路层鬼添加循环FCS教研位
物理层由物理设备完成如网卡,数据链路层也是网卡实现的,传输层和网络层与系统相关
上3层有应用实现,下4层由物理设备或者操作系统实现
PDU
  PDU: Protocol Data Unit,协议数据单元是指对等层次之间传递的数据单位
  物理层的 PDU是数据位 bit
  数据链路层的 PDU是数据帧 frame
  网络层的PDU是数据包 packet
  传输层的 PDU是数据段 segment
  其他更高层次的PDU是消息 message
3种通讯模式
1.单播(unicasl)点对点但是其他人也能接受到数据,只是被电脑忽略。将网卡设置成混杂模式不管是不是你的目的地址都会接收,单播并不安全。
2.广播(broadcasl)对网络覆盖内的所有人
3.组播(multicast)对一个小组
局域网Local Area Network简称(LAN)
LAN  组成
  Computers
   PCs
   Servers
  Interconnections
   NICs 网卡
  Media 网线
  Network devices
   Hubs   集线器
   Switches  交换机
   Routers   路由器
  Protocols  通讯网络协议
   Ethernet
   IP
   ARP
   DHCP
网络线缆和接口
Twisted-Pair  双绞线
10(10Mbps)Base(基带传输,传输的是数字信号。宽带传输的是模拟信号)T(双绞线)
Unshielded (UTP)非屏蔽
Shielded (STP)屏蔽
Coaxial
10(10Mbps)Base2(200米), 10Base5(500米)
ThinNet
ThickNet
Fiber-Optic
线序
T568B  橙白,橙,绿白,蓝,蓝白,绿,棕白,棕。网速低于100M以内只用1236,4跟线,千兆的时候8跟都用上,如果不按线序来排,会使电磁干扰特别严重,导致丢包严重。
T568A  1,3, 2,6    互换
UTP直通线(Straight-Through)
Cable 10BASE-T/
100BASE-TX Straight-Through
1,2(TX)号线负责发送数据
3,6(RX)号线负责接收数据
如果是两台电脑同线序相连无法通讯
想要连通需要1.3 和 2,6相连
同设备交叉,不同设备直连
因为现在的电脑网卡有智能识别,所以大多数时候不需要交叉
百兆1.2传送数据
3. 6  接收数据
千兆8跟线都可发送接收数据
1000BASE-T GBIC
GBIC 是Giga Bitrate Interface Converter的缩写,是将千兆位电 的缩写,是将千兆位电
信号转换为光信号的接口器件。
Ethernet Evolution(以太网)
IEEE国际电子电器工程师协会
IEEE802.3 10M
IEEE802.3u  百兆
IEEE802.3z 千兆光纤
IEEE802.3ab普通网线千兆
IEEE802.5以太环网
IEEE802.3没有考虑协议复杂性
Novell网络  ipx 网络层  ip
以太网 数据联络层,没有标识出上层服务协议的复杂性
Ethernet Frame2结构
1.8字节的前导信息
2.6个字节的目标MAC物理地址
3.6个字节的源地址
4.上层协议的类型  2个字节
5.数据  46-1500个字节
6.教研位  4个字节
72-1526个字节
Ethernet Frame  和2不同的是4.长度
数据链路层
ether 00:0c:29:45:bf:14  物理地址  网卡出厂的时候指定一个世界唯一的地址
00:0c:29:    国际组织分配给厂家的地址
45:bf:14     厂家自己指定
MAC地址
b8 b7 b6 b5 b4 b3 b2(0代表国际指定1代表本地指定)b1(0代表单播1代表多播)
48个1就是广播

 

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/97270

发表评论

登录后才能评论

联系我们

400-080-6560

在线咨询:点击这里给我发消息

邮件:1823388528@qq.com

工作时间:周一至周五,9:30-18:30,节假日同时也值班