Q1:创建一个10G分区,并格式为ext4文件系统;
(1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;
[root@promote ~]# fdisk /dev/sdb #将/dev/sdb进行分区操作; Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0xb294ca06. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) 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 #创建新分区 Command action e extended p primary partition (1-4) P #选择建立主分区 Partition number (1-4): 1 #为主分区编码1号 First cylinder (1-2610, default 1): #使用默认起始柱面 Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +10G #大小为10G Command (m for help): w #保存退出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@promote ~]# mke2fs -t ext4 -b 2048 -L MYDATA -m 2 /dev/sdb1 mke2fs 1.41.12 (17-May-2010) 文件系统标签=MYDATA 操作系统:Linux 块大小=2048 (log=1) 分块大小=2048 (log=1) Stride=0 blocks, Stripe width=0 blocks 657408 inodes, 5245206 blocks 104904 blocks (2.00%) reserved for the super user 第一个数据块=0 Maximum filesystem blocks=543162368 321 block groups 16384 blocks per group, 16384 fragments per group 2048 inodes per group Superblock backups stored on blocks: 16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816, 1327104, 2048000, 3981312 正在写入inode表: 完成 Creating journal (32768 blocks): 完成 Writing superblocks and filesystem accounting information: 完成 This filesystem will be automatically checked every 33 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@promote ~]# tune2fs -o acl /dev/sdb1 #默认挂载属性包含acl tune2fs 1.41.12 (17-May-2010)
(2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳;
[root@promote ~]# mount -o noexec,noatime /dev/sdb1 /data/mydata
Q2:创建一个大小为1G的swap分区,并创建好文件系统,并启用之;
[root@promote ~]# fdisk /dev/sdb 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 #创建新分区 Command action e extended p primary partition (1-4) P #选择创建主分区 Partition number (1-4): 2 #创建的主分区编号为2 First cylinder (1307-2610, default 1307): Using default value 1307 Last cylinder, +cylinders or +size{K,M,G} (1307-2610, default 2610): +1G #大小1G Command (m for help): t #更改分区类型,原本的为83 Partition number (1-4): 2 #对第二块主分区磁盘进行修改 Hex code (type L to list codes): 82 #分区类型swap分区代码:82 Changed system type of partition 2 to 82 (Linux swap / Solaris) Command (m for help): p #可以看到/dev/sdb2已经修改为swap分区 Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0xb294ca06 Device Boot Start End Blocks Id System /dev/sdb1 1 1306 10490413+ 83 Linux /dev/sdb2 1307 1438 1060290 82 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: 设备或资源忙. 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@promote ~]# partx -a /dev/sdb #多输入两遍,重新加载磁盘分区; BLKPG: Device or resource busy error adding partition 1 [root@promote ~]# partx -a /dev/sdb BLKPG: Device or resource busy error adding partition 1 BLKPG: Device or resource busy error adding partition 2 [root@promote ~]# mkswap /dev/sdb2 Setting up swapspace version 1, size = 1060284 KiB no label, UUID=ec026d7c-f173-4746-94db-1b93ffdb44ee [root@promote ~]# swapon /dev/sdb2
Q3:写一个脚本
-
(1)、获取并列出当前系统上的所有磁盘设备;
-
(2)、显示每个磁盘设备上每个分区相关的空间使用信息;
#!/bin/bash # fdisk -l /dev/s[dh][a-z] | grep -o "Disk /dev/s[dh][a-z]" | cut -d" " -f2 echo -e "\n" df -h
Q4:总结RAID的各个级别及其组合方式和性能的不同;
见另一篇博客;
Q5:创建一个大小为10G的RAID1,要求有一个空闲盘,而且CHUNK大小为128k;
#首先创建3个10G分区,/dev/sdb1,/dev/sdc1,/dev/sdd1 [root@CentOS7_2 ~]# mdadm --create /dev/md0 --auto=yes --level=1 --chunk=128K --raid-devices=2 --spare-devices=1 /dev/sd{b1,c1,d1}
Q6:创建一个大小为4G的RAID5设备,chunk大小为256k,格式化ext4文件系统,要求可开机自动挂载至/backup目录,而且不更新访问时间戳,且支持acl功能;
#首先创建3个2G分区,/dev/sdb2,/dev/sdc2,/dev/sdd2 [root@CentOS7_2 ~]# mdadm --create /dev/md1 --auto=yes --level=5 --chunk=256K --raid-devices=2 --spare-devices=1 /dev/sd{b2,c2,d2} [root@CentOS7_2 ~]# mke2fs -t ext4 /dev/md1 [root@CentOS7_2 ~]# vim /etc/fstab # /dev/md1 /backup ext4 noatime,acl 0 0
Q7:写一个脚本
-
(1) 接受一个以上文件路径作为参数;
-
(2) 显示每个文件拥有的行数;
-
(3) 总结说明本次共为几个文件统计了其行数;
#方法一: #!/bin/bash # [ $# -lt 1 ] && echo "At least one path!" && exit 1 for i in $*;do echo "$i has $(wc -l $i | cut -d" " -f1) lines." done echo echo $# #方法二: #!/bin/bash # if [ $# -eq 0 ];then echo "At least one path!" exit 1 fi for i in $*;do echo "$i has $(cat $i | wc -l) lines." done echo echo $#
Q8:写一个脚本
-
(1) 传递两个以上字符串当作用户名;
-
(2) 创建这些用户;且密码同用户名;
-
(3) 总结说明共创建了几个用户;
#方法一: #!/bin/bash # [ $# -lt 2 ] && echo "At least two strings!" && exit 1 declare -i sum=0 for i in $*;do id $i &> /dev/null && echo "user $i has existed!" || (useradd $i && echo "$i" | passwd --stdin $i) sum+=1 done echo "You added $sum users." #方法二: #!/bin/bash # if [ $# -lt 2 ];then echo "At least two strings!" exit 1 fi declare -i sum=0 for i in $*;do if id $i &> /dev/null;then echo "user $i has existed!" continue else useradd $i sum+=1 echo "$i" | passwd --stdin $i fi done echo "You added $sum users."
Q9:写一个脚本,新建20个用户,visitor1-visitor20;计算他们的ID之和;
#!/bin/bash # declare -i sum=0 for i in {1..20};do id visitor$i &> /dev/null && echo "user visitor$i has existed!" || useradd visitor$i && echo "visitor$i has been added successfully!" id_num=$(grep "visitor$i" /etc/passwd | cut -d":" -f3) let sum+=$id_num done echo "The sum of the twenty users'ID is $sum."
【注】批量删除这20个用户,在shell命令行窗口中输入: [root@promote ~]# for i in {1..20};do userdel -r visitor$i;done
Q10:写一脚本,分别统计/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#号开头的行数之和,以及总的空白行数;
#!/bin/bash # declare -i sum=0 for i in {/etc/rc.d/rc.sysinit,/etc/rc.d/init.d/functions,/etc/fstab};do echo "File:$i has $(grep "^#.*" $i | wc -l) lines to be started with '#'." add=$(grep "^[[:space:]]*$" $i | wc -l) let sum+=$add done echo "三个文件总的空白行数:$sum"
Q11:写一个脚本,显示当前系统上所有默认shell为bash的用户的用户名、UID以及此类所有用户的UID之和;
#!/bin/bash # grep "bash$" /etc/passwd | cut -d":" -f1,3 declare -i id_sum=0 for i in $(grep "bash$" /etc/passwd | awk -F: '{print $3}');do id_sum+=i done echo "默认shell为bash用户的UID之和为:$id_sum"
Q12:写一个脚本,显示当前系统上所有,拥有附加组的用户的用户名;并说明共有多少个此类用户;
#!/bin/bash # cat /etc/group | cut -d: -f1,4 | grep -v ":$" | cut -d: -f1 num=$(cat /etc/group | cut -d: -f1,4 | grep -v ":$" | cut -d: -f1 | wc -l) echo "拥有附加组用户有$num个" #特别注意:grep -v的用法;
Q13:创建一个由至少两个物理卷组成的大小为20G的卷组;要求,PE大小为8M;而在卷组中创建一个大小为5G的逻辑卷mylv1,格式化为ext4文件系统,开机自动挂载至/users目录,支持acl;
#在磁盘上进行分区,分别为/dev/sb3和/dev/sc3 #将/dev/sb3和/dev/sc3创建成PV [root@CentOS7_2 ~]# pvcreate /dev/sdb3 Physical volume "/dev/sdb3" successfully created [root@CentOS7_2 ~]# pvcreate /dev/sdc3 Physical volume "/dev/sdc3" successfully created [root@CentOS7_2 ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 centos lvm2 a-- 119.51g 64.00m /dev/sdb3 lvm2 --- 5.00g 5.00g /dev/sdc3 lvm2 --- 15.00g 15.00g #用/dev/sb3和/dev/sc3创建的两个PV,生成一个VG,PE大小为8M [root@CentOS7_2 ~]# vgcreate -s 8M myvg /dev/sd{b3,c3} Volume group "myvg" successfully created [root@CentOS7_2 ~]# vgs VG #PV #LV #SN Attr VSize VFree centos 1 3 0 wz--n- 119.51g 64.00m myvg 2 0 0 wz--n- 19.98g 19.98g #myvg创建逻辑卷LV:大小5G,名为mylv1 [root@CentOS7_2 ~]# lvcreate -L 5G -n mylv1 myvg Logical volume "mylv1" created. #对mylv1格式化 [root@CentOS7_2 ~]# mkfs.ext4 /dev/myvg/mylv1 #对mylv1设置开机自动挂载 [root@CentOS7_2 ~]# vim /etc/fstab 添加: /dev/myvg/mylv1 /users ext4 defaults,acl 0 0 [root@CentOS7_2 ~]# mount -a [root@CentOS7_2 ~]# df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/mapper/centos-root 50G 1.7G 49G 4% / devtmpfs 483M 0 483M 0% /dev tmpfs 493M 0 493M 0% /dev/shm tmpfs 493M 6.8M 487M 2% /run tmpfs 493M 0 493M 0% /sys/fs/cgroup /dev/mapper/centos-home 68G 33M 68G 1% /home /dev/sda1 497M 125M 373M 26% /boot tmpfs 99M 0 99M 0% /run/user/0 /dev/mapper/myvg-mylv1 4.8G 20M 4.6G 1% /users
Q14:新建用户magedu;其家目录为/users/magedu,而后su切换至此用户,复制多个文件至家目录;
[root@promote /]# mkdir users [root@promote /]# useradd -d /users/magedu magedu [root@promote users]# su - magedu [magedu@promote ~]$ pwd /users/magedu [magedu@promote ~]$ cp /etc/fstab /etc/networks ./ [magedu@promote ~]$ ls fstab networks
Q15:扩展mylv1至9G,确保扩展完成后原有数据完全可用;
[root@CentOS7_2 ~]# df -h /dev/myvg/mylv1 文件系统 容量 已用 可用 已用% 挂载点 /dev/mapper/myvg-mylv1 4.8G 20M 4.6G 1% /users [root@CentOS7_2 ~]# 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@CentOS7_2 ~]# resize2fs /dev/myvg/mylv1 resize2fs 1.42.9 (28-Dec-2013) Filesystem at /dev/myvg/mylv1 is mounted on /users; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 2 The filesystem on /dev/myvg/mylv1 is now 2359296 blocks long. [root@CentOS7_2 ~]# df -h /dev/myvg/mylv1 文件系统 容量 已用 可用 已用% 挂载点 /dev/mapper/myvg-mylv1 8.8G 23M 8.3G 1% /users
Q16:缩减mylv1至7G,确保缩减完成后原有数据完全可用;
[root@CentOS7_2 ~]# umount /dev/myvg/mylv1 [root@CentOS7_2 ~]# e2fsck -f /dev/myvg/mylv1 e2fsck 1.42.9 (28-Dec-2013) 第一步: 检查inode,块,和大小 第二步: 检查目录结构 第3步: 检查目录连接性 Pass 4: Checking reference counts 第5步: 检查簇概要信息 /dev/myvg/mylv1: 11/589824 files (0.0% non-contiguous), 75551/2359296 blocks [root@CentOS7_2 ~]# resize2fs -f /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@CentOS7_2 ~]# 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@CentOS7_2 ~]# mount -a [root@CentOS7_2 ~]# df -h /dev/myvg/mylv1 文件系统 容量 已用 可用 已用% 挂载点 /dev/mapper/myvg-mylv1 6.8G 23M 6.5G 1% /users
Q17:对mylv1创建快照,并通过备份数据;要求保留原有的属主属组等信息;
[root@CentOS7_2 ~]# lvcreate -L 20M -s -p r -n mylv1snap1 /dev/myvg/mylv1 Rounding up size to full physical extent 24.00 MiB Logical volume "mylv1snap1" created.
原创文章,作者:N24_VC,如若转载,请注明出处:http://www.178linux.com/64601