1、创建一个10G分区,并格式为ext4文件系统;
(1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;
(2) 挂载至datamydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳
[root@ns1 ~]# mkfs.ext4 -b 2048 -m 2 -L MYDATA /dev/sdc1 [root@ns1 ~]# tune2fs -o acl /dev/sdc1 tune2fs 1.41.12 (17-May-2010) [root@ns1 ~]# mount -o noexec,noatime,acl /dev/sdc1 /datamydata/ [root@ns1 ~]# cat /etc/mtab | grep sdc1 /dev/sdc1 /datamydata ext4 rw,noexec,noatime,acl 0 0 [root@ns1 ~]# echo "/dev/sdc1 /datamydata ext4 defaults,noexec,noatime,acl 0 0">>/etc/fstab [root@ns1 ~]# mount -a [root@ns1 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda5 14G 8.9G 4.0G 70% / tmpfs 1.9G 72K 1.9G 1% /dev/shm /dev/sda1 190M 42M 139M 23% /boot /dev/sda3 2.0G 3.1M 1.9G 1% /tmp /dev/sdc1 9.8G 13M 9.6G 1% /datamydata
2、创建一个大小为1G的swap分区,并创建好文件系统,并启用之;
方法1:(利用磁盘分区做swap分区)
# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x972e4c2d.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): n//添加一个分区
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1//分区号,输入1
First cylinder (1-2610, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610):
Using default value 2610
Command (m for help): w//保存配置
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
格式化分区设备:
# mkswap /dev/sdb1
Setting up swapspace version 1, size = 20964788 KiB
no label, UUID=01896901-c843-4b2c-b627-3ab208325ece
启用swap分区:
# swapon /dev/sdb1
# echo "/dev/sdb1 swap swap defaults 0 0">>/etc/fstab
查看swap分区:
# cat /proc/swaps
FilenameTypeSizeUsedPriority
/dev/sda2 partition41943000-1
/dev/sdb1 partition209647880 -2
# free -m
total used free shared buffers cached
Mem: 3818 425 3393 1 72 129
-/+ buffers/cache: 223 3594
Swap: 24569 0 24569
方法2:(利用磁盘文件做swap分区)
# dd if=/dev/zero of=/swapfile1 bs=1M count=512
记录了512+0 的读入
记录了512+0 的写出
536870912字节(537 MB)已复制,29.1873 秒,18.4 MB/秒
# mkswap /swapfile1
Setting up swapspace version 1, size = 524284 KiB
no label, UUID=3cba4d1a-d846-4e5c-b5b2-7d638bb9c345
You have new mail in /var/spool/mail/root
# swapon /swapfile1
# cat /proc/swaps
FilenameTypeSizeUsedPriority
/dev/sda2 partition41943000-1
/dev/sdb1 partition209647880-2
/swapfile1 file5242840-3
# echo "/swapfile1 swap swap defaults 0 0">>/etc/fstab
3、写一个脚本
(1)、获取并列出当前系统上的所有磁盘设备;
(2)、显示每个磁盘设备上每个分区相关的空间使用信息;
#!/bin/bash #created by molewan # show the disks names and the space fdisk -l | grep -o "^/[^[:space:]]\+" >/disk.txt df -h>/space.txt # fdisk -l | grep -o "^/[^[:space:]]\+" /dev/sda1 /dev/sda2 /dev/sda3 /dev/sda4 /dev/sda5 # df -h Filesystem Size Used Avail Use% Mounted on /dev/sda5 14G 8.4G 4.5G 66% / tmpfs 1.9G 72K 1.9G 1% /dev/shm /dev/sda1 190M 42M 139M 23% /boot /dev/sda3 2.0G 3.1M 1.9G 1% /tmp
4、总结RAID的各个级别及其组合方式和性能的不同;
5、创建一个大小为10G的RAID1,要求有一个空闲盘,而且CHUNK大小为128k;
6、创建一个大小为4G的RAID5设备,chunk大小为256k,格式化ext4文件系统,要求可开机自动挂载至backup目录,而且不更新访问时间戳,且支持acl功能;
说明:关于RAID,因为生成环境肯定会接触到,但用到软RAID的情况真心不太多,所以重点精力还是投在对概念的理解以及应用场景的理解,比如说服务器出厂时默认的RAID通常为RAID1(2块盘),大于2块盘一般会用RAID5,一般还会用一块热备盘,已便出现故障后,能够顶上去。

7、写一个脚本
(1) 接受一个以上文件路径作为参数;
(2) 显示每个文件拥有的行数;
(3) 总结说明本次共为几个文件统计了其行数;
# cat collet.sh
#!/bin/bash
#creted by molewan
#
declare -i i=0
if [ $# -lt 1 ];then
echo "please input file path,for example:/etc/passwd"
exit 2
fi
for file in $*;do
if [ -f $file ] ;then
echo "$file:$(wc -l $file | awk '{print $1}')"
let i++
else
echo "please input file path,for example:/etc/passwd"
fi
done
echo "file counts:$i"
验证结果:
[root@ns1 ~]# bash collet.sh 1
please input file path,for example:/etc/passwd
file counts:0
[root@ns1 ~]# bash collet.sh passwd
please input file path,for example:/etc/passwd
file counts:0
[root@ns1 ~]# bash collet.sh /etc/pass
please input file path,for example:/etc/passwd
file counts:0
[root@ns1 ~]# file /etc/pass
/etc/pass: cannot open `/etc/pass' (No such file or directory)
[root@ns1 ~]# bash collet.sh /etc/passwd
/etc/passwd:76
file counts:1
[root@ns1 ~]# bash collet.sh /etc/passwd /etc/filesystems
/etc/passwd:76
/etc/filesystems:9
file counts:2
8、写一个脚本
(1) 传递两个以上字符串当作用户名;
(2) 创建这些用户;且密码同用户名;
(3) 总结说明共创建了几个用户;
#!/bin/bash # declare -i i=0 if [ $# -lt 2 ];then echo "please input two username,for example:zhang3 li4" exit 2 fi for username in $@; do id $username &> /dev/null if [ $? -eq 0 ]; then echo "$username exist" else useradd $username echo "$username" | passwd --stdin $username &> /dev/null let i++ fi done echo "add user count: $i"
9、写一个脚本,新建20个用户,visitor1-visitor20;计算他们的ID之和;
#!/bin/bash # declare -i sum=0; for ((i=1;i<=20;i++)); do useradd visitor$i; echo "useradd visitor$i success!" sum+=$(id -u visitor$i) done echo "All user uid sum: $sum"
10、写一脚本,分别统计/etc/rc.d /rc/sysinit、/etc/rc.d /init.d/functions和/etc/fstab文件中以#号开头的行数之和,以及总的空白行数;
#!/bin/bash
#
declare -i sum1=0;
declare -i sum2=0;
for i in {/etc/rc.d/rc.sysinit,/etc/rc.d/init.d/functions,/etc/fstab}; do
sum1+=$( grep -c '^#' $i )
sum2+=$( grep -c '^[[:space:]]*$' $i )
done
echo "#start line number: $sum1"
echo "space line number: $sum2"
11、写一个脚本,显示当前系统上所有默认shell为bash的用户的用户名、UID以及此类所有用户的UID之和;
#!/bin/bash
#
declare -i sum=0;
declare -i i=0;
SHELLUSER=$(grep "/bin/bash" /etc/passwd | awk -F ":" '{print $1}'\t)
USERUID=$(grep "/bin/bash" /etc/passwd | awk -F ":" '{print $3}'\t)
for i in $USERUID;do
let sum+=$i
done
echo "user id sum=$sum"
#####################################
echo "users is $SHELLUSER"
#####################################
echo "userid is $USERUID"
12、写一个脚本,显示当前系统上所有,拥有附加组的用户的用户名;并说明共有多少个此类用户;
实际我们是想找出带有,隔开的
[root@ns1 ~]# id adm
uid=3(adm) gid=4(adm) 组=4(adm),3(sys)
#!/bin/bash
#
declare -i i=0;
for user in `cut -d: -f1 /etc/passwd`; do
group=$(id $user | cut -d" " -f3 | awk -F, '{print $2}')
if [ -n "$group" ]; then
echo $user
let i++
fi
done
echo "user number: $i"
测试结果:
[root@ns1 ~]# bash 12.sh
bin
daemon
adm
postfix
amandabackup
user number: 5
13、创建一个由至少两个物理卷组成的大小为20G的卷组;要求,PE大小为8M;而在卷组中创建一个大小为5G的逻辑卷mylv1,格式化为ext4文件系统,开机自动挂载至users目录,支持acl;
思路:
a.将磁盘分区转换为8e模式的(LVM卷)
b.磁盘卷转换为物理卷(pvcreate)
c.创建vg(vgcreate)
d.基于vg创建lv
e.格式化分区,并编写/etc/fstab,配置好挂载的属性
分区并打好标签:
[root@ns1 usermagedu]# fdisk /dev/sdd
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x9eb30a74.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2610, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +10G
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@ns1 usermagedu]# fdisk /dev/sde
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x7cb5d846.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2610, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +10G
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@ns1 usermagedu]# partx /dev/sdd
# 1: 63- 20980889 ( 20980827 sectors, 10742 MB)
# 2: 0- -1 ( 0 sectors, 0 MB)
# 3: 0- -1 ( 0 sectors, 0 MB)
# 4: 0- -1 ( 0 sectors, 0 MB)
[root@ns1 usermagedu]# partx /dev/sde
# 1: 63- 20980889 ( 20980827 sectors, 10742 MB)
# 2: 0- -1 ( 0 sectors, 0 MB)
# 3: 0- -1 ( 0 sectors, 0 MB)
# 4: 0- -1 ( 0 sectors, 0 MB)
转化为pv卷
[root@ns1 usermagedu]# pvcreate /dev/sdd1
Physical volume "/dev/sdd1" successfully created
[root@ns1 usermagedu]# pvcreate /dev/sde1
Physical volume "/dev/sde1" successfully created
[root@ns1 usermagedu]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdd1 lvm2 --- 10.00g 10.00g
/dev/sde1 lvm2 --- 10.00g 10.00g
创建VG参数:
[root@ns1 usermagedu]# vgcreate myvg -s 8M /dev/sdd1 /dev/sde1
Volume group "myvg" successfully created
[root@ns1 usermagedu]# vgdisplay
--- Volume group ---
VG Name myvg
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 20.00 GiB
PE Size 8.00 MiB
Total PE 2560
Alloc PE / Size 0 / 0
Free PE / Size 2560 / 20.00 GiB
VG UUID 6SRviy-OTzG-SyjB-9d2m-ZNgQ-y1kn-zLAfWy
创建LV:
[root@ns1 usermagedu]# lvcreate -L 5G -n myvlv1 myvg
Logical volume "myvlv1" created.
[root@ns1 usermagedu]# lvdisplay
--- Logical volume ---
LV Path /dev/myvg/myvlv1
LV Name myvlv1
VG Name myvg
LV UUID JGiFW6-8hI9-7BqR-srof-2UdS-i2mG-x9ZKrQ
LV Write Access read/write
LV Creation host, time ns1.example.com, 2016-08-22 10:40:48 +0800
LV Status available
# open 0
LV Size 5.00 GiB
Current LE 640
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
[root@ns1 usermagedu]# mkfs.ext4 /dev/myvg/myvlv1
[root@ns1 ~]# mkdir -p /users
[root@ns1 ~]# echo "/dev/myvg/myvlv1 /users ext4 defaults,acl 0 0">>/etc/fstab
[root@ns1 ~]# mount -a
[root@ns1 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda5 14G 8.9G 4.0G 70% /
tmpfs 1.9G 72K 1.9G 1% /dev/shm
/dev/sda1 190M 42M 139M 23% /boot
/dev/sda3 2.0G 3.1M 1.9G 1% /tmp
/dev/sdc1 9.8G 13M 9.6G 1% /datamydata
/dev/mapper/myvg-myvlv1
4.8G 10M 4.6G 1% /users
14、新建用户magedu;其家目录为usersmagedu,而后su切换至此用户,复制多个文件至家目录;
[root@ns1 ~]# adduser magedu -d /usermagedu [root@ns1 ~]# cd /usermagedu/ [root@ns1 usermagedu]# ls -ld /usermagedu/ drwx------ 4 magedu magedu 4096 Aug 22 10:30 /usermagedu/ [root@ns1 usermagedu]# su - magedu [magedu@ns1 ~]$ cp /etc/sysconfig/network . [magedu@ns1 ~]$ ls network [magedu@ns1 ~]$ pwd /usermagedu
15、扩展mylv1至9G,确保扩展完成后原有数据完全可用;
[root@ns1 users]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert myvlv1 myvg -wi-ao---- 5.00g [root@ns1 users]# vgs VG #PV #LV #SN Attr VSize VFree myvg 2 1 0 wz--n- 20.00g 15.00g [root@ns1 users]# lvextend -L 9G /dev/myvg/myvlv1 [root@ns1 users]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert myvlv1 myvg -wi-ao---- 9.00g [root@ns1 users]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda5 14G 8.9G 4.0G 70% / tmpfs 1.9G 72K 1.9G 1% /dev/shm /dev/sda1 190M 42M 139M 23% /boot /dev/sda3 2.0G 3.1M 1.9G 1% /tmp /dev/sdc1 9.8G 13M 9.6G 1% /datamydata /dev/mapper/myvg-myvlv1 4.8G 11M 4.6G 1% /users [root@ns1 users]# resize2fs /dev/myvg/myvlv1 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/myvg/myvlv1 is mounted on /users; on-line resizing required old desc_blocks = 1, new_desc_blocks = 1 Performing an on-line resize of /dev/myvg/myvlv1 to 2359296 (4k) blocks. The filesystem on /dev/myvg/myvlv1 is now 2359296 blocks long. [root@ns1 users]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda5 14G 8.9G 4.0G 70% / tmpfs 1.9G 72K 1.9G 1% /dev/shm /dev/sda1 190M 42M 139M 23% /boot /dev/sda3 2.0G 3.1M 1.9G 1% /tmp /dev/sdc1 9.8G 13M 9.6G 1% /datamydata /dev/mapper/myvg-myvlv1 8.8G 12M 8.3G 1% /users
16、缩减mylv1至7G,确保缩减完成后原有数据完全可用;
[root@ns1 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda5 14G 8.9G 4.0G 70% / tmpfs 1.9G 72K 1.9G 1% /dev/shm /dev/sda1 190M 42M 139M 23% /boot /dev/sda3 2.0G 3.1M 1.9G 1% /tmp /dev/sdc1 9.8G 13M 9.6G 1% /datamydata /dev/mapper/myvg-myvlv1 8.8G 21M 8.3G 1% /users 卸载挂载的LV(与AIX不同,缩小LV存在风险,必须先卸载分区) [root@ns1 ~]# umount /dev/mapper/myvg-myvlv1 [root@ns1 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda5 14G 8.9G 4.0G 70% / tmpfs 1.9G 72K 1.9G 1% /dev/shm /dev/sda1 190M 42M 139M 23% /boot /dev/sda3 2.0G 3.1M 1.9G 1% /tmp /dev/sdc1 9.8G 13M 9.6G 1% /datamydata 检查文件系统是否存在坏块: [root@ns1 ~]# e2fsck -f /dev/mapper/myvg-myvlv1 e2fsck 1.41.12 (17-May-2010) 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/mapper/myvg-myvlv1: 11/589824 files (0.0% non-contiguous), 74975/2359296 blocks 缩小文件系统到7G: [root@ns1 ~]# resize2fs /dev/mapper/myvg-myvlv1 7G resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/mapper/myvg-myvlv1 to 1835008 (4k) blocks. The filesystem on /dev/mapper/myvg-myvlv1 is now 1835008 blocks long. 缩小LV: [root@ns1 ~]# lvreduce -L 7G /dev/myvg/myvlv1 WARNING: Reducing active logical volume to 7.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce myvlv1? [y/n]: y//根据提示进行操作 Size of logical volume myvg/myvlv1 changed from 9.00 GiB (1152 extents) to 7.00 GiB (896 extents). Logical volume myvlv1 successfully resized 重新进行分区挂载: [root@ns1 ~]# mount -a [root@ns1 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda5 14G 8.9G 4.0G 70% / tmpfs 1.9G 72K 1.9G 1% /dev/shm /dev/sda1 190M 42M 139M 23% /boot /dev/sda3 2.0G 3.1M 1.9G 1% /tmp /dev/sdc1 9.8G 13M 9.6G 1% /datamydata /dev/mapper/myvg-myvlv1 6.8G 21M 6.4G 1% /users
17、对mylv1创建快照,并通过备份数据;要求保留原有的属主属组等信息;
原理:LVM 快照利用一种称为“写时复制(COW – Copy-On-Write)”的技术来跟踪和维持其数据的一致性。它的原理比较简单,就是跟踪原始卷上块的改变, 在这些数据被改变之前将其复制到快照自己的预留空间里(顾名思义称为写时复制)。 当对快照进行读取的时候,被修改的数据从快照的预留空间中读取,未修改的数据则重定向到原始卷上去读取,因此在快照的文件系统与设备之间多了一层COW设备。
[root@ns1 ~]# lvcreate -L 1G -n mylv1_snapshot -p r -s /dev/myvg/myvlv1 Logical volume "mylv1_snapshot" created. [root@ns1 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv1_snapshot myvg sri-a-s--- 1.00g myvlv1 0.00 myvlv1 myvg owi-aos--- 7.00g [root@ns1 ~]# mount /dev/myvg/mylv1_snapshot /lvmsnapshot/ mount: block device /dev/mapper/myvg-mylv1_snapshot is write-protected, mounting read-only [root@ns1 ~]# ls -lrt /lvmsnapshot/ total 16 drwx------ 2 root root 16384 Aug 22 10:57 lost+found [root@ns1 users]# ls -lrt total 16 drwx------ 2 root root 16384 Aug 22 10:57 lost+found 关于LVM快照的应用,后面我会写一篇文章补充
原创文章,作者:Net21-冰冻vs西瓜,如若转载,请注明出处:http://www.178linux.com/38880


评论列表(1条)
有方法,有思路,有过程,有结果,很棒