linux硬盘挂载,linux硬盘挂载

1、首先,我们聊下硬盘。

我们都知道硬盘是用来存储文件的,当然配合着文件系统(文件系统管理着对应的硬件资源)。

硬盘由多个磁盘组成,每个磁盘有两面,每面有一个磁头,磁盘上有多个磁道。

硬盘被分为多个扇区,当前正常一个扇区是512byte。第一个扇区存储主引导记录及分区表信息。

52bd1db0deaf4f392f55297317398b14.png

硬盘存储容量=磁头数×磁道(柱面)数×每道扇区数×每扇区字节数

(1)硬盘有数个盘片,每盘片两个面,每个面一个磁头

(2)盘片被划分为多个扇形区域即扇区

(3)同一盘片不同半径的同心圆为磁道

(4)不同盘片相同半径构成的圆柱面即柱面

(5)公式: 存储容量=磁头数×磁道(柱面)数×每道扇区数×每扇区字节数

(6)信息记录可表示为:××磁道(柱面),××磁头,××扇区

2、linux下的硬盘分区及挂载

2.1 一切都以文件的形式体现

在linux中,所有的设备都以文件的形式体现,包括硬盘。

设备文件路径 /dev 路径中。如下图,机器中存在一个硬盘 sda。并存在分区 sda1,sda2,sda5.

9ebc7f4691e991b2d544941e19a3db80.png

另外 / 路径是linux系统的根路径。

如下该机器,sda1分区挂载在根路径下,该分区85G大小。

jetxu@ubuntu:/dev$ df -h

Filesystem      Size  Used Avail Use% Mounted on

udev            982M  4.0K  982M   1% /dev

tmpfs           199M  1.7M  197M   1% /run

/dev/sda1        85G   23G   58G  29% /

none            4.0K     0  4.0K   0% /sys/fs/cgroup

none            5.0M     0  5.0M   0% /run/lock

none            992M  144K  992M   1% /run/shm

none            100M   36K  100M   1% /run/user

2.2 新增硬盘并进行分区

我们该机器进行举例

该机器是一个windows上使用vmware建立的虚拟机,ubuntu系统。

首先我们添加硬盘,添加后需要重启。

a6b9666831fa880032313d8fe08a46a4.png

添加之后,在/dev中我们可以看到新添加的硬盘设备: /dev/sdb

b9fac6ac35166483b61d88bdd26a9fac.png

2.3硬盘分区

查找到新添的硬盘文件后(/dev/sdb),我们需要对其进行分区

jetxu@ubuntu:/dev$ sudo fdisk sdb

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel

Building a new DOS disklabel with disk identifier 0x1fc07a59.

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)

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 sdb: 5368 MB, 5368709120 bytes

255 heads, 63 sectors/track, 652 cylinders, total 10485760 sectors

Units = sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x1fc07a59

Device Boot      Start         End      Blocks   Id  System

Command (m for help): n

Partition type:

p   primary (0 primary, 0 extended, 4 free)

e   extended

Select (default p): p

Partition number (1-4, default 1): 1

First sector (2048-10485759, default 2048):

Using default value 2048

Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759): +2G

Command (m for help): w

The partition table has been altered!

Calling ioctl() to re-read partition table.

Syncing disks.

上面的过程我们添加了一个2Gbyte的分区 sdb1

上面我们注意到分区number只能是1~4,这是因为:

我们前面聊过第一个扇区存储主引导记录及分区信息,

主引导记录需要446byte,每个分区信息需要16byte,而一个扇区只有512byte,

因此(512-446)/16 ---- 4 最多4个分区。

当然我们可以选择用 3 个主分区加 1 个扩展分区的方法,扩展分区中能够创建无限个逻辑分区。

如下图,分区后我们可以找到分区文件 /dev/sdb1

3ec3aed01bfbbb3a1e9c1afb54062658.png

2.4格式化分区

我们将/dev/sdb1 格式化为ext4 (ext4是其中一种文件系统格式)

jetxu@ubuntu:/dev$ mkfs.ext4 /dev/sdb1

mke2fs 1.42.9 (4-Feb-2014)

mkfs.ext4: Permission denied while trying to determine filesystem size

jetxu@ubuntu:/dev$ sudo mkfs.ext4 /dev/sdb1

mke2fs 1.42.9 (4-Feb-2014)

Filesystem label=

OS type: Linux

Block size=4096 (log=2)

Fragment size=4096 (log=2)

Stride=0 blocks, Stripe width=0 blocks

131072 inodes, 524288 blocks

26214 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=536870912

16 block groups

32768 blocks per group, 32768 fragments per group

8192 inodes per group

Superblock backups stored on blocks:

32768, 98304, 163840, 229376, 294912

Allocating group tables: done

Writing inode tables: done

Creating journal (16384 blocks): done

Writing superblocks and filesystem accounting information: done

2.5挂载分区

挂载大致可以理解为将存储设备与某个路径挂接在一起。

挂载分区可以使用mount进行临时挂载,也可以直接修改fstab进行挂载。

2.5.1 mount临时挂载

mount临时挂载的设备,在重启系统后,不再挂载。挂载期间创建、写入的文件不会丢失。

jetxu@ubuntu:~$ mkdir mynewdisk

jetxu@ubuntu:~$ ls

bin  Desktop  Documents  Downloads  examples.desktop  Music  myLearn  mynewdisk  Pictures  Public  Templates  Videos  works

jetxu@ubuntu:~$ sudo mount /dev/sdb1 ~/mynewdisk

挂载后

jetxu@ubuntu:/dev$ df -h

Filesystem      Size  Used Avail Use% Mounted on

udev            982M  4.0K  982M   1% /dev

tmpfs           199M  1.7M  197M   1% /run

/dev/sda1        85G   23G   58G  29% /

none            4.0K     0  4.0K   0% /sys/fs/cgroup

none            5.0M     0  5.0M   0% /run/lock

none            992M  144K  992M   1% /run/shm

none            100M   36K  100M   1% /run/user

/dev/sdb1       2.0G  3.1M  1.8G   1% /home/jetxu/mynewdisk

这样我们就可以使用新加进来的硬盘资源了。对 /home/jetxu/mynewdisk 路径的操作有基于新加的硬盘。

2.5.2 永久挂载

对/etc/fstab文件做如下添加

/dev/sdb1       /home/jetxu/mynewdisk ext4 defaults 0 0

jetxu@ubuntu:/dev$ cat /etc/fstab

# /etc/fstab: static file system information.

#

# Use 'blkid' to print the universally unique identifier for a

# device; this may be used with UUID= as a more robust way to name devices

# that works even if disks are added and removed. See fstab(5).

#

#            

# / was on /dev/sda1 during installation

UUID=1664b5bb-45e6-43f5-bfe7-3ae34ccaa6ac /               ext4    errors=remount-ro 0       1

# swap was on /dev/sda5 during installation

UUID=e714a2eb-4856-4081-893f-29bd947a3dc2 none            swap    sw              0       0

/dev/fd0        /media/floppy0  auto    rw,user,noauto,exec,utf8 0       0

/dev/sdb1       /home/jetxu/mynewdisk ext4 defaults 0 0

重启后(启动过程系统自动挂载)

jetxu@ubuntu:/dev$ df -h

Filesystem      Size  Used Avail Use% Mounted on

udev            982M  4.0K  982M   1% /dev

tmpfs           199M  1.7M  197M   1% /run

/dev/sda1        85G   23G   58G  29% /

none            4.0K     0  4.0K   0% /sys/fs/cgroup

none            5.0M     0  5.0M   0% /run/lock

none            992M  144K  992M   1% /run/shm

none            100M   32K  100M   1% /run/user

/dev/sdb1       2.0G  3.1M  1.8G   1% /home/jetxu/mynewdisk

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值