第七周作业
Tags: 作业练习
一、 创建一个10G分区,并格式为ext4文件系统;
(1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;
(2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳;
```
1、格式化一块10G的磁盘空间出来
fdisk /dev/sdb
n
1
p
默认起始
+10G
2、创建为ext4的文件系统
mk2fs -t ext4 -b 2048 -L MYDATA -m 2 /dev/sdb1
3、修改默认挂载属性包含acl
tune2fs -o acl /dev/sdb1
4、挂载至/data/mydata
mkdir -p /data/mydata
amount -o noexec noatime /data/mydata /dev/sdb1
开机自动挂载:
在/etc/fstab中添加一行
LALEL=MYDATA /data/mydata ext4 defaults,acl,noexec,noatime 0 0
mount -a
```
二、创建一个大小为1G的swap分区,并创建好文件系统,并启用之;
```
1、先格式化一块1G的磁盘分区
fdisk /dev/sdb
n
2
p
默认起始值
+1G
调整磁盘类型为swap可用
t
82
w
2、重读分区
kpartx -af /dev/sdb
3、创建swap文件系统
(1)、临时
mkswap /dev/sdb2
swapon /dev/sdb2
(2)、开机启动挂载
编辑/etc/fstab,增加
/dev/sdb2 swap swap defaults 0 0
```
三、写一个脚本
(1)、获取并列出当前系统上的所有磁盘设备;
(2)、显示每个磁盘设备上每个分区相关的空间使用信息;
```
LANG='en_us.UTF-8' #修改某些非英文语言系统
disks=`fdisk -l|grep '^Disk /dev/[s|h]d[a-z|]'|cut -d: -f1|cut -d\ -f2`
echo disks
for i in disks;do
fdisk -l i
done;
```
四、总结RAID的各个级别及其组合方式和性能的不同;
- RAID常用级别有 RAID0,RAID1,RAID5,RAID10
RAID0
RAID0,最少有2个磁盘,在存入数据的时候,把数据拆分成片段,依次将数据存入两个磁盘中。示意图如下:
性能
多个磁盘同时拆开保存一段数据,所以速度比一般的要快。
冗余
无冗余能力,有一个磁盘坏掉,数据丢失。数据完好的概率是单独用一个磁盘的1/n。
适合对于性能要求比较高,数据不太重要的地方使用。一般用于个人应用。
RAID1
Paste_Image.pngRAID1,最少有2个磁盘,在存入数据的时候,每个数据片段,复制一份,存入多个磁盘中。示意图如下
性能
复制存储数据,相当于一份数据要存储多变,性能很低。
冗余
有比较强的冗余能力,主磁盘读取不到数据时,会在备用磁盘上进行读取。
费用也很高,适用于存放重要数据,如服务器和数据库存储等领域。
RAID5
Paste_Image.pngRAID5是在raid0的基础上,通过校验码计算的方式,通过几份数据,计算出他们的校验码。如果有一块磁盘出问题,可以通过其他磁盘对应数据块的数据计算出问题盘的数据。所以是raid0和raid1的折中方案。示例图如下:
性能
读取速度接近于RAID0,写的速度略高于RAID0,多了一个校验盘的写入。
冗余能力
有一定的冗余能力,允许一块磁盘出现问题。
是一种存储性能、数据安全和存储成本兼顾的存储解决方案。
RAID10
Paste_Image.pngRAID10是RAID1和RAID0的结合使用。将一对磁盘组成RAID1后,再讲多对RAID1按照RAID0的方式组合起来,示意图如下:
性能
读:RAID10=RAID0 写:RAID10=RAID1
冗余能力
跟RAID1一样,允许数据对应位置的磁盘坏掉一个。每组镜像最多只能坏一块。
磁盘数量
4个或4个以上(必须成对增加)
五、创建一个大小为10G的RAID1,要求有一个空闲盘,而且CHUNK大小为128k;
1、 格式化三块大小为5G(由于磁盘太小就5G)的分区/dev/sdb1 /dev/sdb2 /dev/sdb3
2、创建raid1
[root@localhost ~]# mdadm -C /dev/md0 -l 1 -c 128 -n 2 -x 1 /dev/sdb1 /dev/sdb2 /dev/sdb3
mdadm: /dev/sdb1 appears to contain an ext2fs file system
size=10485760K mtime=Thu Oct 13 10:38:39 2016
mdadm: Note: this array has metadata at the start and
may not be suitable as a boot device. If you plan to
store '/boot' on this device please ensure that
your boot-loader understands md/v1.x metadata, or use
--metadata=0.90
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
3、查看raid1状态
[root@localhost ~]# mdadm -D /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Thu Oct 13 15:06:49 2016
Raid Level : raid1
Array Size : 5238784 (5.00 GiB 5.36 GB)
Used Dev Size : 5238784 (5.00 GiB 5.36 GB)
Raid Devices : 2
Total Devices : 3
Persistence : Superblock is persistent
Update Time : Thu Oct 13 15:07:12 2016
State : clean, resyncing
Active Devices : 2
Working Devices : 3
Failed Devices : 0
Spare Devices : 1
Resync Status : 91% complete
Name : localhost.localdomain:0 (local to host localhost.localdomain)
UUID : ebc9581c:20f4b9a8:102088b0:8e7ad539
Events : 14
Number Major Minor RaidDevice State
0 8 17 0 active sync /dev/sdb1
1 8 18 1 active sync /dev/sdb2
2 8 19 - spare /dev/sdb3
六. 创建一个大小为4G的RAID5设备,chunk大小为256k,格式化ext4文件系统,要求可开机自动挂载至/backup目录,而且不更新访问时间戳,且支持acl功能;
1、格式化三块大小为5G的分区/dev/sdb1 /dev/sdb2 /dev/sdb3
2、创建raid5设备
[root@localhost ~]# mdadm -C /dev/md0 -l 5 -n 3 -c 256 /dev/sdb1 /dev/sdb2 /dev/sdb3
mdadm: /dev/sdb1 appears to contain an ext2fs file system
size=10485760K mtime=Thu Oct 13 10:38:39 2016
mdadm: /dev/sdb1 appears to be part of a raid array:
level=raid1 devices=2 ctime=Thu Oct 13 15:06:49 2016
mdadm: /dev/sdb2 appears to be part of a raid array:
level=raid1 devices=2 ctime=Thu Oct 13 15:06:49 2016
mdadm: /dev/sdb3 appears to be part of a raid array:
level=raid1 devices=2 ctime=Thu Oct 13 15:06:49 2016
Continue creating array? t
Continue creating array? (y/n) y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
3、查看raid5
[root@localhost ~]# mdadm -D /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Thu Oct 13 15:18:35 2016
Raid Level : raid5
Array Size : 10477568 (9.99 GiB 10.73 GB)
Used Dev Size : 5238784 (5.00 GiB 5.36 GB)
Raid Devices : 3
Total Devices : 3
Persistence : Superblock is persistent
Update Time : Thu Oct 13 15:19:05 2016
State : clean
Active Devices : 3
Working Devices : 3
Failed Devices : 0
Spare Devices : 0
Layout : left-symmetric
Chunk Size : 256K
Name : localhost.localdomain:0 (local to host localhost.localdomain)
UUID : 94fc5461:5eb50e79:7e2a1c15:f98de37b
Events : 18
Number Major Minor RaidDevice State
0 8 17 0 active sync /dev/sdb1
1 8 18 1 active sync /dev/sdb2
3 8 19 2 active sync /dev/sdb3
4、格式化为ext4系统
[root@localhost ~]# mke2fs -t ext4 /dev/md0
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=64 blocks, Stripe width=128 blocks
655360 inodes, 2619392 blocks
130969 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=2151677952
80 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
Allocating group tables: 完成
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
5、开机挂载/backup
[root@localhost ~]# mkdir /backup
[root@localhost ~]# vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue Aug 2 19:48:21 2016
#
# 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
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=5f193028-e7c9-4aed-9506-3a37e24ce9d4 /boot xfs defaults 0 0
/dev/mapper/centos-home /home xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
#/dev/sdb2 swap swap defaults 0 0
/dev/md0 /backup ext4 defaults,noatime,acl 0 0
七. 写一个脚本
(1) 接受一个以上文件路径作为参数;
(2) 显示每个文件拥有的行数;
(3) 总结说明本次共为几个文件统计了其行数;
#!/bin/bash
if [ $# -lt 1 ];then
echo 'at least one param'
exit 1
fi
TEXT=0
for i in $*;do
if [ ! -f $i ];then
echo "${i} is not a ASCII text";
else
echo "${i} have "$(wc -l < ${i})" lines"
let TEXT++
fi
done
echo "count file numbers:"$TEXT
八、 写一个脚本
(1) 传递两个以上字符串当作用户名;
(2) 创建这些用户;且密码同用户名;
(3) 总结说明共创建了几个用户;
#!/bin/bash
if [ $# -lt 1 ];then
echo 'at least one param'
exit 1
fi
TEXT=0
for i in $*;do
if [ ! -f $i ];then
echo "${i} is not a ASCII text";
else
echo "${i} have "$(wc -l < ${i})" lines"
let TEXT++
fi
done
echo "count file numbers:"$TEXT
九. 写一个脚本,新建20个用户,visitor1-visitor20;计算他们的ID之和;
#!/bin/bash
# create 20 users,visitor1-visitor-20; and count the sum of their ID
idSum=0
for i in $(seq 20);do
if ! grep "^visitor${i}\>" /etc/passwd &> /dev/null;then
useradd visitor$i
fi
let idSum=$idSum+`id -u visitor${i}`
done
echo $idSum
十. 写一脚本,分别统计/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#号开头的行数之和,以及总的空白行数;
#!/bin/bash
# count lines begin with '^#' of those file:/etc/rc.d/rc.sysinit、/etc/rc.d/functions、/etc/fstab
# count space lines of those file:/etc/rc.d/rc.sysinit、/etc/rc.d/functions、/etc/fstab
startWithJingLines=0
spaceLines=0
files='/etc/rc.d/rc.sysinit /etc/rc.d/init.d/functions /etc/fstab'
for i in $files;do
if [ -f $i ];then
let startWithJingLines=${startWithJingLines}+$(grep '^#' ${i}|wc -l)
let spaceLines=${spaceLines}+$(grep '^$' ${i}|wc -l)
else
echo "$i not exits."
fi
done
echo "There has $startWithJingLines lines begin with # in the three files."
echo "There has $spaceLines space-line in the three files."
十一. 写一个脚本,显示当前系统上所有默认shell为bash的用户的用户名、UID以及此类所有用户的UID之和;
#!/bin/bash
userName="The user that default shell is bash has:"
uidSum=0
for i in $(grep '/bin/bash$' /etc/passwd);do
name=$(echo $i | cut -d: -f1)
uid=$(echo $i | cut -d: -f3)
userName=${userName}\ $name
let uidSum=${uidSum}+$uid
done
echo $userName
echo "The uid sum that default shell is bash is:"$uidSum
十二. 写一个脚本,显示当前系统上所有,拥有附加组的用户的用户名;并说明共有多少个此类用户;
#!/bin/bash
userName="The user that has odditional group has:"
num=0
for i in $(cat /etc/passwd|cut -d: -f1);do
name=$(echo $i | cut -d: -f1)
groups=$(id -G $name | wc -w)
if [ $groups -ge 2 ];then
userName=$userName\ $name
let num++
fi
done
echo $userName
echo "The sum user that has odditional group is:"$num
十三. 创建一个由至少两个物理卷组成的大小为20G的卷组;要求,PE大小为8M;而在卷组中创建一个大小为5G的逻辑卷mylv1,格式化为ext4文件系统,开机自动挂载至/users目录,支持acl;
1、先格式化2个分区,且调整分区系统类型为8e
2、创建pv
[root@localhost ~]# pvcreate /dev/sdc1
Can't open /dev/sdc1 exclusively. Mounted filesystem?
[root@localhost ~]# dmsetup remove sdc1
[root@localhost ~]# pvcreate /dev/sdc1
Physical volume "/dev/sdc1" successfully created
[root@localhost ~]# pvdisplay /dev/sdc1
"/dev/sdc1" is a new physical volume of "10.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdc1
VG Name
PV Size 10.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID GsVY4P-kA1b-Ji2I-pERJ-coFA-x6ml-q9eAbo
[root@localhost ~]# dmsetup remove sdc2
[root@localhost ~]# pvcreate /dev/sdc2
Physical volume "/dev/sdc2" successfully created
3、创建vg
[root@localhost ~]# vgcreate -s 8M /dev/sdc1 /dev/sdc2
/dev/sdc1: already exists in filesystem
Run `vgcreate --help' for more information.
[root@localhost ~]# vgcreate -s 8M myvg /dev/sdc1 /dev/sdc2
Volume group "myvg" successfully created
[root@localhost ~]# vgdisplay myvg
--- Volume group ---
VG Name myvg
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 15.01 GiB
PE Size 8.00 MiB
Total PE 1921
Alloc PE / Size 0 / 0
Free PE / Size 1921 / 15.01 GiB
VG UUID yW3dZh-TPgd-AUPN-3gfW-f8UR-cUU8-W1tXdf
4、创建lvm
[root@localhost ~]# lvcreate -L 5G -n mylvm myvg
Logical volume "mylvm" created.
[root@localhost ~]# lvdisplay /dev/myvg/mylvm
--- Logical volume ---
LV Path /dev/myvg/mylvm
LV Name mylvm
VG Name myvg
LV UUID jEkZ4X-BicC-mDfQ-TmQ6-lIgZ-MTNN-JSnXgb
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2016-10-14 06:36:18 +0800
LV Status available
# open 0
LV Size 5.00 GiB
Current LE 640
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:2
5、格式化为ext4文件系统
[root@localhost ~]# mke2fs -t ext4 /dev/myvg/mylvm
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
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 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
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
6、开机挂载
vim /etc/fstab
[root@localhost ~]# mkdir /users
[root@localhost ~]# vim /etc/fstab
/dev/myvg/mylvm /users ext4 defaults,acl 0 0
十四. 新建用户magedu;其家目录为/users/magedu,而后su切换至此用户,复制多个文件至家目录;
[root@localhost ~]# useradd magedu -d /users/magedu
[magedu@localhost ~]$ cp /etc/passwd /etc/fstab ./
[magedu@localhost ~]$ ls
fstab passwd
十五. 扩展mylv1至9G,确保扩展完成后原有数据完全可用;
[root@localhost ~]# lvextend -L 9G /dev/myvg/mylvm
Size of logical volume myvg/mylvm changed from 5.00 GiB (640 extents) to 9.00 GiB (1152 extents).
Logical volume mylvm successfully resized.
[root@localhost ~]# lvdisplay /dev/myvg/mylvm
--- Logical volume ---
LV Path /dev/myvg/mylvm
LV Name mylvm
VG Name myvg
LV UUID jEkZ4X-BicC-mDfQ-TmQ6-lIgZ-MTNN-JSnXgb
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2016-10-14 06:36:18 +0800
LV Status available
# open 0
LV Size 9.00 GiB
Current LE 1152
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:2
[root@localhost ~]# resize2fs /dev/myvg/mylvm
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/myvg/mylvm to 2359296 (4k) blocks.
The filesystem on /dev/myvg/mylvm is now 2359296 blocks long.
十六. 缩减mylv1至7G,确保缩减完成后原有数据完全可用;
1、先取消挂载
[root@localhost ~]# umount /dev//myvg/mylvm
2、文件系统强制检测
[root@localhost ~]# e2fsck -f /dev/myvg/mylvm
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/myvg/mylvm: 11/589824 files (0.0% non-contiguous), 72671/2359296 blocks
[root@localhost ~]# resize2fs /dev/myvg/mylvm 7G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/myvg/mylvm to 1835008 (4k) blocks.
The filesystem on /dev/myvg/mylvm is now 1835008 blocks long.
3、缩减逻辑边界
[root@localhost ~]# lvreduce -L 7G /dev/myvg/mylvm
WARNING: Reducing active logical volume to 7.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce myvg/mylvm? [y/n]: y
Size of logical volume myvg/mylvm changed from 9.00 GiB (1152 extents) to 7.00 GiB (896 extents).
Logical volume mylvm successfully resized.
十七. 对mylv1创建快照,并通过备份数据;要求保留原有的属主属组等信息;
[root@localhost ~]# lvcreate -L 512M -p r -s -n mylvm_snapshot /dev/myvg/mylvm
Logical volume "mylvm_snapshot" created.
[root@localhost ~]# mount /dev/myvg/mylvm_snapshot /mnt
网友评论