Linux磁碟分割槽及自動掛載

千秋,發表於2020-12-28

文章目錄


 

一、磁碟分割槽

1、檢視裝置

(裝置就是磁碟或者USB等)

[root@jydr ~]# lsblk -ip
NAME                        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
/dev/sda                      8:0    0   45G  0 disk 
|-/dev/sda1                   8:1    0    1M  0 part 
|-/dev/sda2                   8:2    0    1G  0 part /boot
|-/dev/sda3                   8:3    0   39G  0 part 
| |-/dev/mapper/centos-root 253:0    0   37G  0 lvm  /
| `-/dev/mapper/centos-swap 253:1    0    2G  0 lvm  [SWAP]
`-/dev/sda4                   8:4    0    1G  0 part /root/mymount
/dev/sr0                     11:0    1 1024M  0 rom  
/dev/loop0                    7:0    0    1G  0 loop /mnt
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

可以看到主要裝置是/dev/sda

2、磁碟分割槽型別

[root@jydr ~]# parted /dev/sda print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 48.3GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt 
Disk Flags: pmbr_boot

Number  Start   End     Size    File system  Name              Flags
 1      1049kB  2097kB  1049kB                                 bios_grub
 2      2097kB  1076MB  1074MB  xfs
 3      1076MB  42.9GB  41.9GB                                 lvm
 4      42.9GB  44.0GB  1074MB  xfs          Linux filesystem
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

Partition Table: gpt # 看這裡,還有可能是mbr 或者 loop或者不知道型別

gpt時使用gdisk分割槽 mbr使用fdisk分割槽,其他型別基本就不是預設的磁碟,直接是一個分割槽或者外接磁碟

3、給磁碟分割槽

[root@jydr ~]# gdisk /dev/sda
GPT fdisk (gdisk) version 0.8.10

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

Found valid GPT with protective MBR; using GPT.

Command (? for 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

檢視分割槽資訊 按p

Command (? for help): p
Disk /dev/sda: 94371840 sectors, 45.0 GiB # 注意刪除數目 94371840
Logical sector size: 512 bytes
Disk identifier (GUID): 44DCEBA0-6073-4F0C-A880-390C5AF8AE86
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 94371806
Partitions will be aligned on 2048-sector boundaries
Total free space is 8392637 sectors (4.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048            4095   1024.0 KiB  EF02  
   2            4096         2101247   1024.0 MiB  0700  
   3         2101248        83884031   39.0 GiB    8E00  
   4        83884032        85981183   1024.0 MiB  8300  Linux filesystem
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

注意在上表的Code一欄

  • 8300代表Linux檔案系統
  • 8200代表swpa
  • 0700代表Windows檔案系統

分割槽操作

Command (? for help): n       
Partition number (5-128, default 5):  
First sector (34-94371806, default = 85981184) or {+-}size{KMGTP}: 
Last sector (85981184-94371806, default = 94371806) or {+-}size{KMGTP}: +1g # 必設定大小,根據自己需要設定
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/sda.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot.
The operation has completed successfully.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

分割槽後重啟一下,lsblk就可以看見新分割槽了

也可以輸入更新指令檢視

cat /proc/partitions
partprobe -s
cat /proc/partitions
  • 1
  • 2
  • 3

4、格式化新的分割槽

檢視新建的分割槽,以及其他分割槽是什麼格式

[root@jydr ~]# blkid
/dev/mapper/centos-root: UUID="f33356bb-33f3-4ff1-923e-253a275b81a0" TYPE="xfs" 
/dev/sda3: UUID="83s7NF-qZRT-b2xi-0Bh6-0RSm-3jr1-QpAa7Z" TYPE="LVM2_member" PARTUUID="b5c31c9d-6eb8-4d2f-bbb1-9912031b156f" 
/dev/sda2: UUID="663c4d89-a1fd-4921-ba90-8f05993be32c" TYPE="xfs" PARTUUID="a570f71a-1aa1-469c-8ecb-f0370ea96ccb" 
/dev/sda4: UUID="ebe2810a-06e4-4df1-94b3-e23f8adf6635" TYPE="xfs" PARTLABEL="Linux filesystem" PARTUUID="87f2bd84-e9ee-4a32-8259-5ab4dff0037d" 
/dev/mapper/centos-swap: UUID="1a781a42-1cf3-4a43-82d9-026f9e3701d9" TYPE="swap" 
/srv/loopdev: UUID="727b8115-3045-4e36-be77-f870bc6fd248" TYPE="xfs" 
/dev/loop0: UUID="727b8115-3045-4e36-be77-f870bc6fd248" TYPE="xfs" 
/dev/sda1: PARTUUID="71c738bb-ae3b-4b69-a7fc-02fce086db00" 
/dev/sda5: PARTLABEL="Linux filesystem" PARTUUID="3a2b86c1-722e-4c4b-beab-5f81f2990258" 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

現在2020年常用的linux格式有ext4和xfs,格式化指令是mkfs.ext4(ubuntu)和mkfs.xfs (CentOS)

[root@jydr ~]# mkfs.xfs /dev/sda5
meta-data=/dev/sda5              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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

完成後這個磁碟就可以掛載使用了

5、給已經分割槽的磁碟區域分割槽

# 建立一個大小為1*512m 的空檔案
dd if=/dev/zero of=/srv/newdev bs=1M count=512

# 格式化該檔案
mkfs.xfs -f /srv/newdev

# 檢視資訊
blkid /srv/newdev

# 找到UUID掛載
mount -o loop UUID="6fb87c04-e80a-4979-b359-98490097f83b" /mnt

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

二、自動掛載

vim /etc/fstab
# 其中新增一行,home目錄以後就用掛載的分割槽了
UUID=ebe2810a-06e4-4df1-94b3-e23f8adf6635 /home xfs defaults 0 0

# 掛載之後要測試一下,不能直接重啟系統
mount -a
如果成功就成功,不成功就檢測錯誤。

相關文章