爲虛擬機添加虛擬磁盤

下面主要是關於虛擬磁盤添加到虛擬機以後,如何分區和格式化的過程。android

磁盤分區

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
evans@master: /dev $ sudo fdisk -l
 
Disk /dev/sda : 42.9 GB, 42949672960 bytes
255 heads, 63 sectors /track , 5221 cylinders, total 83886080 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 identifier: 0x000b5cfd
 
    Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048    81788927    40893440   83  Linux
/dev/sda2        81790974    83884031     1046529    5  Extended
/dev/sda5        81790976    83884031     1046528   82  Linux swap / Solaris
 
Disk /dev/sdb : 64.4 GB, 64424509440 bytes
255 heads, 63 sectors /track , 7832 cylinders, total 125829120 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 identifier: 0x00000000
 
Disk /dev/sdb doesn't contain a valid partition table

/dev/sdb 就是新增的磁盤,上面尚未任何分區信息,下面來着手分區。因爲是第二塊磁盤,且個人需求也比較簡單,直接在上面創建一個分區就能夠了。ubuntu

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
evans@master: /dev $ sudo 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 0xf1dfadb5.
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)
 
# 查看已經的分區信息,如今尚未,因此下面的列表也是空的
Command (m for help): p
 
Disk /dev/sdb : 64.4 GB, 64424509440 bytes
255 heads, 63 sectors /track , 7832 cylinders, total 125829120 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 identifier: 0xf1dfadb5
 
    Device Boot      Start         End      Blocks   Id  System
 
# 創建新分區
Command (m for help): n
Partition type :
    p   primary (0 primary, 0 extended, 4 free )
    e   extended
 
# 先了一個主分區,開始和結束的扇區用的都是默認值,直接回車就行了
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-125829119, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-125829119, default 125829119):
Using default value 125829119
 
# 分區創建好後,再次查看分區信息,列表中就多了一項 /dev/sdb1
Command (m for help): p
 
Disk /dev/sdb : 64.4 GB, 64424509440 bytes
255 heads, 63 sectors /track , 7832 cylinders, total 125829120 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 identifier: 0xf1dfadb5
 
    Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   125829119    62913536   83  Linux
 
# 將分區信息寫入磁盤。若是不作這一步就直接退出的話,以上操做將所有無效。
Command (m for help): w
The partition table has been altered!
 
Calling ioctl() to re- read partition table.
Syncing disks.

完成分區操做後,咱們再來看看磁盤上的變化:bash

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
evans@master: /dev $ sudo fdisk -l
 
Disk /dev/sda : 42.9 GB, 42949672960 bytes
255 heads, 63 sectors /track , 5221 cylinders, total 83886080 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 identifier: 0x000b5cfd
 
    Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048    81788927    40893440   83  Linux
/dev/sda2        81790974    83884031     1046529    5  Extended
/dev/sda5        81790976    83884031     1046528   82  Linux swap / Solaris
 
Disk /dev/sdb : 64.4 GB, 64424509440 bytes
128 heads, 39 sectors /track , 25206 cylinders, total 125829120 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 identifier: 0xf1dfadb5
 
    Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   125829119    62913536   83  Linux

最後一行,代表分區信息已經寫入成功。第一將使用 fdisk -l 時,顯示的信息表示 /dev/sdb 上沒有有效的信息,此次有了。分區完成後,接着就開始格式化磁盤。ide

磁盤格式化

查看已有磁盤的格式,爲了保持一致,新磁盤也採用一樣的格式ui

1
2
3
4
5
6
7
evans@master: /dev $ df -T
Filesystem     Type     1K-blocks     Used Available Use% Mounted on
/dev/sda1      ext4      40251776 30332612   7874492  80% /
udev           devtmpfs   2053336        4   2053332   1% /dev
tmpfs          tmpfs       824260      840    823420   1% /run
none           tmpfs         5120        0      5120   0% /run/lock
none           tmpfs      2060648     1412   2059236   1% /run/shm

已有磁盤 /dev/sda1 用的是 ext4 ,新磁盤也採用一樣的格式。spa

1
sudo mkfs -t ext4 /dev/sdb1

格式化完成後,磁盤就準備就緒了。可是這個磁盤必需「加載」後,纔可以被使用。感受就像是,在一個孤島上創建一個房子,但人們卻沒法去使用。加載的過程,就若是是將孤島與大陸鏈接起來。操作系統

加載磁盤

先臨時加載磁盤,很是簡單code

1
2
sudo mkdir /android
sudo mount /dev/sdb1 /android

好了,加載完成。如今,在 /android 目錄下創建的全部文件,都將被存儲在新的磁盤上。可是一旦系統重啓,你又得從新加載一次了。若是以爲麻煩,那麼就往下看看怎麼自動加載。ci

自動加載磁盤

自動加載是指在系統啓動後,由操做系統來自動完成磁盤的加載。你的第一塊磁盤 /dev/sda1 就是這樣被加載的,安裝操做系統時,這一步已經由安裝程序順便作了,如今咱們本身來作。固然,過程也是簡單的,只須要修改一下/etc/fstab 。rem

查看磁盤的UUID

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
evans@master:~$ sudo df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        39G   29G  7.6G  80% /
udev            2.0G   12K  2.0G   1% /dev
tmpfs           805M  844K  805M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            2.0G  1.4M  2.0G   1% /run/shm
/dev/sdb1        60G  180M   56G   1% /home/evans/codebase/android   ## 記下路徑
 
evans@master:~$ sudo blkid
/dev/sda1 : UUID= "6314c61b-4ba2-43b6-933a-8575f69f93b5" TYPE= "ext4"
/dev/sda5 : UUID= "5609fe6e-3a96-4fa5-924e-be18161f439c" TYPE= "swap"
/dev/sdb1 : UUID= "842afa50-6a3c-4910-862d-c662c37bf081" TYPE= "ext4"   ## 記下UUID
 
evans@master:~$ sudo vi /etc/fstab
 
   9 # / was on /dev/sda1 during installation
  10 UUID=6314c61b-4ba2-43b6-933a-8575f69f93b5 /               ext4    errors=remount-ro 0       1
 
# 將第10行復制而後再粘貼,再對其進行修改。
# 須要修改的內容有兩處
# 一處是UUID,用上面的UUID進行替換
# 二是路徑,將 / 替換爲上面的本身的路徑
 
   9 # / was on /dev/sda1 during installation
  10 UUID=6314c61b-4ba2-43b6-933a-8575f69f93b5 /               ext4    errors=remount-ro 0       1
  11
  12 # /home/evans/codebase/android on /dev/sdb1 during installation
  13 UUID=842afa50-6a3c-4910-862d-c662c37bf081 /home/evans/codebase/android               ext4    errors=remount-ro 0       1
相關文章
相關標籤/搜索