1.創(chuàng)建一個(gè)10G分區(qū)既鞠,并格式為ext4文件系統(tǒng)
(1)要求其block大小為2048行瑞,預(yù)留空間百分比為2叹阔,卷標(biāo)為MYDATA截酷,默認(rèn)掛載屬性包含acl
(2)掛載至/data/mydata目錄,要求掛載時(shí)禁止程序自動(dòng)運(yùn)行溪厘,且不更新文件的訪問時(shí)間戳
先分區(qū)/dev/sdb
[root@localhost study]# fdisk -l
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 /dev/sda: 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: 0x000e36a0
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 41943039 20458496 8e Linux LVM
Disk /dev/mapper/centos_localhost-root: 18.8 GB, 18756927488 bytes, 36634624 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 /dev/mapper/centos_localhost-swap: 2147 MB, 2147483648 bytes, 4194304 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
[root@localhost study]# 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.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x4d2d8180.
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, default 2048): +10G
Last sector, +sectors or +size{K,M,G} (20971520-41943039, default 41943039):
Using default value 41943039
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 study]# partx -a /dev/sda
partx: /dev/sda: error adding partitions 1-2
[root@localhost study]# partx -a /dev/sdb
partx: /dev/sdb: error adding partition 1
[root@localhost study]# partx -a /dev/sdb
partx: /dev/sdb: error adding partition 1
然后創(chuàng)建文件系統(tǒng)
[root@localhost study]# mke2fs -b 2048 -m 2 -t ext4 -L MYDATA /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, 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
掛載到/data/mydata
[root@localhost study]# tune2fs -o acl /dev/sdb1
tune2fs 1.42.9 (28-Dec-2013)
[root@localhost study]# mkdir -p /data/mydata
[root@localhost study]# mount -o noatime,noexec /dev/sdb1 /data/mydata
[root@localhost study]# mount | grep sdb1
/dev/sdb1 on /data/mydata type ext4 (rw,noexec,noatime,seclabel,data=ordered)
2.創(chuàng)建一個(gè)大小為1G的swap分區(qū)胡本,并創(chuàng)建好文件系統(tǒng),并啟用之
[root@localhost study]# 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 (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +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: 0x4d2d8180
Device Boot Start End Blocks Id System
/dev/sdb1 20971520 41943039 10485760 83 Linux
/dev/sdb2 2048 2099199 1048576 82 Linux swap / Solaris
Partition table entries are not in disk order
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 study]# partx -a /dev/sdb
partx: /dev/sdb: error adding partition 1
[root@localhost study]# partx -a /dev/sdb
partx: /dev/sdb: error adding partitions 1-2
[root@localhost study]# cat /proc/partitions
major minor #blocks name
8 16 20971520 sdb
8 17 10485760 sdb1
8 18 1048576 sdb2
8 0 20971520 sda
8 1 512000 sda1
8 2 20458496 sda2
11 0 7587840 sr0
253 0 18317312 dm-0
253 1 2097152 dm-1
[root@localhost study]# mkswap -L SWAP /dev/sdb2
Setting up swapspace version 1, size = 1048572 KiB
LABEL=SWAP, UUID=255b7a03-0e28-4c0d-b692-5ddf3c133f75
[root@localhost study]# swapon /dev/sdb2
3.寫一個(gè)腳本
(1)獲取并列出當(dāng)前系統(tǒng)上的所有磁盤設(shè)備
(2)顯示所有磁盤設(shè)備上每個(gè)分區(qū)相關(guān)的空間使用信息
#!/bin/bash
#
fdisk -l /dev/[sh]d[a-z] | grep -o "Disk /dev/[sh]d[a-z]"
echo
fdisk -l
4.總結(jié)RAID的各級(jí)別及其組合方式和性能的不同
- raid-0:
條帶卷畸悬,讀寫性能有所提升
可用空間:N*min(S1,S2...)
無容錯(cuò)能力
最少磁盤數(shù):2
- raid-1:
讀性能提升侧甫、寫性能略有下降
可用空間:1*min(S1,S2...)
有冗余能力
最少磁盤數(shù):2 - raid-5:
讀,寫性能提升
可用空間:(N-1)*min(S1,S2...)
有容錯(cuò)能力:1塊磁盤
最少磁盤數(shù):3 - raid-6:
讀寫性能提升
可用空間:(N-2)*(S1,S2...)
有容錯(cuò)能力:2塊磁盤
最少磁盤數(shù):4 - raid-10:
讀寫性能提升
可用空間:N*min(S1,S2...)
有容錯(cuò)能力:每組鏡像最多只能壞一塊
最少磁盤數(shù):4
5蹋宦、創(chuàng)建一個(gè)大小為10G的RAID1披粟,要求有一個(gè)空閑盤,而且CHUNK大小為128k;
mdadm -C /dev/md0 -c 128 -n 3 -l 1 /dev/sdb{5,6,7}
6冷冗、創(chuàng)建一個(gè)大小為4G的RAID5設(shè)備守屉,chunk大小為256k,格式化ext4文件系統(tǒng)蒿辙,要求可開機(jī)自動(dòng)掛載至/backup目錄拇泛,而且不更新訪問時(shí)間戳,且支持acl功能思灌;
mdadm -C /dev/md0 -c 256 -n 3 -l 5 /dev/sdb{5,6,7}
mke2fs -t ext4 /dev/md0
mkdir /backup
echo "/dev/md0 /backup ext4 defaults,noatime,acl 0 0" >>/etc/fstab
7.寫一個(gè)腳本
(1)接收一個(gè)以上文件路徑參數(shù)
(2)顯示每個(gè)文件擁有的行數(shù)
(3)總結(jié)說明本次共為幾個(gè)文件統(tǒng)計(jì)了幾行
#!/bin/bash
#
if [ $# -lt 1 ];then
echo "At least one filesname "
exit 2
fi
for i in $*;do
line=$(cat $i | wc -l)
echo "Number of rows in $i is $line"
done
echo "total of $# files"
[root@localhost study]# bash Sum_wc.sh /etc/passwd /etc/fstab
Number of rows in /etc/passwd is 44
Number of rows in /etc/fstab is 11
total of 2 files
8.寫一個(gè)腳本
(1)傳遞兩個(gè)以上字符串當(dāng)作用戶名
(2)創(chuàng)建這些用戶俺叭,且密碼同用戶名相同
(3)總結(jié)說明共創(chuàng)建了幾個(gè)用戶
[root@localhost study]# cat creat_user.sh
#!/bin/bash
if [ $# -lt 2 ];then
echo "At least two username "
exit 2
fi
for i in $*;do
if grep "^$i\>" /etc/passwd &> /dev/null;then
echo "User $i exists"
else
useradd $i
echo $i | passwd --stdin $i &> /dev/null
echo "Add user $i finished"
fi
done
echo "total of Create $# users"
[root@localhost study]# bash creat_user.sh user1 user2 user3 user4
Add user user1 finished
Add user user2 finished
Add user user3 finished
Add user user4 finished
total of Create 4 users
9.寫一個(gè)腳本,新建20個(gè)用戶泰偿,visitorl-visitor20熄守;計(jì)算他們的ID之和;
[root@localhost study]# bash idsum_20user.sh
id sum :20290
[root@localhost study]# cat idsum_20user.sh
#!/bin/bash
#
for i in {1..20};do
id visitor$i &> /dev/null && continue
useradd visitor$i
let sum+=`id -u visitor$i`
done
echo "id sum :$sum"
10.寫一個(gè)腳本耗跛,分別統(tǒng)計(jì)/etc/rc.d/rc.sysinit /etc/rc.d/init.d/functions和/etc/fstab 文件中以#號(hào)開頭的函數(shù)之和裕照,以及總的空白行數(shù);
[root@localhost study]# cat practice10.sh
#!/bin/bash
#
for file in /etc/rc.d/rc.sysinit /etc/rc.d/init.d/functions /etc/fstab;do
line=`grep "^#" $file|wc -l`
echo "$file lines num : $line"
line=`grep "^$" $file|wc -l`
echo "$file null lines :$line"
done
[root@localhost study]# bash practice10.sh
grep: /etc/rc.d/rc.sysinit: No such file or directory
/etc/rc.d/rc.sysinit lines num : 0
grep: /etc/rc.d/rc.sysinit: No such file or directory
/etc/rc.d/rc.sysinit null lines :0
/etc/rc.d/init.d/functions lines num : 31
/etc/rc.d/init.d/functions null lines :67
/etc/fstab lines num : 7
/etc/fstab null lines :1
[root@localhost study]#
11.寫一個(gè)腳本调塌,顯示當(dāng)前系統(tǒng)上所有默認(rèn)shell為bash的用戶的用戶名晋南,UID以及此類所有用戶的UID之和;
[root@localhost study]# bash usersum.sh
root:0
study:1000
user1:1001
user2:1002
user3:1003
user4:1004
visitor1:1005
visitor2:1006
visitor3:1007
visitor4:1008
visitor5:1009
visitor6:1010
visitor7:1011
visitor8:1012
visitor9:1013
visitor10:1014
visitor11:1015
visitor12:1016
visitor13:1017
visitor14:1018
visitor15:1019
visitor16:1020
visitor17:1021
visitor18:1022
visitor19:1023
visitor20:1024
Users ID sum is:25300
[root@localhost study]# cat usersum.sh
#!/bin/bash
#
grep "bin/bash$" /etc/passwd |cut -d":" -f1,3
for i in `grep "bin/bash$" /etc/passwd |awk -F ":" '{print $3}'`;do
let sum+=$i
done
echo "Users ID sum is:$sum"
[root@localhost study]#
12.寫一個(gè)腳本羔砾,顯示當(dāng)前系統(tǒng)上所有搬俊,擁有附加組的用戶的用戶名,并說明共有多少個(gè)此類用戶蜒茄;
[root@localhost study]# cat q12.sh
#!/bin/bash
#
grep -v ":$" /etc/group |awk -F ":" '{print $1}'
echo "This users has $(grep -v ":$" /etc/group |awk -F ":" '{print $1}'|wc -l)."
[root@localhost study]# bash q12.sh
mail
kvm
study
This users has 3.
13.創(chuàng)建一個(gè)至少兩個(gè)物理卷組成的大小為20g的卷組;要求餐屎,PE大小為8M檀葛;而在卷組中創(chuàng)建一個(gè)大小為5G的邏輯卷mylv1,格式化為ext4文件系統(tǒng)腹缩,開機(jī)自動(dòng)掛載至/users目錄屿聋,支持acl
[root@localhost study]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos_localhost
PV Size 19.51 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 4994
Free PE 10
Allocated PE 4984
PV UUID IRTczn-KJya-LP55-eKz2-Yu50-YWkv-SGcasG
"/dev/sdb1" is a new physical volume of "10.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 10.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID X7fSyM-KecT-FGeZ-vFPY-xrcF-aFKe-LZGTWr
"/dev/sdb3" is a new physical volume of "5.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb3
VG Name
PV Size 5.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID tEnrWR-1oXw-uqhr-uzPu-s7V3-VkRx-QuyrOK
[root@localhost study]# vgcreate -s 8M myvg /dev/sdb1 /dev/sdb3
Volume group "myvg" successfully created
[root@localhost study]# lvcreate -L 5G -n mylvm myvg
Logical volume "mylvm" created.
[root@localhost study]# mke2fs -t ext4 /dev/myvg/mylvm
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 study]# mkdir /users
[root@localhost study]# echo "/dev/myvg/mylvm /users ext4 defaults,acl 0 0" >> /etc/fstab
[root@localhost study]#
14.新建用戶magedu空扎,其家目錄為/users/magedu,而后su切換至此用戶润讥,復(fù)制多個(gè)文件至家目錄转锈;
usetadd magedu -d /users/magedu
cp /etc/skel/* /user/magedu
su - magedu
15.擴(kuò)展mylvm至9g,確保擴(kuò)展完成后原有數(shù)據(jù)完全可用
[root@localhost study]# lvextend -L 9G /dev/myvg/mylvm
Size of logical volume myvg/mylvm changed from 5.00 GiB (640 extents) to 9.00 GiB (1152 extents).
Logical volume mylvm successfully resized.
[root@localhost study]# resize2fs /dev/myvg/mylvm
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/myvg/mylvm to 2359296 (4k) blocks.
The filesystem on /dev/myvg/mylvm is now 2359296 blocks long.
[root@localhost study]#
16.縮減mylv1至7g楚殿,確贝榭縮減完成后原有數(shù)據(jù)完全可用
[root@localhost study]# umount /dev/myvg/mylvm
umount: /dev/myvg/mylvm: not mounted
[root@localhost study]# e2fsck -f /dev/myvg/mylvm
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/mylvm: 11/589824 files (0.0% non-contiguous), 75551/2359296 blocks
[root@localhost study]# resize2fs /dev/myvg/mylvm
resize2fs 1.42.9 (28-Dec-2013)
The filesystem is already 2359296 blocks long. Nothing to do!
[root@localhost study]# resize2fs /dev/myvg/mylvm 7G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/myvg/mylvm to 1835008 (4k) blocks.
The filesystem on /dev/myvg/mylvm is now 1835008 blocks long.
[root@localhost study]# lvreduce -L 7G /dev/myvg/mylvm
WARNING: Reducing active logical volume to 7.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce mylvm? [y/n]: y
Size of logical volume myvg/mylvm changed from 9.00 GiB (1152 extents) to 7.00 GiB (896 extents).
Logical volume mylvm successfully resized.
[root@localhost study]# mount /dev/myvg/mylvm /users/
17.對(duì)mylv1創(chuàng)建快照,并通過備份數(shù)據(jù)脆粥;要求保留原有的屬主屬組等信息
[root@localhost study]# lvcreate -L 1G -p r -s -n mysnap /dev/myvg/mylvm
Logical volume "mysnap" created.
[root@localhost study]#