1、创建一个10G分区,并格式为ext4文件系统;
[root@localhost ~]# fdisk /dev/sda 欢迎使用 fdisk (util-linux 2.23.2)。 更改将停留在内存中,直到您决定将更改写入磁盘。 使用写入命令前请三思。 命令(输入 m 获取帮助):n Partition type: p primary (3 primary, 0 extended, 1 free) e extended Select (default e): p 已选择分区 4 起始 扇区 (102586368-209715199,默认为 102586368): 将使用默认值 102586368 Last 扇区, +扇区 or +size{K,M,G} (102586368-209715199,默认为 209715199):+10G 分区 4 已设置为 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) 正在同步磁盘。 [root@localhost ~]#
[root@localhost ~]# partx -a /dev/sda
(1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;
[root@localhost ~]# mke2fs -t ext4 -b 2048 -L 'MYDATA' -m 2 /dev/sda5 mke2fs 1.42.9 (28-Dec-2013) 文件系统标签=MYDATA OS type: Linux 块大小=2048 (log=1) 分块大小=2048 (log=1) Stride=0 blocks, Stripe width=0 blocks 327680 inodes, 2621440 blocks 52428 blocks (2.00%) reserved for the super user 第一个数据块=0 Maximum filesystem blocks=540016640 160 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 Allocating group tables: 完成 正在写入inode表: 完成 Writing superblocks and filesystem accounting information: 完成
(2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳;
[root@localhost ~]# mount -o nodiratime,noexec /dev/sda5 /data/mydata/
2、创建一个大小为1G的swap分区,并创建好文件系统,并启用之;
[root@localhost ~]# fdisk /dev/sda 欢迎使用 fdisk (util-linux 2.23.2)。 更改将停留在内存中,直到您决定将更改写入磁盘。 使用写入命令前请三思。 命令(输入 m 获取帮助):n All primary partitions are in use 添加逻辑分区 7 起始 扇区 (115175424-123557887,默认为 115175424): 将使用默认值 115175424 Last 扇区, +扇区 or +size{K,M,G} (115175424-123557887,默认为 123557887):+1G 分区 7 已设置为 Linux 类型,大小设为 1 GiB 命令(输入 m 获取帮助):t 分区号 (1-7,默认 7):7 Hex 代码(输入 L 列出所有代码):82 已将分区“Linux”的类型更改为“Linux swap / Solaris” 命令(输入 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) 正在同步磁盘。
[root@localhost ~]# partx -a 7 /dev/sda
[root@localhost ~]# mkswap /dev/sda7 正在设置交换空间版本 1,大小 = 1048572 KiB 无标签,UUID=6151d94b-d2e7-473a-80c5-9085822aa01e
[root@localhost ~]# swapon /dev/sda7
3、写一个脚本
(1)、获取并列出当前系统上的所有磁盘设备;
(2)、显示每个磁盘设备上每个分区相关的空间使用信息;
#!/bin/bash echo "Print all disks information" echo " " diskinfo=$(fdisk -l /dev/sd[a-z] | grep '^Disk[[:space:]]/'| awk '{print $1,$2,$3,$4}') echo $diskinfo echo " " echo "Print partations space information" echo " " for i in $(fdisk -l | egrep "^/dev/sd[a-z][[:digit:]]" | awk '{print $1}' ) do df -h $i done
4、总结RAID的各个级别及其组合方式和性能的不同;
RAID0:无容错能力,提升读、写性能,可用空间为N,最少磁盘数为2 RAID1:有容错能力,提升读性能,写性能略有下降,可用空间为N/2,最少磁盘数为2 RAID5:提供一块磁盘的容错能力,提升读、写性能,可用空间为N-1,最少磁盘数为3 RAID6:提供两块磁盘的容错能力,提升读、写性能,可用空间为N-2,最少磁盘数为4 RAID10:有容错能力,但每组镜像最多只能坏一块磁盘,提升读、写性能,可用空间为N/2,最少磁盘数为4
5、创建一个大小为10G的RAID1,要求有一个空闲盘,而且CHUNK大小为128k;
添加三块10G大小的磁盘,
[root@localhost ~]# 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 Partition type: p primary (0 primary, 1 extended, 3 free) l logical (numbered from 5) Select (default p): l Adding logical partition 5 First sector (4096-207620095, default 4096): Using default value 4096 Last sector, +sectors or +size{K,M,G} (4096-207620095, default 207620095): +10G Partition 5 of type Linux and of size 10 GiB is set Command (m for help): n Partition type: p primary (0 primary, 1 extended, 3 free) l logical (numbered from 5) Select (default p): l Adding logical partition 6 First sector (20977664-207620095, default 20977664): Using default value 20977664 Last sector, +sectors or +size{K,M,G} (20977664-207620095, default 207620095): +10G Partition 6 of type Linux and of size 10 GiB is set Command (m for help): n Partition type: p primary (0 primary, 1 extended, 3 free) l logical (numbered from 5) Select (default p): l Adding logical partition 7 First sector (41951232-207620095, default 41951232): Using default value 41951232 Last sector, +sectors or +size{K,M,G} (41951232-207620095, default 207620095): +10G Partition 7 of type Linux and of size 10 GiB is set Command (m for help): t Partition number (1,5-7, default 7): 5 Hex code (type L to list all codes): fd Changed type of partition 'Linux' to 'Linux raid autodetect' Command (m for help): t Partition number (1,5-7, default 7): 6 Hex code (type L to list all codes): fd Changed type of partition 'Linux' to 'Linux raid autodetect' Command (m for help): t Partition number (1,5-7, default 7): 7 Hex code (type L to list all codes): fd Changed type of partition 'Linux' to 'Linux raid autodetect' Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@localhost ~]# partx -a /dev/sdb partx: /dev/sdb: error adding partition 1 partx: /dev/sdb: error adding partitions 5-7
[root@localhost ~]# mdadm -C /dev/md0 -n2 -l1 -ayes -c128 -x1 /dev/sdb5 /dev/sdb6 /dev/sdb7
[root@localhost ~]# mdadm -D /dev/md0 /dev/md0: Version : 1.2 Creation Time : Fri Aug 5 16:00:06 2016 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 : Fri Aug 5 16:00:58 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 : e14beb8f:691289a4:23e23350:2ee02f88 Events : 17 Number Major Minor RaidDevice State 0 8 21 0 active sync /dev/sdb5 1 8 22 1 active sync /dev/sdb6 2 8 23 - spare /dev/sdb7 [root@localhost ~]#
6、创建一个大小为4G的RAID5设备,chunk大小为256k,格式化ext4文件系统,要求可开机自动挂载至/backup目录,而且不更新访问时间戳,且支持acl功能;
添加磁盘3块大小为2G的磁盘
步骤略。。。。
创建RAID5设备
[root@localhost ~]# mdadm -C /dev/md1 -n3 -l5 -c256 -ayes /dev/sdb8 /dev/sdb9 /dev/sdb10 mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md1 started.
格式化设备,开机自动挂载至/backup,不更新访问时间戳,支持acl功能
[root@localhost ~]# mke2fs -t ext4 /dev/md1 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 ~]#
[root@localhost ~]# mount -o noatime,acl /dev/md1 /backup
7、写一个脚本
(1) 接受一个以上文件路径作为参数;
(2) 显示每个文件拥有的行数;
(3) 总结说明本次共为几个文件统计了其行数;
#!/bin/bash # Version: 0.0.1 # Auther: Anaconda # Description: Number of statistics file echo "Please input the filename,I will help you statistics the file lines." read -p "Enter the filename:" filename i=0 for file in $filename;do if [ ! -e $file ];then echo "No such file." exit 3 fi echo "The number of rows in $file is:$(cat $file | wc -l)" let i=i+1 done echo "Now, I help you statistics $i file lines."
8、写一个脚本
(1) 传递两个以上字符串当作用户名;
(2) 创建这些用户;且密码同用户名;
(3) 总结说明共创建了几个用户;
#!/bin/bash # Version: 0.0.1 # Auther: Anaconda # Description: Add users. echo "Now, I will help you add users, the password as well as username." read -p "Please input the username:" username i=0 for users in $username;do if id $users &> /dev/null then echo "$users is exist." else useradd $users echo "$users" | passwd --stdin $users fi let i=i+1 done echo "Now, I help you add $i users."
9、写一个脚本,新建20个用户,visitor1-visitor20;计算他们的ID之和;
#!/bin/bash # Version: 0.0.1 # Auther: Anaconda # Description: Add twenty users visitor1 to visitor20 and calculate their UID sum. x=0 y=0 for i in {1..20};do if id visitor$i &> /dev/null;then echo "The visitor$i is exist." uid1=$(cat /etc/passwd | grep "visitor$i" | cut -d: -f3) let y=y+$uid1 else useradd visitor$i uid2=$(cat /etc/passwd | grep "visitor$i" | cut -d: -f3) let x=x+$uid2 fi done sum=$[ x+y ] echo "The UID sum of user visitor1 to user visitor 20 is: $sum"
10、写一脚本,分别统计/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#号开头的行数之和,以及总的空白行数;
#!/bin/bash # Version: 0.0.1 # Auther: Anaconda # Description: Statistics of the /etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions and /etc/fstab file to # number at the beginning of the line number # and the total number of blank lines. for i in /etc/rc.d/rc.sysinit /etc/rc.d/init.d/functions /etc/fstab;do if [ ! -e $i ];then echo "No such file: $i" continue else x=0 y=0 x=$(cat $i | grep -o "^[#]*" | wc -l) y=$(cat $i | grep "^$" | wc -l) echo "The file of $i to # number at the beginning of the line number is $x." echo "The file of $i to total numer of blank number is $y." fi done
11、写一个脚本,显示当前系统上所有默认shell为bash的用户的用户名、UID以及此类所有用户的UID之和;
#!/bin/bash # Version: 0.0.1 # Auther: Anaconda # Description: Show the users and uid of this system that the default shell as wall as username,and calculate their uid sum. cat /etc/passwd | grep "bash$" | cut -d: -f1,3 i=0 for sum in $(cat /etc/passwd | grep "bash$" | cut -d: -f3);do let i=$sum+i done echo "The UID sum is:$i"
12、写一个脚本,显示当前系统上所有,拥有附加组的用户的用户名;并说明共有多少个此类用户;
#!/bin/bash # Version: 0.0.1 # Auther: Anaconda # Description: Show this system which user have additional group and statistics it. for users in $(cat /etc/passwd | cut -d: -f3);do if [ -n "$users" ];then unames=$(cat /etc/passwd | grep "$users" | cut -d: -f1) echo $unames let i=i+1 fi done echo "A total of such users is: $i"
13、创建一个由至少两个物理卷组成的大小为20G的卷组;要求,PE大小为8M;而在卷组中创建一个大小为5G的逻辑卷mylv1,格式化为ext4文件系统,开机自动挂载至/users目录,支持acl;
[root@localhost ~]# pvcreate /dev/sdc1 /dev/sdc2 Physical volume "/dev/sdc1" successfully created Physical volume "/dev/sdc2" successfully created [root@localhost ~]# vgcreate -s 8M testvg /dev/sdc1 /dev/sdc2 Volume group "testvg" successfully created [root@localhost ~]# lvcreate -L 5G -n mylv1 testvg Logical volume "mylv1" created. [root@localhost ~]# mke2fs -t ext4 /dev/testvg/mylv1 [root@localhost ~]# mkdir /users [root@localhost ~]# mount -o auto,acl /dev/testvg/mylv1 /users
14、新建用户magedu;其家目录为/users/magedu,而后su切换至此用户,复制多个文件至家目录;
[root@localhost ~]# useradd -d /users/magedu magedu [root@localhost ~]# cp /etc/fstab /etc/inittab /var/log/anaconda/ifcfg.log /users/magedu/
15、扩展mylv1至9G,确保扩展完成后原有数据完全可用;
[root@localhost ~]# lvextend -L +4G /dev/testvg/mylv1 Size of logical volume testvg/mylv1 changed from 5.00 GiB (640 extents) to 9.00 GiB (1152 extents). Logical volume mylv1 successfully resized. [root@localhost ~]# resize2fs /dev/testvg/mylv1 resize2fs 1.42.9 (28-Dec-2013) Filesystem at /dev/testvg/mylv1 is mounted on /users; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 2 The filesystem on /dev/testvg/mylv1 is now 2359296 blocks long. [root@localhost ~]# lvdisplay --- Logical volume --- LV Path /dev/testvg/mylv1 LV Name mylv1 VG Name testvg LV UUID nb0bjj-RDOC-Lnjo-PDKE-CwIK-Peta-DeLnSN LV Write Access read/write LV Creation host, time localhost.localdomain, 2016-08-15 11:31:10 +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 8192 Block device 253:3 [root@localhost ~]# cat /users/magedu/fstab # # /etc/fstab # Created by anaconda on Fri Mar 11 22:14:07 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/centos-root / xfs defaults 0 0 UUID=73822afe-bc00-431b-8851-0151d8054a67 /boot xfs defaults 0 0 /dev/mapper/centos-home /home xfs defaults 0 0 /dev/mapper/centos-swap swap swap defaults 0 0
16、缩减mylv1至7G,确保缩减完成后原有数据完全可用;
[root@localhost ~]# umount /dev/testvg/mylv1 [root@localhost ~]# e2fsck -f /dev/testvg/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/testvg/mylv1: 22/589824 files (0.0% non-contiguous), 75562/2359296 blocks [root@localhost ~]# resize2fs /dev/testvg/mylv1 7G resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/testvg/mylv1 to 1835008 (4k) blocks. The filesystem on /dev/testvg/mylv1 is now 1835008 blocks long. [root@localhost ~]# lvreduce -L -2G /dev/testvg/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 testvg/mylv1 changed from 9.00 GiB (1152 extents) to 7.00 GiB (896 extents). Logical volume mylv1 successfully resized. [root@localhost ~]# mount -o auto,acl /dev/testvg/mylv1 /users [root@localhost ~]# cat /users/magedu/fstab # # /etc/fstab # Created by anaconda on Fri Mar 11 22:14:07 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/centos-root / xfs defaults 0 0 UUID=73822afe-bc00-431b-8851-0151d8054a67 /boot xfs defaults 0 0 /dev/mapper/centos-home /home xfs defaults 0 0 /dev/mapper/centos-swap swap swap defaults 0 0
17、对mylv1创建快照,并通过备份数据;要求保留原有的属主属组等信息;
[root@localhost ~]# lvcreate -L 7G -p r -s -n mylv1_snapshot /dev/testvg/mylv1 Logical volume "mylv1_snapshot" created.
原创文章,作者:Anaconda,如若转载,请注明出处:http://www.178linux.com/24717