1、首先,我們聊下硬盤。
我們都知道硬盤是用來存儲文件的,當然配合着文件系統(文件系統管理着對應的硬件資源)。
硬盤由多個磁盤組成,每個磁盤有兩面,每面有一個磁頭,磁盤上有多個磁道。
硬盤被分爲多個扇區,當前正常一個扇區是512byte。第一個扇區存儲主引導記錄及分區表信息。
硬盤存儲容量=磁頭數×磁道(柱面)數×每道扇區數×每扇區字節數
(1)硬盤有數個盤片,每盤片兩個面,每個面一個磁頭
(2)盤片被劃分爲多個扇形區域即扇區
(3)同一盤片不同半徑的同心圓爲磁道
(4)不同盤片相同半徑構成的圓柱面即柱面
(5)公式: 存儲容量=磁頭數×磁道(柱面)數×每道扇區數×每扇區字節數
(6)信息記錄可表示爲:××磁道(柱面),××磁頭,××扇區
2、linux下的硬盤分區及掛載
2.1 一切都以文件的形式體現
在linux中,所有的設備都以文件的形式體現,包括硬盤。
設備文件路徑 /dev 路徑中。如下圖,機器中存在一個硬盤 sda。並存在分區 sda1,sda2,sda5.
另外 / 路徑是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系統。
首先我們添加硬盤,添加後需要重啓。
添加之後,在/dev中我們可以看到新添加的硬盤設備: /dev/sdb
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
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).
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / 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