linux新增新磁碟和建立分割槽

來朵小紅花發表於2020-12-03

Linux磁碟概念及其管理工具fdisk:http://www.linuxidc.com/Linux/2016-08/134664.htm
一、
進入linux虛擬機器 右鍵 open in terminal
su 輸入密碼切換為root使用者
fdisk -l 檢視磁碟情況
我的目前只有sda一個磁碟
新增sdb磁碟:關機,選單欄中找到虛擬機器—設定,點選硬碟—新增,按步驟走就可以了,啟動虛擬機器,就有sdb了

二、
雖然硬碟分割槽表中最多能儲存四個分割槽,但我們實際使用時一般只分為兩個分割槽,一個是主分割槽(Primary Partion)一個是擴充套件分割槽(extended partition)兩種,主分割槽可以馬上被使用但不能再分割槽,擴充套件分割槽必須再進行分割槽後才能使用,也就是說它必須還要進行二次分割槽。那麼由擴充分割槽再分下去的是什麼呢?它就是邏輯分割槽(Logical Partion),況且邏輯分割槽沒有數量上限制。 對習慣於使用Dos或Windows的朋友來說,有幾個分割槽就有幾個驅動器,並且每個分割槽都會獲得一個字母識別符號,然後就可以選用這個字母來指定在這個分割槽上的檔案和目錄,它們的檔案結構都是獨立的,非常好理解。
但是初上手Red Hat Linux嗎,可就有點惱人了。因為對Linux使用者來說無論有幾個分割槽,分給哪一目錄使用,它歸根結底就只有一個根目錄,一個獨立且唯一的檔案結構。Red Hat Linux中每個分割槽都是用來組成整個檔案系統的一部分,因為它採用了一種叫“掛載點”的處理方法,它的整個檔案系統中包含了一整套的檔案和目錄,且將一個分割槽和一個目錄聯絡起來。這時要載入的一個分割槽將使它的儲存空間在一個目錄下獲得。

1、 fdisk -l 檢視磁碟情況
這裡寫圖片描述

2、 下面對/dev/sdb 進行分割槽:

[root@localhost Desktop]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x1949791c.
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): m //輸入m檢視幫助文件
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
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)
#fdisk選中/dev/sdb 輸入m所有基本選項都出現,輸入n新建分割槽
Command (m for help): n
Command action
e extended //擴充套件分割槽
p primary partition (1-4) //主分割槽
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
29
30
31
32
33
34
35
36
37
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
29
30
31
32
33
34
35
36
37
3、有擴充套件分割槽和主分割槽,邏輯分割槽在擴充套件分割槽中建立。注意到括號中的1-4,最多隻能建四個主分割槽(包括擴充套件分割槽)。先建一個主分割槽:

#繼續上面的操作
Command (m for help): n
Command action
e extended //擴充套件分割槽
p primary partition (1-4) //主分割槽

p #輸入p建立主分割槽
Partition number (1-4): 1 #分割槽號為1
First cylinder (1-2610, default 1): #直接回車預設從第一個柱面開始劃分
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +2G
#加空間大小,這裡有很多種選擇:+後面單位可以接M,G,K(記得要大寫)表示劃分你所加的空間,也可以是柱面數。不管怎樣都不能超過該磁碟剩餘的空間否則無效。

Command (m for help): p #分好後檢視分割槽資訊,剛所做的所有一目瞭然。

Disk /dev/sdb: 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: 0x1949791c

Device Boot Start End Blocks Id System
/dev/sdb1 1 262 2104483+ 83 Linux
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
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
4、同上所述建立擴充套件分割槽:

Command (m for help): n
Command action
e extended
p primary partition (1-4)
e
Partition number (1-4): 4
First cylinder (263-2610, default 263):
Using default value 263
Last cylinder, +cylinders or +size{K,M,G} (263-2610, default 2610): +4G

Command (m for help): p

Disk /dev/sdb: 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: 0x1949791c

Device Boot Start End Blocks Id System
/dev/sdb1 1 262 2104483+ 83 Linux
/dev/sdb4 263 785 4200997+ 5 Extended
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
5、擴充套件分割槽建好就可以在擴充套件分割槽建立邏輯分割槽了

Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l #建立邏輯分割槽
First cylinder (263-785, default 263):
Using default value 263
Last cylinder, +cylinders or +size{K,M,G} (263-785, default 785): +2G

Command (m for help): p

Disk /dev/sdb: 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: 0x1949791c

Device Boot Start End Blocks Id System
/dev/sdb1 1 262 2104483+ 83 Linux
/dev/sdb4 263 785 4200997+ 5 Extended
/dev/sdb5 263 524 2104483+ 83 Linux
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
6、上面顯示已經建好一個主分割槽,一個邏輯分割槽,但是這些現在還沒有生效,需要儲存退出。

Command (m for help): w #儲存退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
1
2
3
4
5
6
1
2
3
4
5
6
7、退出後檢視:

[root@localhost Desktop]# fdisk -l

Disk /dev/sda: 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: 0x00053377

Device Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 39 2358 18631680 83 Linux
/dev/sda3 2358 2611 2031616 82 Linux swap / Solaris

Disk /dev/sdb: 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: 0x1949791c

Device Boot Start End Blocks Id System
/dev/sdb1 1 262 2104483+ 83 Linux
/dev/sdb4 263 785 4200997+ 5 Extended
/dev/sdb5 263 524 2104483+ 83 Linux
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
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
8、這時需要給它設定檔案系統並進行格式化。
mkfs -t ext3 /dev/sdb5
給它設定ext3的檔案系統,並格式化。

[root@localhost Desktop]# mkfs -t ext3 /dev/sdb5
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
131648 inodes, 526120 blocks
26306 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=541065216
17 block groups
32768 blocks per group, 32768 fragments per group
7744 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912

Writing inode tables: done
Creating journal (16384 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
9、在新建個目錄,mkdir /data15 ;
用來掛載這個分割槽。mount /dev/sdb5 /data15 ;

[root@localhost Desktop]# mkdir /data15;
[root@localhost Desktop]# mount /dev/sdb5 /data15;
1
2
3
1
2
3
10、檢視是否掛載成功:df -TH /data15/

[root@localhost Desktop]# df -TH /data15/
Filesystem Type Size Used Avail Use% Mounted on
/dev/sdb5 ext3 2.2G 71M 2.0G 4% /data15
1
2
3
4
1
2
3
4
11、這樣就可以正常使用了。可是重啟之後又要手動掛載怎麼辦?
很簡單,我們只要配置一下就OK了。
vim /etc/fstab
編輯檔案 將/dev/sdb5 /data15 ext3 defaults 0 0加入即可

/etc/fstab

Created by anaconda on Mon Oct 24 02:05:13 2016

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=147a6e0f-8fa6-4bb5-a27a-91aaa7cf178f / ext4 defaults 1 1
UUID=410da4b5-ac87-43e3-ab4a-b33df31e38cf /boot ext4 defaults 1 2
UUID=d0c4ed83-d5d3-4245-af14-1434f4b3438f swap swap defaults 0 0
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
/dev/sdb5 /data15 ext3 defaults 0 0
~
~
~
~
“/etc/fstab” 16L, 841C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
【o】 英文小寫字母o,在目前游標所在行的下一行處插入新的一行並開始插入
【O】 英文大寫字母O,在目前游標所在行的上一行處插入新的一行並開始插入
:wq儲存退出

11、檢視

[root@localhost Desktop]# ls /data*
/data15:
lost+found
1
2
3
4
1
2
3
4
重複以上操作,對sdb1進行掛載

[root@localhost Desktop]# mkfs -t ext3 /dev/sdb1
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
131648 inodes, 526120 blocks
26306 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=541065216
17 block groups
32768 blocks per group, 32768 fragments per group
7744 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912

Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

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

[root@localhost Desktop]# mount /dev/sdb1 /data5

[root@localhost Desktop]# df -TH /data5/
Filesystem Type Size Used Avail Use% Mounted on
/dev/sdb1 ext3 2.2G 71M 2.0G 4% /data5
[root@localhost Desktop]# vim /etc/fstab
[root@localhost Desktop]# ls /data*
/data15:
lost+found

/data5:
lost+found
[root@localhost Desktop]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 18G 4.0G 13G 24% /
tmpfs 491M 372K 491M 1% /dev/shm
/dev/sda1 291M 34M 242M 13% /boot
/dev/sdb5 2.0G 68M 1.9G 4% /data15
/dev/sdb1 2.0G 68M 1.9G 4% /data5
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
同上,又建立了一個邏輯分割槽

[root@localhost Desktop]# fdisk /dev/sdb

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): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
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)

Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (525-785, default 525):
Using default value 525
Last cylinder, +cylinders or +size{K,M,G} (525-785, default 785): 4G
Value out of range.
Last cylinder, +cylinders or +size{K,M,G} (525-785, default 785): 3G
Value out of range.
Last cylinder, +cylinders or +size{K,M,G} (525-785, default 785): 1G
Value out of range.
Last cylinder, +cylinders or +size{K,M,G} (525-785, default 785):
Using default value 785

Command (m for help): p

Disk /dev/sdb: 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: 0x1949791c

Device Boot Start End Blocks Id System
/dev/sdb1 1 262 2104483+ 83 Linux
/dev/sdb4 263 785 4200997+ 5 Extended
/dev/sdb5 263 524 2104483+ 83 Linux
/dev/sdb6 525 785 2096451 83 Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
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)
Syncing disks.
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
附:1. linux下分割槽大小調整

  1. linux建立邏輯卷及擴充套件

相關文章