磁盘分区创建及bash 简单应用

存储 存储设备
以下是磁盘分区创建及bash 简单应用,希望对磁盘分区困惑的小伙伴们得到帮助。

 1、创建一个10G分区,并格式为ext4文件系统;

(1)要求其block大小为2048,预留空间百分比为2,卷标为MYDATA,默认挂载属性包含acl;

(2)挂载至/data/mydata目录,要求挂载是禁止程序自动运行,并且更新文件的访问时间戳;

[[203331]]

  1. [root@danry ~]# fdisk /dev/sdb  
  2. Welcome to fdisk (util-linux 2.23.2). 
  3.  
  4. Changes will remain in memory only, until you decide to write them. 
  5. Be careful before using the write command. 
  6.  
  7. Device does not contain a recognized partition table 
  8. Building a new DOS disklabel with disk identifier 0xb74243b2. 
  9.  
  10. Command (m for help): n 
  11. Partition type: 
  12.    p   primary (0 primary, 0 extended, 4 free
  13.    e   extende 
  14. Select (default p): p 
  15. Partition number (1-4, default 1): 1 
  16. First sector (2048-41943039, default 2048):  
  17. Using default value 2048 
  18. Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +10G 
  19. Partition 1 of type Linux and of size 10 GiB is set 
  20.  
  21. Command (m for help): P 
  22.  
  23. Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors 
  24. Units = sectors of 1 * 512 = 512 bytes 
  25. Sector size (logical/physical): 512 bytes / 512 bytes 
  26. I/O size (minimum/optimal): 512 bytes / 512 bytes 
  27. Disk label type: dos 
  28. Disk identifier: 0xb74243b2 
  29.  
  30.    Device Boot      Start         End      Blocks   Id  System 
  31. /dev/sdb1            2048    20973567    10485760   83  Linux 
  32.  
  33. Command (m for help): W 
  34. The partition table has been altered! 
  35.  
  36. Calling ioctl() to re-read partition table
  37. Syncing disks. 
  38. [root@danry ~]# mke2fs -t ext4 -L MYDATA -m 2 -b 2048 /dev/sdb1 
  39. mke2fs 1.42.9 (28-Dec-2013) 
  40. Filesystem label=MYDATA 
  41. OS type: Linux 
  42. Block size=2048 (log=1) 
  43. Fragment size=2048 (log=1) 
  44. Stride=0 blocks, Stripe width=0 blocks 
  45. 655360 inodes, 5242880 blocks 
  46. 104857 blocks (2.00%) reserved for the super user 
  47. First data block=0 
  48. Maximum filesystem blocks=273678336 
  49. 320 block groups 
  50. 16384 blocks per group, 16384 fragments per group 
  51. 2048 inodes per group 
  52. Superblock backups stored on blocks:  
  53.     16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816, 1327104,  
  54.     2048000, 3981312 
  55.  
  56. Allocating group tables: done                             
  57. Writing inode tables: done                             
  58. Creating journal (32768 blocks): done 
  59. Writing superblocks and filesystem accounting information: done    
  60.  
  61. [root@danry ~]# tune2fs -o acl /dev/sdb1 
  62. tune2fs 1.42.9 (28-Dec-2013) 
  63. [root@danry ~]# blkid /dev/sdb1 
  64. /dev/sdb1: LABEL="MYDATA" UUID="fa82387d-d7ab-41ec-be8b-54b25b4c57ac" TYPE="ext4"  
  65. [root@danry ~]#  
  66.  
  67. [root@danry ~]# mount -o acl,noexec,noatime /dev/sdb1 /data/Mydata 

2、创建一个大小为1G的swap分区,并创建好文件系统,并启用之;

  1. [root@danry ~]# fdisk /dev/sdb  
  2. Welcome to fdisk (util-linux 2.23.2). 
  3.  
  4. Changes will remain in memory only, until you decide to write them. 
  5. Be careful before using the write command. 
  6.  
  7.  
  8. Command (m for help): n 
  9. Partition type: 
  10.    p   primary (1 primary, 0 extended, 3 free
  11.    e   extended 
  12. Select (default p): p 
  13. Partition number (2-4, default 2): 2 
  14. First sector (20973568-41943039, default 20973568):  
  15. Using default value 20973568 
  16. Last sector, +sectors or +size{K,M,G} (20973568-41943039, default 41943039): +1G 
  17. Partition 2 of type Linux and of size 1 GiB is set 
  18.  
  19. Command (m for help): P 
  20.  
  21. Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors 
  22. Units = sectors of 1 * 512 = 512 bytes 
  23. Sector size (logical/physical): 512 bytes / 512 bytes 
  24. I/O size (minimum/optimal): 512 bytes / 512 bytes 
  25. Disk label type: dos 
  26. Disk identifier: 0xb74243b2 
  27.  
  28.    Device Boot      Start         End      Blocks   Id  System 
  29. /dev/sdb1            2048    20973567    10485760   83  Linux 
  30. /dev/sdb2        20973568    23070719     1048576   83  Linux 
  31.  
  32. Command (m for help): W 
  33. The partition table has been altered! 
  34.  
  35. Calling ioctl() to re-read partition table
  36.  
  37. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. 
  38. The kernel still uses the old table. The new table will be used at 
  39. the next reboot or after you run partprobe(8) or kpartx(8) 
  40. Syncing disks. 
  41.  
  42. [root@danry ~]# partx  -a /dev/sdb 
  43. partx: /dev/sdb: error adding partition 1 
  44. [root@danry ~]# mkswap /dev/sdb2 
  45. Setting up swapspace version 1, size = 1048572 KiB 
  46. no label, UUID=ab39844d-56fd-412e-bdb8-08703c3dbf81 
  47. [root@danry ~]# swapo 
  48. swapoff  swapon    
  49. [root@danry ~]# swapo 
  50. swapoff  swapon    
  51. [root@danry ~]# swapon  /dev/sdb2  
  52.  
  53. [root@danry ~]# free -m 
  54.               total        used        free      shared  buff/cache   available 
  55. Mem:           1823         282        1169           9         371        1344 
  56. Swap:          3071           0        3071 
  57. [root@danry ~]# 

3、写一个脚本

(1)获取并列出当前系统上的所有磁盘设备;

(2)显示每个磁盘设备上每个分区相关的空间使用信息;

  1. [root@localhost Desktop]# cat lsdisk.sh  
  2. #!/bin/bash 
  3. #Author:Danry 
  4. #Description: Display disk partition information! 
  5. display=`ls /dev/[s,h]d[a-z]` 
  6. fdisk -l $display 
  7. [root@localhost Desktop]# 
  8.  
  9. [root@localhost Desktop]# bash lsdisk.sh  
  10. Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors 
  11. Units = sectors of 1 * 512 = 512 bytes 
  12. Sector size (logical/physical): 512 bytes / 512 bytes 
  13. I/O size (minimum/optimal): 512 bytes / 512 bytes 
  14. Disk label type: dos 
  15. Disk identifier: 0x0008508b 
  16.  
  17.    Device Boot      Start         End      Blocks   Id  System 
  18. /dev/sda1   *        2048     1026047      512000   83  Linux 
  19. /dev/sda2         1026048    41943039    20458496   8e  Linux LVM 
  20.  
  21. Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors 
  22. Units = sectors of 1 * 512 = 512 bytes 
  23. Sector size (logical/physical): 512 bytes / 512 bytes 
  24. I/O size (minimum/optimal): 512 bytes / 512 bytes 
  25.  
  26. [root@localhost Desktop]# 

4、总结Raid的哥哥级别及其组合方式和性能不同;

Raid2、Raid3、Raid4现在几乎已经很少用到了

Raid0:至少两块硬盘,一个数据打散后写入到不同的硬盘当中,其增加读写性能,降低了数据的安全性,磁盘可用率100%。

Raid1:至少两块硬盘,一块硬盘写数据,一块硬盘做备份,其增加数据安全性,降低了数据的读写性能,磁盘可用率50%。

Raid4:至少三块硬盘,其中两块硬盘存放数据,一块硬盘存放校验值,增加了数据的读写性,也增加了数据的安全性,磁盘可用率(磁盘数-1)

Raid5:至少三块硬盘,其中两块硬盘存放数据,一块硬盘存放校验值,其每次数据校验盘是轮流交替的,增加了数据的读写性,也增加了数据的安全性,磁盘可用率(磁盘数-1)

Raid10:至少4块硬盘,其组合先引用Raid1的安全特性,再引用Raid0的读写特,若4快盘则:俩俩为一组,每组最多可坏一块硬盘,磁盘可用率50%性。

Raid01:至少4块硬盘,其组合是先引用Raid0的读写特性,再引用Raid1安全特性,若4快盘则:俩俩为一组,每组最多可坏一块硬盘,磁盘可用率50%。

5、创建一个大小为10G的Raid1,要求一个空闲盘,而且CHUN大小为128K;

  1. [root@localhost Desktop]# mdadm -C /dev/md1 -n 2 -x 1 -c 128 -l 1 /dev/sdb1 /dev/sdb2 /dev/sdb3 
  2. mdadm: /dev/sdb1 appears to contain an ext2fs file system 
  3.        size=10485760K  mtime=Sat Mar  4 13:38:10 2017 
  4. mdadm: Note: this array has metadata at the start and 
  5.     may not be suitable as a boot device.  If you plan to 
  6.     store '/boot' on this device please ensure that 
  7.     your boot-loader understands md/v1.x metadata, or use 
  8.     --metadata=0.90 
  9. Continue creating array?  
  10. Continue creating array? (y/n) y 
  11. mdadm: Defaulting to version 1.2 metadata 
  12. mdadm: array /dev/md1 started. 
  13. [root@localhost scripts]# mdadm -D /dev/md1  
  14. /dev/md1: 
  15.         Version : 1.2 
  16.   Creation Time : Sat Mar  4 16:27:42 2017 
  17.      Raid Level : raid1 
  18.      Array Size : 5238784 (5.00 GiB 5.36 GB) 
  19.   Used Dev Size : 5238784 (5.00 GiB 5.36 GB) 
  20.    Raid Devices : 2 
  21.   Total Devices : 3 
  22.     Persistence : Superblock is persistent 
  23.  
  24.     Update Time : Sat Mar  4 16:27:58 2017 
  25.           State : clean, resyncing  
  26.  Active Devices : 2 
  27. Working Devices : 3 
  28.  Failed Devices : 0 
  29.   Spare Devices : 1 
  30.  
  31.   Resync Status : 64% complete 
  32.  
  33.            Name : localhost.localdomain:1  (local to host localhost.localdomain) 
  34.            UUID : 345d614a:1c8921d7:4a62d97c:6d9555cc 
  35.          Events : 10 
  36.  
  37.     Number   Major   Minor   RaidDevice State 
  38.        0       8       17        0      active sync   /dev/sdb1 
  39.        1       8       18        1      active sync   /dev/sdb2 
  40.  
  41.        2       8       19        -      spare   /dev/sdb3 

6、创建一个大小为4G的Raid5设备,chunk大小为256K,格式化ext4文件系统,要求自动挂载至、backup目录,而且不更新访问时间戳,切支持acl功能;

  1. [root@localhost Desktop]# mdadm -C /dev/md5 -n 4 -c 256 -l 5 /dev/sdb1 /dev/sdb2 /dev/sdb3 /dev/sdb4 
  2. mdadm: /dev/sdb1 appears to contain an ext2fs file system 
  3.        size=10485760K  mtime=Sat Mar  4 13:38:10 2017 
  4. mdadm: /dev/sdb1 appears to be part of a raid array: 
  5.        level=raid1 devices=2 ctime=Sat Mar  4 16:27:42 2017 
  6. Continue creating array? y 
  7. mdadm: Defaulting to version 1.2 metadata 
  8. mdadm: array /dev/md5 started. 
  9. [root@localhost scripts]# mdadm -D /dev/md5  
  10. /dev/md5: 
  11.         Version : 1.2 
  12.   Creation Time : Sat Mar  4 16:33:41 2017 
  13.      Raid Level : raid5 
  14.      Array Size : 3142656 (3.00 GiB 3.22 GB) 
  15.   Used Dev Size : 1047552 (1023.17 MiB 1072.69 MB) 
  16.    Raid Devices : 4 
  17.   Total Devices : 4 
  18.     Persistence : Superblock is persistent 
  19.  
  20.     Update Time : Sat Mar  4 16:33:47 2017 
  21.           State : clean  
  22.  Active Devices : 4 
  23. Working Devices : 4 
  24.  Failed Devices : 0 
  25.   Spare Devices : 0 
  26.  
  27.          Layout : left-symmetric 
  28.      Chunk Size : 256K 
  29.  
  30.            Name : localhost.localdomain:5  (local to host localhost.localdomain) 
  31.            UUID : afc5cf68:aa1f4f34:f3047c47:d0b6f61c 
  32.          Events : 18 
  33.  
  34.     Number   Major   Minor   RaidDevice State 
  35.        0       8       17        0      active sync   /dev/sdb1 
  36.        1       8       18        1      active sync   /dev/sdb2 
  37.        2       8       19        2      active sync   /dev/sdb3 
  38.        4       8       20        3      active sync   /dev/sdb4 
  39. [root@localhost Desktop]# echo "/dev/md5 /backup ext4 defaults,acl,noatime,nodiratime 0 0" >> /etc/fstab [root@localhost scripts]# mount -a 

7、写一个脚本

(1)、接受一个以上字符串当做用户名;

(2)、显示每个文件拥有的行数;

(3)、总结说明本次共为几个文件统计了其行数;

  1. #!/bin/bash 
  2. #Author:Danry 
  3. #Description: 
  4. if [ $# -le 1 ] 
  5.     then 
  6.         echo "please input someting" && exit 2 
  7. fi 
  8.  
  9. for i in $* 
  10.     do  
  11.         echo "$i line:$(cat $i | wc -l)" 
  12.     done  
  13.         echo "there are $# files" 
  14.  
  15. [root@danry Desktop]# bash test.sh abc.txt  
  16. abc.txt line:6 
  17. there are 1 files 
  18.  
  19.  
  20. [root@danry Desktop]# cat abc.txt  
  21. hao  
  22. nihao  
  23. hello  
  24. world 
  25. good 
  26. morning` 
  27. [root@danry Desktop]# 

8、写一个脚本

(1)、传递两个以上字符串当作用户名;

(2)、创建这些用户;且密码通用户名;

(3)、总结说明共创建了几个用户;

  1. #!/bin/bash 
  2. #Author:Danry 
  3. #Description: 
  4. if [ $# -le 1 ] 
  5.     then  
  6.         echo "Must be more than two characters" && exit 2  
  7. fi 
  8.  
  9. sum=0 
  10. for user in $@ 
  11. do 
  12.     if id $user &> /dev/null 
  13.         then 
  14.             echo "user $user already exists!" 
  15.             continue 
  16.     else 
  17.         useradd $user  
  18.         echo "$user" | passwd --stdin $user  &>/dev/null 
  19.         echo "$user has been created." 
  20.         let sum++ 
  21.     fi 
  22. done 
  23.  
  24. echo "$sum users created." 
  25.  
  26. [root@danry Desktop]# bash test2.sh  nihao nibuhao 
  27. nihao has been created. 
  28. nibuhao has been created. 
  29. 2 users created. 
  30. [root@danry Desktop]# 

9、写一个脚本,新增20个用户,vistitor1-visitor20;计算它们的ID之和;

  1. #!/bin/bash 
  2. for n in `seq 20` 
  3.     do 
  4.         username=visitor$n 
  5.         useradd $username 
  6.         uid=`id -u $username` 
  7.         let sumuserid+=$uid 
  8.  
  9.     done 
  10.     echo "The users id SUM:$sumuserid" 
  11.  
  12. [root@localhost Desktop]# bash test.sh  
  13. Creating mailbox file: File exists 
  14. Creating mailbox file: File exists 
  15. Creating mailbox file: File exists 
  16. Creating mailbox file: File exists 
  17. Creating mailbox file: File exists 
  18. Creating mailbox file: File exists 
  19. Creating mailbox file: File exists 
  20. Creating mailbox file: File exists 
  21. Creating mailbox file: File exists 
  22. Creating mailbox file: File exists 
  23. Creating mailbox file: File exists 
  24. Creating mailbox file: File exists 
  25. Creating mailbox file: File exists 
  26. Creating mailbox file: File exists 
  27. Creating mailbox file: File exists 
  28. Creating mailbox file: File exists 
  29. Creating mailbox file: File exists 
  30. Creating mailbox file: File exists 
  31. Creating mailbox file: File exists 
  32. Creating mailbox file: File exists 
  33. The users id SUM:20210 
  34. [root@localhost Desktop]# 
  35.  
  36. [root@localhost Desktop]# tail -20 /etc/passwd 
  37. visitor1:x:1001:1001::/home/visitor1:/bin/bash 
  38. visitor2:x:1002:1002::/home/visitor2:/bin/bash 
  39. visitor3:x:1003:1003::/home/visitor3:/bin/bash 
  40. visitor4:x:1004:1004::/home/visitor4:/bin/bash 
  41. visitor5:x:1005:1005::/home/visitor5:/bin/bash 
  42. visitor6:x:1006:1006::/home/visitor6:/bin/bash 
  43. visitor7:x:1007:1007::/home/visitor7:/bin/bash 
  44. visitor8:x:1008:1008::/home/visitor8:/bin/bash 
  45. visitor9:x:1009:1009::/home/visitor9:/bin/bash 
  46. visitor10:x:1010:1010::/home/visitor10:/bin/bash 
  47. visitor11:x:1011:1011::/home/visitor11:/bin/bash 
  48. visitor12:x:1012:1012::/home/visitor12:/bin/bash 
  49. visitor13:x:1013:1013::/home/visitor13:/bin/bash 
  50. visitor14:x:1014:1014::/home/visitor14:/bin/bash 
  51. visitor15:x:1015:1015::/home/visitor15:/bin/bash 
  52. visitor16:x:1016:1016::/home/visitor16:/bin/bash 
  53. visitor17:x:1017:1017::/home/visitor17:/bin/bash 
  54. visitor18:x:1018:1018::/home/visitor18:/bin/bash 
  55. visitor19:x:1019:1019::/home/visitor19:/bin/bash 
  56. visitor20:x:1020:1020::/home/visitor20:/bin/bash 
  57. [root@localhost Desktop]# 
责任编辑:武晓燕 来源: 运维部落
相关推荐

2009-10-19 18:01:35

Linux磁盘分区

2020-01-11 19:00:01

GParted磁盘分区工具开源

2011-01-18 09:51:59

Linux磁盘分区

2011-08-10 15:54:12

虚拟磁盘VHD

2011-01-18 10:25:19

Linux磁盘分区

2011-01-18 10:00:59

Linux磁盘分区

2017-05-10 07:00:20

磁盘分区dcfldd工具备份分区

2009-12-15 08:50:58

Windows 7系统分区

2014-06-09 10:09:31

Linux磁盘分区

2013-08-23 14:07:45

磁盘分区

2014-06-06 13:51:36

磁盘分区IO分析

2019-02-11 14:50:21

Linux分区命令

2014-05-30 09:44:18

磁盘分区Windows平台

2017-03-09 14:58:19

GPartedLinux磁盘分区

2023-01-31 14:00:01

TIAPUUID

2021-07-30 23:05:22

Windows 11Windows微软

2022-04-12 17:39:14

Linux磁盘分区

2009-05-18 09:23:00

网管远程备份

2010-03-02 15:09:26

Linux mount

2018-06-25 11:58:23

Linuxparted磁盘分区
点赞
收藏

51CTO技术栈公众号