1、创建一个10G分区,并格式为ext4文件系统;
[root@localhost ~]# fdisk /dev/sda 命令(输入 m 获取帮助):n All primary partitions are in use 添加逻辑分区 5 起始 扇区 (41945088-83886079,默认为 41945088): 将使用默认值 41945088 Last 扇区, +扇区 or +size{K,M,G} (41945088-83886079,默认为 83886079):+10G 分区 5 已设置为 Linux 类型,大小设为 10 GiB 命令(输入 m 获取帮助):w The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: 设备或资源忙. 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) 正在同步磁盘。
(1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;
[root@localhost ~]# partprobe [root@localhost ~]# mke2fs -t ext4 -b 2048 -m 2 -L MYDATA /dev/sda5
(2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳;
[root@localhost ~]# mount -o noexec,noatime /dev/sda5 /data/mydata/
2、创建一个大小为1G的swap分区,并创建好文件系统,并启用之;
[root@localhost ~]# fdisk /dev/sda 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. Command (m for help): n All primary partitions are in use Adding logical partition 6 First sector (62918656-83886079, default 62918656): Using default value 62918656 Last sector, +sectors or +size{K,M,G} (62918656-83886079, default 83886079): +1G Partition 6 of type Linux and of size 1 GiB is set Command (m for help): t Partition number (1-6, default 6): 6 Hex code (type L to list all codes): 82 Changed type of partition 'Linux' to 'Linux swap / Solaris' 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@localhost ~]# partprobe [root@localhost ~]# mkswap /dev/sda6 Setting up swapspace version 1, size = 1048572 KiB no label, UUID=6ae279f8-d9b5-44c1-9896-386ec7f088db [root@localhost ~]# swapon /dev/sda6 [root@localhost ~]# swapon NAME TYPE SIZE USED PRIO /dev/sda2 partition 2G 0B -1 /dev/sda6 partition 1024M 0B -2
3、写一个脚本
(1)、获取并列出当前系统上的所有磁盘设备;
(2)、显示每个磁盘设备上每个分区相关的空间使用信息;
#!/bin/bash # fdisk -l | grep "^Disk /dev/[sh]d[a-z]" fdisk -l | grep "^/dev/[sh]d[a-z]"
4、总结RAID的各个级别及其组合方式和性能的不同;
RAID 0称为条带:可以把多块硬盘连成一个容量更大的硬盘群,可以提高磁 盘的性能和吞吐量。RAID 0没有冗余或错误修复能力,成本低,要求至少两个磁盘,一般只是在那些对数 据安全性要求不高的情况下才被使用。 RAID 1称为磁盘镜像:把一个磁盘的数据镜像到另一个磁盘上,在不影响性能情况下最大限度的保证系统的可靠性和可修复性上,具有很高的数据冗余能力,但磁盘利用 率为50%,故成本最高,多用在保存关键性的重要数据的场合。RAID 1的操作方式是把用户写入硬盘的数据百分之百地自动复制到另外一个硬盘上。 RAID 5可以理解为是RAID 0和RAID 1的折衷方案。RAID 5可以为系统提供数据安全保障,但保障程度要比Mirror低而磁盘空间利用率要比Mirror高。RAID 5具有和RAID 0相近似的数据读取速度,只是多了一个奇偶校验信息,写入数据的速度比对单个磁盘进行写入操作稍慢。同时由于多个数据对应一个奇偶校验信息,RAID 5的磁盘空间利用率要比RAID 1高,存储成本相对较低。 RAID10也被称为镜象阵列条带。象RAID0一样,数据跨磁盘抽取;象RAID1一样,每个磁盘都有一个镜象磁盘, 所以RAID 10的另一种会说法是 RAID 0+1。RAID10提供100%的数据冗余,支持更大的卷尺寸,但价格也相对较高。对大多数只要求具有冗余度而不必考虑价格的应用来说,RAID10提 供最好的性能。使用RAID10,可以获得更好的可靠性,因为即使两个物理驱动器发生故障(每个阵列中一个),数据仍然可以得到保护。
5、创建一个大小为10G的RAID1,要求有一个空闲盘,而且CHUNK大小为128k;
[root@localhost ~]# mdadm -C /dev/md/raid1 -n 2 -l mirror -c 128 -x 1 /dev/sdb1 /dev/sdb2 /dev/sdb3 [root@localhost ~]# cat /proc/mdstat Personalities : [raid1] md127 : active raid1 sdb3[2](S) sdb2[1] sdb1[0] 10477568 blocks super 1.2 [2/2] [UU] unused devices: <none>
6、创建一个大小为4G的RAID5设备,chunk大小为256k,格式化ext4文件系统,要求可开机自动挂载至/backup目录,而且不更新访问时间戳,且支持acl功能;
[root@localhost ~]# mdadm -C /dev/md/raid5 -n 3 -l raid5 -c 256 /dev/sdb5 /dev/sdb6 /dev/sdb7 mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md/raid5 started. [root@localhost ~]# mke2fs -t ext4 /dev/md/raid5 mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=64 blocks, Stripe width=128 blocks 262144 inodes, 1047552 blocks 52377 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=1073741824 32 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 Allocating group tables: done Writing inode tables: done Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done [root@localhost ~]# vim /etc/fstab /dev/md/raid5 /backup ext4 noatime,acl 0 0
7、写一个脚本
(1) 接受一个以上文件路径作为参数;
(2) 显示每个文件拥有的行数;
(3) 总结说明本次共为几个文件统计了其行数;
#!/bin/bash # declare -i cnt=0 if [ $# -lt 1 ] then echo "at least input one file" exit 1 fi for i in $* do if [ -e $i ] then wc -l $i cnt+=1 else echo "$i isn't exist" fi done echo "$cnt files have been counted"
8、写一个脚本
(1) 传递两个以上字符串当作用户名;
(2) 创建这些用户;且密码同用户名;
(3) 总结说明共创建了几个用户;
#!/bin/bash # declare -i count=0 if [ $# -lt 2 ] then echo "input at least two usernames" exit 1 fi for username in $* do id $username &> /dev/null if [ $? -eq 0 ] then echo "$username existed" else useradd $username echo "$username"|passwd --stdin $username &>/dev/null count+=1 fi done echo "$count users have been created"
9、写一个脚本,新建20个用户,visitor1-visitor20;计算他们的ID之和;
#!/bin/bash # declare -i count=0 for i in {1..20} do useradd visitor$i &> /dev/null uid=$(id -u visitor$i) count+=$uid done echo "the sum of uid is $count"
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 file in /etc/rc.d/rc.sysinit /etc/rc.d/init.d/functions /etc/fstab do num1=$(grep "^#" $file | wc -l) num2=$(grep "^[[:space:]]*$" $file | wc -l) sum1+=num1 sum2+=num2 done echo "total # started lines:$sum1" echo "total blank lines:$sum2"
11、写一个脚本,显示当前系统上所有默认shell为bash的用户的用户名、UID以及此类所有用户的UID之和;
#!/bin/bash # grep "/bin/bash" /etc/passwd | cut -d: -f 1,3 declare -i sum=0 for i in $(grep "/bin/bash" /etc/passwd | cut -d: -f 3) do sum+=$i done echo "sum of uid is $sum"
12、写一个脚本,显示当前系统上所有,拥有附加组的用户的用户名;并说明共有多少个此类用户;
#!/bin/bash # declare -i count=0 for username in $(cat /etc/passwd | cut -d: -f 1) do id $username | cut -d ' ' -f 3 |grep "," &> /dev/null if [ $? -eq 0 ] then echo "$username" count+=1 fi done echo "the sum of users is $count"
13、创建一个由至少两个物理卷组成的大小为20G的卷组;要求,PE大小为8M;而在卷组中创建一个大小为5G的逻辑卷mylv1,格式化为ext4文件系统,开机自动挂载至/users目录,支持acl;
[root@localhost ~]# pvcreate /dev/sdb1 [root@localhost ~]# pvcreate /dev/sdb2 [root@localhost ~]# vgcreate -s 8M myvg /dev/sdb[12] [root@localhost ~]# lvcreate -L 5G -n mylv1 myvg [root@localhost ~]# mke2fs -t ext4 /dev/myvg/mylv1 [root@localhost ~]# vim /etc/fstab /dev/myvg/mylv1 /users ext4 acl 0 0
14、新建用户magedu;其家目录为/users/magedu,而后su切换至此用户,复制多个文件至家目录;
[root@localhost ~]# useradd magedu -d /users/magedu [root@localhost ~]# su - magedu [magedu@localhost ~]$ cp -r /etc/rc.d/* ~
15、扩展mylv1至9G,确保扩展完成后原有数据完全可用;
[root@localhost ~]# lvextend -L 9G /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 ~]# resize2fs /dev/myvg/mylv1 9G resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/myvg/mylv1 to 2359296 (4k) blocks. The filesystem on /dev/myvg/mylv1 is now 2359296 blocks long.
16、缩减mylv1至7G,确保缩减完成后原有数据完全可用;
[root@localhost ~]# umount /dev/myvg/mylv1 [root@localhost ~]# e2fsck -f /dev/myvg/mylv1 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 /dev/myvg/mylv1: 11/589824 files (0.0% non-contiguous), 75551/2359296 blocks [root@localhost ~]# resize2fs /dev/myvg/mylv1 7G resize2fs 1.42.9 (28-Dec-2013) 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 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 ~]# mount -a
17、对mylv1创建快照,并通过备份数据;要求保留原有的属主属组等信息;
[root@localhost ~]# lvcreate -s -L 30M -n snapmylv1 -p r /dev/myvg/mylv1 Rounding up size to full physical extent 32.00 MiB Logical volume "snapmylv1" created.
原创文章,作者:zhangxiaola,如若转载,请注明出处:http://www.178linux.com/49982
评论列表(1条)
出于脚本编程严谨角度,第8题知道判断用户是否存在,其实第九题也需要做个判断,除非你的系统本身没有此用户。