1、创建一个10G分区,并格式为ext4文件系统
(1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl
(2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳
# fdisk /dev/sdb <<EOF
> n
> p
> 1
>
> +10G
> w
> EOF
~]# lsblk |grep sdb
sdb 8:16 0 15G 0 disk
└─sdb1 8:17 0 10G 0 part
~]# mkfs.ext4 -b 2048 -m 2 -L MYDATA /dev/sdb1
~]# tune2fs -l /dev/sdb1 |grep -i "block size\|MYDATA"
Filesystem volume name: MYDATA
Block size: 2048
~]# mount -o noexec,noatime,acl /dev/sdb1 /data/mydata/
~]# mount |grep /dev/sdb1
/dev/sdb1 on /data/mydata type ext4 (rw,noexec,noatime,acl)
2、创建一个大小为1G的swap分区,并创建好文件系统,并启用之
~]# fdisk /dev/sdb <<EOF
> n
> p
> 2
>
> +1G
> w
> EOF
~]# lsblk |grep sdb2
└─sdb2 8:18 0 1G 0 part
~]# mkswap /dev/sdb2
Setting up swapspace version 1, size = 1060284 KiB
no label, UUID=98a0f232-a11e-43ba-bbb7-ccb830ac975e
~]# swapon /dev/sdb2
~]# lsblk |grep -i "swap"
└─VolGroup-lv_swap (dm-1) 253:1 0 992M 0 lvm [SWAP]
└─sdb2 8:18 0 1G 0 part [SWAP]
3、写一个脚本
(1)、获取并列出当前系统上的所有磁盘设备
(2)、显示每个磁盘设备上每个分区相关的空间使用信息
#!/bin/bash
#
diskinfo=$(fdisk -l |grep -io "^disk /dev/s[a-z][a-z]")
usagedisk=$(df -h |awk '(NR>1){print "Device is:" $1,"Usage:",$3}'
)
echo -e "$diskinfo"
echo
echo -e "$usagedisk"
~]# bash sda.sh
Disk /dev/sda
Disk /dev/sdb
Device is:/dev/mapper/VolGroup-lv_root Usage: 1.2G
Device is:tmpfs Usage: 0
Device is:/dev/sda1 Usage: 33M
Device is:/dev/sdb1 Usage: 77M
4、总结RAID的各个级别及其组合方式和性能的不同
RAID-0
条带
读写性能提升;可用空间{M*min(S1,S2,...)}
,无容错能力,最少磁盘数2
个
RAID-1
镜像、备份
读性能提升、写性能略有下降
可用空间:1*min(S1,S2...)
有冗余能力
最小磁盘数2
个
RAID-4
单块盘负责奇偶校验,压力较大;
异或运算:1101,0110,1011
,数字相同为0
,数字不同为1
RAID-5
(2
块盘写数据,1
块盘奇偶校验)左对称,校验盘可放第一个盘
读写性能提升
可用空间:(n-1)*min(S1,S2...)
有容错能力:1
块磁盘
最少磁盘数:3,3+
RAID-6
校验盘为2
块
读写性能提升
可用空间:(N-2)*min(S1,S2...)
有容错能力:2
块磁盘
最少磁盘:4,4+
混合类型
RAID-10
先做raid1,后做raid0
读写性能提升
可用空间:N*min(S1,S2....)/2
有冗余能力:每组镜像最多只能坏一块
最少磁盘数:4,4+
5、创建一个大小为10G的RAID1,要求有一个空闲盘,而且CHUNK大小为128k
]# mdadm -C /dev/md0 -n 2 -l 1 -c 128 -x 1 /dev/sdb{5,6,7}
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
mdadm: largest drive (/dev/sdb5) exceeds size (2100352K) by more than 1%
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
~]# cat /proc/mdstat
Personalities : [raid1]
md0 : active raid1 sdb7[2](S) sdb6[1] sdb5[0]
2100352 blocks super 1.2 [2/2] [UU]
unused devices: <none>
6、创建一个大小为4G的RAID5设备,chunk大小为256k,格式化ext4文件系统,要求可开机自动挂载至/backup目录,而且不更新访问时间戳,且支持acl功能
~]# mdadm -C /dev/md5 -n 3 -l 5 -c 256 /dev/sdb{5,6,7}
mdadm: /dev/sdb5 appears to be part of a raid array:
level=raid1 devices=2 ctime=Thu Oct 27 04:23:01 2016
mdadm: /dev/sdb6 appears to be part of a raid array:
level=raid1 devices=2 ctime=Thu Oct 27 04:23:01 2016
mdadm: /dev/sdb7 appears to be part of a raid array:
level=raid1 devices=2 ctime=Thu Oct 27 04:23:01 2016
mdadm: largest drive (/dev/sdb5) exceeds size (2100224K) by more than 1%
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md5 started.
~]# mdadm --query /dev/md5
/dev/md5: 4.01GiB raid5 3 devices, 1 spare. Use mdadm --detail for more detail.
~]# tail -1 /etc/fstab
/dev/md5 /backup ext4 defaults,acl,noatime 0 0
7、写一个脚本
(1) 接受一个以上文件路径作为参数;
(2) 显示每个文件拥有的行数;
(3) 总结说明本次共为几个文件统计了其行数;
#!/bin/bash
#
if [ $# -lt 1 ];then
echo "Usage:bash $(basename $0) {file|--help}"
exit 3
fi
if [ $# -ge 1 ];then
if [ -f $1 ];then
for i in $*;do
lines=$(cat $i |wc -l)
echo "Count $* file and $i is $lines lines."
done
else
echo "Not file,please input file."
fi
fi
8、写一个脚本
(1) 传递两个以上字符串当作用户名;
(2) 创建这些用户;且密码同用户名;
(3) 总结说明共创建了几个用户;
#!/bin/bash
#
if [ $# -lt 2 ];then
echo "Must enter two parameters"
exit 8
fi
for i in $*;do
useradd $i
echo "$i" |passwd --stdin $i &>/dev/null
done
echo "Add users $#"
9、写一个脚本,新建20个用户,visitor1-visitor20;计算他们的ID之和;
#!/bin/bash
#
declare -i sum=0
for ((i=1;i<=20;i++));do
useradd visitor$i
uid=$(id -u visitor$i)
let sum+=uid
done
echo "Adding users uid is $sum"
10、写一脚本,分别统计/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#号开头的行数之和,以及总的空白行数;
#!/bin/bash
#
for i in /etc/rc.d/rc.sysinit /etc/rc.d/init.d/functions /etc/fstab;do
line1=$(sed -n '/^#/p' $i |wc -l)
line2=$(sed -n '/^[[:space:]]*$/p' $i|wc -l)
let line3+=$line1
let line4+=$line2
done
echo "Lines beginning with # are $line3 and $line4 blank lines"
11、写一个脚本,显示当前系统上所有默认shell为bash的用户的用户名、UID以及此类所有用户的UID之和;
#!/bin/bash
#
grep "bin/bash$" /etc/passwd |awk -F ":" '{print $1,$3}'
for i in `grep "bin/bash$" /etc/passwd |awk -F ":" '{print $3}'`
do
let sum+=$i
done
echo "Users ID sum is:$sum"
12、写一个脚本,显示当前系统上所有,拥有附加组的用户的用户名;并说明共有多少个此类用户;
#!/bin/bash
#
grep -v ":$" /etc/group |awk -F ":" '{print $1}'
echo "This users has $(grep -v ":$" /etc/group |awk -F ":" '{print $1}'|wc -l)."
13、创建一个由至少两个物理卷组成的大小为20G的卷组;要求,PE大小为8M;而在卷组中创建一个大小为5G的逻辑卷mylv1,格式化为ext4文件系统,开机自动挂载至/users目录,支持acl;
1. 创建lvm分区
~]# fdisk -l |egrep "(sdb1|sdc1)"
/dev/sdb1 1 2610 20964793+ 8e Linux LVM
/dev/sdc1 1 2610 20964793+ 8e Linux LVM
2.创建pv
~]# pvcreate /dev/sdb1
dev_is_mpath: failed to get device for 8:17
Physical volume "/dev/sdb1" successfully created
~]# pvcreate /dev/sdc1
dev_is_mpath: failed to get device for 8:33
Physical volume "/dev/sdc1" successfully created
~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 VolGroup lvm2 a-- 9.51g 0
/dev/sdb1 lvm2 a-- 19.99g 19.99g
/dev/sdc1 lvm2 a-- 19.99g 19.99g
3.创建卷组,PE大小为8M。
~]# vgcreate -s 8M vgtest /dev/sdb1 /dev/sdc1
Volume group "vgtest" successfully created
~]# vgs
VG #PV #LV #SN Attr VSize VFree
VolGroup 1 2 0 wz--n- 9.51g 0
vgtest 2 0 0 wz--n- 39.98g 39.98g
4.创建逻辑卷
~]# lvcreate -L 5G -n mylv1 vgtest
Logical volume "mylv1" created
~]# lvscan
ACTIVE '/dev/vgtest/mylv1' [5.00 GiB] inherit
5.格式化
~]# mkfs.ext4 /dev/mapper/vgtest-mylv1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
...
6.开机挂载
~]# cat /etc/fstab |tail -1
UUID="798bbb93-2c50-4573-a98f-db27a6b9ac58" /users ext4 defaults,acl 0 0
~]# blkid /dev/mapper/vgtest-mylv1
/dev/mapper/vgtest-mylv1: UUID="798bbb93-2c50-4573-a98f-db27a6b9ac58" TYPE="ext4"
~]# mount -a
~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 8.5G 1.2G 6.9G 15% /
tmpfs 242M 0 242M 0% /dev/shm
/dev/sda1 485M 33M 427M 8% /boot
/dev/mapper/vgtest-mylv1 5.0G 138M 4.6G 3% /users
~]# mount |grep acl
/dev/mapper/vgtest-mylv1 on /users type ext4 (rw,acl)
14、新建用户magedu;其家目录为/users/magedu,而后su切换至此用户,复制多个文件至家目录;
~]# useradd -d /users/magedu magedu
~]# ll -a /users/magedu/
total 24
drwx------ 3 magedu magedu 4096 Nov 22 04:33 .
drwxr-xr-x 4 root root 4096 Nov 22 04:33 ..
-rw-r--r-- 1 magedu magedu 18 Jul 18 2013 .bash_logout
-rw-r--r-- 1 magedu magedu 176 Jul 18 2013 .bash_profile
-rw-r--r-- 1 magedu magedu 124 Jul 18 2013 .bashrc
drwxr-xr-x 4 magedu magedu 4096 Sep 21 07:08 .mozilla
~]# su - magedu
Welcome to Linux 2016-11-22-04:36:53
~]$ pwd
/users/magedu
~]$ cp -r /etc/fstab /etc/hosts /users/magedu/
15、扩展mylv1至9G,确保扩展完成后原有数据完全可用;
1.扩展lv
~]# lvextend -L 9G /dev/mapper/vgtest-mylv1
Extending logical volume mylv1 to 9.00 GiB
Logical volume mylv1 successfully resized
2.扩展文件系统
~]# resize2fs /dev/mapper/vgtest-mylv1
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vgtest-mylv1 is mounted on /users; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/mapper/vgtest-mylv1 to 2359296 (4k) blocks.
The filesystem on /dev/mapper/vgtest-mylv1 is now 2359296 blocks long.
~]# df -h |grep mylv1
/dev/mapper/vgtest-mylv1 8.9G 140M 8.3G 2% /users
~]# cat /users/magedu/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
16、缩减mylv1至7G,确保缩减完成后原有数据完全可用;
1.卸载LV
~]# umount /dev/mapper/vgtest-mylv1
2.检测文件系统
~]# e2fsck -f /dev/mapper/vgtest-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/mapper/vgtest-mylv1: 21/589824 files (0.0% non-contiguous), 72681/2359296 blocks
3.压缩文件系统容量
~]# resize2fs /dev/mapper/vgtest-mylv1 7G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/vgtest-mylv1 to 1835008 (4k) blocks.
The filesystem on /dev/mapper/vgtest-mylv1 is now 1835008 blocks long.
4.压缩LV
~]# lvreduce -L 7G /dev/vgtest/mylv1
WARNING: Reducing active logical volume to 7.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce mylv1? [y/n]: y
Reducing logical volume mylv1 to 7.00 GiB
Logical volume mylv1 successfully resized
5.重新挂载
~]# mount -a
~]# df -h |grep "mylv1"
/dev/mapper/vgtest-mylv1 6.9G 140M 6.5G 3% /users
17、对mylv1创建快照,并通过备份数据;要求保留原有的属主属组等信息;
~]# lvcreate -L 7G -p r -s -n mylv1_snap /dev/vgtest/mylv1
~]# lvscan
ACTIVE Original '/dev/vgtest/mylv1' [7.00 GiB] inherit
ACTIVE Snapshot '/dev/vgtest/mylv1_snap' [7.00 GiB] inherit
ACTIVE '/dev/VolGroup/lv_root' [8.54 GiB] inherit
ACTIVE '/dev/VolGroup/lv_swap' [992.00 MiB] inherit
原创文章,作者:N22-白蚁,如若转载,请注明出处:http://www.178linux.com/60314
评论列表(1条)
整体作业完成非常棒,第九题添加用户之前可以做一个用户是否存在的判断,逻辑稍微严谨一些会增加脚本的健壮性。