1、创建一个10G分区,并格式为ext4文件系统;
(1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;
(2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳;
[root@localhost ~]# fdisk /dev/sda
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
命令(输入 m 获取帮助):n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
分区号 (3,4,默认 3):
起始 扇区 (90193920-251658239,默认为 90193920):
将使用默认值 90193920
Last 扇区, +扇区 or +size{K,M,G} (90193920-251658239,默认为 251658239):+10G
分区 3 已设置为 Linux 类型,大小设为 10 GiB
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
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)
正在同步磁盘。
[root@localhost ~]# partx -a /dev/sda
partx: /dev/sda: error adding partitions 1-3
[root@localhost ~]# partx -a /dev/sda
partx: /dev/sda: error adding partitions 1-3
[root@localhost ~]# mke2fs -t ext4 -b 2048 -m 2 /dev/sda3
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=2048 (log=1)
分块大小=2048 (log=1)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 5242880 blocks
104857 blocks (2.00%) reserved for the super user
第一个数据块=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: 完成
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
[root@localhost ~]# tune2fs -o acl -L MYDATA /dev/sda3
tune2fs 1.42.9 (28-Dec-2013)
[root@localhost ~]# mkdir -p /data/mydata
[root@localhost ~]# mount -o noexec,noatime /dev/sda3 /data/mydata
[root@localhost ~]# tune2fs -l /dev/sda3
tune2fs 1.42.9 (28-Dec-2013)
Filesystem volume name: MYDATA
Last mounted on: <not available>
Filesystem UUID: c92424b0-608c-4b36-b0b1-34e195a3e50f
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: has_journal ext_attr resize_inode dir_index filetype needs_recovery extent 64bit flex_bg sparse_super huge_file uninit_bg dir_nlink extra_isize
Filesystem flags: signed_directory_hash
Default mount options: user_xattr acl
Filesystem state: clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 655360
Block count: 5242880
Reserved block count: 104857
Free blocks: 5121266
Free inodes: 655349
First block: 0
Block size: 2048
Fragment size: 2048
Group descriptor size: 64
Reserved GDT blocks: 512
Blocks per group: 16384
Fragments per group: 16384
Inodes per group: 2048
Inode blocks per group: 256
Flex block group size: 16
Filesystem created: Mon Jul 3 13:17:08 2017
Last mount time: Mon Jul 3 13:28:50 2017
Last write time: Mon Jul 3 13:28:50 2017
Mount count: 2
Maximum mount count: -1
Last checked: Mon Jul 3 13:17:08 2017
Check interval: 0 (<none>)
Lifetime writes: 225 MB
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)
First inode: 11
Inode size: 256
Required extra isize: 28
Desired extra isize: 28
Journal inode: 8
Default directory hash: half_md4
Directory Hash Seed: ba9fed8b-cb70-46fa-b0bb-b86ef90d2948
Journal backup: inode blocks
[root@localhost ~]# mount | grep "/dev/sda3"
/dev/sda3 on /data/mydata type ext4 (rw,noexec,noatime,seclabel,data=ordered)
2、创建一个大小为1G的swap分区,并创建好文件系统,并启用之;
[root@localhost ~]# fdisk /dev/sda
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
命令(输入 m 获取帮助):n
Partition type:
p primary (3 primary, 0 extended, 1 free)
e extended
Select (default e): e
已选择分区 4
起始 扇区 (111165440-251658239,默认为 111165440):
将使用默认值 111165440
Last 扇区, +扇区 or +size{K,M,G} (111165440-251658239,默认为 251658239):
将使用默认值 251658239
分区 4 已设置为 Extended 类型,大小设为 67 GiB
命令(输入 m 获取帮助):n
All primary partitions are in use
添加逻辑分区 5
起始 扇区 (111167488-251658239,默认为 111167488):
将使用默认值 111167488
Last 扇区, +扇区 or +size{K,M,G} (111167488-251658239,默认为 251658239):+1G
分区 5 已设置为 Linux 类型,大小设为 1 GiB
命令(输入 m 获取帮助):t
分区号 (1-5,默认 5):5
Hex 代码(输入 L 列出所有代码):82
已将分区“Linux”的类型更改为“Linux swap / Solaris”
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
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)
正在同步磁盘。
[root@localhost ~]# partx -a /dev/sda
partx: /dev/sda: error adding partitions 1-5
[root@localhost ~]# partx -a /dev/sda
partx: /dev/sda: error adding partitions 1-5
[root@localhost ~]# mkswap /dev/sda5
mkswap: /dev/sda5: warning: wiping old linux_raid_member signature.
正在设置交换空间版本 1,大小 = 10485756 KiB
无标签,UUID=80095efc-eed6-4f8b-9ba6-44a8e0afccc2
[root@localhost ~]# swapon /dev/sda5
[root@localhost ~]# swapon -s
文件名 类型 大小 已用 权限
/dev/dm-1 partition 2097148 0 -1
/dev/sda5 partition 10485756 0 -2
3、写一个脚本
(1)、获取并列出当前系统上的所有磁盘设备;
(2)、显示每个磁盘设备上每个分区相关的空间使用信息;
#!/bin/bash SD=$(fdisk -l | grep -o "\</dev/sd[a-z]\>") for FILESYSTEM in $SD ; do df -lh $FILESYSTEM; done
4、总结RAID的各个级别及其组合方式和性能的不同;
磁盘阵列全名是Redundant Arrays of Independent Disk,RAID,英翻中的意思为:独立冗余磁盘阵列,旧称廉价冗余磁盘阵列,RAID可以通过一个技术,将多个较小的磁盘整合成一个较大的磁盘装置;而这个较大的磁盘功能不知是存储,还具有资料保护的功能。整个RAID由于选择等级不同使得整合后的磁盘具有不同的功能,常见的level有这几种:
RAID-0:
这种模式若使用相同型号容量的磁盘来组成效果最佳。这种模式RAID会先将磁盘切出等量的区块chunk,当文件要存入RAID时先按照chunk的大小切割好,再依次存放到各个磁盘中去,由于磁盘会交错存放数据,因此数据会均匀的存储到各个磁盘上去,由于数据已经被切割并放置到不同的磁盘上,因此每个磁盘所负责的数据量都降低了。越多颗磁盘组成的RAID0性能会越好,因为每颗磁盘负责的数据量更低,每颗磁盘的容量最终会变成RAID0的总容量。由于数据是分散存放在各个磁盘上的,若有一磁盘损坏则整个RAID上的数据都将遗失。
RAID-1:
这种模式也是需要相同容量的磁盘,最好是一模一样的磁盘。如果不同容量的磁盘组成RAID-1,则以容量最小的一颗磁盘为主。这种模式的主要目的是让同一份数据,完整的保存在两颗磁盘上。一份数据传送到RAID-1后会被分成两股,并分别写到各个磁盘中去,由于同一份数据会被分别写入其他不同的磁盘,因此如果要写入100M数据时,数据先传送到I/O总线后会被复制多份到各个磁盘,结果数据量就变大了,因此在大量写入的 RAID-1的情况下,写入的性能会变得非常差。由于两颗磁盘内的数据一模一样,所以任何一颗磁盘损坏时,磁盘的数据还是可以完整的保存下来的。
RAID-5:
RAID-5至少需要3颗以上的磁盘才能组成这种类型的磁盘阵列,这种磁盘阵列的数据写入有些类似于RAID-0,不过每个循环的写入过程中,在每颗磁盘还加入一个同位检查数据,这个数据会记录其他磁盘的备份数据,用于当有磁盘损毁时的救援。每个循环写入时都会有部分的同位校验码被记录起来,且每次都依次记录在不同的磁盘上,因此当任何一个磁盘损坏时都能通过其他两个磁盘计算出此磁盘内的数据。不过由于有同位校验码存在,RAID5的总容量是整体磁盘数量减一颗,若损毁的磁盘数大于等于2颗时,整个RAID5数据将损毁。在读写性能上读取性能和RAID0有的一比,写性能由于数据在写入时要计算校验码,所以写性能增加有限。
RAID-6:
由于RAID5仅支持损毁一颗磁盘,后来又发展出另一种等级RAID6.RAID6最少需要4颗磁盘才能组成,使用2颗磁盘作校验码,此时磁盘损坏2颗时仍然能正常读取数据,读写性能上与RAID5类似。
RAID-01:
RAID01是先将2颗磁盘作RAID0,再将两颗组成RAID0的磁盘组成RAID1,此种阵列方式兼备了RAID0的优势,又具备RAID1的备份优势,不过若两组RAID0中同时损坏一个磁盘时,RAID01上的数据将有可能遗失,非常不安全。于是便出现了RAID0。
RAID-10:
RAID10在读写性能上与RAID10相同,不同的是他先将2个磁盘组成RAID1,然后再组成RAID0,此时每组RAID1都能损坏1颗磁盘。理论上最大磁盘的损坏量至少可以达到磁盘数的一半,数据的安全性极佳。
总结
| 项目 | RAID0 | RAID1 | RAID10 | RAID5 | RAID6 |
| 最少磁盘数 | 2 | 2 | 4 | 3 | 4 |
| 最大容错磁盘数 | 无 | n-1 | n/2 | 1 | 2 |
| 数据安全性 | 完全没有 | 最佳 | 最佳 | 好 | 比RAID5好 |
| 理论写入性能 | n | 1 | n/2 | <n-1 | <n-2 |
| 理论读出性能 | n | n | n | <n-1 | <n-2 |
| 可用容量 | n | 1 | n/2 | n-1 | n-2 |
5、创建一个大小为10G的RAID1,要求有一个空闲盘,而且CHUNK大小为128k;
[root@localhost ~]# fdisk /dev/sda
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
命令(输入 m 获取帮助):n
All primary partitions are in use
添加逻辑分区 5
起始 扇区 (111167488-251658239,默认为 111167488):
将使用默认值 111167488
Last 扇区, +扇区 or +size{K,M,G} (111167488-251658239,默认为 251658239):+10G
分区 5 已设置为 Linux 类型,大小设为 10 GiB
命令(输入 m 获取帮助):n
All primary partitions are in use
添加逻辑分区 6
起始 扇区 (132141056-251658239,默认为 132141056):
将使用默认值 132141056
Last 扇区, +扇区 or +size{K,M,G} (132141056-251658239,默认为 251658239):+10G
分区 6 已设置为 Linux 类型,大小设为 10 GiB
命令(输入 m 获取帮助):n
All primary partitions are in use
添加逻辑分区 7
起始 扇区 (153114624-251658239,默认为 153114624):
将使用默认值 153114624
Last 扇区, +扇区 or +size{K,M,G} (153114624-251658239,默认为 251658239):+10G
分区 7 已设置为 Linux 类型,大小设为 10 GiB
命令(输入 m 获取帮助):t
分区号 (1-7,默认 7):5
Hex 代码(输入 L 列出所有代码):fd
已将分区“Linux”的类型更改为“Linux raid autodetect”
命令(输入 m 获取帮助):t
分区号 (1-7,默认 7):6
Hex 代码(输入 L 列出所有代码):fd
已将分区“Linux”的类型更改为“Linux raid autodetect”
命令(输入 m 获取帮助):t
分区号 (1-7,默认 7):7
Hex 代码(输入 L 列出所有代码):fd
已将分区“Linux”的类型更改为“Linux raid autodetect”
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
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)
正在同步磁盘。
[root@localhost ~]# partx -a /dev/sda
partx: /dev/sda: error adding partitions 1-6
[root@localhost ~]# partx -a /dev/sda
partx: /dev/sda: error adding partitions 1-7
[root@localhost ~]# mdadm -C /dev/md0 -a yes -c 128K -n 2 -x 1 -l 1 /dev/sda{5,6,7}
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
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
[root@localhost ~]# mdadm -D /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Mon Jul 3 13:02:33 2017
Raid Level : raid1
Array Size : 10477568 (9.99 GiB 10.73 GB)
Used Dev Size : 10477568 (9.99 GiB 10.73 GB)
Raid Devices : 2
Total Devices : 3
Persistence : Superblock is persistent
Update Time : Mon Jul 3 13:03:29 2017
State : clean, resyncing
Active Devices : 2
Working Devices : 3
Failed Devices : 0
Spare Devices : 1
Resync Status : 41% complete
Name : localhost.localdomain:0 (local to host localhost.localdomain)
UUID : 5978a305:cc3929ac:b06a5262:e756335a
Events : 6
Number Major Minor RaidDevice State
0 8 5 0 active sync /dev/sda5
1 8 6 1 active sync /dev/sda6
2 8 7 - spare /dev/sda7
6、创建一个大小为4G的RAID5设备,chunk大小为256k,格式化ext4文件系统,要求可开机自动挂载至/backup目录,而且不更新访问时间戳,且支持acl功能;
[root@localhost ~]# fdisk /dev/sda
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
命令(输入 m 获取帮助):n
All primary partitions are in use
添加逻辑分区 5
起始 扇区 (111167488-251658239,默认为 111167488):
将使用默认值 111167488
Last 扇区, +扇区 or +size{K,M,G} (111167488-251658239,默认为 251658239):+2G
分区 5 已设置为 Linux 类型,大小设为 2 GiB
命令(输入 m 获取帮助):n
All primary partitions are in use
添加逻辑分区 6
起始 扇区 (115363840-251658239,默认为 115363840):
将使用默认值 115363840
Last 扇区, +扇区 or +size{K,M,G} (115363840-251658239,默认为 251658239):+2G
分区 6 已设置为 Linux 类型,大小设为 2 GiB
命令(输入 m 获取帮助):n
All primary partitions are in use
添加逻辑分区 7
起始 扇区 (119560192-251658239,默认为 119560192):
将使用默认值 119560192
Last 扇区, +扇区 or +size{K,M,G} (119560192-251658239,默认为 251658239):+2G
分区 7 已设置为 Linux 类型,大小设为 2 GiB
命令(输入 m 获取帮助):t
分区号 (1-7,默认 7):5
Hex 代码(输入 L 列出所有代码):fd
已将分区“Linux”的类型更改为“Linux raid autodetect”
命令(输入 m 获取帮助):t
分区号 (1-7,默认 7):6
Hex 代码(输入 L 列出所有代码):fd
已将分区“Linux”的类型更改为“Linux raid autodetect”
命令(输入 m 获取帮助):t
分区号 (1-7,默认 7):7
Hex 代码(输入 L 列出所有代码):fd
已将分区“Linux”的类型更改为“Linux raid autodetect”
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
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)
正在同步磁盘。
[root@localhost ~]# partx -a /dev/sda
partx: /dev/sda: error adding partitions 1-7
[root@localhost ~]# partx -a /dev/sda
[root@localhost ~]# mdadm -C /dev/md0 -a yes -n 3 -l 5 -c 256K /dev/sda{5,6,7}
mdadm: /dev/sda6 appears to be part of a raid array:
level=raid1 devices=2 ctime=Mon Jul 3 13:02:33 2017
mdadm: /dev/sda7 appears to be part of a raid array:
level=raid1 devices=2 ctime=Mon Jul 3 13:02:33 2017
Continue creating array? yes
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
[root@localhost ~]# mdadm -D /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Mon Jul 3 13:50:54 2017
Raid Level : raid5
Array Size : 20955136 (19.98 GiB 21.46 GB)
Used Dev Size : 10477568 (9.99 GiB 10.73 GB)
Raid Devices : 3
Total Devices : 3
Persistence : Superblock is persistent
Update Time : Mon Jul 3 13:56:11 2017
State : clean
Active Devices : 3
Working Devices : 3
Failed Devices : 0
Spare Devices : 0
Layout : left-symmetric
Chunk Size : 256K
Name : localhost.localdomain:0 (local to host localhost.localdomain)
UUID : 96fbb2b4:f258315a:1d8bf46d:fb634a02
Events : 18
Number Major Minor RaidDevice State
0 8 5 0 active sync /dev/sda5
1 8 6 1 active sync /dev/sda6
3 8 7 2 active sync /dev/sda7
[root@localhost ~]# mke2fs -t ext4 /dev/md0
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=64 blocks, Stripe width=128 blocks
1310720 inodes, 5238784 blocks
261939 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=2153775104
160 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, 1605632, 2654208,
4096000
Allocating group tables: 完成
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
[root@localhost ~]# mkdir /backup
[root@localhost ~]# blkid /dev/md0
/dev/md0: UUID="c355dac6-07f1-4db8-be6d-7db631b5b2a5" TYPE="ext4"
[root@localhost ~]# vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Jun 15 18:59:54 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/cl-root / xfs defaults 0 0
UUID=e2876e74-00e5-4f80-818d-1e2f47a3fe64 /boot xfs defaults 0 0
/dev/mapper/cl-home /home xfs defaults 0 0
/dev/mapper/cl-swap swap swap defaults 0 0
UUID=c355dac6-07f1-4db8-be6d-7db631b5b2a5 /backup ext4 defaults,acl,noatime 0 0
~
7、写一个脚本
(1) 接受一个以上文件路径作为参数;
(2) 显示每个文件拥有的行数;
(3) 总结说明本次共为几个文件统计了其行数;
#!/bin/bash
if [ $# -lt 1 ]; then
echo "need a parameter";
fi
for FILEPATH in $*;do
if [ -f $FILEPATH ];then
line=$(wc -l $FILEPATH | cut -d " " -f 1);
echo "$FILEPATH has $line lines";
let sumline=$sumline+$line;
else
echo "$FILEPATH Must a File"
exit 2
fi
done
echo "A total of $# files are displayed this time,with a total of $sumline lines"
8、写一个脚本
(1) 传递两个以上字符串当作用户名;
(2) 创建这些用户;且密码同用户名;
(3) 总结说明共创建了几个用户;
if [ $# -lt 1 ] ;then
echo "need a string"
exit 2
fi
for USERNAME in $*;do
if [ $(echo $USERNAME | wc -m) -lt 4 ];then
echo "need more than 2 chars"
exit 2
fi
if id $USERNAME &> /dev/null ; then
echo "$USERNAME is exist"
exit 2
else
useradd $USERNAME;
echo "$USERNAME" | passwd --stdin $USERNAME > /dev/null;
echo "$USERNAME already created"
let SUM+=1
fi
done
echo "$SUM user has created"
9、写一个脚本,新建20个用户,visitor1-visitor20;计算他们的ID之和;
#!/bin/bash
SUM=0
for i in {1..20};do
useradd visitor$i
UIDNUM=$(id -u visitor$i) let SUM=$SUM+$UIDNUM
done
echo "UIDSUM=$SUM"
原创文章,作者:M36-Masuri,如若转载,请注明出处:http://www.178linux.com/79049


评论列表(2条)
可以看出有在很认真的整理,这些将来都是自己的理论积累,加油。
@马哥教育:第三题