本周作业内容:
1、创建一个10G分区,并格式为ext4文件系统;
(1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;
首先要添加一块磁盘设备,然后进行分区格式化
[root@localhost ~]# fdisk /dev/sdb ##创建一个分区sdb5大小为5G(磁盘比较小) [root@localhost ~]#mkfs.ext4 -L 'MYDATA' -b 2048 -m 2 /dev/sdb5
[root@localhost ~]#partx -a /dev/sdb
(2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳;
[root@localhost ~]#mkdir -pv /data/mydata [root@localhost ~]#mount -o acl,nodiratime,noexec /dev/sdb5 /data/mydata/
##知识点:mount挂载文件系统选项
-o options:(挂载文件系统的选项)
async:异步模式;
sync:同步模式;
atime/noatime:包含目录和文件;
diratime/nodiratime:目录的访问时间戳
auto/noauto:是否支持自动挂载
exec/noexec:是否支持将文件系统上应用程序运行为进程
dev/nodev:是否支持在此文件系统上使用设备文件;
suid/nosuid:
remount:重新挂载
ro:
rw:
user/nouser:是否允许普通用户挂载此设备
acl:启用此文件系统上的acl功能:-访问控制列表
注意:上述选项可多个同时使用,彼此使用逗号分隔;
默认挂载选项:defaults
rw, suid, dev, exec, auto, nouser, and async
2、创建一个大小为1G的swap分区,并创建好文件系统,并启用之;
##创建swap交换分区前要调整分区id为82
[root@localhost ~]# mkswap /dev/sdb6 [root@localhost ~]# swapon /dev/sdb6 ##启用交换分区 [root@localhost ~]# fdisk -l /dev/sdb Disk /dev/sdb: 10.7 GB, 10737418240 bytes 255 heads, 63 sectors/track, 1305 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: 0x7f785d31 Device Boot Start End Blocks Id System /dev/sdb1 1 1305 10482381 5 Extended /dev/sdb5 1 654 5253192 83 Linux /dev/sdb6 655 786 1060258+ 82 Linux swap / Solaris
3、写一个脚本
(1)、获取并列出当前系统上的所有磁盘设备;
(2)、显示每个磁盘设备上每个分区相关的空间使用信息;
[root@liu homework7]# cat diskinfo.sh #!/bin/bash # #Description: show disk names and the space fdisk -l | grep "^/" | cut -d' ' -f1 df -h 测试脚本: [root@liu homework7]# bash diskinfo.sh /dev/sda1 /dev/sda2 /dev/sda3 /dev/sdc1 /dev/sdc5 /dev/sdc6 /dev/sdc7 /dev/sdb1 /dev/sdb5 /dev/sdb6 Filesystem Size Used Avail Use% Mounted on /dev/sda3 14G 3.5G 9.5G 27% / tmpfs 504M 0 504M 0% /dev/shm /dev/sda1 194M 28M 157M 16% /boot /dev/sr0 3.6G 3.6G 0 100% /mnt ##也可以使用awk格式化输出Disk信息 fdisk -l | awk '/^Disk.*dev/{print $0}’ [root@liu homework7]# fdisk -l |awk '/^Disk.*dev/{print $0}' Disk /dev/sda: 17.2 GB, 17179869184 bytes Disk /dev/sdc: 10.7 GB, 10737418240 bytes Disk /dev/sdb: 10.7 GB, 10737418240 bytes
4、总结RAID的各个级别及其组合方式和性能的不同;
RAID级别 组合方式 性能 raid0 条带式,2,2+ 提升读写性能,无容错 raid1 镜像式,2,2+ 读性能提升,写性能下降,有容错能力 raid5 奇偶校验,3,3+ 读写性能提升,可容错1块盘 raid10 先镜像,分条带,4,4+ 读写性能提升,有容错能力:每组镜像最多只能坏一块
5、创建一个大小为10G的RAID1,要求有一个空闲盘,而且CHUNK大小为128k;
[root@localhost ~]# mdadm -C /dev/md0 -n 2 -l 1 -a yes -c 128 -x 1 /dev/sdb{5,6,7} [root@localhost ~]# mdadm -D /dev/md0 /dev/md0: Version : 1.2 Creation Time : Sun Sep 25 07:51:15 2016 Raid Level : raid1 Array Size : 4196800 (4.00 GiB 4.30 GB) Used Dev Size : 4196800 (4.00 GiB 4.30 GB) Raid Devices : 2 Total Devices : 3 Persistence : Superblock is persistent Update Time : Sun Sep 25 07:51:37 2016 State : clean Active Devices : 2 Working Devices : 3 Failed Devices : 0 Spare Devices : 1 Name : localhost.localdomain:0 (local to host localhost.localdomain) UUID : 4f1f09ea:b6c7fe62:0d0d9760:6ddbe110 Events : 17 Number Major Minor RaidDevice State 0 8 22 0 active sync /dev/sdb6 1 8 23 1 active sync /dev/sdb7 2 8 21 - spare /dev/sdb5
6、创建一个大小为4G的RAID5设备,chunk大小为256k,格式化ext4文件系统,要求可开机自动挂载至/backup目录,而且不更新访问时间戳,且支持acl功能;
[root@localhost ~]# mdadm -C /dev/md5 -n 3 -l 5 -a yes -c 256 /dev/sdb{5,6,7} [root@localhost ~]# mke2fs -t ext4 -L mydata /dev/md5 [root@localhost ~]# mount -o acl,nodiratime /dev/md5 /data/mydata/ [root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 18G 3.7G 13G 23% / tmpfs 495M 72K 495M 1% /dev/shm /dev/sda1 477M 35M 418M 8% /boot /dev/md5 2.0G 3.1M 1.9G 1% /data/mydata [root@localhost ~]# vim /etc/fstab 添加一行: /dev/md5 /data/mydata ext4 nodiratime,acl,defaults 0 0
7、写一个脚本
(1) 接受一个以上文件路径作为参数;
(2) 显示每个文件拥有的行数;
(3) 总结说明本次共为几个文件统计了其行数;
[root@liu homework7]# cat 7.sh #!/bin/bash # declare -i i=0 if [ $# -lt 1 ];then echo "please input at least one file path" exit 1 fi for file in $*;do if [ -f $file ];then echo "The $file line number : $(wc -l $file | cut -d' ' -f1)" let i++ else echo "Please input the correct file path" fi done echo "File counts: $i" 脚本测试: [root@liu homework7]# bash 7.sh please input at least one file path [root@liu homework7]# bash 7.sh /etc/passwd /etc/adda The /etc/passwd line number : 76 Please input the correct file path File counts: 1 [root@liu homework7]# bash 7.sh /etc/passwd /etc/fstab The /etc/passwd line number : 76 The /etc/fstab line number : 18 File counts: 2
8、写一个脚本
(1) 传递两个以上字符串当作用户名;
(2) 创建这些用户;且密码同用户名;
(3) 总结说明共创建了几个用户;
[root@liu homework7]# cat 8.sh #!/bin/bash # #create user and pasword=username count usernames declare -i i=0 if [ $# -lt 2 ];then echo "Please at least input two username." exit 1 fi for username in $@;do id $username &> /dev/null if [ $? -eq 0 ];then echo "$username exist" else useradd $username echo "$username" | passwd --stdin $username &> /dev/null let i++ fi done echo "add user count:$i" 测试脚本: [root@liu homework7]# bash 8.sh testuser1 testuser2 add user count:2 [root@liu homework7]# ls /home/ bash liu testbash user10 user16 user4 useradd1 users5 basher mandirva testuser1 user11 user17 user5 users1 users6 centos mysql testuser2 user12 user18 user6 users10 users7 fedora nologin tuser1 user13 user19 user7 users2 users8 hadoop openstacks user user14 user2 user8 users3 users9
9、写一个脚本,新建20个用户,visitor1-visitor20;计算他们的ID之和;
[root@localhost scripttest]# cat 9.sh #!/bin/bash # declare -i sum=0 for i in {1..20};do useradd visitor$i sum+=$(id -u visitor$i) done [root@localhost scripttest]# bash 9.sh The sum of userid is: 10210 [root@localhost scripttest]# ls /home/ Ljohn visitor11 visitor14 visitor17 visitor2 visitor4 visitor7 visitor1 visitor12 visitor15 visitor18 visitor20 visitor5 visitor8 visitor10 visitor13 visitor16 visitor19 visitor3 visitor6 visitor9 echo "The sum of userid is: $sum"
10、写一脚本,分别统计/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#号开头的行数之和,以及总的空白行数;
#!/bin/bash # declare -i sum1=0 declare -i sum2=0 for i in {/etc/rc.d/rc.sysinit,/etc/rc.d/init.d/functions,/etc/fstab};do sum1+=$( grep -c '^#' $i ) sum2+=$( grep -c '^[[:space:]]*$' $i ) done echo "# line number: $sum1" echo "space line number: $sum2" [root@liu homework7]# bash 10.sh # line number: 91 space line number: 176
11、写一个脚本,显示当前系统上所有默认shell为bash的用户的用户名、UID以及此类所有用户的UID之和;
[root@liu homework7]# cat 11.sh #!/bin/bash # declare -i sum=0; declare -i i=0; USERSHELL=$(grep "/bin/bash" /etc/passwd | awk -F':' '{print $1}'\t) USERUID=$(grep "/bin/bash" /etc/passwd | awk -F':' '{print $3}'\t) for i in $USERUID;do let sum+=$i done echo "user id sum=$sum" echo "user is $USERSHELL" echo "userid is $USERUID"
测试脚本user id sum=171286user is rootliubashbashertestbashcentosuseradd1mageiaopenstacksuser1hadoopuserid is 050050150250350650711003003300530083009
12、写一个脚本,显示当前系统上所有,拥有附加组的用户的用户名;并说明共有多少个此类用户;
[root@liu homework7]# cat 12.sh #!/bin/bash # declare -i i=0 for user in `awk -F: '{print$1}' /etc/passwd`;do group=$(id $user | cut -d' ' -f3 | awk -F, '{print $2}') if [ -n "$group" ];then echo $user let i++ fi done echo "user number: $i" 测试脚本: [root@liu homework7]# bash 12.sh bin daemon adm postfix slackware openstacks user number: 6
13、创建一个由至少两个物理卷组成的大小为20G的卷组;要求,PE大小为8M;而在卷组中创建一个大小为5G的逻辑卷mylv1,格式化为ext4文件系统,开机自动挂载至/users目录,支持acl;
创建步骤: 1.创建一个分区并将分区类型改为LVM逻辑卷:8e 2.将该分区,创建PV使用(pvcreate) 3.创建卷组VG(vgcreate) 4.在卷组上创建lv 5.格式化逻辑卷LVM分区为ext4 挂载LVM,最后写到/etc/fstab文件中。 创建过程: [root@localhost ~]# pvcreate /dev/sdb{5,6} Physical volume "/dev/sdb5" successfully created Physical volume "/dev/sdb6" successfully created [root@localhost ~]# vgcreate -s 8M myvg /dev/sdb{5,6} Volume group "myvg" successfully created [root@localhost ~]# lvcreate -L 5G -n mylv1 myvg Logical volume "mylv1" created. [root@localhost ~]# lvdisplay --- Logical volume --- LV Path /dev/myvg/mylv1 LV Name mylv1 VG Name myvg LV UUID MLImqe-kcdt-6IPO-SHF6-vqRD-kvlP-urhEub LV Write Access read/write LV Creation host, time localhost.localdomain, 2016-09-25 21:06:26 +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 [root@localhost ~]# mkfs.ext4 /dev/myvg/mylv1 [root@localhost ~]# mkdir /users [root@localhost ~]# vim /etc/fstab 添加一行 /dev/myvg/mylv1 /users ext4 defaults,acl 0 0 [root@localhost ~]# mount -a [root@localhost ~]# mount /dev/mapper/VolGroup-lv_root on / type ext4 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0") /dev/sda1 on /boot type ext4 (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) /dev/mapper/myvg-mylv1 on /users type ext4 (rw,acl) [root@localhost ~]# cd /users/ [root@localhost users]# cp /etc/fstab ./ ;ls fstab lost+found
14、新建用户magedu;其家目录为/users/magedu,而后su切换至此用户,复制多个文件至家目录;
[root@localhost /]# useradd -d /users/magedu magedu [root@localhost /]# su - magedu [magedu@localhost ~]$ cp /etc/{fstab,passwd} ./ [magedu@localhost ~]$ ls fstab passwd
15、扩展mylv1至9G,确保扩展完成后原有数据完全可用;
[root@localhost magedu]# lvresize -L +4G /dev/myvg/mylv1 Size of logical volume myvg/mylv1 changed from 5.00 GiB (640 extents) to 9.00 GiB ( 1152 extents). Logical volume mylv1 successfully resized. [root@localhost magedu]# lvdisplay /dev/myvg/mylv1 --- Logical volume --- LV Path /dev/myvg/mylv1 LV Name mylv1 VG Name myvg LV UUID MLImqe-kcdt-6IPO-SHF6-vqRD-kvlP-urhEub LV Write Access read/write LV Creation host, time localhost.localdomain, 2016-09-25 21:06:26 +0800 LV Status available # open 1 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 magedu]# ls ; cat /users/magedu/fstab fstab passwd # # /etc/fstab # Created by anaconda on Wed Sep 7 23:19:06 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/VolGroup-lv_root / ext4 defaults 1 1 UUID=4a767f89-ceff-4b8b-9099-8652681b1a4f /boot ext4 defaults 1 2/dev/mapper/VolGroup-lv_swap 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 /dev/myvg/mylv1 /users ext4 defaults,acl 0 0
16、缩减mylv1至7G,确保缩减完成后原有数据完全可用;
[root@localhost ~]# umount /dev/myvg/mylv1 [root@localhost ~]# e2fsck -f /dev/myvg/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/myvg/mylv1: 24/327680 files (0.0% non-contiguous), 55915/1310720 blocks [root@localhost ~]# resize2fs /dev/myvg/mylv1 7G resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/myvg/mylv1 to 1835008 (4k) blocks. The filesystem on /dev/myvg/mylv1 is now 1835008 blocks long. [root@localhost ~]# lvreduce -L 7G /dev/myvg/mylv1 WARNING: Reducing active logical volume to 7.00 GiB. THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce myvg/mylv1? [y/n]: y Size of logical volume myvg/mylv1 changed from 9.00 GiB (1152 extents) to 7.00 GiB (896 extents). Logical volume mylv1 successfully resized. [root@localhost ~]# lvdisplay /dev/myvg/mylv1 --- Logical volume --- LV Path /dev/myvg/mylv1 LV Name mylv1 VG Name myvg LV UUID MLImqe-kcdt-6IPO-SHF6-vqRD-kvlP-urhEub LV Write Access read/write LV Creation host, time localhost.localdomain, 2016-09-25 21:06:26 +0800 LV Status available # open 0 LV Size 7.00 GiB Current LE 896 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:2 [root@localhost ~]# mount /dev/myvg/mylv1 /users/ [root@localhost ~]# cat /users/magedu/issue CentOS release 6.8 (Final) Kernel \r on an \m
17、对mylv1创建快照,并通过备份数据;要求保留原有的属主属组等信息;
[root@localhost ~]# lvcreate -L 512M -s -p r -n mysnap /dev/myvg/mylv1 Logical volume "mysnap" created. [root@localhost ~]# lvdisplay /dev/myvg/mysnap --- Logical volume --- LV Path /dev/myvg/mysnap LV Name mysnap VG Name myvg LV UUID vQf13M-bZz0-c0N4-sjSB-Gweb-Iy0c-jvqJby LV Write Access read only LV Creation host, time localhost.localdomain, 2016-09-25 21:41:01 +0800 LV snapshot status active destination for mylv1 LV Status available # open 0 LV Size 7.00 GiB Current LE 896 COW-table size 512.00 MiB 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 256 Block device 253:5 [root@localhost ~]# mkdir /lvsnap [root@localhost ~]# mount /dev/myvg/mysnap /lvsnap/ mount: block device /dev/mapper/myvg-mysnap is write-protected, mounting read-only [root@localhost lvsnap]# ls -l total 24 -rw-r--r--. 1 root root 824 Sep 25 21:11 fstab drwx------. 2 root root 16384 Sep 25 21:07 lost+found drwx------. 4 magedu magedu 4096 Sep 25 21:30 magedu [root@localhost lvsnap]# ll /users/ total 24 -rw-r--r--. 1 root root 824 Sep 25 21:11 fstab drwx------. 2 root root 16384 Sep 25 21:07 lost+found drwx------. 4 magedu magedu 4096 Sep 25 21:30 magedu
本文出自 “Ljohn” 博客,请务必保留此出处http://ljohn.blog.51cto.com/11932290/1856504