iT邦幫忙

2022 iThome 鐵人賽

DAY 7
0
自我挑戰組

Linux in my LIFE系列 第 7

我的Linux生活日記 07-硬碟分割

  • 分享至 

  • xImage
  •  

最近忙到爆炸了,所以我就拿很久以前的筆記發文偷懶,算是在工作中寫筆記的好處吧?

在工作上有一顆新的硬碟新增到主機上,順便紀錄一下處理步驟吧。

處理步驟如下:

  1. 取得硬碟資訊,確認實際要格式化那一顆硬碟
  2. 規劃硬碟分割格式
  3. 執行分割
  4. 新增要掛載的目標目錄
  5. 掛載目錄
  6. 取得硬碟 UUID
  7. 寫入 fstab

取得硬碟資訊

在分割硬碟之前記得先確認一下新增硬碟的詳細資訊,例如:硬碟容量大小等等。如果不知道可以下df -h記下哪些硬碟已經被使用了,之接下來檢查目前有哪些硬碟還沒有被掛載就可以知道目標的硬碟資訊。

debian10@debian10:~$ df -h # 檢查目前的硬碟狀況
Filesystem      Size  Used Avail Use% Mounted on
udev            2.0G     0  2.0G   0% /dev
tmpfs           395M  5.5M  390M   2% /run
/dev/vda1        49G  2.0G   44G   5% /
tmpfs           2.0G     0  2.0G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup
tmpfs           375M     0  375M   0% /run/user/1000

再用 fdisk -l查詢要新增的硬碟是那一顆。

debian10@debian10:~$ sudo fdisk -l # 列出所有的硬碟狀況
Disk /dev/vda: 50 GiB, 53687091200 bytes, 104857600 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
Disklabel type: dos
Disk identifier: 0xc20490bc

Device     Boot     Start       End   Sectors  Size Id Type
/dev/vda1  *         2048 102856703 102854656   49G 83 Linux
/dev/vda2       102858750 104855551   1996802  975M  5 Extended
/dev/vda5       102858752 104855551   1996800  975M 82 Linux swap / Solaris


Disk /dev/vdb: 50 GiB, 53687091200 bytes, 104857600 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

這次我是新增的硬碟為/dev/vdb這一顆,接下來就是分割硬碟。

計劃分割表

分割硬碟目前有分gdiskfdisk

gdisk:是 GPT

cgdisk /dev/sdc

fdisk:是 MBR

在實際應用上,GPT可以規劃2TB以上的硬碟

sudo gdisk /dev/sdb

# n -> 新增磁區
# p -> 邏輯磁區
# w -> 寫入分割表
debian10@debian10:~$ sudo gdisk /dev/vdb
GPT fdisk (gdisk) version 1.0.3

Partition table scan:
  MBR: not present
  BSD: not present
  APM: not present
  GPT: not present

Creating new GPT entries.

Command (? for help): help
b	back up GPT data to a file
c	change a partition's name
d	delete a partition
i	show detailed information on a partition
l	list known partition types
n	add a new partition
o	create a new empty GUID partition table (GPT)
p	print the partition table
q	quit without saving changes
r	recovery and transformation options (experts only)
s	sort partitions
t	change a partition's type code
v	verify disk
w	write table to disk and exit
x	extra functionality (experts only)
?	print this menu

Command (? for help): p
Disk /dev/vdb: 104857600 sectors, 50.0 GiB
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): AA29729A-1F35-4579-B7F9-2071211B4A00
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 104857566
Partitions will be aligned on 2048-sector boundaries
Total free space is 104857533 sectors (50.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name

Command (? for help): n
Partition number (1-128, default 1): 1
First sector (34-104857566, default = 2048) or {+-}size{KMGTP}: 
Last sector (2048-104857566, default = 104857566) or {+-}size{KMGTP}: 
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to /dev/vdb.
The operation has completed successfully.

執行分割

規劃完畢後,就是開始分割硬碟了。

debian10@debian10:~$ sudo mkfs.ext4 /dev/vdb
mke2fs 1.44.5 (15-Dec-2018)
Found a gpt partition table in /dev/vdb
Proceed anyway? (y,N) Y
Creating filesystem with 13107200 4k blocks and 3276800 inodes
Filesystem UUID: 85b639c6-b297-4cd6-b382-5a6677f6efd1
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
	4096000, 7962624, 11239424

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (65536 blocks): done
Writing superblocks and filesystem accounting information: done 

新增掛載目錄資料夾

分割完成之後,就是規劃這個硬碟要掛載到目錄哪裡。如果沒有,就得自己新增。

mkdir -p /home/pollochang/data

掛載

確認是那一個目錄之後就是透過mount掛載了。

sudo mount /dev/sda /home/pollochang/data

查詢 UUID

root@loki:~# blkid
/dev/vda1: UUID="f4bba83d-f429-4f62-8659-c29faf16fc7e" TYPE="ext4" PARTUUID="750e807c-01"
/dev/vda5: UUID="d23a345e-7058-4cc8-970d-738d17759cbb" TYPE="swap" PARTUUID="750e807c-05"
/dev/vdb1: UUID="326448fe-b2d2-4cef-a45c-4414e17d1725" TYPE="ext4" PARTUUID="b9a263ed-01"

寫到fstab中

如果重新開機,你會發現之前掛載的硬碟消失了!不對!新的硬碟還須要新增到/etc/fstab這個目錄中

sudo vim /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>

... 部份內容省略

UUID=326448fe-b2d2-4cef-a45c-4414e17d1725 /data           ext4    defaults          0       1

...部份內容省略

以上是我常用的硬碟分個標準流程,當然還有另一種狀況:LVM 新增 physical volume 、 physical volume 擴充空間。

乾脆我把筆記整理上來當作鐵人賽的文章吧,順便整理筆記一下。

如果文章內容有錯,請不吝色請教 m)(.__.)(m)Thank you.


上一篇
我的Linux生活日記 06-我的開發環境設定(二)
下一篇
我的Linux生活日記 08-LVM-新增硬碟-LVM
系列文
Linux in my LIFE32
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言