磁盘划分
管理Linux系统中的硬盘设备最常用的方法就当属是fdisk命令了,这条命令提供了添加、删除、转换分区等等功能于一身的“一站式服务”,不过这条命令的参数是交互式的,而不是像我们以前直接写到命令后面的参数一样,这样在管理硬盘设备的时候特别方便,可以根据需求动态的调整。
目的: 将/dev/vdb划分出/u01,/u02,swap三个分区,并设置成开机自动挂载
1.划分磁盘
--查看磁盘信息:
[root@app-01 ~]# fdisk -l Disk /dev/vda: 42.9 GB, 42949672960 bytes 255 heads, 63 sectors/track, 5221 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: 0x0003d038 Device Boot Start End Blocks Id System /dev/vda1 * 1 5222 41940992 83 Linux Disk /dev/vdb: 536.9 GB, 536870912000 bytes 16 heads, 63 sectors/track, 1040253 cylinders Units = cylinders of 1008 * 512 = 516096 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000
--划分磁盘/dev/vdb
[root@app-01 ~]# fdisk /dev/vdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x9f199567. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) 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): n --建立新分区 Command action e extended p primary partition (1-4) p --选择建立主分区 Partition number (1-4): 1 First cylinder (1-1040253, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-1040253, default 1040253): +300G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (624154-1040253, default 624154): Using default value 624154 Last cylinder, +cylinders or +size{K,M,G} (624154-1040253, default 1040253): +12G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 3 First cylinder (649121-1040253, default 649121): Using default value 649121 Last cylinder, +cylinders or +size{K,M,G} (649121-1040253, default 1040253): Using default value 1040253 Command (m for help): p --打印分区信息 Disk /dev/vdb: 536.9 GB, 536870912000 bytes 16 heads, 63 sectors/track, 1040253 cylinders Units = cylinders of 1008 * 512 = 516096 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x9f199567 Device Boot Start End Blocks Id System /dev/vdb1 1 624153 314573080+ 83 Linux /dev/vdb2 624154 649120 12583368 83 Linux /dev/vdb3 649121 1040253 197131032 83 Linux Command (m for help): w --保存分区 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
注意:发现有些时候系统并没有自动的将分区信息同步给Linux内核,而且这种情况似乎还比较常见,但不能算作是严重的bug,因为如果设备信息没有被抽象为文件,我们也可以输入partprobe命令来手动的同步信息到内核,而且一般推荐敲击两次会效果更佳,但一旦遇到连这个命令都不能解决的时候,咱们就重启计算机吧,这个方法一定会有用的。
[root@app-01 ~]# file /dev/sdb1 /dev/sdb2: cannot open (No such file or directory) [root@app-01 ~]# partprobe [root@app-01 ~]# partprobe [root@app-01 ~]# file /dev/sdb1 /dev/sdb1: block special
2.格式化分区
--格式化普通分区
mkfs.ext4 /dev/vdb1
mkfs.ext4 /dev/vdb3
--格式化swap分区
[root@app-01 ~]# mkswap /dev/vdb2 Setting up swapspace version 1, size = 12583364 KiB no label, UUID=5f48bdfe-81a8-43a1-b9dc-45ed9b21124f
3.挂载分区
--建立挂载目录
[root@app-01 ~]# mkdir /u01 [root@app-01 ~]# mkdir /u02
--挂载普通分区
[root@app-01 ~]# mount /dev/vdb1 /u01 [root@app-01 ~]# mount /dev/vdb3 /u02
--挂载swap
[root@app-01 ~]# swapon /dev/vdb2 [root@app-01 ~]# free -m total used free shared buffers cached Mem: 7872 774 7097 0 86 482 -/+ buffers/cache: 205 7666 Swap: 12288 0 12288
4.设置开机自动挂载
/dev/vdb1 /u01 ext4 defaults 0 0 /dev/vdb3 /u02 ext4 defaults 0 0 /dev/vdb2 swap swap defaults 0 0
5.查看磁盘的UUID
方法一:
[root@app-01 jdk1.7.0_80]# ll /dev/disk/by-uuid/ total 0 lrwxrwxrwx 1 root root 10 Feb 22 11:02 0ac3b66b-c1c6-4838-b13f-fdf5ee066c26 -> ../../vdb3 lrwxrwxrwx 1 root root 10 Feb 22 11:08 1fd6f7f3-a722-4637-91de-327d32c3e05b -> ../../vdb1 lrwxrwxrwx 1 root root 10 Feb 22 11:00 5f48bdfe-81a8-43a1-b9dc-45ed9b21124f -> ../../vdb2 lrwxrwxrwx 1 root root 10 Feb 22 10:48 80b9b662-0a1d-4e84-b07b-c1bf19e72d97 -> ../../vda1
方法二:
[root@app-01 jdk1.7.0_80]# blkid /dev/vdb1 /dev/vdb1: UUID="1fd6f7f3-a722-4637-91de-327d32c3e05b" TYPE="xfs"