CentOS 掛載(U盤NTFS格式,新硬盤,增加交換分區,擴展根分區等)


1、掛載fat或者fat32分區的U盤

如果是用VM安裝的linux,在vm里掛載U盤有兩個前提:

第一,主機里的service要啟動:

第二,U盤是連接到虛擬機,而不是主機,需要確認這點:

2、使用fdisk命令先檢查一下U盤是否已經加載

fdisk -l

設備/dev/sdb1 就是插入的U盤,FAT32分區

加載:
[root@bigdata-senior01 ~]# ll /mnt/usb
總用量 0
[root@bigdata-senior01 ~]# mount /dev/sdb1 /mnt/usb
[root@bigdata-senior01 ~]# ll /mnt/usb
總用量 760
-rwxr-xr-x 1 root root  34494 1月   2 01:22 autorun.ico
-rwxr-xr-x 1 root root    236 1月   2 01:22 autorun.inf
-rwxr-xr-x 1 root root     14 1月   2 00:50 CentOS_BuildTag
... ...
用df命令可以再檢查一下:
[root@bigdata-senior01 ~]# df -h
文件系統                 容量  已用  可用 已用% 掛載點
/dev/mapper/centos-root   17G  4.2G   13G   25% /
devtmpfs                 901M     0  901M    0% /dev
tmpfs                    912M     0  912M    0% /dev/shm
tmpfs                    912M  8.6M  904M    1% /run
tmpfs                    912M     0  912M    0% /sys/fs/cgroup
/dev/sda1               1014M  143M  872M   15% /boot
tmpfs                    183M     0  183M    0% /run/user/1004
/dev/sdb1                 15G  4.2G   11G   30% /mnt/usb
當不再使用U盤的時候,需要卸載設備
[root@bigdata-senior01 ~]# umount /dev/sdb1
[root@bigdata-senior01 ~]# ll /mnt/usb
總用量 0

2、掛載NTFS分區的U盤

2.1、安裝NTFS-3G

CentOS默認情況下並不識別NTFS分區格式,需要使用一個開源的軟件來支持。NTFS-3G 是一個開源的軟件,可以實現 Linux、Free BSD、Mac OSX、NetBSD 和 Haiku 等操作系統中的 NTFS 讀寫支持。它可以安全且快速地讀寫 Windows 系統的 NTFS 分區,而不用擔心數據丟失。

自帶的yum源沒有這個軟件,要用第三方的軟件源,這里采用阿里的epel。

[root@bigdata-senior01 ~]# cd /etc/yum.repos.d/
[root@bigdata-senior01 yum.repos.d]# wget http://mirrors.aliyun.com/repo/epel-7.repo

yum -y install ntfs-3g

安裝NTFS-3G后,使用fdisk -l才能看到ntfs格式的u盤

這個U盤,是一個通用PE制作的啟動U盤,被分成了兩個分區,啟動分區是FAT16,另外數據分區是NTFS,這里需要加載/dev/sdb1

掛載NTFS格式U盤
[root@bigdata-senior01 yum.repos.d]# ll /mnt/usb
總用量 0

[root@bigdata-senior01 yum.repos.d]# mount -t ntfs-3g /dev/sdb1 /mnt/usb
The disk contains an unclean file system (0, 0).
The file system wasn't safely closed on Windows. Fixing.

[root@bigdata-senior01 yum.repos.d]# ll /mnt/usb
總用量 0
drwxrwxrwx 1 root root 0 1月   9 09:54 backup
drwxrwxrwx 1 root root 0 1月   1 22:37 GHO
drwxrwxrwx 1 root root 0 1月   9 09:53 System Volume Information

參數-t ntfs-3g是可以省略的。CentOS7以后可以自動識別。
[root@bigdata-senior01 yum.repos.d]# umount /dev/sdb1
[root@bigdata-senior01 yum.repos.d]# ll /mnt/usb
總用量 0
[root@bigdata-senior01 yum.repos.d]# mount /dev/sdb1 /mnt/usb
[root@bigdata-senior01 yum.repos.d]# ll /mnt/usb
總用量 0
drwxrwxrwx 1 root root 0 1月   9 09:54 backup
drwxrwxrwx 1 root root 0 1月   1 22:37 GHO
drwxrwxrwx 1 root root 0 1月   9 09:53 System Volume Information

3、掛載一塊新硬盤

3.1、物理設備命名規則

在 Linux 系統中一切都是文件,硬件設備也不例外。系統內核中的 udev 設備管理器會自動把硬件名稱規范起來。
目的是讓用戶通過設備文件的名字可以猜出設備大致的屬性以及分區信息等;這對於陌生的設備來說特別方便。
另外,udev設備管理器的服務會一直以守護進程的形式運行並偵聽內核發出的信號來管理
/dev目錄下的設備文件。
常見的設備:
IDE設備       /dev/hd[a-d]
SCSI/SATA/U盤  /dev/sd[a-p]
軟驅        /dev/fd[0-1]
打印機       /dev/lp[0-15]
光驅        /dev/cdrom
鼠標         /dev/mouse
磁帶機       /dev/st0 或/dev/ht0
系統采用 a~p 來代表 16 塊不同的硬盤(默認從 a 開始分配)。
硬盤的分區編號:
1) 主分區或擴展分區的編號從 1 開始,到 4 結束;
2) 邏輯分區從編號 5 開始

/dev/sda 表示第一塊硬盤,這是由系統內核的識別順序來決定的,和設備主板的插槽順序沒有完全一致的對應關系。
/dev/sda1 表示第一塊硬盤的分區編號為1的分區,這個編號一般是順延,但是可以手動指定,所以1號分區不一定就是在最前。

3.2、主分區、邏輯分區

硬盤設備是由大量的扇區組成的,每個扇區的容量為 512 字節。
其中第一個扇區最重要,它里面保存着主引導記錄(Master boot record,MBR)與分區表信息。
就第一個扇區來講,主引導記錄需要占用
446 字節,分區表為 64 字節,結束符占用 2 字節;
其中分區表中每記錄一個分區信息就需要
16 字節,這樣一來最多只有64/16= 4 個分區,只有4個分區信息可以寫到第一個扇區中,這 4 個分區就是4個主分區。

第一個扇區最多只能創建出 4 個分區。了解決分區個數不夠的問題,將第一個扇區的分區表中 16 字節(屬於主分區信息)的空間拿出來指向另外一個分區。
擴展分區其實並不是一個真正的分區,而像是一個占用 16 字節分區表空間的指針,一個指向另外一個分區的指針,這是一個單向鏈表結構。
這樣一來,用戶一般會選擇使用 3 個主分區加 1 個擴展分區的方法,然后在擴展分區中創建出數個邏輯分區,從而來滿足大於4多分區的需求。

一般情況下:主分區為sda1,sda2,sda3,如果有sda4,那么就沒有邏輯分區。如果需要邏輯分區,那么sda4將會空出來,邏輯分區會從sda5開始計算。

3.3 開始掛載新硬盤

如果是在vm中加載新硬盤,停止vm后編輯虛擬機,點擊新增,按缺省的添加一塊scsi的20g硬盤。

從上面的知識,掛載的新硬盤是第二塊硬盤,設備應該是/dev/sdb

1)先用fdisk -l命令查看是否有新的硬盤被識別。

2)開始分區:

fdisk命令在指定硬盤名稱后會進入交互命令模式,根據提示按m鍵可以查看所有可用命令參數。

[root@bigdata-senior01 ~]# fdisk /dev/sdb
歡迎使用 fdisk (util-linux 2.23.2)。

更改將停留在內存中,直到您決定將更改寫入磁盤。
使用寫入命令前請三思。

Device does not contain a recognized partition table
使用磁盤標識符 0xfe9194c7 創建新的 DOS 磁盤標簽。

命令(輸入 m 獲取幫助):m
命令操作
   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)

命令(輸入 m 獲取幫助):

#輸入n來創建分區,因為是一個新的硬盤,我們直接創建主分區
命令(輸入 m 獲取幫助):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
分區號 (1-4,默認 1):1
起始 扇區 (2048-41943039,默認為 2048):
將使用默認值 2048
Last 扇區, +扇區 or +size{K,M,G} (2048-41943039,默認為 41943039):+2G
分區 1 已設置為 Linux 類型,大小設為 2 GiB

命令(輸入 m 獲取幫助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.
正在同步磁盤。
#上面,我們分了一個主分區,然后大小為2g,最后用w命令寫入變更。

#查看新的分區是否有效
[root@bigdata-senior01 ~]# file /dev/sdb1
/dev/sdb1: block special

#這說明已經生效,如果出現
/dev/sdb1: cannot open (No such file or directory)
那么需要執行partprobe命令進行內核通知,或者重啟系統。

3)開始格式化:

#打mkfs后,按兩下tab鍵補全命令后,可以看到可以使用的格式化命令
#將/dev/sdb1格式化成xfs格式,這個是CentOS7新的格式
[root@bigdata-senior01 ~]# mkfs
mkfs         mkfs.btrfs   mkfs.cramfs  mkfs.ext2    mkfs.ext3    mkfs.ext4    mkfs.minix   mkfs.xfs
[root@bigdata-senior01 ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=131072 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=524288, 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

4)開始掛載

[root@bigdata-senior01 ~]# mount /dev/sdb1  /backup
[root@bigdata-senior01 ~]# df -h
文件系統                 容量  已用  可用 已用% 掛載點
/dev/mapper/centos-root   17G  4.2G   13G   25% /
devtmpfs                 901M     0  901M    0% /dev
tmpfs                    912M     0  912M    0% /dev/shm
tmpfs                    912M  8.6M  904M    1% /run
tmpfs                    912M     0  912M    0% /sys/fs/cgroup
/dev/sda1               1014M  143M  872M   15% /boot
tmpfs                    183M     0  183M    0% /run/user/1004
/dev/sdb1                2.0G   33M  2.0G    2% /backup

#備份/etc目錄文件到/backup/etc_backup
[root@bigdata-senior01 /]# cp -R /etc/* /backup/etc_backup/

[root@bigdata-senior01 /]# du -sh /backup/etc_backup/
34M    /backup/etc_backup/

5)永久掛載

[root@bigdata-senior01 /]# vi /etc/fstab 
#
# /etc/fstab
# Created by anaconda on Sun Apr 29 17:25:33 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=fa75616e-a122-4c73-9fd4-b1d50a4af91a /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/dev/sdb1               /backup                 xfs     defaults        0 0

6)查看是否分區已經格式化

如果分區沒有格式化,是無法掛載的,通過命令blkid可以查看格式化信息

[root@bigdata-senior01 /]# blkid
/dev/sda1: UUID="fa75616e-a122-4c73-9fd4-b1d50a4af91a" TYPE="xfs" 
/dev/sda2: UUID="LeV3fD-l62t-F4Gl-Tybf-HC2m-ki71-byH5cc" TYPE="LVM2_member" 
/dev/sdb1: UUID="32eb878b-3d7a-450e-9668-0f65c2be7d05" TYPE="xfs" 
/dev/sr0: UUID="2018-05-03-20-55-23-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" 
/dev/mapper/centos-root: UUID="07a00359-cd62-458a-a150-f2d4a367abe3" TYPE="xfs" 
/dev/mapper/centos-swap: UUID="49c39eb4-4181-4c37-947c-2dc6311fbeea" TYPE="swap" 

4、添加交換分區

4.1、查看目前的交換分區情況

[es@bigdata-senior01 ~]$ swapon -s
文件名                類型        大小    已用    權限
/dev/dm-1                                  partition    2097148    0    -1

#查看/etc/fstab里自動掛載的情況
[es@bigdata-senior01 dev]$ cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sun Apr 29 17:25:33 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=fa75616e-a122-4c73-9fd4-b1d50a4af91a /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/dev/sdb1               /backup                 xfs     defaults  xfs   0 0

#發現/dev/mapper/centos-swap是交換分區,有點不對,再查看
[es@bigdata-senior01 ~]$ ll /dev/mapper
總用量 0
lrwxrwxrwx 1 root root       7 1月  15 09:57 centos-root -> ../dm-0
lrwxrwxrwx 1 root root       7 1月  15 09:57 centos-swap -> ../dm-1
crw------- 1 root root 10, 236 1月  15 09:57 control

#原來/dev/mapper/centos-swap是鏈接到/dev/dm-1,這就和swapon -s命令的結果對上了,但是/dev/dm-1又是什么呢,它不是一個分區設備文件,但是屬於塊文件。
#CentOS可以使用文件作為交換分區,也可以使用一個分區作為交換分區,所以/dev/dm-1是一個分區文件。

 4.2 、添加交換分區

[root@bigdata-senior01 ~]# fdisk /dev/sdb
歡迎使用 fdisk (util-linux 2.23.2)。

更改將停留在內存中,直到您決定將更改寫入磁盤。
使用寫入命令前請三思。


命令(輸入 m 獲取幫助):p

磁盤 /dev/sdb:21.5 GB, 21474836480 字節,41943040 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512 字節 / 512 字節
I/O 大小(最小/最佳):512 字節 / 512 字節
磁盤標簽類型:dos
磁盤標識符:0xfe9194c7

   設備 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux

命令(輸入 m 獲取幫助):n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
分區號 (2-4,默認 2):2
起始 扇區 (4196352-41943039,默認為 4196352):
將使用默認值 4196352
Last 扇區, +扇區 or +size{K,M,G} (4196352-41943039,默認為 41943039):+3G
分區 2 已設置為 Linux 類型,大小設為 3 GiB

命令(輸入 m 獲取幫助):p

磁盤 /dev/sdb:21.5 GB, 21474836480 字節,41943040 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512 字節 / 512 字節
I/O 大小(最小/最佳):512 字節 / 512 字節
磁盤標簽類型:dos
磁盤標識符:0xfe9194c7

   設備 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux
/dev/sdb2         4196352    10487807     3145728   83  Linux

命令(輸入 m 獲取幫助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 設備或資源忙.
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)
正在同步磁盤。
[root@bigdata-senior01 ~]# file /dev/sdb2
/dev/sdb2: cannot open (No such file or directory)
[root@bigdata-senior01 ~]# partprobe
Warning: 無法以讀寫方式打開 /dev/sr0 (只讀文件系統)。/dev/sr0 已按照只讀方式打開。
[root@bigdata-senior01 ~]# file /devsdb2
/devsdb2: cannot open (No such file or directory)

[root@bigdata-senior01 ~]# partprobe
Warning: 無法以讀寫方式打開 /dev/sr0 (只讀文件系統)。/dev/sr0 已按照只讀方式打開。
[root@bigdata-senior01 ~]# file /dev/sdb2
/dev/sdb2: block special

#使用blkid命令查看一下分區的格式化信息
[root@bigdata-senior01 ~]# blkid
/dev/sda1: UUID="fa75616e-a122-4c73-9fd4-b1d50a4af91a" TYPE="xfs"
/dev/sda2: UUID="LeV3fD-l62t-F4Gl-Tybf-HC2m-ki71-byH5cc" TYPE="LVM2_member"
/dev/sdb1: UUID="32eb878b-3d7a-450e-9668-0f65c2be7d05" TYPE="xfs"
/dev/sr0: UUID="2018-05-03-20-55-23-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/mapper/centos-root: UUID="07a00359-cd62-458a-a150-f2d4a367abe3" TYPE="xfs"
/dev/mapper/centos-swap: UUID="49c39eb4-4181-4c37-947c-2dc6311fbeea" TYPE="swap"

#發現/dev/sdb2沒有格式化,使用 SWAP 分區專用的格式化命令 mkswap,對新建的主分區進行格式化操作
[root@bigdata-senior01 ~]# mk
mkdict            mkfifo            mkfs.btrfs        mkfs.ext4         mkinitrd          mktemp
mkdir             mkfontdir         mkfs.cramfs       mkfs.minix        mklost+found      
mkdumprd          mkfontscale       mkfs.ext2         mkfs.xfs          mknod             
mke2fs            mkfs              mkfs.ext3         mkhomedir_helper  mkswap            
[root@bigdata-senior01 ~]# mkswap /dev/sdb2
正在設置交換空間版本 1,大小 = 3145724 KiB
無標簽,UUID=a0971ea7-e603-40b3-94ff-88f82389717a

#查看一下格式化后的情況
[root@bigdata-senior01 ~]# blkid
/dev/sda1: UUID="fa75616e-a122-4c73-9fd4-b1d50a4af91a" TYPE="xfs"
/dev/sda2: UUID="LeV3fD-l62t-F4Gl-Tybf-HC2m-ki71-byH5cc" TYPE="LVM2_member"
/dev/sdb1: UUID="32eb878b-3d7a-450e-9668-0f65c2be7d05" TYPE="xfs"
/dev/sr0: UUID="2018-05-03-20-55-23-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/mapper/centos-root: UUID="07a00359-cd62-458a-a150-f2d4a367abe3" TYPE="xfs"
/dev/mapper/centos-swap: UUID="49c39eb4-4181-4c37-947c-2dc6311fbeea" TYPE="swap"
/dev/sdb2: UUID="a0971ea7-e603-40b3-94ff-88f82389717a" TYPE="swap"

#掛載到系統上
[root@bigdata-senior01 ~]# swapon /dev/sdb2

#查看交換分區情況,從2g->5g
[root@bigdata-senior01 ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           1.8G        171M        1.3G        8.6M        269M        1.4G
Swap:          5.0G          0B        5.0G

[root@bigdata-senior01 ~]# swapon -s
文件名                類型        大小    已用    權限
/dev/dm-1                                  partition    2097148    0    -1
/dev/sdb2                                  partition    3145724    0    -2

#永久掛載交換分區
[root@bigdata-senior01 ~]# vi /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sun Apr 29 17:25:33 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=fa75616e-a122-4c73-9fd4-b1d50a4af91a /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/dev/sdb1               /backup                 xfs     defaults        0 0
/dev/sdb2               swap                    swap    defaults        0 0

4.3、用文件作為交換分區

1)創建要作為swap分區的文件:
增加1GB大小的交換分區,則命令寫法如下,其中的count等於想要的塊的數量(bs*count=文件大小)。
# dd if=/dev/zero of=/root/swapfile bs=1M count=1024

2)格式化為交換分區文件:
# mkswap /root/swapfile #建立swap的文件系統

3)啟用交換分區文件:
# swapon /root/swapfile #啟用swap文件

4)使系統開機時自啟用,在文件/etc/fstab中添加一行:
/root/swapfile swap swap defaults 0 0

5)關閉交換分區swapoff /root/swapfile

5、總結幾個查看磁盤情況的命令

fdisk -l #查看系統當下掛載磁盤情況
fdisk /dev/sdb #對為sdb磁盤分區
mkfs.xfs /dev/sdb1 #格式化sdb1分區
mkswap /dev/sdb2 #格式化為交換分區
blkid #查看磁盤情況
lsblk #查看分區和磁盤
cfdisk /dev/sda  #查看分區、操作分區、格式化分區等
df -hT #查看分區掛載、容量等
du -sh /backup #統計當前目錄各文件夾大小

 6、根分區空間不足,如何擴展,將上例剩余的分區全部用於擴展根分區

[root@bigdata-senior01 ~]# df -h
文件系統                 容量  已用  可用 已用% 掛載點
/dev/mapper/centos-root   17G  4.2G   13G   25% /
devtmpfs                 901M     0  901M    0% /dev
tmpfs                    912M     0  912M    0% /dev/shm
tmpfs                    912M  8.6M  904M    1% /run
tmpfs                    912M     0  912M    0% /sys/fs/cgroup
/dev/sda1               1014M  143M  872M   15% /boot
/dev/sdb1                2.0G   67M  2.0G    4% /backup
tmpfs                    183M     0  183M    0% /run/user/1004

6.1、將/dev/sdb剩余空間全部使用分區成LVM分區

[root@bigdata-senior01 ~]# fdisk /dev/sdb
歡迎使用 fdisk (util-linux 2.23.2)。

更改將停留在內存中,直到您決定將更改寫入磁盤。
使用寫入命令前請三思。


命令(輸入 m 獲取幫助):p

磁盤 /dev/sdb:21.5 GB, 21474836480 字節,41943040 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512 字節 / 512 字節
I/O 大小(最小/最佳):512 字節 / 512 字節
磁盤標簽類型:dos
磁盤標識符:0xfe9194c7

   設備 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux
/dev/sdb2         4196352    10487807     3145728   83  Linux

命令(輸入 m 獲取幫助):n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
分區號 (3,4,默認 3):3
起始 扇區 (10487808-41943039,默認為 10487808):
將使用默認值 10487808
Last 扇區, +扇區 or +size{K,M,G} (10487808-41943039,默認為 41943039):
將使用默認值 41943039
分區 3 已設置為 Linux 類型,大小設為 15 GiB

命令(輸入 m 獲取幫助):t #修改分區
分區號 (1-3,默認 3):3
Hex 代碼(輸入 L 列出所有代碼):L

 0  空              24  NEC DOS         81  Minix / 舊 Linu bf  Solaris        
 1  FAT12           27  隱藏的 NTFS Win 82  Linux 交換 / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 隱藏的 C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux 擴展      c7  Syrinx         
 5  擴展            41  PPC PReP Boot   86  NTFS 卷集       da  非文件系統數據 
 6  FAT16           42  SFS             87  NTFS 卷集       db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux 純文本    de  Dell 工具      
 8  AIX             4e  QNX4.x 第2部分  8e Linux LVM       df  BootIt         
 9  AIX 可啟動      4f  QNX4.x 第3部分  93  Amoeba          e1  DOS 訪問       
 a  OS/2 啟動管理器 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad 休 eb  BeOS fs        
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT            
 f  W95 擴展 (LBA)  54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC  
11  隱藏的 FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor      
12  Compaq 診斷     5c  Priam Edisk     a9  NetBSD          f4  SpeedStor      
14  隱藏的 FAT16 <3 61  SpeedStor       ab  Darwin 啟動     f2  DOS 次要       
16  隱藏的 FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS    
17  隱藏的 HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST 智能睡眠    65  Novell Netware  b8  BSDI swap       fd  Linux raid 自動
1b  隱藏的 W95 FAT3 70  DiskSecure 多啟 bb  Boot Wizard 隱  fe  LANstep        
1c  隱藏的 W95 FAT3 75  PC/IX           be  Solaris 啟動    ff  BBT            
1e  隱藏的 W95 FAT1 80  舊 Minix       
Hex 代碼(輸入 L 列出所有代碼):8e
已將分區“Linux”的類型更改為“Linux LVM”

命令(輸入 m 獲取幫助):p

磁盤 /dev/sdb:21.5 GB, 21474836480 字節,41943040 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512 字節 / 512 字節
I/O 大小(最小/最佳):512 字節 / 512 字節
磁盤標簽類型:dos
磁盤標識符:0xfe9194c7

   設備 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux
/dev/sdb2         4196352    10487807     3145728   83  Linux
/dev/sdb3        10487808    41943039    15727616   8e  Linux LVM

命令(輸入 m 獲取幫助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 設備或資源忙.
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)
正在同步磁盤。
[root@bigdata-senior01 ~]# file /dev/sdb3
/dev/sdb3: cannot open (No such file or directory)
[root@bigdata-senior01 ~]# partprobe
Warning: 無法以讀寫方式打開 /dev/sr0 (只讀文件系統)。/dev/sr0 已按照只讀方式打開。
[root@bigdata-senior01 ~]# file /dev/sdb3
/dev/sdb3: block special

6.2、創建物理卷

[root@bigdata-senior01 ~]# fdisk -l

磁盤 /dev/sda:21.5 GB, 21474836480 字節,41943040 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512 字節 / 512 字節
I/O 大小(最小/最佳):512 字節 / 512 字節
磁盤標簽類型:dos
磁盤標識符:0x000af76c

   設備 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM

磁盤 /dev/sdb:21.5 GB, 21474836480 字節,41943040 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512 字節 / 512 字節
I/O 大小(最小/最佳):512 字節 / 512 字節
磁盤標簽類型:dos
磁盤標識符:0xfe9194c7

   設備 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux
/dev/sdb2         4196352    10487807     3145728   83  Linux
/dev/sdb3        10487808    41943039    15727616 8e Linux LVM

磁盤 /dev/mapper/centos-root:18.2 GB, 18249416704 字節,35643392 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512 字節 / 512 字節
I/O 大小(最小/最佳):512 字節 / 512 字節


磁盤 /dev/mapper/centos-swap:2147 MB, 2147483648 字節,4194304 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512 字節 / 512 字節
I/O 大小(最小/最佳):512 字節 / 512 字節

[root@bigdata-senior01 ~]# pvcreate /dev/sdb3
  Physical volume "/dev/sdb3" successfully created.

6.3、擴展卷組

[root@bigdata-senior01 ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos  #卷組名
  PV Size               <19.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              4863
  Free PE               0
  Allocated PE          4863
  PV UUID               LeV3fD-l62t-F4Gl-Tybf-HC2m-ki71-byH5cc
   
  "/dev/sdb3" is a new physical volume of "<15.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb3
  VG Name               
  PV Size               <15.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               Wgpb02-82cG-b8Ft-V38g-QPwW-5TL0-XgRqLJ
   
[root@bigdata-senior01 ~]# vgextend centos /dev/sdb3 #將新的物理卷加入到卷組中(vg=centos)
  Volume group "centos" successfully extended
[root@bigdata-senior01 ~]# vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               33.99 GiB
  PE Size               4.00 MiB
  Total PE              8702
  Alloc PE / Size       4863 / <19.00 GiB
  Free PE / Size       3839 / <15.00 GiB #空余的加入了
  VG UUID               Gzx0ol-t83a-Sfq3-ydPQ-Kddb-2ry6-71u7Ih

6.4、擴展邏輯卷

[root@bigdata-senior01 ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                sKV6uz-b4Zh-gf5W-x09d-HaX1-Up0X-rdvYeg
  LV Write Access        read/write
  LV Creation host, time localhost, 2018-04-29 17:25:31 +0800
  LV Status              available
  # open                 2
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                81HqZi-xFgr-Z9a6-jX5p-KVI6-Oahi-JXNEoT
  LV Write Access        read/write
  LV Creation host, time localhost, 2018-04-29 17:25:32 +0800
  LV Status              available
  # open                 1
  LV Size                <17.00 GiB
  Current LE             4351
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
[root@bigdata-senior01 ~]# lvextend -l +100%free /dev/mapper/centos-root
  Size of logical volume centos/root changed from <17.00 GiB (4351 extents) to 31.99 GiB (8190 extents).
  Logical volume centos/root successfully resized.
[root@bigdata-senior01 ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                sKV6uz-b4Zh-gf5W-x09d-HaX1-Up0X-rdvYeg
  LV Write Access        read/write
  LV Creation host, time localhost, 2018-04-29 17:25:31 +0800
  LV Status              available
  # open                 2
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                81HqZi-xFgr-Z9a6-jX5p-KVI6-Oahi-JXNEoT
  LV Write Access        read/write
  LV Creation host, time localhost, 2018-04-29 17:25:32 +0800
  LV Status              available
  # open                 1
  LV Size                31.99 GiB
  Current LE             8190
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
#檢查一下分區文件系統
[root@bigdata-senior01 ~]# blkid /dev/mapper/centos-root
/dev/mapper/centos-root: UUID="07a00359-cd62-458a-a150-f2d4a367abe3" TYPE="xfs"
#使用xfs分區擴展命令擴展xfs分區系統,ext4分區使用resize2fs命令
[root@bigdata-senior01 ~]# xfs_growfs /dev/mapper/centos- centos-root centos-swap [root@bigdata-senior01 ~]# xfs_growfs /dev/mapper/centos-root #ext4用resize2fs meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=1113856 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0 spinodes=0 data = bsize=4096 blocks=4455424, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 4455424 to 8386560 [root@bigdata-senior01 ~]# df -h 文件系統 容量 已用 可用 已用% 掛載點 /dev/mapper/centos-root 32G 4.2G 28G 13% / devtmpfs 901M 0 901M 0% /dev tmpfs 912M 0 912M 0% /dev/shm tmpfs 912M 8.6M 904M 1% /run tmpfs 912M 0 912M 0% /sys/fs/cgroup /dev/sda1 1014M 143M 872M 15% /boot /dev/sdb1 2.0G 67M 2.0G 4% /backup tmpfs 183M 0 183M 0% /run/user/1004

17+15=32,擴展完成。

警告:在生產環境中,盡量不要把其他塊硬盤擴充到根里,每加一塊硬盤到根目錄相當於增加了50%硬盤故障風險,得不償失。目前在xfs分區格式下,文件系統無法縮小,

如果第二塊硬盤損壞,可能會導致整個系統的崩潰,解決起來也非常麻煩。

6.5、總結一下擴展卷

1)fdisk /dev/sdb 分區,設置成LVM分區格式,使用partprobe,確保分區有效
2)pvcreate /dev/sdb3,創建物理卷
3)vgextend VolGroup /dev/sdb3,擴展卷組
4)lvextend -l +100%free /dev/mapper/centos-root,擴展邏輯卷
5)xfs_growfs /dev/mapper/centos-root,擴展文件系統
6)df -h,檢查是否擴展完成

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM