1、創建一個10G分區,并格式為ext4文件系統;
(1) 要求其block大小為2048, 預留空間百分比為2, 卷標為MYDATA, 默認掛載屬性包含acl;
[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, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): 1 First sector (2048-41943039, de fault 2048): 2048 Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +10G Partition 1 of type Linux and of size 10 GiB is set Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@localhost ~]# kpartx -af /dev/sdb [root@localhost ~]# mke2fs -t ext4 -b 2048 -L MYDATA -m 2 /dev/sdb1 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, 5242368 blocks 104847 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 [root@localhost ~]# tune2fs -o acl /dev/sdb1 tune2fs 1.42.9 (28-Dec-2013)
(2) 掛載至/data/mydata目錄,要求掛載時禁止程序自動運行,且不更新文件的訪問時間戳;
[root@localhost ~]# mount -o noexec,noatime /dev/sdb1 /data/mydata/ [root@localhost ~]# mount | grep sdb1 /dev/sdb1 on /data/mydata type ext4 (rw,noexec,noatime,data=ordered)
2、創建一個大小為1G的swap分區,并創建好文件系統,并啟用之;
[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 (1 primary, 0 extended, 3 free) e extended Select (default p): p Partition number (2-4, default 2): 2 First sector (20973568-41943039, default 20973568): Using default value 20973568 Last sector, +sectors or +size{K,M,G} (20973568-41943039, default 41943039): +1G Partition 2 of type Linux and of size 1 GiB is set Command (m for help): t Partition number (1,2, default 2): 2 Hex code (type L to list all codes): 82 Changed type of partition 'Linux' to 'Linux swap / Solaris' Command (m for help): p Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x27485810 Device Boot Start End Blocks Id System /dev/sdb1 2048 20973567 10485760 83 Linux /dev/sdb2 20973568 23070719 1048576 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: 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 ~]# kpartx -af /dev/sdb [root@localhost ~]# mkswap /dev/sdb2 Setting up swapspace version 1, size = 1048572 KiB no label, UUID=70fcddaf-9a5f-445a-95c1-a56a3a062290 [root@localhost ~]# swapon /dev/sdb2
3、寫一個腳本
(1)獲取并列出當前系統上的所有磁盤設備;
(2)顯示每個磁盤設備上每個分區相關的空間使用信息;
#!/bin/bash disks=$(fdisk -l | grep -o '^Disk /dev/[sh]d[a-z]' | awk '{print $2}') for i in $disks;do fdisk -l $i done
4、總結RAID的各個級別及其組合方式和性能的不同;
RAID級別 | RAID0(條帶卷) | RAID1(鏡像卷) | RAID5 | RAID6 | RAID10(最理想) | RAID01 |
性能 | 讀、寫性能提升 | 讀性能提升、寫性能略有下降 | 讀、寫性能提升 | 讀、寫性能提升 | 讀、寫性能提升 | 讀、寫性能提升 |
可用空間 | N*min(S1,S2,…) | 1*min(S1,S2,…) | (N-1)*min(S1,S2,…) | (N-2)*min(S1,S2,…) | N*min(S1,S2,…)/2 | N*min(S1,S2,…)/2 |
容錯能力 | 無容錯能力 | 有容錯能力 | 有容錯能力,最多壞1塊磁盤 | 有容錯能力,最多壞2塊磁盤 | 有容錯能力,每組鏡像最多只能壞一塊 | 有容錯能力,最多壞1塊磁盤 |
最少磁盤數 | 2 | 2 | 3 | 4 | 4 | 4 |
5、創建一個大小為10G的RAID1,要求有一個空閑盤,而且CHUNK大小為128k;
[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, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): 1 First sector (2048-104857599, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-104857599, default 104857599): +10G Partition 1 of type Linux and of size 10 GiB is set Command (m for help): n Partition type: p primary (1 primary, 0 extended, 3 free) e extended Select (default p): p Partition number (2-4, default 2): 2 First sector (20973568-104857599, default 20973568): Using default value 20973568 Last sector, +sectors or +size{K,M,G} (20973568-104857599, default 104857599): +10G Partition 2 of type Linux and of size 10 GiB is set Command (m for help): n Partition type: p primary (2 primary, 0 extended, 2 free) e extended Select (default p): p Partition number (3,4, default 3): 3 First sector (41945088-104857599, default 41945088): Using default value 41945088 Last sector, +sectors or +size{K,M,G} (41945088-104857599, default 104857599): +10G Partition 3 of type Linux and of size 10 GiB is set Command (m for help): t Partition number (1-3, default 3): 1 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-3, default 3): 2 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-3, default 3): 3 Hex code (type L to list all codes): fd Changed type of partition 'Linux' to 'Linux raid autodetect' Command (m for help): p Disk /dev/sdb: 53.7 GB, 53687091200 bytes, 104857600 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: 0x27485810 Device Boot Start End Blocks Id System /dev/sdb1 2048 20973567 10485760 fd Linux raid autodetect /dev/sdb2 20973568 41945087 10485760 fd Linux raid autodetect /dev/sdb3 41945088 62916607 10485760 fd 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 [root@localhost ~]# mdadm -C /dev/md0 -a yes -l 1 -n 2 -c 128 -x 1 /dev/sdb{1..3} mdadm: /dev/sdb1 appears to contain an ext2fs file system size=10484736K mtime=Thu Nov 3 13:35:50 2016 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. [root@localhost ~]# mdadm -D /dev/md0 /dev/md0: Version : 1.2 Creation Time : Thu Nov 3 16:52:41 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 : Thu Nov 3 16:53:42 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 : 70557e56:38472230:42b58bb6:32cd87d4 Events : 17 Number Major Minor RaidDevice State 0 8 17 0 active sync /dev/sdb1 1 8 18 1 active sync /dev/sdb2 2 8 19 - spare /dev/sdb3
6、創建一個大小為4G的RAID5設備,chunk大小為256k,格式化ext4文件系統,要求可開機自動掛載至/backup目錄,而且不更新訪問時間戳,且支持acl功能;
[root@localhost ~]# mdadm -C /dev/md1 -a yes -l 5 -n 3 -c 256 /dev/sdb{5,6,7} mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md1 started. [root@localhost ~]# mdadm -D /dev/md1 /dev/md1: Version : 1.2 Creation Time : Thu Nov 3 17:11:12 2016 Raid Level : raid5 Array Size : 8380416 (7.99 GiB 8.58 GB) Used Dev Size : 4190208 (4.00 GiB 4.29 GB) Raid Devices : 3 Total Devices : 3 Persistence : Superblock is persistent Update Time : Thu Nov 3 17:11:48 2016 State : clean Active Devices : 3 Working Devices : 3 Failed Devices : 0 Spare Devices : 0 Layout : left-symmetric Chunk Size : 256K Name : localhost.localdomain:1 (local to host localhost.localdomain) UUID : 7234dc92:b7e635b3:06765516:3846f923 Events : 18 Number Major Minor RaidDevice State 0 8 21 0 active sync /dev/sdb5 1 8 22 1 active sync /dev/sdb6 3 8 23 2 active sync /dev/sdb7 [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 524288 inodes, 2095104 blocks 104755 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2145386496 64 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, 1605632 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done [root@localhost ~]# vim /etc/fstab /dev/md1 /backup ext4 defaults,noatime,acl 0 0
7、寫一個腳本
(1) 接受一個以上文件路徑作為參數;
(2) 顯示每個文件擁有的行數;
(3) 總結說明本次共為幾個文件統計了其行數;
#!/bin/bash [ $# -lt 1 ] && echo "At least one path!" && exit 1 declare -i files_sum=0 for x in $*;do for y in $x/*;do if [ -f $y ];then wc -l $y files_sum=$[$files_sum+1] fi done done echo "共為 $files_sum 個文件統計了其行數"
8、寫一個腳本
(1) 傳遞兩個以上字符串當作用戶名;
(2) 創建這些用戶;且密碼同用戶名;
(3) 總結說明共創建了幾個用戶;
#!/bin/bash [ $# -lt 2 ] && echo "At least two user!" && exit 1 declare -i users_sum=0 for i in $*;do if id $i &> /dev/null;then echo "user $i is existed!" continue else useradd $i echo "$i" | passwd --stdin $i &> /dev/null echo "用戶 $i 創建成功,密碼同用戶名!" users_sum+=1 fi done echo "共創建了 $users_sum 個用戶"
9、寫一個腳本,新建20個用戶,visitor1-visitor20;計算他們的ID之和;
#!/bin/bash declare -i id_sum=0 for i in $(seq 1 20);do useradd visitor$i id_sum+=$(id -u visitor$i) done echo "ID之和為:$id_sum"
10、寫一腳本,分別統計/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#號開頭的行數之和,以及總的空白行數;
#!/bin/bash declare -i lines_sum1=0 declare -i lines_sum2=0 for i in /etc/rc.d/rc.sysinit /etc/rc.d/init.d/functions /etc/fstab;do lines_sum1+=$(grep "^#" $i | wc -l) lines_sum2+=$(grep "^$" $i | wc -l) done echo "以#號開頭的行數之和為:$lines_sum1" echo "總的空白行數和為:$lines_sum2"
11、寫一個腳本,顯示當前系統上所有默認shell為bash的用戶的用戶名、UID以及此類所有用戶的UID之和;
#!/bin/bash declare -i id_sum=0 grep "bash$" /etc/passwd | awk -F: '{printf "Username: %-10s UID: %d\n",$1,$3}' for i in $(grep "bash$" /etc/passwd | awk -F: '{print $3}');do id_sum+=$i done echo "默認shell為bash的用戶的UID之和為:$id_sum"
12、寫一個腳本,顯示當前系統上所有,擁有附加組的用戶的用戶名;并說明共有多少個此類用戶;
#!/bin/bash grep "[^:]\+$" /etc/group | cut -d: -f4 users=$(grep "[^:]\+$" /etc/group | cut -d: -f4 | wc -l) echo "共有 $users 個擁有附加組的用戶"
13、創建一個由至少兩個物理卷組成的大小為20G的卷組;要求,PE大小為8M;而在卷組中創建一個大小為5G的邏輯卷mylv1,格式化為ext4文件系統,開機自動掛載至/users目錄,支持acl;
先創建兩個大小為10G的分區,修改其分區類型為8e(Linux LVM) [root@localhost ~]# pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully created [root@localhost ~]# pvcreate /dev/sdb2 Physical volume "/dev/sdb2" successfully created [root@localhost ~]# vgcreate -s 8M vg01 /dev/sdb1 /dev/sdb2 Volume group "vg01" successfully created [root@localhost ~]# lvcreate -L 5G -n mylv1 vg01 Logical volume "mylv1" created. [root@localhost ~]# mke2fs -t ext4 /dev/vg01/mylv1 mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 327680 inodes, 1310720 blocks 65536 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=1342177280 40 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 (32768 blocks): done Writing superblocks and filesystem accounting information: done [root@localhost ~]# vim /etc/fstab /dev/mapper/vg01-mylv1 /users ext4 defaults,acl 0 0
14、新建用戶magedu;其家目錄為/users/magedu,而后su切換至此用戶,復制多個文件至家目錄;
[root@localhost ~]# useradd -d /users/magedu magedu [root@localhost ~]# su - magedu [magedu@localhost ~]$ cp -r /etc/pam.d /users/magedu/
15、擴展mylv1至9G,確保擴展完成后原有數據完全可用;
[root@localhost ~]# lvextend -L +4G /dev/vg01/mylv1 Size of logical volume vg01/mylv1 changed from 5.00 GiB (640 extents) to 9.00 GiB (1152 extents). Logical volume mylv1 successfully resized. [root@localhost ~]# e2fsck -f /dev/vg01/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/vg01/mylv1: 11/327680 files (0.0% non-contiguous), 58462/1310720 blocks [root@localhost ~]# resize2fs /dev/vg01/mylv1 resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/vg01/mylv1 to 2359296 (4k) blocks. The filesystem on /dev/vg01/mylv1 is now 2359296 blocks long.
16、縮減mylv1至7G,確??s減完成后原有數據完全可用;
[root@localhost ~]# e2fsck -f /dev/vg01/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/vg01/mylv1: 11/589824 files (0.0% non-contiguous), 75551/2359296 blocks [root@localhost ~]# resize2fs /dev/vg01/mylv1 7G resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/vg01/mylv1 to 1835008 (4k) blocks. The filesystem on /dev/vg01/mylv1 is now 1835008 blocks long. [root@localhost ~]# lvreduce -L 7G /dev/vg01/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 vg01/mylv1 changed from 9.00 GiB (1152 extents) to 7.00 GiB (896 extents). Logical volume mylv1 successfully resized.
17、對mylv1創建快照,并通過備份數據;要求保留原有的屬主屬組等信息。
[root@localhost /]# lvcreate -s -L 50M -p r -n snap_mylv /dev/vg01/mylv1 Rounding up size to full physical extent 56.00 MiB Logical volume "snap_mylv" created. [root@localhost /]# mount /dev/vg01/snap_mylv /backup mount: /dev/mapper/vg01-snap_mylv is write-protected, mounting read-only
原創文章,作者:蘿卜,如若轉載,請注明出處:http://www.www58058.com/52410
贊,幾個腳本都寫的不錯,繼續加油~~