天天看點

磁盤管理

硬碟管理:

MBR: Master boot record : 主引導記錄

硬碟的0柱面、0磁頭、1扇區稱為主引導扇區(也叫主引導記錄MBR)。它由三個部分組成,主引導程式、硬碟分區表DPT(Disk Partition table)和分區有效标志(55AA)。

在總共512位元組的主引導扇區裡主引導程式(boot loader)占446個位元組,第二部分是Partition table區(分區表),即DPT,占64個位元組,16*4=64,硬碟中分區有多少以及每一分區的大小都記在其中。第三部分是magic number,占2個位元組,固定為55AA。

magic number:·結束标志字,偏移位址01FE--01FF的2個位元組值為結束标志55AA,稱為“魔數”(magic number)。如果該标志錯誤系統就不能啟動。

磁盤管理步驟:

添加裝置 分區 格式化(建立檔案系統) 建立挂載點 挂載 修改配置檔案

[root@localhost ~]# fdisk -l #檢視可用儲存設備

[root@localhost ~]# fdisk /dev/sda

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 #删除分區

g create a new empty GPT partition table

G create an IRIX (SGI) partition table

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): n #建立一個新的分區

Partition type:

p primary (2 primary, 0 extended, 2 free) #建立一個主分區

e extended #建立一個擴充分區

Select (default p): e

Partition number (3,4, default 3): #指定分區号

First sector (26208256-41943039, default 26208256): #指定開始柱面,預設回車就可以

Using default value 26208256

Last sector, +sectors or +size{K,M,G} (26208256-41943039, default 41943039): +5G #指定結束柱面,預設不指定的話是把剩下的所有空間配置設定給分區,等于說指定分區大小

Partition 3 of type Extended and of size 5 GiB is set

Command (m for help): p #列印分區表

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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 label type: dos

Disk identifier: 0x000a705c

Device Boot Start End Blocks Id System

/dev/sda1 * 2048 1026047 512000 83 Linux

/dev/sda2 1026048 26208255 12591104 8e Linux LVM

/dev/sda3 26208256 36694015 5242880 5 Extended #表明是擴充 分區

Command (m for help): n

p primary (2 primary, 1 extended, 1 free)

l logical (numbered from 5) #建立一個邏輯分區

Select (default p): l

Adding logical partition 5

First sector (26210304-36694015, default 26210304): 

Using default value 26210304

Last sector, +sectors or +size{K,M,G} (26210304-36694015, default 36694015): +1G

Partition 5 of type Linux and of size 1 GiB is set

Command (m for help): p

/dev/sda3 26208256 36694015 5242880 5 Extended

/dev/sda5 26210304 28307455 1048576 83 Linux

#邏輯分區的分區号從5開始,不管之前有沒有第四或者第三個分區

Command (m for help): w #儲存

The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.

The kernel still uses the old table. The new table will be used at

the next reboot or after you run partprobe(8) or kpartx(8)

Syncing disks.

[root@localhost ~]# ls /dev/sda

/dev/sda /dev/sda1 /dev/sda2

[root@localhost ~]# partprobe /dev/sda

/dev/sda /dev/sda1 /dev/sda2 /dev/sda3 /dev/sda4 /dev/sda5

注:如果是RHEL6作業系統的話,使用partx -a /dev/sda 重新擷取分區表

格式化

#mkfs.ext3 /dev/sdb1   或mkfs -t ext3 /dev/sdb1 #RHEL5 格式化

#mkfs.ext4 /dev/sdb1   或mkfs -t ext4 /dev/sdb1 #RHEL6 格式化

mkfs.xfs /dev/sda5 或mkfs -t xfs /dev/sda5

建立一個挂載點

[root@localhost ~]# mkdir /sda5

挂載

[root@localhost ~]# mount /dev/sda5 /sda5

檢視挂載狀況

[root@localhost ~]# df -h

Filesystem Size Used Avail Use% Mounted on

/dev/mapper/rhel-root 10G 3.2G 6.9G 32% /

devtmpfs 2.0G 0 2.0G 0% /dev

tmpfs 2.0G 88K 2.0G 1% /dev/shm

tmpfs 2.0G 8.9M 2.0G 1% /run

tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup

/dev/sr0 3.8G 3.8G 0 100% /mnt

/dev/sda1 197M 125M 72M 64% /boot

tmpfs 394M 20K 394M 1% /run/user/0

/dev/sda5 1014M 33M 982M 4% /sda5

[root@localhost ~]# mount | grep sda5

/dev/sda5 on /sda5 type xfs (rw,relatime,attr2,inode64,noquota)

分區開機自動挂載

[root@localhost ~]# vim /etc/fstab

/dev/sda5 /sda5 xfs defaults 0 0

要挂載的分區 挂載點 檔案系統類型 挂載選項 是否備份 是否檢測

最後兩個0

fs_dump 是否要使用dump指令進行備份. 0為不備份,1為要備份 。

fs_pass - 該字段被fsck指令用來決定在啟動時是否需要被掃描的檔案系統的順序,根檔案系統/對應該字段的值應該為1,其他檔案系統應該為2。若該檔案系統無需在啟動 時掃描則設定該字段為0

使用 UUID實作開機自動挂載

UUID作用

UUID是一個辨別你系統中的儲存設備的字元串,其目的是幫助使用者唯一的确定系統中的所有儲存設備,不管它們是什麼類型的。它可以辨別DVD驅動器,USB儲存設備以及你系統中的硬碟裝置等。

特點:

它是真正的唯一标志符

Linux中的許多關鍵功能現在開始依賴于UUID

擷取裝置的UUID

[root@localhost ~]# blkid | grep sda5

/dev/sda5: UUID="351fb366-4263-416a-b8c6-113f1b54a8b7" TYPE="xfs"

UUID=351fb366-4263-416a-b8c6-113f1b54a8b7 /sda5 xfs defaults 0 0

驗證:

[root@localhost ~]# umount /dev/sda5

[root@localhost ~]# mount -a

如何實作CD光牒的開機自動挂載

/dev/sr0 /mnt iso9660 defaults 0 0

[root@localhost ~]# ll /dev/cdrom

lrwxrwxrwx 1 root root 3 Feb 23 08:19 /dev/cdrom -> sr0

特殊挂載方法:

[root@localhost ~]# mount -t xfs /dev/sda5 /sda5

[root@localhost ~]# mount -o loop rhel-server-7.2-x86_64-dvd /mnt

[root@localhost ~]# mount -o remount,ro /sda5

解除安裝分區

[root@localhost ~]# umount /sda5

無法解除安裝

umount: /sda5: target is busy.

(In some cases useful info about processes that use

the device is found by lsof(8) or fuser(1))

[root@localhost ~]# lsof /sda5

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME

bash 2434 root cwd DIR 8,5 6 128 /sda5

[root@localhost ~]# kill -9 2434

[root@localhost ~]# fuser -m -u -v /sda5

USER PID ACCESS COMMAND

/sda5: root kernel mount (root)/sda5

root 39275 ..c.. (root)bash

[root@localhost ~]# kill -9 39275

本文轉自信自己belive51CTO部落格,原文連結:http://blog.51cto.com/11638205/2048839 ,如需轉載請自行聯系原作者

繼續閱讀