本文以純代碼形式書寫,請各位同仁在閱讀時抓住帶顏色字體的位置。這才是重中之重!!
(小編也不知道使用代碼稱呼呢還是用指令稱呼 O(∩_∩)O哈哈~)
[root@localhost ~]fdisk -l 查看磁盤信息
Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 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: 0x000b4990
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 62914559 30407680 8e Linux LVM
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: 0x24dd6e32
Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 83 Linux
/dev/sdb2 10487808 18876415 4194304 83 Linux
Disk /dev/sdc: 8589 MB, 8589934592 bytes, 16777216 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
這是我剛分區的一快硬盤8G 。 簡單講述一下硬盤命名規則: sda 是指第一塊一盤。 sdb是指第二塊一盤。sdc則是第三塊一盤。以此類推。。。
拓展知識: 假設硬盤分區名稱是: sdb1 ,是指第二塊硬盤分區序號為1。 sdb3則是分區序號為3 。 並不能直接判斷是第三塊分區。
現在需要對這塊硬盤進行分區:
[root@localhost ~]fdisk /dev/sdc
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 0xbb8b6c44.
Command (m for help): n 按 “n” 進入分區模式 。 (非專業術語)
Partition type:
p primary (0 primary, 0 extended, 4 free) 按 "p" 創建主分區
e extended 按 "e" 創建邏輯分區
Select (default p): p
Partition number (1-4, default 1): 1 系統要求您先輸入主分區的編號,主分區的編號范圍是1~4。 默認輸出1
First sector (2048-16777215, default 2048): 此處按回車鍵即可
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-16777215, default 16777215): +2G 輸入分區的容量 (一般以G作為單位)
Partition 1 of type Linux and of size 2 GiB is set
Command (m for help): w 在這里可以按 p 查看剛才分區的信息 。 按 w 為保存分區 。
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~] fdisk -l
Device Boot Start End Blocks Id System
/dev/sdc1 2048 4196351 2097152 83 Linux 這是剛才分區的信息。
Disk /dev/mapper/centos-root: 29.0 GB, 28982640640 bytes, 56606720 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
上述步驟只是對硬盤進行分區,新分區信息系統無法自動化同步給Linux內核需要手動操作。
[root@localhost ~]file /dev/sbc1 這個指令的意思呢 (個人理解):查看分區是否存在。
/dev/sbc1: cannot open (No such file or directory)
[root@localhost ~]partprobe 把新分區信息同步到Linux內核,(建議敲打兩次指令,如果還是不行那就重新啟動系統)
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
[root@localhost ~]partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
[root@localhost ~]
新分區進行格式化。
[root@localhost ~]mkfs 這個是格式化分區的指令之一(肯定還有其它的咯) 連續按兩次Tab鍵就會出現下列文件系統格式。
mkfs mkfs.btrfs mkfs.cramfs mkfs.ext2 mkfs.ext3 mkfs.ext4 mkfs.minix mkfs.xfs 這個是文件系統的格式。(自己想要什么樣的格式,就選擇什么樣的格式)
[root@localhost ~]mkfs.ext4 /dev/sdc1 mkfs使用的語法
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: 此處按回車鍵。。。。
done
[root@localhost ~]
終於到了掛載的時候了。。 。 (好心塞!用的還是午休的時間 ┭┮﹏┭┮)
簡單理解點呢就是:先建立一個目錄文件夾(必須是空目錄)。這個文件夾的作用是用來關聯掛載。變相的來說也就是一個中轉站。
本文以 /.newFS 隱藏目錄作為實驗。(不一定要跟俺一樣哈)
[root@localhost ~]mount /dev/sdc1 /.newFS/
[root@localhost ~] 就是這個樣子。 可以嘗試復制一些目錄到這個 /.newFS
[root@localhost ~]cp -pr /etc/* /.newFS
[root@localhost /.newFS]ls -l /.newFS
total 1336
-rw-r--r--. 1 root root 16 Jan 25 08:11 adjtime
-rw-r--r--. 1 root root 1518 Jan 25 08:11 aliases
-rw-r--r--. 1 root root 12288 Jan 25 08:11 aliases.db
drwxr-xr-x. 2 root root 4096 Jan 25 08:11 alternatives
-rw-------. 1 root root 541 Jan 25 08:11 anacrontab
-rw-r--r--. 1 root root 55 Jan 25 08:11 asound.conf
drwxr-x---. 3 root root 4096 Jan 25 08:11 audisp
drwxr-x---. 3 root root 4096 Jan 25 08:11 audit
drwxr-xr-x. 2 root root 4096 Jan 25 08:11 bash_completion.d
-rw-r--r--. 1 root root 2853 Jan 25 08:11 bashrc
drwxr-xr-x. 2 root root 4096 Jan 25 08:11 binfmt.d
-rw-r--r--. 1 root root 38 Jan 25 08:11 centos-release
-rw-r--r--. 1 root root 51 Jan 25 08:11 centos-release-upstream
drwxr-xr-x. 2 root root 4096 Jan 25 08:11 chkconfig.d 省略其它文件..........
為了能夠讓新的交換分區設備在重啟后依然生效,需要按照下面的格式將相關信息寫入到配置文件中,並記得保存。
[root@localhost ~]vi /etc/fstab 編輯配置文件
#
# /etc/fstab
# Created by anaconda on Tue Jan 16 05:39:55 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=96ff1fb8-970f-4804-af36-140cc753ca5d /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
/dev/sdb2 swap swap default 0 0
/dev/sdc1 /.newFS ext4 defaults 0 0 /dev/sdc1(設備) /.newFS(掛載目錄) ext4(文件系統類型) defaults 0 0 (默認值為0)
編輯完后,記得要保存。
OK , 硬盤分區及掛載到此結束。
對swap分區進行擴容。
先對硬盤進行分區(主分區)及同步到Linux內核。(如何分區請看上面的操作。)
swap分區專用的格式化命令mkswap,對新建的主分區進行格式化操作。
[root@localhost ~]free -m 查看swap分區的信息
total used free shared buff/cache available
Mem: 1839 106 1499 8 233 1558
Swap: 6143 0 6143
[root@localhost ~]
[root@localhost ~]mkswap /dev/sdc2 格式化新分區
Setting up swapspace version 1, size = 3145724 KiB
no label, UUID=fee6a4e0-44ae-4818-a49e-be10a5c0ae1a
[root@localhost ~]swapon /dev/sdc2 准備好的swap分區掛在到系統中
[root@localhost ~]free -m
total used free shared buff/cache available
Mem: 1839 109 1497 8 233 1556
Swap: 9215 0 9215 這個位置6143變成了9215。
[root@localhost ~]vi /etc/fstab
/dev/sdc2 swap swap defaults 0 0 swap分區保存
OK,swap分區擴大到此結束。
注:轉載時請標明出處及作者。如果這篇讓您看起來頭疼的文章對您有所幫助,請給個贊。 Thank you!