Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7770595
  • 博文数量: 701
  • 博客积分: 2150
  • 博客等级: 上尉
  • 技术积分: 13233
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-29 16:28
个人简介

天行健,君子以自强不息!

文章分类

全部博文(701)

文章存档

2019年(2)

2018年(12)

2017年(76)

2016年(120)

2015年(178)

2014年(129)

2013年(123)

2012年(61)

分类: LINUX

2013-12-30 14:38:28

Linux磁盘自动挂载必须,而且必要,不然每次重启后都要挂载就太麻烦啦。


1. 查看系统中磁盘信息,并确定是否需要分区
首先,为了需要,新添加一块磁盘到linux主机中,
目的是让这个新添加的磁盘分区格式化并可以随系统启动自动挂载。

[root@localhost ~]# fdisk -l
【此处是sda的磁盘信息】
Disk /dev/sda: 299.4 GB, 299439751168 bytes
255 heads, 63 sectors/track, 36404 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes


   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14       36404   292310707+  8e  Linux LVM


【此处是sdb的磁盘信息】
Disk /dev/sdb: 6442 MB, 6442450944 bytes
255 heads, 63 sectors/track, 783 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/sdb doesn't contain a valid partition table 【此处显示磁盘dev/sdb尚未格式化】
       
如上所示,系统中有两块磁盘sda、sdb,并提示sdb未含有分区表,需要将其进行分区操作。


2.对磁盘进行分区操作
磁盘容量与主分区、扩展分区、逻辑分区的关系:


硬盘的容量=主分区的容量+扩展分区的容量
扩展分区的容量=各个逻辑分区的容量之和
 
一块物理硬盘只能有: 
一到四个主分区(但其中只能有一个是活动的主分区),
或一到三个主分区,和一个扩展分区。分别对应hda1,hda2,hda3,hda4.
 
Linux 中规定,每一个硬盘设备最多能有 4 个主分区(其中包含扩展分区)构成,
任何一个扩展分区都要占用一个主分区号码,也就是在一个硬盘中,主分区和扩展分区一共最多是 4 个。
 
一块硬盘可以只设主分区,这时主分区可设置4个分区号。


也可以设置成主分区+逻辑分区,这时也是最多4个分区号码,但是变成了4 = 3 + 1.
. 其中4是主分区和扩展分区加起来最多4个;  
. 3是主分区,可以小于或等于3;  
. 1是扩展分区号,占用了一个主分区号。


[root@localhost ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.


The number of cylinders for this disk is set to 182024.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)


Command (m for help): p                            【显示磁盘基本信息】
Disk /dev/sdb: 1497.1 GB, 1497198755840 bytes
255 heads, 63 sectors/track, 182024 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes


   Device Boot      Start         End      Blocks   Id  System


                                                   【此处可以看出,尚未对磁盘进行分区】
Command (m for help):n                           【n:add a new partition】
Command action
e extended                                     【选择主分区还是扩展分区】
p primary partition (1-4)
p
Partition number (1-4): 1                         【选择分区数】
First cylinder (1-182024, default 1):              【设置分区大小。此处选择默认值,直接回车
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-182024, default 182024): 
Using default value 182024
Command (m for help):w                           【如无误输入w保存配置信息,如不保存输入q退出】
The partition table has been altered!


Calling ioctl() to re-read partition table.
Syncing disks


【再检查一下分区成功与否】
$ fdisk /dev/sdb
The number of cylinders for this disk is set to 182024.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)


Command (m for help): p                          【如下查看分区之后的磁盘信息】
Disk /dev/sdb: 1497.1 GB, 1497198755840 bytes
255 heads, 63 sectors/track, 182024 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes


   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1      182024  1462107748+   5  Extended


Command (m for help): q


【如果想划分多个分区,可以按以下操作】
[root@localhost ~]# fdisk /dev/sdb
Command (m for help): p                          【p:print the partintion table】
Disk /dev/sdb: 6442 MB, 6442450944 bytes           【显示磁盘基本信息】
255 heads, 63 sectors/track, 783 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
此处可以看出,尚未对磁盘进行分区
Command (m for help):n                           【n:add a new partition】
Command action
e extended                                     【选择主分区还是扩展分区,在此选择主分区】
p primary partition (1-4)
p
Partition number (1-4): 3                          【选择分区数】
First cylinder (1-783, default 1):                 【设置分区大小。此处定义1000M。就是最接近1000M的柱面号码】
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-783, default 783): +1000M
Command (m for help):w                           【如无误输入w保存配置信息,如不保存输入q退出】
Command (m for help): p                          【如下查看分区之后的磁盘信息】
Disk /dev/sdb: 6442 MB, 6442450944 bytes
255 heads, 63 sectors/track, 783 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device     Boot Start End Blocks Id System
/dev/sdb1 368 783 3341520 83 Linux
/dev/sdb2 124 367 1959930 83 linux
/dev/sdb3 1 123 987966 83 Linux


3.将分区好的磁盘进行格式化操作
[root@localhost ~]# mkfs.xfs -f /dev/sdb            【将磁盘sdb1格式化为xfs文件系统】
meta-data=/dev/sdb               isize=256    agcount=32, agsize=137318400 blks
         =                       sectsz=512   attr=0
data     =                       bsize=4096   blocks=4394188800, imaxpct=25
         =                       sunit=0      swidth=0 blks, unwritten=1
naming   =version 2              bsize=4096  
log      =internal log           bsize=4096   blocks=32768, version=1
         =                       sectsz=512   sunit=0 blks, lazy-count=0
realtime =none                   extsz=4096   blocks=0, rtextents=0


4.将分区格式化完毕的磁盘挂载,检查确认分区格式化完毕。
[root@localhost /]# mkdir /data                  【新建一个目录data】
[root@localhost /]# mount /dev/sdb /data/         【将磁盘sdb挂载到系统中】


[root@localhost /]# cd /data
[root@localhost data]# ls                        【进行写文件测试,检查是否可以对磁盘进行读写(是否格式化)
[root@localhost data]# vim test.txt               【进行写测试】
[root@localhost data]# cat test.txt               【进行读测试】

5.测试无误后将其卸载
[root@localhost mnt]#umount /dev/sdb1

6.修改fstab文件,如下所示:
[root@localhost mnt]# vim /etc/fstab
/dev/VolGroup00/LogVol00 /                       ext3    defaults        1 1
LABEL=/boot             /boot                   ext3    defaults        1 2
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/VolGroup00/LogVol01 swap                    swap    defaults        0 0
【添加一行】
/dev/sdb                /data                   xfs      defaults       0 0


【如果是多个分区,则添加如下多行】
/dev/sdb1 /mnt/sdb1 ext3 defaults 0 0
/dev/sdb2 /mnt/sdb2 ext3 defaults 0 0
/dev/sdb3 /mnt/sdb3 ext3 defaults 0 0


7.重启linux系统,检查挂载情况
[root@localhost sdb1]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                      238G  8.5G  218G   4% /
/dev/sda1              99M   13M   81M  14% /boot
tmpfs                  16G     0   16G   0% /dev/shm
/dev/sdb              1.4T  5.1M  1.4T   1% /data

【多个分区时】
文件系统   1K-块    已用     可用       已用%   挂载点
/dev/sda1  8022104  2893532  4714500     39%      /
tmpfs       127720   0      127720      0%       /dev/shm
/dev/sdb1  3288944  71328   3050540     3%       /mnt/sdb1
/dev/sdb2  1929100  35696   1795408     2%       /mnt/sdb2
/dev/sdb3  972404   17640   905368      2%      /mnt/sdb3

如上所示,将新建的磁盘的三个分区顺利挂载到系统中。

阅读(16430) | 评论(0) | 转发(3) |
给主人留下些什么吧!~~