1. 程式人生 > >Linux掛載新硬盤和創建Swap分區的方法

Linux掛載新硬盤和創建Swap分區的方法

命令行操作 done 恢復 ext4 cti memory 數據塊 分享圖片 內部

Liunx添加新硬盤其實和Windows的操作一樣,但一個是圖形化操作,另一個是命令行操作,不過步驟是一樣,下面就動手演示和講解

技術分享圖片

Linux掛載新硬盤

1、查看硬盤信息

命令:fdisk -l

[root@center ~]# fdisk -l
Disk /dev/vda: 21.5 GB, 21474836480 bytes                                           #第一塊硬盤的信息和分區信息
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0003a7b4

   Device Boot      Start         End      Blocks   Id  System
     /dev/vda1   *           1        2611    20970496   83  Linux

Disk /dev/vdb: 107.4 GB, 107374182400 bytes                                        #第二塊硬盤的信息和分區信息
16 heads, 63 sectors/track, 208050 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

2、創建新硬盤分區

命令:fdisk /dev/vdb

[root@center ~]# fdisk /dev/vdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x1e694286.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won‘t be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to
         switch off the mode (command ‘c‘) and change display units to
         sectors (command ‘u‘).

Command (m for help): n
Command action
   e   extended                                 #e為創建擴展分區
   p   primary partition (1-4)             #p為創建邏輯分區
p
Partition number (1-4): 1           #在這裏輸入1,就進入劃分邏輯分區階段了;
First cylinder (1-208050, default 1): 1       #分區的Start 值,這裏最好直接按回車,否則可能會造成空間浪費;
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-208050, default 208050): 208050      #分區的Over值,我就分一個區
Using default value 208050

Command (m for help): w                             #最後輸入w回車保存退出。
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

fdisk可以用m命令來看fdisk命令的內部命令;
a:命令指定啟動分區;
d:命令刪除一個存在的分區;
l:命令顯示分區ID號的列表;
m:查看fdisk命令幫助;
n:命令創建一個新分區;
p:命令顯示分區列表;
t:命令修改分區的類型ID號;
w:命令是將對分區表的修改存盤讓它發生作用;

3、確認新分區信息

命令:fdisk -l

[root@center ~]# fdisk -l
Disk /dev/vda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0003a7b4

   Device Boot      Start         End      Blocks   Id  System
     /dev/vda1   *           1        2611    20970496   83  Linux

Disk /dev/vdb: 107.4 GB, 107374182400 bytes
16 heads, 63 sectors/track, 208050 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x1e694286

   Device Boot      Start         End      Blocks   Id  System
     /dev/vdb1               1      208050   104857168+  83  Linux           #剛創建好的分區信息

4、格式化分區

命令:mkfs.ext4 /dev/vdb1

[root@center ~]# mkfs.ext4 /dev/vdb1 
mke2fs 1.41.12 (17-May-2010)
Filesystem label=                   #文件系統標簽
OS type: Linux                 #操作系統類型
Block size=4096 (log=2)          #塊大小
Fragment size=4096 (log=2)      #分塊大小
Stride=0 blocks, Stripe width=0 blocks
6553600 inodes, 26214292 blocks
1310714 blocks (5.00%) reserved for the super user
First data block=0          #第一個數據塊
Maximum filesystem blocks=4294967296
800 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
    4096000, 7962624, 11239424, 20480000, 23887872

Writing inode tables: done                           #寫入inode表
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 26 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

Linux的分區類型介紹:
隨著Linux系統在現在業務中的應用,Linux文件系統的弱點也漸漸顯露出來了,其中系統缺省使用的ext2文件系統是非日誌文件系統。這在關鍵行業的應用是一個致命的弱點.而Ext3文件系統是直接從Ext2文件系統發展而來,目前ext3文件系統已經非常穩定可靠。它完全兼容Ext2文件系統。
Ext3的特點:高可用性、數據的完整性、數據的完整性、數據轉換、多種日誌模式
同樣的Ext4完全兼容Ext3,相對於Ext3來說,Ext4支持更大的存儲,Ext3目前所支持的最大16TB文件系統和最大2TB文件,而Ext4分別支持1EB的文件系統,以及16TB 的文件,還有就是Ext3目前只支持32,000個子目錄,而Ext4支持無限數量的子目錄,Ext4引入了現代文件系統中流行的extents概念,每個 extent 為一組連續的數據塊,上述文件則表示為“該文件數據保存在接下來的25,600個數據塊中”,提高了不少效率。

5、創建掛載目錄

命令:mkdir /data

6、掛載分區

命令:mount /dev/vdb1 /data

7、查看硬盤大小以及掛載分區

命令:df -Th

[root@center ~]# df -Th
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/vda1      ext4    20G  1.1G   18G   6% /
tmpfs          tmpfs  3.9G     0  3.9G   0% /dev/shm
/dev/vdb1      ext4    99G   60M   94G   1% /data           #新掛載的分區

8、配置開機自動掛載

命令:vim /etc/fstab
/dev/vdb1 /data ext4 defaults 1 1

[root@center ~]# blkid
/dev/vda1: UUID="b7aae0d4-268c-4b60-914a-f3b48e22819c" TYPE="ext4" 
/dev/vdb1: UUID="5de835dd-5322-46f0-8728-3d4ae7d83b54" TYPE="ext4" 

[root@center ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue Mar 27 04:51: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
#
UUID=b7aae0d4-268c-4b60-914a-f3b48e22819c      /                       ext4      defaults      1 1
UUID=5de835dd-5322-46f0-8728-3d4ae7d83b54     /data                ext4   defaults    1 1
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0

Swap分區

Swap分區其實和Windows上的虛擬內存一樣,Swap分區在系統的物理內存不夠用的時候,把物理內存中的一部分空間釋放出來,以供當前運行的程序使用。那些被釋放的空間可能來自一些很長時間沒有什麽操作的程序,這些被釋放的空間被臨時保存到Swap分區中,等到那些程序要運行時,再從Swap分區中恢復保存的數據到內存中。

下面介紹兩種創建swap的方法:

  • 新建磁盤分區作為swap分區
  • 用文件作為Swap分區

新建磁盤分區作為swap分區

  1. 用fdisk命令對磁盤進行分區,添加swap分區,新建分區,在fdisk中用“t”命令將新添的分區id改為82
    (Linux swap類型)

  2. 格式化swap分區,這裏的sdb2要看您加完後p命令顯示的實際分區設備名

    mkswap /dev/sdb1

  3. 啟動新的swap分區

    swapon /dev/sdb1

  4. 讓系統啟動時能自動啟用這個交換分區,可以編輯/etc/fstab,加入下面一行

    /dev/sdb1 swap swap defaults 0 0

用文件作為Swap分區

1.創建要作為swap分區的文件:增加1GB大小的交換分區,則命令寫法如下,其中的count等於想要的塊的數量(bs*count=文件大小)。

dd if=/dev/zero of=/root/swapfile bs=1M count=1024

2.格式化為交換分區文件,建立swap的文件系統:

mkswap /root/swapfile

3.啟用交換分區文件:

swapon /root/swapfile

4.使系統開機時自啟用,在文件/etc/fstab中添加一行:

/root/swapfile swap swap defaults 0 0

Linux掛載新硬盤和創建Swap分區的方法