1.创建一个10G的分区,并格式为ext4的文件系统;
(1)要求其block大小为2048,预留空间百分比为2,卷标为MYDATA,默认挂载属性包含acl;
(2)挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳;
[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 (2 primary, 0 extended, 2 free) e extended Select (default p): e #创建的是扩展分区 Partition number (3,4, default 3): 3 First sector (41945088-83886079, default 41945088): Using default value 41945088 Last sector, +sectors or +size{K,M,G} (41945088-83886079, default 83886079): Using default value 83886079 Partition 3 of type Extended and of size 20 GiB is set Command (m for help): n Partition type: p primary (2 primary, 1 extended, 1 free) l logical (numbered from 5) Select (default p): l #创建逻辑分区,分配10G Adding logical partition 5 First sector (41947136-83886079, default 41947136): Using default value 41947136 Last sector, +sectors or +size{K,M,G} (41947136-83886079, default 83886079): +10G Partition 5 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 ~]# partprobe /dev/sdb5
创建文件系统并挂载
[root@localhost ~]# mke2fs -t ext4 -b 2048 -m 2 -L MYDATA /dev/sdb5 mke2fs 1.42.9 (28-Dec-2013) Filesystem label=MYDATA #卷标 OS type: Linux Block size=2048 (log=1) #块大小2048 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 #为超级用户预留2%空间 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 ## 默认挂载属性包含acl [root@localhost ~]# tune2fs -o acl /dev/sdb5 tune2fs 1.42.9 (28-Dec-2013) #挂载 [root@localhost ~]# mkdir -p /data/mydata [root@localhost ~]# mount -o noexec,noatime /dev/sdb5 /data/mydata [root@localhost ~]# mount /dev/sdb5 on /data/mydata type ext4 (rw,noexec,noatime,seclabel,data=ordered) [root@localhost ~]# df -lh #查看磁盘使用情况 Filesystem Size Used Avail Use% Mounted on /dev/sdb5 9.8G 13M 9.6G 1% /data/mydata #自动挂载,修改/etc/fstab文件 /dev/sdb5 /data/mydata ext4 defaults,acl,noexec,noatime, 0 0
2.创建一个大小为1G的swap分区,并创建好文件系统,并启用之;
创建一个sdb6分区,大小1G
[root@localhost ~]# mkswap /dev/sdb6 #格式化为swap分区 Setting up swapspace version 1, size = 1048572 KiB no label, UUID=2a049e47-5b17-4311-9c6f-9cf945c7f2cc [root@localhost ~]# blkid /dev/sdb6 /dev/sdb6: UUID="2a049e47-5b17-4311-9c6f-9cf945c7f2cc" TYPE="swap" [root@localhost ~]# free -m #查看现在swap分区大小,为2G total used free shared buff/cache available Mem: 977 118 710 6 148 707 Swap: 2047 0 2047 [root@localhost ~]# swapon /dev/sdb6 #启用/dev/sdb6的swap分区 [root@localhost ~]# free -m total used free shared buff/cache available Mem: 977 119 709 6 148 706 Swap: 3071 0 3071 #此时swap大小为3G,证明sdb6的swap分区已启动
3.写一个脚本:
(1)获取并列出当前系统上的所有磁盘设备;
(2)显示每个磁盘设备上每个分区相关的空间使用信息;
#!/bin/bash # fdisk -l /dev/[sh]d[a-z] | grep "^Disk /dev/[sh]d[a-z]" df -lh | grep "^/dev/[sh]d[a-z]"
1478337359175.jpg
4.总结RAID的各个级别及其组合方式和性能的不同;
RAID:
Redundant Arrays of Independent Disks
独立硬盘冗余阵列(RAID, Redundant Array of Independent Disks),旧称廉价磁盘冗余阵列(Redundant Array of Inexpensive Disks),简称磁盘阵列。其基本思想就是把多个相对便宜的硬盘组合起来,成为一个硬盘阵列组,使性能达到甚至超过一个价格昂贵、容量巨大的硬盘。
参考资料:https://zh.wikipedia.org/wiki/RAID
RAID-0: (追求最大容量、速度)
读、写性能提升;
可用空间:N*min(S1,S2,…)
无容错能力
最少磁盘数:2, 2+
RAID-1:(追求最大安全性)
读性能提升、写性能略有下降;
可用空间:1*min(S1,S2,…)
有冗余能力,安全性高
最少磁盘数:2, 2+
RAID-4:
1101, 0110, 1011
一块硬盘专门生成校验数据(校验盘)
最少磁盘数:3,3+
RAID-5:(追求最大容量、最小预算)
读、写性能提升
可用空间:(N-1)*min(S1,S2,…)
有容错能力:1块磁盘
最少磁盘数:3, 3+
RAID-6:(同RAID 5,但较安全)
读、写性能提升
可用空间:(N-2)*min(S1,S2,…)
有容错能力:2块磁盘
最少磁盘数:4, 4+
混合类型
RAID-10:
读、写性能提升
可用空间:Nmin(S1,S2,…)/2
有容错能力:每组镜像最多只能坏一块;
最少磁盘数:4, 4+
RAID-01:
读、写能力提升
可用空间:Nmin(S1,S2,…)/2
有容错能力:每组镜像最多可坏三块;
最少磁盘数:4, 4+
5.创建一个大小为10G的RAID1,要求有一个空闲盘,而且CHUNK大小为128K;
在/dev/sdb划分三个10G大小的空间,/dev/sdb{5,6.7},并将分区类型改为fd.
1478350124870.jpg
[root@localhost ~]# mdadm -C /dev/md1 -a yes -c 128 -n 2 -x 1 -l 1 /dev/sdb{5,6,7} #创建RAID1 mdadm: /dev/sdb5 appears to be part of a raid array: level=raid1 devices=2 ctime=Sat Nov 5 04:02:27 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 #这里是说这个RAID不能做引导分区 mdadm: /dev/sdb6 appears to be part of a raid array: level=raid1 devices=2 ctime=Sat Nov 5 04:02:27 2016 mdadm: /dev/sdb7 appears to be part of a raid array: level=raid1 devices=2 ctime=Sat Nov 5 04:02:27 2016 Continue creating array? y mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md1 started. [root@localhost ~]# cat /proc/mdstat #查看md的状态 Personalities : [raid1] md1 : active raid1 sdb7[2](S) sdb6[1] sdb5[0] 10473472 blocks super 1.2 [2/2] [UU] unused devices: <none>
1478350468933.jpg
6.创建一个大小为4G的RAID5设备,chunk大小为256K,格式化ext4文件系统,要求可开机自动挂载至/backup目录,而且不更新访问时间戳,且支持acl功能;
删除前面创建的raid1,从/dev/sdb分出3个2G,分区类型设置为fd
1478352601885.jpg
[root@localhost ~]# mdadm -C /dev/md5 -a yes -c 256 -n 3 -l 5 /dev/sdb{7,8,9} mdadm: /dev/sdb7 appears to be part of a raid array: level=raid1 devices=2 ctime=Sat Nov 5 04:14:49 2016 Continue creating array? y mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md5 started. ##格式化为ext4 [root@localhost ~]# mke2fs -t ext4 /dev/md5 [root@localhost ~]# blkid /dev/md5 /dev/md5: UUID="49b8b4c2-58e9-458b-affe-3dec2f5495c8" TYPE="ext4"
在/etc/fstab最后面加上一行.使用UUID更稳.
UUID=49b8b4c2-58e9-458b-affe-3dec2f5495c8 /backup ext4 defaults,acl,noatime 0 0 ##重启查看## [root@localhost ~]# mount /dev/md5 on /backup type ext4 (rw,noatime,seclabel,stripe=128,data=ordered)
可以看看这篇文章:
http://www.toxingwang.com/linux-unix/linux-basic/1048.html
7.写一个脚本:
(1) 接受一个以上文件路径作为参数;
(2) 显示每个文件拥有的行数;
(3) 总结说明本次共为几个文件统计了其行数;
#!/bin/bash # if [ $# -lt 1 ];then echo "至少一个文件路径" && exit 1 fi declare -i sum=0 for i in $*; do if [ ! -f $i ];then echo "$i文件不存在" continue fi echo "文件$i行数为"$(cat $i | wc -l) let sum++ done echo "传入路径$#个,统计有效文件$sum个"
1478355522232.jpg
8.写一个脚本:
(1) 传递两个以上字符串当作用户名;
(2) 创建这些用户;且密码同用户名;
(3) 总结说明共创建了几个用户;
#!/bin/bash if [ $# -lt 2 ];then echo "至少2个用户名" && exit 1 fi declare -i sum=0 for i in $*; do id $i &> /dev/null if [ $? -eq 0 ];then echo "$i用户已存在" continue fi useradd $i echo $i | passwd --stdin $i echo "$i用户添加完成,密码同用户名" let sum++ done echo "一共传入$#个用户,共创建$sum个用户.$[$#-$sum]个用户已存在"
1478357208676.jpg
9.写一个脚本,新建20个用户,visitor1-visitor20;计算他们的ID之和;
#!/bin/bash declare -i sumid=0 for i in {1..20};do useradd visitor$i sumid+=$(id -u visitor$i) done echo "用户id之和为$sumid"
10.写一脚本,分别统计/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#号开头的行数之和,以及总的空白行数;
#!/bin/bash declare -i count1=0 declare -i count2=0 for i in /etc/{rc.d/{rc.sysinit,init.d/functions},fstab};do count1+=$(grep "^#" $i | wc -l) count2+=$(grep "^$" $i | wc -l) done echo "文件#开头的行有$count1行,空白行有$count2行"
11.写一个脚本,显示当前系统上所有默认shell为bash的用户的用户名、UID以及此类所有用户的UID之和;
#!/bin/bash declare -i uid=0 for i in $(grep "/bin/bash$" /etc/passwd | cut -d: -f1);do echo "shell为bash的用户名:$i" uid+=$(id -u $i) done echo "shell为bash的用户id之和为:$uid"
12.写一个脚本,显示当前系统上所有,拥有附加组的用户的用户名;并说明共有多少个此类用户;
#!/bin/bash declare -i count=0 for i in $(cat /etc/passwd | cut -d: -f1);do if [ $(id -G $i | wc -w) -gt 1 ] ;then echo $i let count++ fi done echo "用户附加组的用户个数为:$count"
13.创建一个由至少两个物理卷组成的大小为20G的卷组;要求,PE大小为8M;而在卷组中创建一个大小为5G的逻辑卷mylv1,格式化为ext4文件系统,开机自动挂载至/users目录,支持acl;
创建思路:PV->VG->LV
先格式化两个10G大小的分区/dev/sdb5,/dev/sdb6,分区类型为8e
1478405883742.jpg
[root@localhost ~]# fdisk -l /dev/sdb Disk /dev/sdb: 42.9 GB, 42949672960 bytes, 83886080 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: 0xc4afe58d Device Boot Start End Blocks Id System /dev/sdb1 2048 20973567 10485760 83 Linux /dev/sdb2 20973568 83886079 31456256 5 Extended /dev/sdb5 20975616 41947135 10485760 8e Linux LVM /dev/sdb6 41949184 62920703 10485760 8e Linux LVM ## 创建PV ## [root@localhost ~]# pvcreate /dev/sdb{5,6} Physical volume "/dev/sdb5" successfully created WARNING: linux_raid_member signature detected on /dev/sdb6 at offset 4096. Wipe it? [y/n]: y #这里是前面sdb6创建过软riad,所以提示有信息,问是否清除. Wiping linux_raid_member signature on /dev/sdb6. Physical volume "/dev/sdb6" successfully created [root@localhost ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 centos lvm2 a-- 119.51g 64.00m /dev/sdb5 lvm2 --- 10.00g 10.00g /dev/sdb6 lvm2 --- 10.00g 10.00g ## 创建VG ## [root@localhost ~]# vgcreate -s 8M myvg /dev/sdb{5,6} Volume group "myvg" successfully created [root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree centos 1 3 0 wz--n- 119.51g 64.00m myvg 2 0 0 wz--n- 19.98g 19.98g
1478407698503.jpg
创建lv:
[root@localhost ~]# lvcreate -n mylv1 -L 5G myvg Logical volume "mylv1" created. [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert home centos -wi-ao---- 67.45g root centos -wi-ao---- 50.00g swap centos -wi-ao---- 2.00g mylv1 myvg -wi-a----- 5.00g
1478407939054.jpg
格式化,开机自动挂载:
##格式化为ext4 ## [root@localhost ~]# mke2fs -t ext4 /dev/myvg/mylv1 [root@localhost ~]# blkid /dev/myvg/mylv1 /dev/myvg/mylv1: UUID="528cd557-d46e-4e40-bf54-03fe1209c018" TYPE="ext4" ## 开机自动挂载,编辑/etc/fstab文件,加入一行 ,建议使用UUID挂载## UUID=528cd557-d46e-4e40-bf54-03fe1209c018 /users ext4 defaults,acl 0 0 ##重启,看到挂载中有mylv1就是成功了 [root@localhost ~]# reboot [root@localhost ~]# mount /dev/mapper/myvg-mylv1 on /users type ext4 (rw,relatime,seclabel,data=ordered) [root@localhost ~]# ls /users lost+found
14.新建用户magedu;其家目录为/users/magedu,而后su切换至此用户,复制多个文件至家目录;
[root@localhost ~]# useradd -d /users/magedu magedu [root@localhost ~]# tail -1 /etc/passwd magedu:x:3042:3042::/users/magedu:/bin/bash [root@localhost ~]# su -l magedu Last login: Sun Nov 6 01:07:34 EDT 2016 on pts/0 [magedu@localhost ~]$ pwd /users/magedu ##复制几个文件到家目录 [magedu@localhost ~]$ cp -r /etc/fstab /etc/init.d ./ [magedu@localhost ~]$ ls fstab init.d
15.扩展mylv1至9G,确保扩展完成后原有数据完全可用;
扩展LV:(步骤从上往下)
# lvextend -L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME
# resize2fs /dev/VG_NAME/LV_NAME #只适用ext文件系统.
##扩展到9G [root@localhost ~]# lvextend -L 9G /dev/myvg/mylv1 Size of logical volume myvg/mylv1 changed from 5.00 GiB (640 extents) to 9.00 GiB (1152 extents). Logical volume mylv1 successfully resized. [root@localhost ~]# df -lh #我们可以看到此时mylv1的大小还是没有变的 Filesystem Size Used Avail Use% Mounted on /dev/mapper/myvg-mylv1 4.8G 21M 4.6G 1% /users ## 执行扩展文件系统边界 ## [root@localhost ~]# resize2fs /dev/myvg/mylv1 resize2fs 1.42.9 (28-Dec-2013) Filesystem at /dev/myvg/mylv1 is mounted on /users; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 2 The filesystem on /dev/myvg/mylv1 is now 2359296 blocks long. [root@localhost ~]# df -lh ## 此时mylv1已经扩展到9G Filesystem Size Used Avail Use% Mounted on /dev/mapper/myvg-mylv1 8.8G 23M 8.3G 1% /users ##查看下文件是否还正常 [root@localhost ~]# cat /users/magedu/fstab
16.缩减mylv1至7G,确保缩减完成后原有数据完全可用;
缩减LV:
# umount /dev/VG_NAME/LV_NAME
# e2fsck -f /dev/VG_NAME/LV_NAME #文件系统强制检测
# resize2fs /dev/VG_NAME/LV_NAME #[mMgGtT] #缩减文件系统大小,缩减到大小要能容纳已存在文件大小.
# lvreduce -L [-]#[mMgGtT] /dev/VG_NAME/LV_NAME #缩减LV大小
# mount
[root@localhost ~]# umount /dev/myvg/mylv1 [root@localhost ~]# e2fsck -f /dev/myvg/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/myvg/mylv1: 19/589824 files (0.0% non-contiguous), 75558/2359296 blocks [root@localhost ~]# resize2fs /dev/myvg/mylv1 7G resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/myvg/mylv1 to 1835008 (4k) blocks. The filesystem on /dev/myvg/mylv1 is now 1835008 blocks long. [root@localhost ~]# lvreduce -L 7G /dev/myvg/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 myvg/mylv1 changed from 9.00 GiB (1152 extents) to 7.00 GiB (896 extents). Logical volume mylv1 successfully resized. [root@localhost ~]# mount /dev/myvg/mylv1 /users [root@localhost ~]# cat /users/magedu/fstab
17.对mylv1创建快照,并通过备份数据;要求保留原有的属主属组等信息;
快照:snapshot
lvcreate -L #[mMgGtT] -p r -s -n snapshot_lv_name original_lv_name
[root@localhost ~]# lvcreate -L 100M -p r -s -n mylv1-snapshot /dev/myvg/mylv1 Rounding up size to full physical extent 104.00 MiB Logical volume "mylv1-snapshot" created. [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv1-snapshot myvg sri-a-s--- 104.00m mylv1 0.0 ## 挂载 ## [root@localhost ~]# mount /dev/myvg/mylv1-snapshot /mnt ## 使用vim 修改一下/users/magedu/fstab 文件 [root@localhost mnt]# vim /users/magedu/fstab [root@localhost mnt]# head -2 /users/magedu/fstab #这里加上一行注释 [root@localhost mnt]# head -2 magedu/fstab #快照里面的内容不会变化 #
原创文章,作者:luoluo,如若转载,请注明出处:http://www.178linux.com/57894