天天看點

Linux磁盤與分區

Linux系統磁盤概念

磁盤的結構體系

1. 磁盤的實體結構(外部結構 内部結構) 工作原理

2. 磁盤陣列說明 (raid0 raid1 raid5 raid10 raid01)

  磁盤陣列如何配置

  配置LVM

3. 磁盤分區概念

  給小容量磁盤分區:小于2T  fdisk

  給大容量磁盤分區:大于2T  parted

4. 磁盤格式化(建立檔案系統)

5.磁盤維護管理(使用、挂載使用)

磁盤的外部結構

組成部分

a. 磁盤主軸 決定磁盤轉速

家用 72000 rpm 5400 rpm

企業 15k rpm 10krpm

b. 磁盤盤片 用于存儲資料

c. 磁盤的磁頭 用于讀取資料

d. 磁盤接口 用于連接配接主機闆 陣列卡

磁盤的内部結構

a.磁盤(Disk)

b.磁頭(Head)

作用:用來寫入和讀取資料

特點:盤面數量等于磁頭數量

 **工作原理: 采用徑向運動讀寫資料**

c. 磁道(Track)

作用:用來存儲使用者資料

特點: 有多個同心圓組成

存儲計數: 最外面同心圓 為0磁道

工作原理:磁盤預設按磁道尋找資料

扇區(Sector)

作用說明:用來存儲使用者資料

特點說明:磁盤存儲最小機關

存儲計數:預設磁盤扇區從1扇區開始,扇區大小512位元組

系統存儲最小機關是block

柱面(Cylinder)

作用說明: 用來存儲使用者資料

特點: 不同盤面上相同磁道組成(圓柱體)

工作原理: 磁盤預設按照柱面進行讀寫

單元塊(Units)

特點說明: 表示單個柱面大小

磁盤層次結構--磁盤陣列raid

陣列的配置方法:

  • raid 0 存儲資料效率高 存儲安全性低
  • raid 1 存儲資料效率低 存儲安全性高
  • raid 5 存儲資料效率較高 存儲安全性較高
  • raid 01 效率+備份
  • raid 10 備份+效率

磁盤分區預備知識

a. 系統啟動引導記錄--

-MBR引導記錄 用于引導磁盤空間小于2T

-GPT引導記錄 用于引導磁盤空間大于2T

  • (1)磁盤分區方法
  • (2)實作挂載使用(實作開機自動挂載 /etc/fstab / etc/rc.local)
  • (3)swap分區如何調整大小

情況一: 磁盤分區實踐--磁盤小于2T

fdisk

   第一個裡程: 準備磁盤環境

   準備了一塊新的10G硬碟

第二個裡程: 在系統中檢查是否識别到了新的硬碟

檢查是否有新的磁盤存儲檔案

# ll /dev/sdb

brw-rw----. 1 root disk 8, 16 Apr 28 08:54 /dev/sdb

第三個裡程: 對磁盤進行分區處理(fdisk-- 進行分區處理 檢視分區資訊)

fdisk -l   --- 檢視分區資訊

# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.

Be careful before using the write command.

Device does not contain a recognized partition table

Building a new DOS disklabel with disk identifier 0x6c918c6d.

Command (m for help):   可以對磁盤進行分區了

Command action

d   delete a partition  *****

   删除分區    

g   create a new empty GPT partition table

   建立一個新的空的GPT分區表(可以對大于2T磁盤進行分區)

l   list known partition types

   列出可以分區的類型???

m   print this menu

   輸出幫助菜單

n   add a new partition  *****

   建立增加一個分區

p   print the partition table  *****

   輸出分區的結果資訊

q   quit without saving changes

   不儲存退出

t   change a partition's system id

   改變分區的系統id==改變分區類型(LVM 增加swap分區大小)

u   change display/entry units

   改變分區的方式  是否按照扇區進行劃分

w   write table to disk and exit  *****

   将分區的資訊寫入分區表并退出==儲存分區資訊并退出

開始分區:

a 規劃分4個主分區 每個分區1G

分區操作過程

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-20971519, default 2048):

Using default value 2048

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

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

分區操作檢查:

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x3069f1dd

  Device Boot      Start         End      Blocks   Id  System

/dev/sdb1            2048     2099199     1048576   83  Linux

/dev/sdb2         2099200     4196351     1048576   83  Linux

/dev/sdb3         4196352     6293503     1048576   83  Linux

/dev/sdb4         6293504     8390655     1048576   83  Linux

Command (m for help): n

If you want to create more than four partitions, you must replace a

primary partition with an extended partition first.

b 規劃分3個主分區 1個擴充分區 每個主分區1G  剩餘都給擴充分區

删除分區

Command (m for help): d  

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

Partition 1 is deleted  

建立邏輯分區

/dev/sdb4         6293504    20971519     7339008    5  Extended   有了擴充分區才能邏輯分區

All primary partitions are in use

Adding logical partition 5

First sector (6295552-20971519, default 6295552):

Using default value 6295552

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

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

/dev/sdb4         6293504    20971519     7339008    5  Extended

/dev/sdb5         6295552     8392703     1048576   83  Linux

需求: 劃分2個主分區 劃分2個邏輯分區

sdb1  2G

sdb2  2G

sdb5  3G

sdb6  1G    

第四個裡程: 儲存退出,讓系統可以加載識别分區資訊

讓系統可以加載識别分區檔案

partprobe /dev/sdb

parted

情況二: 磁盤分區實踐--磁盤大于2T

第一個裡程: 準備磁盤環境

虛拟主機中添加一塊3T硬碟

第二個裡程: 使用parted指令進行分區

mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)

                                        建立一個分區表 (預設為mbr)

print [devices|free|list,all|NUMBER]     display the partition table, available devices, free space, all found

                                        partitions, or a particular partition

                                         顯示分區資訊

mkpart PART-TYPE [FS-TYPE] START END     make a partition

                                        建立一個分區

quit                                     exit program

                                        退出分區狀态

rm NUMBER                                delete partition NUMBER

                                        删除分區

修改磁盤分區類型: mklabel gpt

分區方法: mkpart primary 0  2100G

第三個裡程: 加載磁盤分區

partprobe /dev/sdc

  1. 磁盤層次結構--格式化操作(建立檔案系統)mkfs.xfs /dev/sdb1 mkfs -t xfs /dev/sdb2

    建立檔案系統: 磁盤分區存儲資料的方式

    ext3/4 centos6 xfs centos7 格式效率較高 資料存儲效率提升(資料庫伺服器)

\# mkfs.xfs /dev/sdb2

meta-data=/dev/sdb2              isize=512    agcount=4, agsize=65536 blks

        =                       sectsz=512   attr=2, projid32bit=1

        =                       crc=1        finobt=0, sparse=0

data     =                       bsize=4096   blocks=262144, imaxpct=25

        =                       sunit=0      swidth=0 blks

naming   =version 2              bsize=4096   ascii-ci=0 ftype=1

log      =internal log           bsize=4096   blocks=2560, version=2

        =                       sectsz=512   sunit=0 blks, lazy-count=1

realtime =none                   extsz=4096   blocks=0, rtextents=0

  1. 磁盤層次結構--磁盤挂載應用

mount /dev/sdb1 /mount01

mount /dev/sdb2 /mount02    

  1. 檢查确認:

# df -h

/dev/sdb1      1014M   33M  982M   4% /mount01

/dev/sdb2      1014M   33M  982M   4% /mount02

  1. 如何實作開機自動挂載:方法一: 将挂載指令放入/etc/rc.local

# tail -2 /etc/rc.local

mount /dev/sdb2 /mount02

  1. 系統開機加載rc.local流程:加載/etc/rc.local --> /etc/rc.d/rc.local --> 以絕對路徑方式執行/etc/rc.d/rc.local chmod +x /etc/rc.d/rc.local

    方法二: 在/etc/fstab檔案中進行設定

    UUID=e2fc8646-2b36-47cc-a35a-8c13208f4d0b / xfs defaults 0 0UUID=34fc45ba-c38c-42bc-a120-90f9d5dd2382 /boot xfs defaults 0 0UUID=62100743-6f8a-4f83-a37d-e2088c4830e2 swap swap defaults 0 0 挂載磁盤檔案(儲存設備) 挂載點 指定檔案系統類型 挂載的參數 是否備份磁盤 是否檢查磁盤 mount 挂載的磁盤檔案 挂載點
  2. 企業磁盤常見問題:
    1. 磁盤滿的情況 No space left on devicea 存儲的資料過多了 模拟磁盤空間不足 dd if=/dev/zero of=/tmp/kang.txt bs=10M count=100 block存儲空間不足了 解決方式: a 删除沒用的資料 b 找出大的沒用的資料

  find / -type f -size +xxx

  du -sh /etc/sysconfig/network-scripts/*|sort -h

    1. 補充: 按照數值排序指令

# cat num.txt |sort -n

# 1

# 10

# 11

# 12

# 2

# 3

# 6

# 9

# cat num.txt |sort -n -k2

    1. b 存儲的資料過多了inode存儲空間不足了: 出現了大量小檔案
  1. 如何調整swap分區大小第一個曆程: 将磁盤分出一部分空間給swap分區使用

dd if=/dev/zero  of=/tmp/1G  bs=100M count=10

  1. 第二個曆程: 将指定磁盤空間作為swap空間使用

mkswap /tmp/1G

Setting up swapspace version 1, size = 1023996 KiB

no label, UUID=6dd70684-dec2-48cf-8fd9-f311548bbb4f

  1. 第三個曆程: 加載使用swap空間

swapon /tmp/1G

swapon: /tmp/1G: insecure permissions 0644, 0600 suggested.

# free -h

             total        used        free      shared  buff/cache   available

Mem:           1.9G        225M        575M        9.6M        1.2G        1.5G

Swap:          2.0G          0B        2.0G

# swapoff /tmp/1G

Mem:           1.9G        224M        576M        9.6M        1.2G        1.5G

Swap:          1.0G          0B        1.0G

# rm /tmp/1G -f

  1. 磁盤知識總結
    1. 掌握磁盤體系結果
    2. 磁盤的内部和外部結構(磁頭 磁道 扇區 柱面) 運維---存儲服務
    3. 磁盤分區的方法fdisk 劃分小于2T的磁盤parted 劃分大于2T的磁盤
    4. 如何實作格式化和自動挂載 存儲服務nfs /etc/rc.local /etc/fstab
    5. 磁盤分區滿的原因 No space left on devicea block占用過多了 df -h 解決方式: 删除大的沒用的資料 如何找到大的檔案 find / -type f -size +500M|xargs rm du -sh /etc/sysconfig

      b inode占用過多了 df -i 解決方式: 删除大量的沒用的小檔案

    6. 如何調整swap空間大小 --- tomcat(java) 512M dd mkswap swapon/off