1.创建一个10G分区,并格式为ext4文件系统
(1)要求其block大小为2048,预留空间百分比为2,卷标为MYDATA,默认挂载属性包含acl
(2)挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳
先分区/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
然后创建文件系统
[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.创建一个大小为1G的swap分区,并创建好文件系统,并启用之
[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.写一个脚本
(1)获取并列出当前系统上的所有磁盘设备
(2)显示所有磁盘设备上每个分区相关的空间使用信息
#!/bin/bash
#
fdisk -l /dev/[sh]d[a-z] | grep -o "Disk /dev/[sh]d[a-z]"
echo
fdisk -l
4.总结RAID的各级别及其组合方式和性能的不同
- raid-0:
条带卷,读写性能有所提升
可用空间:N*min(S1,S2...)
无容错能力
最少磁盘数:2
- raid-1:
读性能提升、写性能略有下降
可用空间:1*min(S1,S2...)
有冗余能力
最少磁盘数:2 - raid-5:
读,写性能提升
可用空间:(N-1)*min(S1,S2...)
有容错能力:1块磁盘
最少磁盘数:3 - raid-6:
读写性能提升
可用空间:(N-2)*(S1,S2...)
有容错能力:2块磁盘
最少磁盘数:4 - raid-10:
读写性能提升
可用空间:N*min(S1,S2...)
有容错能力:每组镜像最多只能坏一块
最少磁盘数:4
5、创建一个大小为10G的RAID1,要求有一个空闲盘,而且CHUNK大小为128k;
mdadm -C /dev/md0 -c 128 -n 3 -l 1 /dev/sdb{5,6,7}
6、创建一个大小为4G的RAID5设备,chunk大小为256k,格式化ext4文件系统,要求可开机自动挂载至/backup目录,而且不更新访问时间戳,且支持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.写一个脚本
(1)接收一个以上文件路径参数
(2)显示每个文件拥有的行数
(3)总结说明本次共为几个文件统计了几行
#!/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.写一个脚本
(1)传递两个以上字符串当作用户名
(2)创建这些用户,且密码同用户名相同
(3)总结说明共创建了几个用户
[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.写一个脚本,新建20个用户,visitorl-visitor20;计算他们的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.写一个脚本,分别统计/etc/rc.d/rc.sysinit /etc/rc.d/init.d/functions和/etc/fstab 文件中以#号开头的函数之和,以及总的空白行数;
[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.写一个脚本,显示当前系统上所有默认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.写一个脚本,显示当前系统上所有,拥有附加组的用户的用户名,并说明共有多少个此类用户;
[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.创建一个至少两个物理卷组成的大小为20g的卷组;要求,PE大小为8M;而在卷组中创建一个大小为5G的逻辑卷mylv1,格式化为ext4文件系统,开机自动挂载至/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切换至此用户,复制多个文件至家目录;
usetadd magedu -d /users/magedu
cp /etc/skel/* /user/magedu
su - magedu
15.扩展mylvm至9g,确保扩展完成后原有数据完全可用
[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,确保缩减完成后原有数据完全可用
[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.对mylv1创建快照,并通过备份数据;要求保留原有的属主属组等信息
[root@localhost study]# lvcreate -L 1G -p r -s -n mysnap /dev/myvg/mylvm
Logical volume "mysnap" created.
[root@localhost study]#