随机数、熵池

bash可以生成随机数,范围是0-32768【65535/2】,它是一个系统变量$RANDOM

[jiakang@jiakang tmp]$ echo $RANDOM
15163
[jiakang@jiakang tmp]$ echo $RANDOM
23259
[jiakang@jiakang tmp]$ echo $RANDOM
25631

随机数生成器:熵池

熵池是存放随机数的池子,我们上面bash生成的随机数安全性很低,甚至是通过算法可以破解,所以出现了熵池;

熵池是让/dev/random使用的,它产生的随机数是硬件不规律形成的,比如我们间隔按鼠标的时间,安全性非常高,熵池中的值是剪切过来的,所以可能会被用完出现阻塞用户的进程。

/dev/urandom:也是取熵池中的随机数,但是取完后会用bash自动模拟,不会出现阻塞。

/dev/random

/dev/urandom

mknod创建设备

mknod mydev c 66 0【c代表字符设备,ls - l每行的最前面那个字符代表文件类型,66代表主设备号,0代表次设备号】

[root@jiakang a]# mknod mydev c 66 0
[root@jiakang a]# ls -il            
总用量 28
412296 drwxr-xr-x. 3 root root  4096 3月  28 16:34 b
412310 -rwxr-xr-x. 1 root root   133 4月  13 14:34 case.sh
412301 crw-r--r--. 1 root root 66, 0 4月  15 18:42 mydev

还可以在创建设备的时候指定使用权限

[root@jiakang a]# mknod -m 640 mydev1 c 66 1
[root@jiakang a]# ls -l
总用量 28
drwxr-xr-x. 3 root root  4096 3月  28 16:34 b
-rwxr-xr-x. 1 root root   133 4月  13 14:34 case.sh
crw-r--r--. 1 root root 66, 0 4月  15 18:42 mydev
crw-r-----. 1 root root 66, 1 4月  15 18:47 mydev1

当我们有个硬件连接到linux上面,这个设备会有个自己的设备号,然后我们的linux也有这个设备号,所以就对接上了。linux向这个设备号发送东西,就会发送到对应的硬件上。

下面是两个设备【终端设备】:

[root@jiakang a]# tty
/dev/pts/0

[jiakang@jiakang ~]$ tty
/dev/pts/1

[root@jiakang a]# echo "hello jiakang" >> /dev/pts/1

[jiakang@jiakang ~]$ hello jiakang

根目录的重要性

根目录是文件系统的访问路口,如:/a/b,现在把b单独分区了,我要想访问b分区,必须先找到根目录的/a/b,b文件存的是b分区的访问入口;在/a/b/c中,现在再把c单独分区,那访问的时候先找根分区然后找到b文件相应的找到了b分区,再找c文件找到c分区。所以如果根很重要,根分区崩了,其他分区也没了

fdisk查看/创建分区

查看

查看磁盘、分区的使用情况 fdisk -l [/dev/PATH]

[root@jiakang ~]# fdisk -l盘【查看磁盘的使用情况】

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000478b0

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26         287     2097152   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             287        1306     8182784   83  Linux
[root@jiakang ~]# fdisk -l /dev/sda1【查看某个分区的使用情况】

Disk /dev/sda1: 209 MB, 209715200 bytes
255 heads, 63 sectors/track, 25 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

创建、创建

fdisk /dev/sda

  • p:显示当前硬件的分区,包括没保存的改动

  • n:创建新分区【e:扩展分区 p:主分区】

  • d:删除一个分区

  • w:保存退出

  • q:不保存退出

  • t:修改分区类型【L:显示支持的分区类型】

  • l:显示所支持的所有类型

下面的代码我要创建一个新的主分区,但是没有多余的柱面【空间】了

[root@jiakang ~]# fdisk /dev/sda【管理磁盘分区】

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): m【m获取帮助】
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): p【打印显示当前分区使用状况】

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders【共1305个柱面】
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000478b0

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26         287     2097152   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             287        1306     8182784   83  Linux

Command (m for help): n【创建新分区】
Command action
   e   extended
   p   primary partition (1-4)
p【创建主分区】
Selected partition 4
No free sectors available

Command (m for help): q【退出】
...

删除第3个主分区,然后再重新划分柱面

...
...
Command (m for help): d【删除分区】
Partition number (1-4): 3【删除第三个主分区】

Command (m for help): p

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000478b0

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26         287     2097152   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.

创建主分区3,扩展分区4,逻辑分区5

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (287-1305, default 287): 【起始柱面,这直接回车就是默认】
Using default value 287
Last cylinder, +cylinders or +size{K,M,G} (287-1305, default 1305): +2G【结束柱面,指定大小】

Command (m for help): p

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000478b0

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26         287     2097152   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             287         548     2098834   83  Linux【这是新增的主分区3柱面287-548】

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e【创建扩展分区】
Selected partition 4
First cylinder (549-1305, default 549): 【回车】
Using default value 549
Last cylinder, +cylinders or +size{K,M,G} (549-1305, default 1305): 【回车】
Using default value 1305

Command (m for help): n
First cylinder (549-1305, default 549): 
Using default value 549
Last cylinder, +cylinders or +size{K,M,G} (549-1305, default 1305): +1G

Command (m for help): p

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000478b0

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26         287     2097152   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             287         548     2098834   83  Linux
/dev/sda4             549        1305     6080602+   5  Extended
/dev/sda5             549         680     1060258+  83  Linux【类型编号83,类型Linux】

上面的sda5类型编号是83,类型为Linux,我们可以调到其他类型

 Command (m for help): t【修改分区类型】
Partition number (1-5): 5【修改第5个分区】
Hex code (type L to list codes): L【查看有哪些分区类型,83就是刚才的Linux】

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      41  PPC PReP Boot   85  Linux extended  c7  Syrinx         
 5  Extended        42  SFS             86  NTFS volume set da 
...
...
Hex code (type L to list codes): 82【调整为82交换分区】
Changed system type of partition 5 to 82 (Linux swap / Solaris)

Command (m for help): p

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000478b0

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26         287     2097152   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             287         548     2098834   83  Linux
/dev/sda4             549        1305     6080602+   5  Extended
/dev/sda5             549         680     1060258+  82  Linux swap / Solaris【改为了82】

w 保存退出修改

Command (m for help): 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)
Syncing disks.
[root@jiakang ~]# fdisk -l

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000478b0

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26         287     2097152   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             287         548     2098834   83  Linux
/dev/sda4             549        1305     6080602+   5  Extended
/dev/sda5             549         680     1060258+  82  Linux swap / Solaris

partprobe让内核重新识别分区表

上面的分区表成功后需要内核去识别,/proc/partitions目录是当前内核识别的分区表,可以通过partprobe去让内核重新识别,但是在这可能遇到下面的错误

[root@jiakang ~]# cat /proc/partitions
major minor  #blocks  name

   8        0   10485760 sda
   8        1     204800 sda1
   8        2    2097152 sda2
   8        3    8182784 sda3
[root@jiakang ~]# partprobe
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (设备或资源忙).  As a result, it may not reflect all of your changes until after reboot.
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。
Error: 无效的分区表 - /dev/sr0 出现递归分区。
[root@jiakang ~]# cat /proc/partitions
major minor  #blocks  name

   8        0   10485760 sda
   8        1     204800 sda1
   8        2    2097152 sda2
   8        3    8182784 sda3
[root@jiakang ~]# ls -l /dev/sr0
brw-rw----+ 1 root cdrom 11, 0 4月  16 09:55 /dev/sr0

从上面的结果看,sr0是光驱设备,百度了下/dev/sr0这个设备,也所是光驱。于是管它重载分区表失败是不是由于“Error: 无效的分区表 - /dev/sr0 出现递归分区。”引起的,先把光驱移除了再说,反正是虚拟机上。

把虚拟机上的光驱设备移除后,重新启动系统,再重新新建分区,并重新执行partprobe命令重载分区表,错误信息“Error: 无效的分区表 - /dev/sr0 出现递归分区。”没有了,只是警告提示“设备或资源忙”还在。这时直接格式化新建的逻辑分区/dev/sda5还是提示“没有那个文件或目录”。于是重启系统,再格式化/dev/sda5,这回成功了。

也就是说,之前新建分区格式化失败,是由于虚拟机的光驱/dev/sr0设备导致了分区表递归。另外由于是在同一块硬盘上操作,所以必须要重启系统才能使新建的分区写入分区表生效。

卸载光驱后:

[root@jiakang ~]# partprobe           
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (设备或资源忙).  As a result, it may not reflect all of your changes until after reboot.
[root@jiakang ~]# cat /proc/partitions
major minor  #blocks  name

   8        0   10485760 sda
   8        1     204800 sda1
   8        2    2097152 sda2
   8        3    8182784 sda3

重启系统

MDZZ!!!不知道有没有发现上面的刚开始柱面不足,我删除了第3个主分区,我的根目录及所有的文件都在里面,相当于删除了根目录,导致系统崩溃:“客户机操作系统已禁用 CPU。请关闭或重置虚拟机 ”

重新安装了一下linux,这次我加载了两块磁盘,新建了sdb1分区:

[root@localhost ~]# fdisk -l

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0002b305

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26         548     4194304   83  Linux
Partition 2 does not end on cylinder boundary.
/dev/sda3             548         809     2097152   82  Linux swap / Solaris
Partition 3 does not end on cylinder boundary.
/dev/sda4             809        1306     3988480    5  Extended
/dev/sda5             810        1306     3987456   83  Linux

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xd25c91c2

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         200     1606468+  83  Linux

然而这次发现不用内核去识别了,直接就可以自动去识别,而且还可以格式化【我sdb1是新建的刚刚】

[root@localhost ~]# cat /proc/partitions
major minor  #blocks  name

   8        0   10485760 sda
   8        1     204800 sda1
   8        2    4194304 sda2
   8        3    2097152 sda3
   8        4          1 sda4
   8        5    3987456 sda5
   8       16   10485760 sdb
   8       17    1606468 sdb1
[root@localhost ~]# mkfs -t ext4 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
...
...

查阅资料获取的信息是:不是当前使用的磁盘不需要让内核重新去获取了,教训就是没事就快照,别不小心删了根目录!!

partx

redhat6的partprobe失效可以用partx

[root@localhost ~]# partx -a /dev/sdb1 /dev/sdb

Big_fat_cat
207 声望10 粉丝