初识Linux(第三天:硬盘分区)

硬盘的分区

Linux为各种IDE设备分配了一个由hd前缀组成的文件。各种SCSI设备,则被分配了一个由sd前缀组成的文件,编号方法为拉丁字母表顺序。如第一个IDE设备(如IDE硬盘或IDE光驱),Linux定义为hda;第二个IDE设备就定义为hdb;下面以此类推。而SCSI设备就应该是sda、sdb、sdc等。USB磁盘通常会被识别为SCSI设备,因此其设备名可能是sda。
简单来说就是

设备设备名称
IDEhda、hdb、hdc
SCSIsda、sdb、sdc

在 Linux中规定,每一个硬盘设备最多能有4个主分区(其中包含扩展分区)。任何一个扩展分区都要占用一个主分区号码。在一个硬盘中,主分区和扩展分区一共最多是4个。编号方法为阿拉伯数字顺序。需要注意的是,主分区按1234编号,扩展分区中的逻辑分区,编号直接从5开始,无论是否有2号或3号主分区。对于第一个IDE硬盘的第一主分区,则编号为hdal,而第二个IDE硬盘的第一个逻辑分区编号应为hdb5。
在这里插入图片描述
我们可以使用df命令用于显示目前在Linux系统上的文件系统的磁盘使用情况统计。

[root@localhost usr]# df -Th
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/sda2      ext4    18G  3.3G   14G  20% /
tmpfs          tmpfs  491M  224K  491M   1% /dev/shm
/dev/sda1      ext4   291M   34M  242M  13% /boot

第一列指定文件系统的名称,第三列指定一个特定的文件系统的总量。被使用的和可用的量使用列指定使用的内存的百分比,而最后一栏"安装在"指定的文件系统的挂载点。所谓的挂载点就是将磁盘的某个分区挂载到Linux某一个目录下以便访问。如第一个分区sda1(/dev/sda1)挂载到/boot目录下,第二个分区sda2(/dev/sda2)挂载到/目录下。

磁盘分区

下面我们可以对硬盘进行重新分区。和Windows一样,在Linux下用于磁盘分区的命令也是fdisk。注意磁盘分区操作会对数据造成损失,因此需要谨慎。
首先我们打开虚拟机设置
(1)右击虚拟机,选择“设置”菜单,打开“虚拟机设置”窗体。双击硬盘(SCSI)
在这里插入图片描述
(2)单击左下角的添加按钮,打开“添加硬件向导”窗体,单击下一步按钮。
在这里插入图片描述
(3)选择磁盘类型和创建新虚拟磁盘。
在这里插入图片描述
在这里插入图片描述
(4) 指定磁盘容量,将磁盘大小设置为5GB,选择磁盘存储为单个文件
在这里插入图片描述
(5)将名称改为work.vmdk,单击完成按钮,完成磁盘的新建
在这里插入图片描述
创建了一个新磁盘后,当前系统有两个硬盘。需要对sdb2硬盘进行分区和格式化。

然后使用fdisk命令的l选项查看硬盘分区

[root@localhost Desktop]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x000b9729

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          39      307200   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              39        2358    18631680   83  Linux
/dev/sda3            2358        2611     2031616   82  Linux swap / Solaris

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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

当前虚拟机有两个磁盘,sda为原来的磁盘,有3个分区。sdb为刚创建的磁盘,没有分区。
再者对新建的磁盘sdb进行分区

[root@localhost Desktop]# fdisk /dev/sdb
......
输入m,查看帮助
Command (m for help): 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)

选择n,添加一个新的分区

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p

p是主分区,e是扩展分区。第一个主分区为sdb1,大小为2GB。我们现在选择创建主分区。

Partition number (1-4): 1
First cylinder (1-652, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652): +2G

创建好了主分区sdb1,接着创建一个扩展分区sdb2。

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e
Partition number (1-4): 2
First cylinder (263-652, default 263):              
Using default value 263
Last cylinder, +cylinders or +size{K,M,G} (263-652, default 652): 

// 不填表示默认为剩下的所有空间都分配给sdb2
Using default value 652

Command (m for help): p
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0x06e640c1
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         262     2104483+  83  Linux
/dev/sdb2             263         652     3132675    5  Extended

查看所有分区,打印分区表

Command (m for help): p
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0x06e640c1
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         262     2104483+  83  Linux
/dev/sdb2             263         652     3132675    5  Extended

接着创建逻辑分区

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (263-652, default 263): 
Using default value 263
Last cylinder, +cylinders or +size{K,M,G} (263-652, default 652):   
Using default value 652

查看所有分区,打印分区表

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0x06e640c1

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         262     2104483+  83  Linux
/dev/sdb2             263         652     3132675    5  Extended
/dev/sdb5             263         652     3132643+  83  Linux

现在共有3个分区,主分区sdb1,扩展分区sdb2,逻辑分区sdb5。输入w退出。

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
You have mail in /var/spool/mail/root

mkfs格式化分区
使用mkfs命令格式主分区sdb1,格式化文件系统为ext4。

[root@localhost Desktop]# mkfs -t ext4 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131648 inodes, 526120 blocks
26306 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=541065216
17 block groups
32768 blocks per group, 32768 fragments per group
7744 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912

Writing inode tables: done                            
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 24 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

使用mkfs命令格式逻辑分区sdb5,格式化文件系统为ext3。

[root@localhost Desktop]# mkfs -t ext3 /dev/sdb5
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
195840 inodes, 783160 blocks
39158 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=805306368
24 block groups
32768 blocks per group, 32768 fragments per group
8160 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912

Writing inode tables: done                            
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 28 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

格式化完成之后,就是将sdb1和sdb5挂载到/mnt目录下。

[root@localhost Desktop]# mount /dev/sdb1 /mnt/
[root@localhost Desktop]# mount /dev/sdb5 /mnt/
[root@localhost Desktop]# mount

如果出现如下字样

/dev/sdb1 on /mnt type ext4 (rw)
/dev/sdb5 on /mnt type ext3 (rw)

说明sdb1和sdb5已经挂载成功

如果想卸载挂载到mnt目录下的逻辑分区sdb5

[root@localhost Desktop]# umount /dev/sdb5

版权声明:本文为qq_33890533原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。