linux下面挂载新硬盘(转)

在Linux上添加新的硬盘
字体大小: 小 中 大
系统:Redhat AS3 UP3
硬盘:scsi
注意:# 表示是root用户执行的命令

[root@cncmail data1]# fdisk -l ## 这里是查看目前系统上有几块硬盘

Disk /dev/sda: 36.4 GB, 36401479680 bytes
255 heads, 63 sectors/track, 4425 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 255 2048256 83 Linux
/dev/sda2 256 1530 10241437+ 83 Linux
/dev/sda3 4296 4425 1044225 82 Linux swap
/dev/sda4 1531 4295 22209862+ f Win95 Ext'd (LBA)
/dev/sda5 1531 2805 10241406 83 Linux
/dev/sda6 2806 4295 11968393+ 83 Linux

Partition table entries are not in disk order

Disk /dev/sdb: 36.7 GB, 36703918080 bytes ## 这里发现/dev/sdb,容量36.7G,切未被分区
255 heads, 63 sectors/track, 4462 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdc doesn't contain a valid partition table
[root@linux root]# fdisk /dev/sdb ## 接下去就对/dev/sdb分区进行分区

The number of cylinders for this disk is set to 4462.
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): 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/sdb: 36.7 GB, 36703918080 bytes
255 heads, 63 sectors/track, 4462 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

Command (m for help): n ## 增加一个分区
Command action
e extended
p primary partition (1-4)
## 因为通常选择主分区,所以这里打一个p
p
Partition number (1-4): 1 ## 这里因为是第一个分却,所以只选择1,如果是第二个分区,则选择2,依次类推
First cylinder (1-4462, default 1): ## 新分区起始的磁盘块数
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-4462, default 4462):
如果要分区10G,这里可以直接输入:+10240M,因为这里要全部使用硬盘空间,则用默认
Using default value 4462

Command (m for help): p

Disk /dev/sdb: 36.7 GB, 36703918080 bytes
255 heads, 63 sectors/track, 4462 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 4462 35840983+ 83 Linux
## 这里第一个分区已经分好了,接下去得把这个分区写入硬盘,用w
Command (m for help): w
The partition table has been altered!

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

下面的工作就是对该硬盘进行格式,我这里是格式化成ext3
[root@linux root]# mke2fs -j /dev/sdb1
mke2fs 1.32 (09-Nov-2002)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
4480448 inodes, 8960245 blocks
448012 blocks (5.00%) reserved for the super user
First data block=0
274 block groups
32768 blocks per group, 32768 fragments per group
16352 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624

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

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

检查一下,是否已经格式话好
[root@linux root]# fdisk -l

Disk /dev/sda: 36.4 GB, 36401479680 bytes
255 heads, 63 sectors/track, 4425 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 255 2048256 83 Linux
/dev/sda2 256 1530 10241437+ 83 Linux
/dev/sda3 4296 4425 1044225 82 Linux swap
/dev/sda4 1531 4295 22209862+ f Win95 Ext'd (LBA)
/dev/sda5 1531 2805 10241406 83 Linux
/dev/sda6 2806 4295 11968393+ 83 Linux

Partition table entries are not in disk order

Disk /dev/sdb: 36.7 GB, 36703918080 bytes
255 heads, 63 sectors/track, 4462 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 4462 35840983+ 83 Linux

分区分好,也格式化好了,下面就是挂载
我把/dev/sdb1挂载到/data1下
[root@linux root]# mkdir /data1 ## 首先建立挂载的目录data1
[root@linux root]# vi /etc/fstab ##
修改/etc/fstab,使系统启动就可以自动挂载,当然也可以用mount命令,这里我习惯用这个!

LABEL=/ / ext3 defaults 1 1
LABEL=/bak /bak ext3 defaults,quota 1 2
/dev/sdb1 /data1 ext3 defaults 1 2
none /dev/pts devpts gid=5,mode=620 0 0
none /proc proc defaults 0 0
none /dev/shm tmpfs defaults 0 0
LABEL=/usr /usr ext3 defaults 1 2
LABEL=/var /var ext3 defaults 1 2
/dev/sda3 swap swap defaults 0 0
/dev/cdrom /mnt/cdrom udf,iso9660 noauto,owner,kudzu,ro 0 0
/dev/fd0 /mnt/floppy auto noauto,owner,kudzu 0 0

重起后,把系统重起一下,查看是否挂载成功:
[root@linux data1]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda1 2.0G 454M 1.4G 25% /
/dev/sda6 12G 53M 11G 1% /bak
/dev/sdb1 34G 33M 32G 1% /data1
none 250M 0 250M 0% /dev/shm
/dev/sda2 9.7G 1.5G 7.7G 17% /usr
/dev/sda5 9.7G 8.6G 559M 95% /var

这里看到/dev/sda6 12G 53M 11G 1% /bak
说明已经挂载成功。

========================================================================

  1、拆机,硬盘挂上

  2、重启机器,fdisk -l 查看

  [[email protected]:/dev]#fdisk -l

  Disk /dev/sda: 21.4 GB, 21474836480 bytes

  255 heads, 63 sectors/track, 2610 cylinders

  Units = cylinders of 16065 * 512 = 8225280 bytes

  Device Boot Start End Blocks Id System

  /dev/sda1 * 1 33 265041 83 Linux

  /dev/sda2 34 294 2096482+ 82 Linux swap / Solaris

  /dev/sda3 295 2610 18603270 83 Linux

  Disk /dev/sdb: 32.2 GB, 32212254720 bytes

  255 heads, 63 sectors/track, 3916 cylinders

  Units = cylinders of 16065 * 512 = 8225280 bytes

  Device Boot Start End Blocks Id System

  /dev/sdb3 1 3916 31455238+ 83 Linux

  Disk /dev/sdc: 21.4 GB, 21474836480 bytes

  255 heads, 63 sectors/track, 2610 cylinders

  Units = cylinders of 16065 * 512 = 8225280 bytes

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

  很好,刚挂上去的第三块盘认到了,sdc,21.4G,不过盘上还没有任何分区。

  3、创建分区。执行 fdisk /dev/sdc

  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 2610.

  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):

  提示我输入命令,可怜的记不住命令,只能打一个m,for help了 ^-^

  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)

  大概看了一个m出来的命令,毫不犹豫选择了 n ,添加一个新分区

  Command (m for help): n

  Command action

  e extended

  p primary partition (1-4)

  e

  Partition number (1-4): 1

  First cylinder (1-2610, default 1):

  Using default value 1

  Last cylinder or +size or +sizeM or +sizeK (1-2610, default 2610):

  Using default value 2610

  Command (m for help): w

  The partition table has been altered!

  Calling ioctl() to re-read partition table.

  Syncing disks.

  最后一个w是把分区信息写入磁盘并退出fdisk。

  然后执行fdisk -l

  [[email protected]:/dev]#fdisk -l

  Disk /dev/sda: 21.4 GB, 21474836480 bytes

  255 heads, 63 sectors/track, 2610 cylinders

  Units = cylinders of 16065 * 512 = 8225280 bytes

  Device Boot Start End Blocks Id System

  /dev/sda1 * 1 33 265041 83 Linux

  /dev/sda2 34 294 2096482+ 82 Linux swap / Solaris

  /dev/sda3 295 2610 18603270 83 Linux

  Disk /dev/sdb: 32.2 GB, 32212254720 bytes

  255 heads, 63 sectors/track, 3916 cylinders

  Units = cylinders of 16065 * 512 = 8225280 bytes

  Device Boot Start End Blocks Id System

  /dev/sdb3 1 3916 31455238+ 83 Linux

  Disk /dev/sdc: 21.4 GB, 21474836480 bytes

  255 heads, 63 sectors/track, 2610 cylinders

  Units = cylinders of 16065 * 512 = 8225280 bytes

  Device Boot Start End Blocks Id System

  /dev/sdc1 1 2610 20964793+ 5 Extended

  得意忘形了一下,然后直接执行

  mount /dev/sdc1 /datalog

[[email protected]:/dev]#mount /dev/sdc1 /datalog

  mount: you must specify the filesystem type

  [[email protected]:/dev]#mount -t ext3 /dev/sdc1 /datalog

  mount: wrong fs type, bad option, bad superblock on /dev/sdc1,

  missing codepage or other error

  (aren't you trying to mount an extended partition,

  instead of some logical partition inside?)

  In some cases useful info is found in syslog - try

  dmesg | tail or so

  才想到创建了逻辑盘还没有创建逻辑分区呢,也没有创建文件系统,然后再执行一下:fdisk /dev/sdc

  Command (m for help): n

  Command action

  l logical (5 or over)

  p primary partition (1-4)

  l

  First cylinder (1-2610, default 1):

  Using default value 1

  Last cylinder or +size or +sizeM or +sizeK (1-2610, default 2610):

  Using default value 2610

  Command (m for help): w

  The partition table has been altered!

  Calling ioctl() to re-read partition table.

  Syncing disks.

  再查看一下

  [[email protected]:/root]#fdisk -l

  Disk /dev/sda: 21.4 GB, 21474836480 bytes

  255 heads, 63 sectors/track, 2610 cylinders

  Units = cylinders of 16065 * 512 = 8225280 bytes

  Device Boot Start End Blocks Id System

  /dev/sda1 * 1 33 265041 83 Linux

  /dev/sda2 34 294 2096482+ 82 Linux swap / Solaris

  /dev/sda3 295 2610 18603270 83 Linux

  Disk /dev/sdb: 32.2 GB, 32212254720 bytes

  255 heads, 63 sectors/track, 3916 cylinders

  Units = cylinders of 16065 * 512 = 8225280 bytes

  Device Boot Start End Blocks Id System

  /dev/sdb3 1 3916 31455238+ 83 Linux

  Disk /dev/sdc: 21.4 GB, 21474836480 bytes

  255 heads, 63 sectors/track, 2610 cylinders

  Units = cylinders of 16065 * 512 = 8225280 bytes

  Device Boot Start End Blocks Id System

  /dev/sdc1 1 2610 20964793+ 5 Extended

  /dev/sdc5 1 2610 20964762 83 Linux

  很好,一切正常

  4、创建文件系统

  执行mkfs.ext3 /dev/sdc5

  mke2fs 1.39 (29-May-2006)

  Filesystem label=

  OS type: Linux

  Block size=4096 (log=2)

  Fragment size=4096 (log=2)

  2621440 inodes, 5241190 blocks

  262059 blocks (5.00%) reserved for the super user

  First data block=0

  Maximum filesystem blocks=0

  160 block groups

  32768 blocks per group, 32768 fragments per group

  16384 inodes per group

  Superblock backups stored on blocks:

  32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,

  4096000

  Writing inode tables: done

  Creating journal (32768 blocks): done

  Writing superblocks and filesystem accounting information: done

  This filesystem will be automatically checked every 33 mounts or

  180 days, whichever comes first. Use tune2fs -c or -i to override.

  正常

  5、挂载到根目录下的datalog目录

  mount /dev/sdc5 /datalog

  再执行df

  Filesystem 1K-blocks Used Available Use% Mounted on

  /dev/sda3 18020368 15983692 1106516 94% /

  /dev/sda1 256666 15923 227491 7% /boot

  tmpfs 777688 0 777688 0% /dev/shm

  /dev/sdb3 30961664 24018284 5370620 82% /data

  /dev/sdc5 20635668 176200 19411232 1% /datalog

=====================================================================

对于新手学习,mount 命令,一定会有很多疑问。其实我想疑问来源更多的是对linux系统本身特殊性了解问题。 linux是基于文件系统,所有的设备都会对应于:/dev/下面的设备。如:

[chengmo@centos5 dev]$ ls | grep -E  "^(sd|hd)"
hdc
sda
sda1
sda2
sdb
sdb1

sd*代表scic硬盘

因此,需要进行设备访问时候,我们需要进行系统挂载(mount). 那么mount到底能够挂载那些设备呢。 我们可以看一下。

一、mount可以用来挂载什么:

不同的操作系统使用不同的文件系统格式。MS-DOS支持FAT16文件系统,Windows98支持FAT16、FAT32文件系统,WindowsNT支持FAT16、NTFS文件系统,Windows2000则支持FAT16、FAT32、NTFS三种文件系统格式,现在的xp可以支持FAT32,NTFS,现在最新版本的windows 7 引入了新的WinFS文件系统。 而 Linux差不多支持所有的文件系统格式,但一般使用ext2或ext3文件系统。很多用户使用的是windows操作系统,如果想在运行的Linux下访问其它文件系统中的资源的话,就要用Linux mount命令来实现。

 

二、mount使用格式:

mount命令[-参数] [设备名称] [挂载点]

说明:

[挂载点]必须是一个已经存在的目录,这个目录可以不为空,但挂载后这个目录下以前的内容将不可用,umount以后会恢复正常。

[设备名称] 可以是一个分区,一个usb设备,光驱,软盘,网络共享等。

常见参数说明:

Mount 挂载文件系统使用帮助
参数 说明
常见参数:
-t vfstype

挂载指定的设备类型:adfs, affs, autofs, coda, coherent, cramfs, devpts, efs, ext, ext2, ext3, hfs, hpfs, iso9660, jfs, minix, msdos, ncpfs, nfs, nfs4, ntfs, proc, qnx4, ramfs, reiserfs, romfs, smbfs, sysv, tmpfs, udf, ufs, umsdos, vfat, xenix, xfs, xiafs


一般文件类型,可以不需要指定就可以有相同自动检测(adfs, bfs, cramfs, ext, ext2, ext3, hfs, hpfs, iso9660, jfs, minix, ntfs, qnx4, reiserfs, romfs, udf, ufs, vxfs, xfs, xiafs 等文件系统),如果探测失败,就将访问/etc/filesystems ,以指定文件系统探测。说明:如果清楚文件系统,最好指定具体类型。探测错误将带来灾难性的。

多个文件系统可以用“,”分割开来
mount -a -t nomsdos,ext
将挂载fstab文件指定所有的文件系统,除了类型为 msdos 和 ext 的那一些。

常见文件类型

  1. ext2 linux目前常用的文件系统
  2. msdos MS-DOS的fat,就是fat16 vfat
  3. windows98常用的fat32
  4. nfs 网络文件系统
  5. smbfs windows共享系统
  6. iso9660 CD-ROM光盘标准文件系统
  7. ntfs windows NT/2000/XP的文件系统
  8. auto 自动检测文件系统
-o options 指定挂载系统选项:多个选项可以用","分割.某些选项只有在出现在文件 /etc/fstab 中时才有意义。下列选项可以用于任何要挂载的文件系统(但是并非所有文件系统都关心它们,例如,选项 sync 在今天只对 ext2,ext3 和 ufs 有效)
选项 说明
async 所有对文件系统的 I/O 操作都必须是异步完成的。
atime 每次存取时更新 inode 存取时间。这是默认选项。
auto 可以在使用 -a 选项时被挂载。
defaults 包含以下选项:rw, suid, dev, exec, auto, nouser,async.
dev 解析文件系统中的字符设备或块设备。
exec 允许执行二进制文件
_netdev 文件系统居于需要网络连接的设备上,避免断网下,不断挂载耗资源
noatime 不更新文件系统的inode存取时间(提升新闻服务器的速度)
noauto 只能被显式地挂载,-a不能使它挂载
nodev 不解析文件系统中的字符设备或块设备
noexec 不允许执行被加载的文件系统中的任何二进制文件
nosuid 不允许set-user-identifier 或set-group-identifier位起作用
nouser 禁止普通的挂载文件系统(默认)
remount 试图重新挂载一个已经挂载的文件系统。通常用来改变挂载标记,如由只读挂载改成可读写的。
ro 以只读方式挂载
rw 以读写方式挂载
suid 允许set-user-identifier 或set-group-identifier位起作用
sync 以同步方式进行I/O操作,突然断电不会掉资料,但加重磁盘负担
dirsync 所有对文件系统内目录的更新应当是同步完成的,影响下列系统调用:creat,link, unlink, symlink, mkdir, rmdir, mknod 以及 rename
user 允许普通的用户挂载文件系统,挂载者的用户名被写入mtab,从而使他可以再次卸载文件系统。这个选项暗含了选项noexec,nosuid, 以及 nodev.
users 允许每个用户挂载卸载文件系统
codepage codepage=XXX代码页
iocharset iocharset=XXX 字符集
loop 挂载回旋设备 经常挂载iso文件用到
username username=user 访问设备的用户名
password password=pass 访问设备的密码
以下参数只对特殊文件系统有用:
uid/gid uid=value 和 gid=value 以特殊用户及组 挂载系统
ownmask ownmask=value owner权限的权限掩码
othmask othmask=value othmask权限的权限掩码
不同文件系统所支持的属性不一定系统。可以查找相关资料
-a 搜索/etc/fstab文件中满足条件的文件系统,进行挂载操作。
格式: mount -a -t type -o options 不需要指定设备/目录
-f 测试mount系统,只检查设备和目录,并不真正挂载文件系统
-n 不把挂载的记录,记录在/etc/mtab 文件中
-r 将文件系统安装为只读,同(-o ro)
-w 将文件系统安装为可读写,同(-o rw)
-L label 挂载具有指定卷标 label 的分区
-w 将文件系统安装为可读写,同(-o rw)
-v 显示详细的挂载进度
-l 列出所有已经挂载的文件系统列表
其它参数:
--bind 将文件层次的一部分重新挂载到其他地方.只挂接单一的文件系统,如果目录有其它文件系统,将不能挂载。如果需要都挂载,可以用:--rbind 参数。
mount --bind olddir newdir 2个目录可以访问系统内容
--move 将一个目录移到另外地方,mount --move olddir newdir
-h 显示帮助
-V 显示版本

 

三、常见分区加载方法:

  • mount挂载iso文件

#mkdir /mnt/iso1

#mount –o loop linuxsetup.iso /mnt/iso1

在linux 不需要虚拟光驱,就可以直接读取iso文件了。

  • mount挂载光驱系统

一般来说CDROM的设备文件是/dev/hdc,使用方法:

#mkdir /mnt/cdrom

#mount /dev/hdc /mnt/cdrom –o iocharset=cp936

默认不指定光驱系统,可以自动搜索得到,将编码指定为中文

  • mount挂载软驱

# mkdir /mnt/floppy

# mount /dev/fd0 /mnt/floppy

默认不指定文件系统,可以自动搜索得到

  • mount挂载windows共享文件(samba)

#mkdir /mnt/winshare

#mount -t smbfs -o username=w,password=w,codepage=936,iocharset=gb2312 //192.168.0.101/share /mnt/winshare

指定访问共享的用户名,密码,codepage指定编码与iocharset同意义。这里的windows 系统是中文简体。

codepage指定文件系统的代码页,简体中文中文代码是936;iocharset指定字符集,简体中文一般用cp936或gb2312

  • mount挂载u盘

如果计算机没有其它SCSI设备和usb外设的情况下,插入的U盘的设备路径是 /dev/sda1,用命令:

#mkdir /mnt/upan

#mount /dev/sda1 /mnt/upan

挂载即可。

  • mount挂载nfs系统

与windows共享连接差不多。需要正确配置服务端的nfs服务。然后通过客户端的:showmount -e 192.168.0.30 可以查看连接。

mount -t nfs 192.168.0.30:/tmp /mnt/nfs

  • umount卸载文件系统

umount  dir

如上面: umout /mnt/upan或者umount /dev/sda1

以上是mount命令使用介绍。其中-o参数最为复杂。 每个不同文件系统所支持参数可能各不相同。如果遇到特殊文件系统。建议查阅相关资料。下一节将说下,与mount 相关的文件:fstab格式。

<script type="text/javascript"></script>

猜你喜欢

转载自qianxunniao.iteye.com/blog/1250182
今日推荐