1. 创建一个10G分区,并格式化为ext4文件系统;
(1) 要求其block大小为2048,预留空间百分比为2%,卷标为MYDATA,默认挂载属性包含acl;
(2) 挂载至/data/mydata目录,要求挂载时禁止程序自动执行,且不更新文件的访问时间戳;
“`
/]# fdisk /dev/sdb
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 (31463424-209715199, default 31463424):
Using default value 31463424
Last sector, +sectors or +size{K,M,G} (31463424-209715199, default 209715199): +10G
Partition 6 of type Linux and of size 10 GiB is set
Command (m for help): p
Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x277399ec
Device Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 83 Linux
/dev/sdb2 20973568 23070719 1048576 82 Linux swap / Solaris
/dev/sdb3 23070720 27265023 2097152 83 Linux
/dev/sdb4 27265024 209715199 91225088 5 Extended
/dev/sdb5 27267072 31461375 2097152 8e Linux LVM
/dev/sdb6 31463424 52434943 10485760 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
~]# kpartx -af /dev/sdb
~]# mke2fs -t ext4 -b 2048 -L MYDATA -m 2 /dev/sdb6
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=MYDATA
OS type: Linux
Block size=2048 (log=1)
Fragment size=2048 (log=1)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 5242880 blocks
104857 blocks (2.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=273678336
320 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
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
“`
2. 创建一个大小为1G的swap分区,并创建好文件系统,并启用之;
“`
~]# fdisk /dev/sdb
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 7
First sector (52436992-209715199, default 52436992):
Using default value 52436992
Last sector, +sectors or +size{K,M,G} (52436992-209715199, default 209715199): +1G
Partition 7 of type Linux and of size 1 GiB is set
Command (m for help): t
Partition number (1-7, default 7): 7
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.
Syncing disks.
~]# kpartx -af /dev/sdb
~]# free -m
total used free shared buff/cache available
Mem: 7806 182 7404 8 220 7353
Swap: 2048 0 2048
~]# mkswap /dev/sdb7
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=e9426246-b5b3-48e6-b034-5ec5f26646a9
~]# swapon /dev/sdb7
~]# free -m
total used free shared buff/cache available
Mem: 7806 180 7401 8 224 7353
Swap: 3072 0 3072
“`
3. 写一个脚本
(1) 获取并列出当前系统上的所有磁盘设备
(2) 显示每个磁盘设备上每个分区相关的空间使用信息
“`
#!/bin/bash
#
for i in `fdisk -l | grep “^Disk /dev” | grep -o “/dev/[[:alpha:]]\+\>”`; do
echo -e “\n$i disk usage: \n”
fdisk -l $i | grep “^/dev/”
done
“`
4. 总结RAID的各个级别及其组合方式和性能的不同;
RAID级别 | 可用空间 | 允许坏盘个数 | 磁盘数限制 | 读写性能
————- | ———— | ———— | ————– | ————–
JBOD | Size = S1+S2+…+SN | 0 | 2+ | 读写性能均没有变化
RAID0 | Size = 2 x min(S1,S2,…) | 0 | 2+ | 读、写性能均有提升
RAID1 | Size = min(S1,S2,S3…) | N-1 | 2+ | 读性能有提升,写性能略有下降
RAID4 | Size = (N-1) x min(S1,S2,…,SN) | 1 | 3+ | 读、写性能均有提升
RAID5 | Size = (N-1) x min(S1,S2,…,SN) | 1 | 3+ | 读、写性能均有提升
RAID6 | Size = (N-2) x min(S1,S2,…,SN) | 2 | 4+ | 读、写性能均有提升
RAID1+0 | Size = N x min(S1,S2,…,SN)/2 | 每个RAID1剩一块健康盘即可 | 4+ | 读、写性能均有提升
RAID0+1 | Size = N x min(S1,S2,…,SN)/2 | RAID1有一个健康的RAID0组即可 | 4+ | 读、写性能均有提升
5. 创建一个大小为10G的RAID1,要求有一个空闲盘,而且CHUNK大小为128K;
“`
通过fdisk命令创建3个10G的分区
~]# fdisk /dev/sdb
/dev/sdb7 52436992 73408511 10485760 fd Linux raid autodetect
/dev/sdb8 73410560 94382079 10485760 fd Linux raid autodetect
/dev/sdb9 94384128 115355647 10485760 fd Linux raid autodetect
~]# mdadm -C /dev/md0 -n 2 -x 1 -l 1 -c 256 /dev/sdb{7,8,9}
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
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
~]# mdadm -D /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Thu Oct 26 13:54:04 2017
Raid Level : raid1
Array Size : 10477568 (9.99 GiB 10.73 GB)
Used Dev Size : 10477568 (9.99 GiB 10.73 GB)
Raid Devices : 2
Total Devices : 3
Persistence : Superblock is persistent
Update Time : Thu Oct 26 13:54:56 2017
State : clean
Active Devices : 2
Working Devices : 3
Failed Devices : 0
Spare Devices : 1
Name : sapbcs.mageedu.com:0 (local to host sapbcs.mageedu.com)
UUID : 88dca9de:813c1f6b:a28ec904:b9f37d67
Events : 17
Number Major Minor RaidDevice State
0 8 23 0 active sync /dev/sdb7
1 8 24 1 active sync /dev/sdb8
2 8 25 – spare /dev/sdb9
“`
6. 创建一个大小为4G的RAID5设备,CHUNK大小为256K,格式化为ext4文件系统,要求可开机自动挂载至/backup目录,而且不更新访问时间戳,且支持acl功能
“`
~]# fdisk /dev/sdb
新建4个4G分区,并调整格式为fd
/dev/sdb10 115357696 123746303 4194304 fd Linux raid autodetect
/dev/sdb11 123748352 132136959 4194304 fd Linux raid autodetect
/dev/sdb12 132139008 140527615 4194304 fd Linux raid autodetect
/dev/sdb13 140529664 148918271 4194304 fd Linux raid autodetect
~]# mdadm -C /dev/md1 -n 3 -x 1 -l 5 -c 256 /dev/sdb{10,11,12,13}
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md1 started.
~]# mke2fs -t ext4 /dev/md1
~]# vim /etc/fstab
/dev/md1 /backup ext4 defaults,acl,noatime 0 0
“`
7. 写一个脚本
(1) 接受一个以上文件路径作为参数;
(2) 显示每个文件拥有的行数;
(3) 总结说明本次共为几个文件统计了行数;
“`
#!/bin/bash
#
if [ $# -lt 1 ];then
echo “Please input at least one path.”
exit 1
fi
for i in `seq 1 $#`; do
file=`echo $@ | cut -d’ ‘ -f$i`
lines=`wc -l $file | cut -d’ ‘ -f1`
echo “There are $lines lines in $file.”
done
echo “There are $# files counted by this script.”
~]# ./count.sh /etc/fstab /etc/rc.d/init.d/functions
There are 12 lines in /etc/fstab.
There are 654 lines in /etc/rc.d/init.d/functions.
There are 2 files counted by this script.
“`
8. 写一个脚本
(1) 传递两个以上字符串当作用户名;
(2) 创建这些用户,且密码同用户名;
(3) 总结说明共创建了几个用户;
“`
#!/bin/bash
#
if [ $# -lt 2 ]; then
echo “You must input at least two username.”
exit
fi
let skipnum=0
for i in `seq 1 $#`; do
user_name=`echo $@ | cut -d’ ‘ -f$i`
if id $user_name &> /dev/null; then
skipnum=$(($skipnum+1))
echo “User $user_name already exist in system.”
else
useradd $user_name &> /dev/null
echo $user_name | passwd $user_name –stdin &> /dev/null
echo “User $user_name has been created.”
fi
done
let sum=$(($#-$skipnum))
echo “There are $sum users created by this script.”
“`
9. 写一个脚本
创建20个用户,visitor1-visitor20;计算他们的ID之和;
“`
#!/bin/bash
#
let sum=0
for i in `seq 1 20`; do
id visitor$i &> /dev/null || useradd visitor$i &> /dev/null
let User_id=`id -u visitor$i`
sum=$(($sum+$User_id))
done
echo “User ID sum is $sum.”
“`
本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/88077