Linux挂载一个磁盘

Linux系统下当原来一块硬盘容量不够用了,需要新添加一块硬盘;或者原来组建RAID磁盘阵列的,现在不需要了,在取消了RAID,重装了系统的情况下,如何使用第二块磁盘

此次的情况是原来是2块硬盘组建的RAID1,现在不需要组建RAID了,就取消了RAID,并重装了系统
在进入系统后,使用fdisk –l命令来查看磁盘状况如下
linux_156:/ # fdisk -l

Disk /dev/sda: 146.8 GB, 146815733760 bytes
255 heads, 63 sectors/track, 17849 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1        2611    20972826   82  Linux swap / Solaris
/dev/sda2   *        2612        3917    10490445   83  Linux
/dev/sda3            3918       17849   111908790    f  W95 Ext'd (LBA)
/dev/sda5            3918       11319    59456533+  83  Linux
/dev/sda6           11320       17847    52436128+  83  Linux

Disk /dev/sdb: 146.8 GB, 146815733760 bytes
255 heads, 63 sectors/track, 17849 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         262     2104483+  82  Linux swap / Solaris
/dev/sdb2   *         263        1568    10490445   83  Linux

从以上信息,可以很明显的看出,我们有2块硬盘,分别是/dev/sda和/dev/sdb,大小都是146.8G
其中磁盘/dev/sda被分为2个主分区和2个逻辑分区大小也差不多是146.8G,但是磁盘/dev/sdb只有2个主分区,其大小也只有12G,不知道为什么丢失了很多空间,我觉得可能是组建RAID1的原因,很多原来的空间未分区,只划分了部分swap分区和装操作系统的/分区,现在有2个办法来解决这个问题:
1. 使用fdisk在原来两个分区的基础上,将未被使用的空间重新分区
2. 格式化/dev/sdb盘,再重新分区
我选择了方法2,因为我觉得原来的那两个分区的数据并没有保留的必要
使用mkfs.ext3    /dev/sdb命令格式化磁盘 注:也可以使用mkfs -t ext3 /dev/sdb命令
mke2fs 1.38 (30-Jun-2005)
/dev/sdb is entire device, not just one partition!     注:系统会提示你/dev/sdb是整个磁盘
Proceed anyway? (y,n) y                       注:这时选Y就好了,下来就开始格式化
以上命令里的ext3,是指将该磁盘格式化成ext3文件系统

格式化完成后,我们再使用fdisk –l命令来查看磁盘状况:


Disk /dev/sda: 146.8 GB, 146815733760 bytes
255 heads, 63 sectors/track, 17849 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1        2611    20972826   82  Linux swap / Solaris
/dev/sda2   *        2612        3917    10490445   83  Linux
/dev/sda3            3918       17849   111908790    f  W95 Ext'd (LBA)
/dev/sda5            3918       11319    59456533+  83  Linux
/dev/sda6           11320       17847    52436128+  83  Linux

Disk /dev/sdb: 146.8 GB, 146815733760 bytes
255 heads, 63 sectors/track, 17849 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdb doesn't contain a valid partition table

然后我们就可以使用fdisk命令来重新分区
fdisk /dev/sdb

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  注:l是列出分区类型,以供我们设置相应分区的类型;
   m   print this menu  注:m 是列出帮助信息;
   n   add a new partition 注:添加一个分区;
   o   create a new empty DOS partition table
   p   print the partition table 注:p列出分区表;
   q   quit without saving changes 注:不保存退出;
   s   create a new empty Sun disklabel   
   t   change a partition's system id  注:t 改变分区类型;
   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)
此时如果选e是建扩展分区,选p建主分区,所以我们选p
p
Partition number (1-4): 1            注:此处选1,表示是第一主分区
First cylinder (1-17849, default 1): 1   注:这个就是分区的Start 值;这里最好直接按回车,如果你输入了一个非默认的数字,会造成空间浪费;
Last cylinder or +size or +sizeM or +sizeK (1-17849, default 17849): 8920
注:这个是定义分区大小的,+200M 就是大小为200M ;当然你也可以根据上面提示的单位cylinder的大小来算,然后来指定 End的数值,在fdisk –l命令中可以看到Units = cylinders of 16065 * 512 = 8225280 bytes,这个就是单位cylinder的大小,我这里选的End的数值是8920,正好是总大小的一半,71G左右
然后再来建一个主分区
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (8921-17849, default 8921): 8921
Last cylinder or +size or +sizeM or +sizeK (8921-17849, default 17849): 17849
最后保存并退出,切记,一定要保存,不然不会生效的
Command (m for help): w
The partition table has been altered!

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

OK,我们再使用fdisk –l命令来查看磁盘状况:
Disk /dev/sda: 146.8 GB, 146815733760 bytes
255 heads, 63 sectors/track, 17849 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1        2611    20972826   82  Linux swap / Solaris
/dev/sda2   *        2612        3917    10490445   83  Linux
/dev/sda3            3918       17849   111908790    f  W95 Ext'd (LBA)
/dev/sda5            3918       11319    59456533+  83  Linux
/dev/sda6           11320       17847    52436128+  83  Linux

Disk /dev/sdb: 146.8 GB, 146815733760 bytes
255 heads, 63 sectors/track, 17849 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        8920    71649868+  83  Linux
/dev/sdb2            8921       17849    71722192+  83  Linux
从上面信息中可以看出,磁盘/dev/sdb已经被划分为2个分区,每个分区大小大概是71G左右,失去的磁盘空间已经被我们找回来了

下面我们开始来想办法挂载文件系统,挂载文件系统,目前有两种方法,一是通过 mount 来挂载,另一种方法是通过/etc/fstab文件来开机自动挂载。
切记,在挂载文件系统之前,需要将/dev/sdb1和/dev/sdb2重新用mkfs -t ext3 命令格式化一下,否则在挂载时会报错:
linux_156:/etc # mount -t ext3 /dev/sdb1 /mnt/data
mount: wrong fs type, bad option, bad superblock on /dev/sdb1,
       missing codepage or other error
       In some cases useful info is found in syslog - try
       dmesg | tail  or so
linux_156:/etc # dmesg | tail
mtrr: type mismatch for cfc00000,200000 old: write-back new: write-combining
mtrr: type mismatch for cf800000,400000 old: write-back new: write-combining
mtrr: type mismatch for cf000000,800000 old: write-back new: write-combining
mtrr: type mismatch for ce000000,1000000 old: write-back new: write-combining
mtrr: type mismatch for cc000000,2000000 old: write-back new: write-combining
eth0: no IPv6 routers present
eth2: no IPv6 routers present
VFS: Can't find ext3 filesystem on dev sdb1.
VFS: Can't find ext3 filesystem on dev sdb1.
VFS: Can't find ext3 filesystem on dev sdb1.

使用mount挂载系统,一旦系统重启之后就需要重新挂载,比较麻烦,所以我们选择通过/etc/fstab文件来开机自动挂载。
/dev/sdb1            /mnt/data            ext3       defaults              0 0
/dev/sdb2            /mnt/db              ext3       defaults              0 0
第一字段:设备名,在这里表示是文件系统; 有时我们把挂载文件系统也说成挂载分区;
第二字段:文件系统的挂载点;
第三字段:文件系统类型;
第四字段:mount 命令的选项,和mount 中的-o 同理;defaults包括这些选项 rw, suid, dev, exec, auto, nouser, async;
第五字段:表示文件系统是否需要dump 备份,1是需要,0 是不需要;
第六字段: 是否在系统启动时,通过fsck磁盘检测工具来检查文件系统,1是需要,0是不需要,2是跳过;

保存退出后,重启系统


猜你喜欢

转载自ice-walf.iteye.com/blog/1037070
今日推荐