1、创建一个10G分区,并格式为ext4文件系统;
(1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;
(2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳;
[root@localhost ~]# fdisk /dev/sda (创建分区)
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 (添加新分区)
First cylinder (10499-15666, default 10499):
Using default value 10499
Last cylinder, +cylinders or +size{K,M,G} (10499-15666, default 15666): +10G (输入分区大小)
[root@localhost ~]# mke2fs -t ext4 -b 2048 -L "MYDATA" -m 2 /dev/sda8 (格式化新分区)
[root@localhost ~]# tune2fs -o acl /dev/sda8 (修改分区默认挂在参数)
[root@localhost ~]# tune2fs -l /dev/sda8 |grep acl (查询是否修改成功)
Default mount options: acl
[root@localhost ~]# mkdir -p /data/mydata (创建/data/mydata目录)
[root@localhost ~]# mount -t ext4 -o noexec,noatime /dev/sda8 /data/mydata/ (挂载/dev/sda8分区)
2、创建一个大小为1G的swap分区,并创建好文件系统,并启用之;
[root@localhost ~]# fdisk /dev/sda (创建分区)
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 (添加新分区)
First cylinder (10499-15666, default 10499):
Using default value 10499
Last cylinder, +cylinders or +size{K,M,G} (10499-15666, default 15666): +1G (输入大小)
Command (m for help): t (更改文件系统类型)
Partition number (1-9): 9
Hex code (type L to list codes): 82 (“82”为swap ID)
Changed system type of partition 9 to 82 (Linux swap / Solaris)
[root@localhost ~]# mkswap /dev/sda9 (格式化swap分区)
Setting up swapspace version 1, size = 1060252 KiB
no label, UUID=6751afd7-cdd0-4438-90fa-f18f4d6391b0
[root@localhost ~]# free (查看启动前的swap大小)
total used free shared buffers cached
Mem: 1004412 142684 861728 0 6944 37928
-/+ buffers/cache: 97812 906600
Swap: 2047992 0 2047992
[root@localhost ~]# swapon /dev/sda9 (启动刚创建swap分区)
[root@localhost ~]# free (查看启动后的swap大小)
total used free shared buffers cached
Mem: 1004412 143180 861232 0 6956 37940
-/+ buffers/cache: 98284 906128
Swap: 3108240 0 3108240
3、写一个脚本
(1)、获取并列出当前系统上的所有磁盘设备;
(2)、显示每个磁盘设备上每个分区相关的空间使用信息;
[root@localhost ~]# vim disk.sh
#!/bin/bash
# Author: liuguohui
# Date: 2016/09/25 17:00
# Email: liuguohui1008@sina.com
# Description: display disk messages
#
echo "Disk Device:"
fdisk -l |grep "^Disk /dev/[sh]d" |cut -d : -f 1
echo "========================================="
echo:"Partitions messages:"
df -h |grep "^/dev/[sh]d"
[root@localhost test]# bash disk.sh (脚本运行效果)
Disk Device;
Disk /dev/sda
===========================================
Partitions messages:
/dev/sda2 20G 387M 18G 3% /
/dev/sda1 194M 27M 158M 15% /boot
/dev/sda6 9.7G 151M 9.0G 2% /home
/dev/sda3 20G 589M 18G 4% /usr
/dev/sda5 20G 229M 19G 2% /var
4、总结RAID的各个级别及其组合方式和性能的不同;
RAID0:
不含校验与冗余的条带存储,多块磁盘组合为RAID0后,数据将被分割并分别存储在每块硬盘中,所以能最大的提升读写性能,但无法容错,如果其中一块磁盘损坏,将导致所有数据都无法使用,至少需要两块磁盘。
RAID1:
不含校验的镜像存储,多块磁盘组合为RAID1后,数据将被同时复制到每块磁盘中,读性能提升,写性能下降,磁盘空间利用率降低,如果其中一个块磁盘损坏,不会影响数据的正常使用,至少需要两块磁盘。
RAID2:
位级别的校验式条带存储,多块磁盘组合为RAID2后,数据将以位(bit)为单位同步式分别存储在不同的磁盘上,并采用海明码对数据进行校验与恢复。拥有校验位的RAID2允许三块磁盘中的任何一块磁盘损坏,并能够对未损坏的两块磁盘进行运算后还原已损坏磁盘上的数据,从而实现数据恢复的功能。对大数据量的读写具有很高的性能,但对少量数据的读写性能反而不好,至少需要三块磁盘。
RAID3:
字节级别的校验式条带存储,该级别的磁盘阵列与RAID2的读写原理相似,仅数据分割的单位为字节。
RAID4:
数据块级别的校验式条带存储,该级别的磁盘阵列与RAID2类似,仅数据分割单位为块。
RAID5:
数据块级别的分布式校验条带存储,多块磁盘组合为RAID5后,数据将以块为单位同步式分别存储在不同的磁盘上,并对数据进行海明码运算,与其他级别不同的是,该级别的海明码会被写入不同的磁盘。在每块磁盘上,既有数据信息也有校验信息,这一改变解决了争用校验盘的问题,使得同一组内并发进行多个写操作。适用于大数据量的操作,在一块硬盘发生故障后,RAID组online变为degraded方式,但I/O读写不受影响,直到故障盘恢复,但如果degraded状态下,又有第二块盘故障,整个RAID组的数据将丢失。
RAID6:
同RAID5最大的区别就是在RAID5的基础上除了具有P校验位以外,还加入了第2个校验位Q位。相当于在RAID5的基础之上再增加一个额外的冗余保护。它使用两个校验磁盘而不是一个,也就是说它能够抵御两块磁盘损坏之后的数据丢失风险。但是其读性能较差。
RAID 0+1
对数据进行分条和镜像,性能和可靠性较高。一个磁盘驱动器发生故障,不会影响性能和可靠性,而在RAID0中,驱动器故障会影响性能和可靠性。另外,磁盘分条技术可以提高性能。
RAID 1+0
与RAID 0+1相似,对数据进行分条和镜像,性能和可靠性较高。不同之处在于RAID10对所有磁盘进行集体分条,然后实现镜像功能。
5、创建一个大小为10G的RAID1,要求有一个空闲盘,而且CHUNK大小为128k;
(已创建好/dev/sda{8,9,10}分区)
[root@localhost ~]# mdadm -C -a yes /dev/md1 -n 2 -l 1 -c 128 -x 1 /dev/sda{8,9,10} (创建raid1)
[root@localhost ~]# mdadm -D /dev/md1 (查看结果)
/dev/md0:
Version : 1.2
Creation Time : Sun Sep 25 12:30:56 2016
Raid Level : raid1
Array Size : 1052032 (1027.38 MiB 1077.28 MB)
Used Dev Size : 1052032 (1027.38 MiB 1077.28 MB)
Raid Devices : 2
Total Devices : 3
Persistence : Superblock is persistent
Update Time : Sun Sep 25 12:31:02 2016
State : clean
Active Devices : 2
Working Devices : 3
Failed Devices : 0
Spare Devices : 1
Name : localhost:0 (local to host localhost)
UUID : 5a504276:7560f20a:c08c7b21:c0459a10
Events : 17
Number Major Minor RaidDevice State
0 8 8 0 active sync /dev/sda8
1 8 9 1 active sync /dev/sda9
2 8 10 – spare /dev/sda10
6、创建一个大小为4G的RAID5设备,chunk大小为256k,格式化ext4文件系统,要求可开机自动挂载至/backup目录,而且不更新访问时间戳,且支持acl功能;
[root@localhost ~]# mdadm -C /dev/md5 -n 3 -l 5 -c 256 /dev/sda{8,9,10} (创建raid5)
[root@localhost ~]# mdadm -D /dev/md5 (查看结果)
/dev/md5:
Version : 1.2
Creation Time : Sun Sep 25 15:19:36 2016
Raid Level : raid5
Array Size : 8391680 (8.00 GiB 8.59 GB)
Used Dev Size : 4195840 (4.00 GiB 4.30 GB)
Raid Devices : 3
Total Devices : 3
Persistence : Superblock is persistent
Update Time : Sun Sep 25 15:21:44 2016
State : clean
Active Devices : 3
Working Devices : 3
Failed Devices : 0
Spare Devices : 0
Layout : left-symmetric
Chunk Size : 256K
Name : localhost:5 (local to host localhost)
UUID : cd8a5920:6c9c1482:f1bafea2:05bc76eb
Events : 20
Number Major Minor RaidDevice State
0 8 8 0 active sync /dev/sda8
1 8 9 1 active sync /dev/sda9
3 8 10 2 active sync /dev/sda10
[root@localhost ~]# mke2fs -t ext4 /dev/md5 (格式化ext4文件系统)
[root@localhost ~]# vim /etc/fstab (编辑/etc/fstab文件)
写入如下:
/dev/md127 /backup ext4 acl,noatime 0 0
7、写一个脚本
(1) 接受一个以上文件路径作为参数;
(2) 显示每个文件拥有的行数;
(3) 总结说明本次共为几个文件统计了其行数;
#!/bin/bash
# Author: liuguohui
# Date: 2016-10-04
# Email: liuguohui1008@sina.com
# Descriptions: count file lines
#
[ $# -lt 2 ] && echo "At least two filename" && eixt 2 (测试条件:参数必须大于2个)
for i in $*; do (使用for循环统计输入文件内容的行数)
echo "filename $i: $(cat $i | wc -l)"
done
echo "total file: $#" (显示一共统计的文件个数)
脚本运行效果:
[root@localhost test]# bash file6.sh /etc/issue /etc/fstab
filename /etc/issue: 3
filename /etc/fstab: 18
total file: 2
8、写一个脚本
(1) 传递两个以上字符串当作用户名;
(2) 创建这些用户;且密码同用户名;
(3) 总结说明共创建了几个用户;
#!/bin/bash
# Author: liuguohui
# Date: 2016-10-04 16:42
# Email: liuguohui1008@sina.com
# Descriptions: useradd user
#
[ $# -lt 2 ] && echo "At least two string" && exit 2 #测试传递的字符串不能少于2个
for i in $*; do
if id $i &> /dev/null; then #测试传递的字符串是否已经存在
echo "$i already exists" && exit 2
fi
useradd $i #创建用户
echo "$i create user finished" #显示成功信息
echo $i |passwd –stdin $i &> /dev/null #指定用户名为用户密码
echo "$i set password successfully" #显示成功信息
done
echo "total users:$#" #统计一共创建的用户数量
[root@localhost test]# bash file5.sh join1 join2 (验证执行脚本结果)
join1 create user finished
join1 set password successfully
join2 create user finished
join2 set password successfully
total users:2
9、写一个脚本,新建20个用户,visitor1-visitor20;计算他们的ID之和;
#!/bin/bash
# Author: liuguohui
# Date: 2016-10-04 18:24
# Email: liuguohui1008@sina.com
# Descriptions: useradd user and count all userid sum
#
declare -i sum=0 #声明变量sum=0
for i in {1..20}; do
useradd visitor$i #创建用户visitor1-visitor20
user_id=$(id -u visitor$i) #取出用户id并赋值给变量user_id
sum=$[$sum+$user_id] #计算所有用户的id和
done
echo $sum #输出最后求和的值
[root@localhost test]# bash file5.sh (脚本执行结果)
10190
10、写一脚本,分别统计/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#号开头的行数之和,以及总的空白行数;
#!/bin/bash
# Author: liuguohui
# Date: 2016-10-04 18:40
# Email: liuguohui1008@sina.com
# Descriptions: total file line
#
[ $# -lt 1 ] && echo "At least a argument" && exit 2 (测试参数必须大于1个)
declare -i line_sum1=0 (声明变量line_sum1=0)
declare -i line_sum2=0 (声明变量line_sum2=0)
for i in $*; do (利用for循环计算题目要求的行数之和)
line=$(grep "^#" $i |wc -l)
line_sum1=$[$line+$line_sum1]
line1=$(grep "^$" $i |wc -l)
line_sum2=$[$line1+$line_sum2]
done
echo "whih # line:$line_sum1" (显示'#'开头的行的总和)
echo "whitespace line:$line_sum2" (显示空白行的总和)
脚本运行结果:
[root@localhost test]# bash file6.sh /etc/rc.d/rc.sysinit /etc/fstab /etc/rc.d/init.d/functions
whih # line:94
whitespace line:206
11、写一个脚本,显示当前系统上所有默认shell为bash的用户的用户名、UID以及此类所有用户的UID之和;
#!/bin/bash
# Author: liuguohui
# Date: 2016-10-04 19:05
# Email: liuguohui1008@sina.com
# Descriptions: total uid_sum
#
declare -i uid_sum=0 (声明变量uid_sum=0)
grep "bash$" /etc/passwd |awk -F: '{print $1,$3}' (使用正则表达式和awk工具输出用户名和UID)
for i in $(grep "bash$" /etc/passwd |awk -F: '{print $3}'); do (使用for循环计算所有符合条件的用户UID之和)
let uid_sum+=$i
done
echo "uid_sum:$uid_sum" (显示UID总和)
12、写一个脚本,显示当前系统上所有,拥有附加组的用户的用户名;并说明共有多少个此类用户;
#!/bin/bash
# Author: liuguohui
# Date: 2016-10-04 19:33
# Email: liuguohui1008@sina.com
# Descriptions: total user
#
declare -i count=0 (声明变量count=0,用于for循环里计算次数)
for i in $(awk -F: '{print $1}' /etc/passwd); do (使用awk输出所有用户,for循环遍历所有用户并利用groups命令列出用户的所属组,再用wc -w统计,如大于3则有附加组)
if [ $(groups $i |wc -w) -gt 3 ]; then
echo "$i" (显示有附加组的用户名)
let count++ (累计符合条件结果的次数)
fi
done
echo "total user: $count" (显示最后符合条件用户的总数)
脚本执行结果:
[root@localhost test]# bash file6.sh
bin
daemon
adm
postfix
total user: 4
13、创建一个由至少两个物理卷组成的大小为20G的卷组;要求,PE大小为8M;而在卷组中创建一个大小为5G的逻辑卷mylv1,格式化为ext4文件系统,开机自动挂载至/users目录,支持acl;
[root@localhost ~]# pvcreate /dev/sda{9,10} (创建PV)
Physical volume "/dev/sda9" successfully created
Physical volume "/dev/sda10" successfully created
root@localhost ~]# pvscan (查询创建结果)
PV /dev/sda9 lvm2 [10.00 GiB]
PV /dev/sda10 lvm2 [10.00 GiB]
Total: 2 [20.00 GiB] / in use: 0 [0 ] / in no VG: 2 [20.00 GiB]
[root@localhost ~]# vgcreate -s 8M lvm_one /dev/sda{9,10} (创建VG)
Volume group "lvm_one" successfully created
[root@localhost ~]# vgdisplay (查询VG信息)
— Volume group —
VG Name lvm_one
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 19.99 GiB
PE Size 8.00 MiB
Total PE 2559
Alloc PE / Size 0 / 0
Free PE / Size 2559 / 19.99 GiB
VG UUID ndRzYS-o1mK-QZP7-0t1H-4iGQ-92Yj-mUShkn
[root@localhost ~]# lvcreate -L 5G -n mylv1 lvm_one (创建LV)
Logical volume "mylv1" created.
[root@localhost ~]# lvdisplay (查看LV信息)
— Logical volume —
LV Path /dev/lvm_one/mylv1
LV Name mylv1
VG Name lvm_one
LV UUID 1kYyft-BTvo-s7Qq-NPp9-onux-LehL-HLPpxD
LV Write Access read/write
LV Creation host, time localhost, 2016-10-07 03:46:58 -0400
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:0
[root@localhost ~]# mke2fs -t ext4 /dev/lvm_one/mylv1 (格式化mylv1)
[root@localhost ~]# vim /etc/fstab (编辑/etc/fstab)
输入如下:
/dev/lvm_one/mylv1 /users ext4 acl 1 2
[root@localhost ~]# mount -a (挂载/etc/fstab文件中所有设备)
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 20G 393M 18G 3% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 194M 27M 158M 15% /boot
/dev/sda6 9.7G 151M 9.0G 2% /home
/dev/sda3 20G 590M 18G 4% /usr
/dev/sda5 20G 230M 19G 2% /var
/dev/mapper/lvm_one-mylv1 5.0G 138M 4.6G 3% /users
14、新建用户magedu;其家目录为/users/magedu,而后su切换至此用户,复制多个文件至家目录;
[root@localhost ~]# useradd -d /users/magedu magedu ;echo "magedu" |passwd –stdin magedu (创建magedu用户,密码为用户名)
Changing password for user magedu.
passwd: all authentication tokens updated successfully.
[root@localhost magedu]# cp /var/log/messages ./ (复制文件到当前目录)
15、扩展mylv1至9G,确保扩展完成后原有数据完全可用;
[root@localhost magedu]# lvextend -L +4G /dev/lvm_one/mylv1 (调整LV大小)
Size of logical volume lvm_one/mylv1 changed from 5.00 GiB (640 extents) to 9.00 GiB (1152 extents).
Logical volume mylv1 successfully resized.
[root@localhost magedu]# lvscan (查看调整结果)
ACTIVE '/dev/lvm_one/mylv1' [9.00 GiB] inherit
[root@localhost ~]# resize2fs /dev/lvm_one/mylv1 9G (调整文件系统大小)
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/lvm_one/mylv1 is mounted on /users; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/lvm_one/mylv1 to 2359296 (4k) blocks.
The filesystem on /dev/lvm_one/mylv1 is now 2359296 blocks long.
[root@localhost magedu]# tail -n 5 messages (查看之前复制的文件)
Oct 5 05:20:28 localhost yum[1538]: Erased: openssh-server
Oct 5 05:20:28 localhost yum[1538]: Erased: openssh
Oct 5 05:20:28 localhost yum[1538]: Erased: libdrm
Oct 5 05:20:29 localhost kernel: Kernel logging (proc) stopped.
Oct 5 05:20:29 localhost rsyslogd: [origin software="rsyslogd" swVersion="5.8.10" x-pid="963" x-info="http://www.rsyslog.com"] exiting on signal 15.
16、缩减mylv1至7G,确保缩减完成后原有数据完全可用;
[root@localhost ~]# umount /users (卸载/dev/lvm_one/mylv1)
[root@localhost ~]# e2fsck -f /dev/lvm_one/mylv1 (强制检查/dev/lvm_one/mylv1)
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/lvm_one/mylv1: 945/589824 files (0.0% non-contiguous), 79125/2359296 blocks
[root@localhost ~]# resize2fs /dev/lvm_one/mylv1 7G (调整文件系统大小)
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/lvm_one/mylv1 to 1835008 (4k) blocks.
The filesystem on /dev/lvm_one/mylv1 is now 1835008 blocks long.
[root@localhost ~]# mount /dev/lvm_one/mylv1 /users (挂载/dev/lvm_one/mylv1)
[root@localhost ~]# df -h (查看/dev/lvm_one/mylv1大小是否改变)
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 20G 393M 18G 3% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 194M 27M 158M 15% /boot
/dev/sda6 9.7G 151M 9.0G 2% /home
/dev/sda3 20G 590M 18G 4% /usr
/dev/sda5 20G 230M 19G 2% /var
/dev/mapper/lvm_one-mylv1 6.9G 165M 6.4G 3% /users
[root@localhost ~]# lvreduce -L 7G /dev/lvm_one/mylv1 (调整/dev/lvm_one/mylv1大小)
WARNING: Reducing active and open logical volume to 7.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lvm_one/mylv1? [y/n]: y
Size of logical volume lvm_one/mylv1 changed from 9.00 GiB (1152 extents) to 7.00 GiB (896 extents).
Logical volume mylv1 successfully resized.
[root@localhost ~]# lvscan (查看是否成功)
ACTIVE '/dev/lvm_one/mylv1' [7.00 GiB] inherit
[root@localhost magedu]# tail -n 5 messages (验证文件是否损坏)
Oct 5 05:20:28 localhost yum[1538]: Erased: openssh-server
Oct 5 05:20:28 localhost yum[1538]: Erased: openssh
Oct 5 05:20:28 localhost yum[1538]: Erased: libdrm
Oct 5 05:20:29 localhost kernel: Kernel logging (proc) stopped.
Oct 5 05:20:29 localhost rsyslogd: [origin software="rsyslogd" swVersion="5.8.10" x-pid="963" x-info="http://www.rsyslog.com"] exiting on signal 15.
17、对mylv1创建快照,并通过备份数据;要求保留原有的属主属组等信息;
[root@localhost magedu]# lvcreate -s -L 512M -n mylv1-snap /dev/lvm_one/mylv1 (创建快照)
Logical volume "mylv1-snap" created.
[root@localhost magedu]# mount /dev/lvm_one/mylv1-snap /mnt (挂载快照)
[root@localhost magedu]# cp -a /mnt /tmp/backup/ (通过快照备份数据到/tmp/backup/目录下)
原创文章,作者:heianyangguo,如若转载,请注明出处:http://www.178linux.com/48962