1、创建一个10G分区,并格式为ext4文件系统;
(1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;
[root@localhost ~]# fdisk /dev/sdb #创建10G分区 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 0xf0ff800a. 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-251658239, default 2048): #回车从默认扇区开始分区 Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-251658239, default 251658239): +10G #划分10G空间 Partition 1 of type Linux and of size 10 GiB is set Command (m for help): p #打印分区表信息 Disk /dev/sdb: 128.8 GB, 128849018880 bytes, 251658240 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: 0xf0ff800a Device Boot Start End Blocks Id System /dev/sdb1 2048 20973567 10485760 83 Linux Command (m for help): w #保存分区信息 The partition table has been altered! [root@localhost ~]# partx -a /dev/sdb #分区立即生效 partx: /dev/sdb: error adding partition 1 [root@localhost ~]# mke2fs -t ext4 -L MYDATA -b 2048 -m 20 /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 1048576 blocks (20.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 #添加acl属性 tune2fs 1.42.9 (28-Dec-2013) [root@localhost ~]# blkid /dev/sdb1 #显示分区的一般信息 /dev/sdb1: LABEL="MYDATA" UUID="b16f489e-a6f6-4e68-8887-3e242952ae0b" TYPE="ext4"(2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳;
[root@localhost ~]# mkdir /data/mydata -p #创建挂载点目录 [root@localhost ~]# mount -o noatime,noexec /dev/sdb1 /data/mydata/ #挂载分区至挂载点 [root@localhost ~]# mount|grep /data/mydata #查看详细挂载信息 /dev/sdb1 on /data/mydata type ext4 (rw,noexec,noatime,seclabel,data=ordered)2、创建一个大小为1G的swap分区,并创建好文件系统,并启用之;
[root@localhost ~]# fdisk /dev/sdb #创建一个1G分区 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-251658239, default 20973568): Using default value 20973568 Last sector, +sectors or +size{K,M,G} (20973568-251658239, default 251658239): +1G Partition 2 of type Linux and of size 1 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 ~]# partx -a /dev/sdb partx: /dev/sdb: error adding partitions 1-2 [root@localhost ~]# mkswap /dev/sdb2 #格式化分区为swap类型 mkswap: /dev/sdb2: warning: wiping old swap signature. Setting up swapspace version 1, size = 1048572 KiB no label, UUID=e3cf8440-4029-4674-97cb-c5c5db641979 [root@localhost ~]# free -m #查看当前内存使用信息,swap为2047 total used free shared buff/cache available Mem: 1824 118 1524 8 181 1542 Swap: 2047 0 2047 [root@localhost ~]# swapon /dev/sdb2 #挂载swap分区 [root@localhost ~]# free -m #挂载后swap变为3071 total used free shared buff/cache available Mem: 1824 119 1524 8 181 1541 Swap: 3071 0 30713、写一个脚本
(1)、获取并列出当前系统上的所有磁盘设备;
(2)、显示每个磁盘设备上每个分区相关的空间使用信息;
#!/bin/bash echo "Hard disk:" ls /dev/[sh]d[a-z] echo "Partition info:" fdisk -l $(fdisk -l|grep "^/dev/[sh]d[a-z]"|cut -d" " -f14、总结RAID的各个级别及其组合方式和性能的不同;
RAID0:将多个磁盘合并成一个大的磁盘,不具有冗余,并行I/O,速度最快。
优点:读写速度快。
缺点:没有冗余功能,如果一个磁盘损坏,则所有的数据都会丢失。
RAID1:两组以上的N个磁盘相互作镜像,同一数据在各组磁盘上均保存一份。
优点:安全性好,任意一块磁盘损坏不会导致数据丢失。读取速度理论上等于硬盘数量的倍数,写入速度仅有微小的降低。
缺点:会造成磁盘空间浪费,实际使用空间只有所有磁盘总和的一半。
RAID5:RAID 5至少需要三颗硬盘,RAID 5不是对存储的数据进行备份,而是把数据和相对应的奇偶校验信息存储到组成RAID5的各个磁盘上,并且奇偶校验信息和相对应的数据分别存储于不同的磁盘上。当一个磁盘数据发生损坏后,可以利用剩下的数据和相应的奇偶校验信息去恢复被损坏的数据。
优点:读取速度快,接近RAID0;数据安全性较好,在只坏一个磁盘的情况下不会造成数据丢失。
缺点:至少需要3块磁盘,并且会损失一个磁盘容量的使用空间;当一块磁盘故障时,写入速度会大幅降低。
RAID10/01:RAID 10是先镜射再分区数据,再将所有硬盘分为两组,视为是RAID 0的最低组合,然后将这两组各自视为RAID 1运作。RAID 01则是跟RAID 10的程序相反,是先分区再将数据镜射到两组硬盘。它将所有的硬盘分为两组,变成RAID 1的最低组合,而将两组硬盘各自视为RAID 0运作。
优点:既保证了数据的安全性,也提高了读写速度。
缺点:会造成一半磁盘空间的损失。
补充说明:实际生产中RAID10使用更广泛,因为:
RAID0+1是先做两个RAID0,然后再做RAID1,因此RAID0+1允许坏多个盘,但只能在坏在同一个RAID0中,不允许两个RAID0都有坏盘。RAID1+0是先做RAID1,然后再做RAID0,因此RAID1+0允许坏多个盘,只要不是一对磁盘坏就可以了。因此说RAID1+0比RAID0+1安全得多,因为在同一对磁盘中,两块磁盘都坏掉的概率很低。
5、创建一个大小为10G的RAID1,要求有一个空闲盘,而且CHUNK大小为128k;
1)使用fdisk工具创建3个10G,Id编号为fd的分区
Device Boot Start End Blocks Id System /dev/sdb1 1 1306 10490413+ fd Linux raid autodetect /dev/sdb2 1307 2612 10490445 fd Linux raid autodetect /dev/sdb3 2613 3918 10490445 fd Linux raid autodetect2)使用mdadm命令创建raid磁盘
-C: 创建模式
-n #: 使用#个块设备来创建此RAID;
-l #:指明要创建的RAID的级别;
-a {yes|no}:自动创建目标RAID设备的设备文件;
-c CHUNK_SIZE: 指明块大小;
-x #: 指明空闲盘的个数;[root@centos6 ~]# mdadm -C /dev/md0 -n 2 -a yes -l 1 -x 1 -c 128 /dev/sdb{1,2,3} mdadm: /dev/sdb1 appears to be part of a raid array: level=raid1 devices=2 ctime=Sun Feb 12 23:04:41 2017 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 mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md0 started. [root@centos6 ~]# cat /proc/mdstat #发现raid磁盘组正在recovery Personalities : [raid1] [raid6] [raid5] [raid4] md0 : active raid1 sdb3[2](S) sdb2[1] sdb1[0] 10482176 blocks super 1.2 [2/2] [UU] [==>..................] resync = 14.6% (1537792/10482176) finish=0.6min speed=219684K/sec unused devices: <none> [root@centos6 ~]# cat /proc/mdstat Personalities : [raid1] [raid6] [raid5] [raid4] md0 : active raid1 sdb3[2](S) sdb2[1] sdb1[0] 10482176 blocks super 1.2 [2/2] [UU] [===>.................] resync = 18.6% (1959296/10482176) finish=0.6min speed=217699K/sec6、创建一个大小为4G的RAID5设备,chunk大小为256k,格式化ext4文件系统,要求可开机自动挂载至/backup目录,而且不更新访问时间戳,且支持acl功能;
1)使用fdisk工具创建3个2G,Id编号为fd的分区
Device Boot Start End Blocks Id System /dev/sdb1 1 262 2104483+ fd Linux raid autodetect /dev/sdb2 263 524 2104515 fd Linux raid autodetect /dev/sdb3 525 786 2104515 fd Linux raid autodetect2)使用mdadm命令创建raid磁盘
[root@centos6 ~]# mdadm -C /dev/md0 -n 3 -a yes -l 5 -c 256 /dev/sdb{1,2,3}3)格式化raid磁盘
[root@centos6 ~]# mke2fs -t ext4 /dev/md04)查看磁盘的UUID信息
[root@centos6 ~]# blkid /dev/md0 /dev/md0: UUID="c72dbdd2-109c-441b-a03e-99adca12676a" TYPE="ext4"5)创建/backup目录,并配置/etc/fstab文件,添加下面行:
UUID=c72dbdd2-109c-441b-a03e-99adca12676a /backup ext4 defaults,acl,nodiratime 0 07、写一个脚本
(1) 接受一个以上文件路径作为参数;
(2) 显示每个文件拥有的行数;
(3) 总结说明本次共为几个文件统计了其行数;
#!/bin/bash if [ $# -le 1 ];then echo "At least 2 arguments!" exit 1 fi cnt=0 for file in $@ do if [ -f $file ];then lines=$(cat $file|wc -l) echo "$file: "$lines let cnt++ else echo "$file not exists." continue fi done echo "$cnt files have been counted."8、写一个脚本
(1) 传递两个以上字符串当作用户名;
(2) 创建这些用户;且密码同用户名;
(3) 总结说明共创建了几个用户;
#!/bin/bash if [ $# -le 1 ];then echo "At least 2 arguments!" exit 1 fi sum=0 for user in $@ do if id $user &>/dev/null;then echo "user $user already exists." continue else useradd $user echo "$user"|passwd --stdin $user &>/dev/null echo "$user has been created." let sum++ fi done echo "$sum users created."9、写一个脚本,新建20个用户,visitor1-visitor20;计算他们的ID之和;
#!/bin/bash sum=0 for i in {1..20} do if id visitor${i} &>/dev/null;then echo "visitor${i} already exists." continue else useradd visitor${i} echo "user visitor${i} has been created." uid=$(id -u visitor${i}) let sum=sum+uid fi done echo "sum: "$sum10、写一脚本,分别统计/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#号开头的行数之和,以及总的空白行数;
#!/bin/bash files="/etc/rc.d/rc.sysinit /etc/rc.d/init.d/functions /etc/fstab" sum1=0 sum2=0 for fn in $files do line1=$(grep "^#" $fn|wc -l) line2=$(grep "^$" $fn|wc -l) let sum1=sum1+line1 let sum2=sum2+line2 done echo "Begin with #: "$sum1 echo "Blank rows: "$sum211、写一个脚本,显示当前系统上所有默认shell为bash的用户的用户名、UID以及此类所有用户的UID之和;
awk -F: 'BEGIN{printf "%-15s%s\n------------------\n","USERNAME","UID"}{if($NF=="/bin/bash"){sum=sum+$3;printf "%-15s%s\n",$1,$3}}END{printf "------------------\nsum: %s\n",sum}' /etc/passwd12、写一个脚本,显示当前系统上所有,拥有附加组的用户的用户名;并说明共有多少个此类用户;
#!/bin/bash users=$(cat /etc/passwd|awk -F: '{print $1}') sum=0 for name in $users do gpcnt=$(id -G $name|xargs -n 1|wc -l) if [ $gpcnt -gt 1 ];then echo "$name" let sum++ fi done echo "$sum users have additional group"13、创建一个由至少两个物理卷组成的大小为20G的卷组;要求,PE大小为8M;而在卷组中创建一个大小为5G的逻辑卷mylv1,格式化为ext4文件系统,开机自动挂载至/users目录,支持acl;
1)使用fdisk创建两个10G,Id为8e的分区
Device Boot Start End Blocks Id System /dev/sdb1 1 1306 10490413+ 8e Linux LVM /dev/sdb2 1307 2612 10490445 8e Linux LVM2)创建pv
[root@centos6 ~]# pvcreate /dev/sdb{1,2} Physical volume "/dev/sdb1" successfully created Physical volume "/dev/sdb2" successfully created [root@centos6 ~]# pvs PV VG Fmt Attr PSize PFree /dev/sdb1 lvm2 ---- 10.00g 10.00g /dev/sdb2 lvm2 ---- 10.00g 10.00g3)创建vg
[root@centos6 ~]# vgcreate -s 8M myvg /dev/sdb1 /dev/sdb2 Volume group "myvg" successfully created [root@centos6 ~]# 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 7XzzmY-TcTV-Tskk-KkJF-DX8j-M3r0-yegZOn4)创建lv
[root@centos6 ~]# lvcreate -L 5G -n mylv1 myvg Logical volume "mylv1" created. [root@centos6 ~]# lvdisplay --- Logical volume --- LV Path /dev/myvg/mylv1 LV Name mylv1 VG Name myvg LV UUID qf4YZA-Fpb3-mSbA-bmE8-akpw-PF6c-jT6qxd LV Write Access read/write LV Creation host, time centos6, 2017-02-13 02:26:03 +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:05)文件系统格式化
[root@centos6 ~]# mkfs -t ext4 /dev/myvg/mylv16)文件系统挂载
[root@centos6 ~]# mkdir /users [root@centos6 ~]# blkid /dev/myvg/mylv1 /dev/myvg/mylv1: UUID="9792d25f-fd1d-46ae-aa88-c14ea5383bc2" TYPE="ext4" [root@centos6 ~]# vim /etc/fstab #添加下面内容 UUID=9792d25f-fd1d-46ae-aa88-c14ea5383bc2 /users ext4 defaults,acl 0 014、新建用户magedu;其家目录为/users/magedu,而后su切换至此用户,复制多个文件至家目录;
[root@centos6 ~]# useradd -d /users/magedu magedu [root@centos6 ~]# echo "magedu"|passwd --stdin magedu [root@centos6 ~]# su - magedu [magedu@centos6 ~]$ cp /etc/yum.repos.d/* /users/magedu15、扩展mylv1至9G,确保扩展完成后原有数据完全可用;
1)使用lvextend命令将当前PV中剩余PE分4G给mylv1(此时并未生效)
[root@centos6 ~]# lvextend -L +4G /dev/myvg/mylv12)使用resize2fs命令使新扩展的磁盘空间生效
[root@centos6 ~]# resize2fs /dev/myvg/mylv1 [root@centos6 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 116G 1.4G 109G 2% / tmpfs 364M 0 364M 0% /dev/shm /dev/sda1 477M 32M 420M 7% /boot /dev/mapper/myvg-mylv1 8.8G 12M 8.3G 1% /users16、缩减mylv1至7G,确保缩减完成后原有数据完全可用;
1)先卸载要缩减的LV
[root@centos6 ~]# umount /dev/myvg/mylv12)进行强制磁盘检测
[root@centos6 ~]# e2fsck -f /dev/myvg/mylv13)resize2fs重定义LV大小为7G
[root@centos6 ~]# resize2fs /dev/myvg/mylv1 7000M4)lvreduce使缩减后的LV生效
[root@centos6 ~]# lvreduce -L 7000M /dev/myvg/mylv15)重新挂载LV并检查分区上原来的文件是否损坏
[root@centos6 ~]# mount /dev/myvg/mylv1 /users/ [root@centos6 magedu]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 116G 1.4G 109G 2% / tmpfs 364M 0 364M 0% /dev/shm /dev/sda1 477M 32M 420M 7% /boot /dev/mapper/myvg-mylv1 6.7G 12M 6.3G 1% /users [root@centos6 magedu]# ls /users/magedu/ CentOS-Base.repo CentOS-fasttrack.repo CentOS-Vault.repo CentOS-Debuginfo.repo CentOS-Media.repo17、对mylv1创建快照,并通过备份数据;要求保留原有的属主属组等信息;
1)创建一个1G的快照文件
[root@centos6 magedu]# lvcreate -L 1G -s -n mysnap /dev/myvg/mylv1 Logical volume "mysnap" created. [root@centos6 magedu]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv1 myvg owi-aos--- 6.84g mysnap myvg swi-a-s--- 1.00g mylv1 0.002)创建并挂载快照目录
[root@centos6 magedu]# mkdir /mnt/snapshot [root@centos6 magedu]# mount /dev/mapper/myvg-mysnap /mnt/snapshot/ [root@centos6 magedu]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 116G 1.4G 109G 2% / tmpfs 364M 0 364M 0% /dev/shm /dev/sda1 477M 32M 420M 7% /boot /dev/mapper/myvg-mylv1 6.7G 12M 6.3G 1% /users /dev/mapper/myvg-mysnap 6.7G 12M 6.3G 1% /mnt/snapshot3)删除mylv1中的数据,并使用镜像文件进行恢复
[root@centos6 magedu]# rm -f /users/magedu/* [root@centos6 magedu]# cp -a /mnt/snapshot/magedu/* /users/magedu/ [root@centos6 magedu]# ll /users/magedu/ total 24 -rw-r--r-- 1 magedu magedu 1991 Feb 13 02:47 CentOS-Base.repo -rw-r--r-- 1 magedu magedu 647 Feb 13 02:47 CentOS-Debuginfo.repo -rw-r--r-- 1 magedu magedu 289 Feb 13 02:47 CentOS-fasttrack.repo -rw-r--r-- 1 magedu magedu 630 Feb 13 02:47 CentOS-Media.repo -rw-r--r-- 1 magedu magedu 6259 Feb 13 02:47 CentOS-Vault.repo
原创文章,作者:N26-西安-方老喵,如若转载,请注明出处:http://www.178linux.com/66990
评论列表(1条)
非常非常棒博客内容,赞一个。