ubuntu為了增強(qiáng)windows用戶的體驗(yàn),拉攏windows用戶加入到linux陣營,做了一些重要改進(jìn):允許直接在windows下安裝ubuntu,就是運(yùn)行安裝盤中那個(gè)叫做wubi.exe的windows執(zhí)行程序;允許windows用戶在沒有剩余未分區(qū)磁盤空間的情況下,將ubuntu安裝在windows分區(qū)的一個(gè)文件內(nèi)(類似vmware的虛擬硬盤)。
當(dāng)初直接在windows下安裝的,用了一段時(shí)間后,覺得ubuntu8.04很不錯(cuò),就想將它從虛擬文件中搬移出來,直接存放到我新建的分區(qū)下以提高ubuntu的磁盤訪問性能。畢竟虛擬磁盤沒有真正的磁盤快。經(jīng)過了一天的研究,最終搬遷成功了,拿出來和大家一起分享。寫此文的目的不是鼓勵(lì)大家也和我一樣也進(jìn)行類似的搬遷行為,只是和大家一起學(xué)習(xí),更能認(rèn)識(shí)到linux操作系統(tǒng)的一些特長和技術(shù)。
步驟如下:
一、創(chuàng)建新分區(qū)
( 因?yàn)楸救税惭bwindows時(shí)將磁盤的最后50G沒有分區(qū),留作以后它用,所以有剩余空間創(chuàng)建新分區(qū))
創(chuàng)建分區(qū)需要在ubuntu操作系統(tǒng)下進(jìn)行,命令為:
sudo fdisk /dev/sda
具體操作見下邊:
administrator@administrator-desktop:~$ sudo fdisk /dev/sda
The number of cylinders for this disk is set to 19457.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Command (m for help): p
Disk /dev/sda: 160.0 GB, 160041885696 bytes
255 heads, 63 sectors/track, 19457 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x1e851e84
Device Boot Start End Blocks Id System
/dev/sda1 * 1 1275 10241406 7 HPFS/NTFS
/dev/sda2 1276 18355 137195100 f W95 Ext'd (LBA)
/dev/sda5 1276 2550 10241406 7 HPFS/NTFS
/dev/sda6 2551 6374 30716248+ 7 HPFS/NTFS
/dev/sda7 6375 8924 20482843+ b W95 FAT32
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (10750-18355, default 10750):
Using default value 10750
Last cylinder or +size or +sizeM or +sizeK (10750-18355, default 18355): +14G
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.
Syncing disks.
administrator@administrator-desktop:~$
說明:
進(jìn)入fdisk后,出現(xiàn)提示符Command (m for help):要求輸入命令,常用命令如下:
p 顯示已有分區(qū)
n 創(chuàng)建新分區(qū)
m 顯示幫助
w 將改動(dòng)寫入磁盤
二、格式化新建分區(qū)
創(chuàng)建分區(qū)后使用fdisk 的p命令再次顯示分區(qū)情況,如下:
/dev/sda1 * 1 1275 10241406 7 HPFS/NTFS
/dev/sda2 1276 18355 137195100 f W95 Ext'd (LBA)
/dev/sda5 1276 2550 10241406 7 HPFS/NTFS
/dev/sda6 2551 6374 30716248+ 7 HPFS/NTFS
/dev/sda7 6375 8924 20482843+ b W95 FAT32
/dev/sda8 8925 10749 14659281 83 Linux
可以看到新分區(qū)的設(shè)備號(hào)為/dev/sda8 ,格式化它!命令為 sudo mkfs -j /dev/sda8
格式化完成后重啟一次。三、拷貝ubuntu操作系統(tǒng)的文件到新分區(qū).
以下操作在維護(hù)模式下進(jìn)行,就是進(jìn)入ubuntu系統(tǒng)時(shí)在grub下選擇recovery mode。本人在一般模式下試過,拷貝某些目錄時(shí)報(bào)了一些錯(cuò)誤,但是沒有驗(yàn)證這些錯(cuò)誤對搬遷后的系統(tǒng)有沒有影響,就直接進(jìn)入了維護(hù)模式進(jìn)行搬遷。
我們看一下都有那些文件和目錄需要拷貝,執(zhí)行命令ls -l /,顯示如下:
drwxr-xr-x 2 root root 4096 2008-06-06 08:48 bin
drwxrwxrwx 3 root root 4096 2008-06-17 09:00 boot
lrwxrwxrwx 1 root root 11 2008-06-13 18:09 cdrom -> media/cdrom
drwxr-xr-x 13 root root 14420 2008-06-17 08:29 dev
drwxr-xr-x 133 root root 12288 2008-06-17 10:18 etc
drwxr-xr-x 3 root root 4096 2008-04-28 16:51 home
drwxrwxrwx 2 root root 4096 2008-06-16 17:10 host
drwxr-xr-x 2 root root 4096 2008-04-23 01:48 initrd
lrwxrwxrwx 1 root root 33 2008-06-13 18:11 initrd.img -> boot/initrd.img-2.6.24-19-generic
drwxr-xr-x 16 root root 4096 2008-06-11 15:01 lib
drwx------ 2 root root 16384 2008-06-13 18:08 lost+found
drwxr-xr-x 5 root root 4096 2008-06-17 10:18 media
drwxr-xr-x 2 root root 4096 2008-06-13 18:12 mnt
drwxr-xr-x 5 root root 4096 2008-05-22 17:50 opt
drwxr-xr-x 2 oracle dba 4096 2008-05-05 09:45 oracle
dr-xr-xr-x 137 root root 0 2008-06-17 16:29 proc
drwxr-xr-x 18 root root 4096 2008-06-13 17:57 root
drwxr-xr-x 2 root root 4096 2008-06-13 18:55 sbin
drwxr-xr-x 2 root root 4096 2008-04-23 01:48 srv
drwxr-xr-x 12 root root 0 2008-06-17 16:29 sys
drwxrwxrwt 16 root root 4096 2008-06-17 10:55 tmp
drwxr-xr-x 13 root root 4096 2008-05-05 09:05 usr
drwxr-xr-x 15 root root 4096 2008-04-23 02:07 var
lrwxrwxrwx 1 root root 30 2008-06-13 18:13 vmlinuz -> boot/vmlinuz-2.6.24-19-generic
上邊列出的文件和目錄中,以下幾個(gè)有特殊的用途,可以無須拷貝:
host目錄 host目錄實(shí)際上映射的是一個(gè)windows分區(qū) ,這個(gè)分區(qū)下存放著ubuntu的虛擬磁盤文件。
lost+found 這個(gè)和文件系統(tǒng)維護(hù)有關(guān),其下存放的是系統(tǒng)非法關(guān)閉后文件系統(tǒng)丟失的數(shù)據(jù)。
media 其他windows分區(qū)和移動(dòng)磁盤一般映射到這個(gè)目錄
mnt 我們將把新建的分區(qū)mount到這個(gè)目錄
proc 內(nèi)核維護(hù)的一個(gè)虛擬文件系統(tǒng)
下邊我們以歸檔的方式拷貝所有其他文件和目錄. 下邊命令的參數(shù)-a表示歸檔方式。歸檔方式拷貝時(shí)會(huì)保留文件和目錄的所有屬性。
sudo mount /dev/sda8 /mnt
sudo cp -a /bin /mnt
sudo cp -a /boot /mnt
sudo cp -a /cdrom /mnt
sudo cp -a /dev /mnt
sudo cp -a /etc /mnt
sudo cp -a /home /mnt
sudo cp -a /initrd /mnt
sudo cp -a /initrd.img /mnt
sudo cp -a /lib /mnt
sudo cp -a /opt /mnt
sudo cp -a /oracle /mnt
sudo cp -a /root /mnt
sudo cp -a /sbin /mnt
sudo cp -a /srv /mnt
sudo cp -a /sys /mnt
sudo cp -a /tmp /mnt
sudo cp -a /usr /mnt
sudo cp -a /var /mnt
sudo cp -a /vmlinuz /mnt
sudo mkdir /mnt/media
sudo mkdir /mnt/mnt
sudo mkdir /mnt/proc
四、修改grub的配置信息
grub配置文件為/boot/grub/menu.lst ,新搬遷的系統(tǒng) 為 /mnt/boot/grub/menu.lst。
(如果你想溝仔對豾indows下的ubuntu,就修改/mnt/boot/grub/menu.lst文件,并在卸載后在windows下安裝grub for dos)
這兩個(gè)文件的主要內(nèi)容為:
title Ubuntu 8.04.1, kernel 2.6.24-19-generic
root (hd0,5)/ubuntu/disks
kernel /boot/vmlinuz-2.6.24-19-generic root=UUID=8200D1BB00D1B67F loop=/ubuntu/disks/root.disk ro quiet splash
initrd /boot/initrd.img-2.6.24-19-generic
將其改為
title Ubuntu 8.04.1, kernel 2.6.24-19-generic
root (hd0,7)
kernel /boot/vmlinuz-2.6.24-19-generic root=UUID=b484f1e0-dc6c-40bf-9d0c-8a7bd9e080ac ro quiet splash
initrd /boot/initrd.img-2.6.24-19-generic
其中(hd0,7)新建對應(yīng)新建的分區(qū)/dev/sda8; b484f1e0-dc6c-40bf-9d0c-8a7bd9e080ac表示新建分區(qū)的UUID編號(hào).
另外可以看到刪除了loop=/ubuntu/disks/root.disk,這句話指出了虛擬磁盤的文件名。搬遷后的系統(tǒng)不再使用虛擬文件,所以刪除它。
分區(qū)的UUID編號(hào)可以通過以下方式查看: ls -l /dev/disk/by-uuid/
lrwxrwxrwx 1 root root 10 2008-06-17 16:29 1020A12220A11030 -> ../../sda1
lrwxrwxrwx 1 root root 10 2008-06-17 16:29 2E207485207455B5 -> ../../sda5
lrwxrwxrwx 1 root root 10 2008-06-17 16:29 5016-D49C -> ../../sda7
lrwxrwxrwx 1 root root 10 2008-06-17 16:29 8200D1BB00D1B67F -> ../../sda6
lrwxrwxrwx 1 root root 10 2008-06-17 16:29 b484f1e0-dc6c-40bf-9d0c-8a7bd9e080ac -> ../../sda8
五、修改fstab文件
文件路徑為:/mnt/etc/fstab
將含root.disk和swap.disk的兩行分別改動(dòng)如下:
/dev/sda8 / ext3 defaults 0 0
/host/swap.disk none swap loop,sw 0 0
上邊的第二行表示swap文件,我們需要將swap也搬遷過來,命令如下:
sudo mkdir /mnt/host
sudo cp -a /host/ubuntu/disks/swap.disk /mnt/host
至此,所有搬遷工作結(jié)束了,重新啟動(dòng),試試看能進(jìn)入新系統(tǒng)嗎?
當(dāng)初直接在windows下安裝的,用了一段時(shí)間后,覺得ubuntu8.04很不錯(cuò),就想將它從虛擬文件中搬移出來,直接存放到我新建的分區(qū)下以提高ubuntu的磁盤訪問性能。畢竟虛擬磁盤沒有真正的磁盤快。經(jīng)過了一天的研究,最終搬遷成功了,拿出來和大家一起分享。寫此文的目的不是鼓勵(lì)大家也和我一樣也進(jìn)行類似的搬遷行為,只是和大家一起學(xué)習(xí),更能認(rèn)識(shí)到linux操作系統(tǒng)的一些特長和技術(shù)。
步驟如下:
一、創(chuàng)建新分區(qū)
( 因?yàn)楸救税惭bwindows時(shí)將磁盤的最后50G沒有分區(qū),留作以后它用,所以有剩余空間創(chuàng)建新分區(qū))
創(chuàng)建分區(qū)需要在ubuntu操作系統(tǒng)下進(jìn)行,命令為:
sudo fdisk /dev/sda
具體操作見下邊:
administrator@administrator-desktop:~$ sudo fdisk /dev/sda
The number of cylinders for this disk is set to 19457.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Command (m for help): p
Disk /dev/sda: 160.0 GB, 160041885696 bytes
255 heads, 63 sectors/track, 19457 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x1e851e84
Device Boot Start End Blocks Id System
/dev/sda1 * 1 1275 10241406 7 HPFS/NTFS
/dev/sda2 1276 18355 137195100 f W95 Ext'd (LBA)
/dev/sda5 1276 2550 10241406 7 HPFS/NTFS
/dev/sda6 2551 6374 30716248+ 7 HPFS/NTFS
/dev/sda7 6375 8924 20482843+ b W95 FAT32
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (10750-18355, default 10750):
Using default value 10750
Last cylinder or +size or +sizeM or +sizeK (10750-18355, default 18355): +14G
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.
Syncing disks.
administrator@administrator-desktop:~$
說明:
進(jìn)入fdisk后,出現(xiàn)提示符Command (m for help):要求輸入命令,常用命令如下:
p 顯示已有分區(qū)
n 創(chuàng)建新分區(qū)
m 顯示幫助
w 將改動(dòng)寫入磁盤
二、格式化新建分區(qū)
創(chuàng)建分區(qū)后使用fdisk 的p命令再次顯示分區(qū)情況,如下:
/dev/sda1 * 1 1275 10241406 7 HPFS/NTFS
/dev/sda2 1276 18355 137195100 f W95 Ext'd (LBA)
/dev/sda5 1276 2550 10241406 7 HPFS/NTFS
/dev/sda6 2551 6374 30716248+ 7 HPFS/NTFS
/dev/sda7 6375 8924 20482843+ b W95 FAT32
/dev/sda8 8925 10749 14659281 83 Linux
可以看到新分區(qū)的設(shè)備號(hào)為/dev/sda8 ,格式化它!命令為 sudo mkfs -j /dev/sda8
格式化完成后重啟一次。三、拷貝ubuntu操作系統(tǒng)的文件到新分區(qū).
以下操作在維護(hù)模式下進(jìn)行,就是進(jìn)入ubuntu系統(tǒng)時(shí)在grub下選擇recovery mode。本人在一般模式下試過,拷貝某些目錄時(shí)報(bào)了一些錯(cuò)誤,但是沒有驗(yàn)證這些錯(cuò)誤對搬遷后的系統(tǒng)有沒有影響,就直接進(jìn)入了維護(hù)模式進(jìn)行搬遷。
我們看一下都有那些文件和目錄需要拷貝,執(zhí)行命令ls -l /,顯示如下:
drwxr-xr-x 2 root root 4096 2008-06-06 08:48 bin
drwxrwxrwx 3 root root 4096 2008-06-17 09:00 boot
lrwxrwxrwx 1 root root 11 2008-06-13 18:09 cdrom -> media/cdrom
drwxr-xr-x 13 root root 14420 2008-06-17 08:29 dev
drwxr-xr-x 133 root root 12288 2008-06-17 10:18 etc
drwxr-xr-x 3 root root 4096 2008-04-28 16:51 home
drwxrwxrwx 2 root root 4096 2008-06-16 17:10 host
drwxr-xr-x 2 root root 4096 2008-04-23 01:48 initrd
lrwxrwxrwx 1 root root 33 2008-06-13 18:11 initrd.img -> boot/initrd.img-2.6.24-19-generic
drwxr-xr-x 16 root root 4096 2008-06-11 15:01 lib
drwx------ 2 root root 16384 2008-06-13 18:08 lost+found
drwxr-xr-x 5 root root 4096 2008-06-17 10:18 media
drwxr-xr-x 2 root root 4096 2008-06-13 18:12 mnt
drwxr-xr-x 5 root root 4096 2008-05-22 17:50 opt
drwxr-xr-x 2 oracle dba 4096 2008-05-05 09:45 oracle
dr-xr-xr-x 137 root root 0 2008-06-17 16:29 proc
drwxr-xr-x 18 root root 4096 2008-06-13 17:57 root
drwxr-xr-x 2 root root 4096 2008-06-13 18:55 sbin
drwxr-xr-x 2 root root 4096 2008-04-23 01:48 srv
drwxr-xr-x 12 root root 0 2008-06-17 16:29 sys
drwxrwxrwt 16 root root 4096 2008-06-17 10:55 tmp
drwxr-xr-x 13 root root 4096 2008-05-05 09:05 usr
drwxr-xr-x 15 root root 4096 2008-04-23 02:07 var
lrwxrwxrwx 1 root root 30 2008-06-13 18:13 vmlinuz -> boot/vmlinuz-2.6.24-19-generic
上邊列出的文件和目錄中,以下幾個(gè)有特殊的用途,可以無須拷貝:
host目錄 host目錄實(shí)際上映射的是一個(gè)windows分區(qū) ,這個(gè)分區(qū)下存放著ubuntu的虛擬磁盤文件。
lost+found 這個(gè)和文件系統(tǒng)維護(hù)有關(guān),其下存放的是系統(tǒng)非法關(guān)閉后文件系統(tǒng)丟失的數(shù)據(jù)。
media 其他windows分區(qū)和移動(dòng)磁盤一般映射到這個(gè)目錄
mnt 我們將把新建的分區(qū)mount到這個(gè)目錄
proc 內(nèi)核維護(hù)的一個(gè)虛擬文件系統(tǒng)
下邊我們以歸檔的方式拷貝所有其他文件和目錄. 下邊命令的參數(shù)-a表示歸檔方式。歸檔方式拷貝時(shí)會(huì)保留文件和目錄的所有屬性。
sudo mount /dev/sda8 /mnt
sudo cp -a /bin /mnt
sudo cp -a /boot /mnt
sudo cp -a /cdrom /mnt
sudo cp -a /dev /mnt
sudo cp -a /etc /mnt
sudo cp -a /home /mnt
sudo cp -a /initrd /mnt
sudo cp -a /initrd.img /mnt
sudo cp -a /lib /mnt
sudo cp -a /opt /mnt
sudo cp -a /oracle /mnt
sudo cp -a /root /mnt
sudo cp -a /sbin /mnt
sudo cp -a /srv /mnt
sudo cp -a /sys /mnt
sudo cp -a /tmp /mnt
sudo cp -a /usr /mnt
sudo cp -a /var /mnt
sudo cp -a /vmlinuz /mnt
sudo mkdir /mnt/media
sudo mkdir /mnt/mnt
sudo mkdir /mnt/proc
四、修改grub的配置信息
grub配置文件為/boot/grub/menu.lst ,新搬遷的系統(tǒng) 為 /mnt/boot/grub/menu.lst。
(如果你想溝仔對豾indows下的ubuntu,就修改/mnt/boot/grub/menu.lst文件,并在卸載后在windows下安裝grub for dos)
這兩個(gè)文件的主要內(nèi)容為:
title Ubuntu 8.04.1, kernel 2.6.24-19-generic
root (hd0,5)/ubuntu/disks
kernel /boot/vmlinuz-2.6.24-19-generic root=UUID=8200D1BB00D1B67F loop=/ubuntu/disks/root.disk ro quiet splash
initrd /boot/initrd.img-2.6.24-19-generic
將其改為
title Ubuntu 8.04.1, kernel 2.6.24-19-generic
root (hd0,7)
kernel /boot/vmlinuz-2.6.24-19-generic root=UUID=b484f1e0-dc6c-40bf-9d0c-8a7bd9e080ac ro quiet splash
initrd /boot/initrd.img-2.6.24-19-generic
其中(hd0,7)新建對應(yīng)新建的分區(qū)/dev/sda8; b484f1e0-dc6c-40bf-9d0c-8a7bd9e080ac表示新建分區(qū)的UUID編號(hào).
另外可以看到刪除了loop=/ubuntu/disks/root.disk,這句話指出了虛擬磁盤的文件名。搬遷后的系統(tǒng)不再使用虛擬文件,所以刪除它。
分區(qū)的UUID編號(hào)可以通過以下方式查看: ls -l /dev/disk/by-uuid/
lrwxrwxrwx 1 root root 10 2008-06-17 16:29 1020A12220A11030 -> ../../sda1
lrwxrwxrwx 1 root root 10 2008-06-17 16:29 2E207485207455B5 -> ../../sda5
lrwxrwxrwx 1 root root 10 2008-06-17 16:29 5016-D49C -> ../../sda7
lrwxrwxrwx 1 root root 10 2008-06-17 16:29 8200D1BB00D1B67F -> ../../sda6
lrwxrwxrwx 1 root root 10 2008-06-17 16:29 b484f1e0-dc6c-40bf-9d0c-8a7bd9e080ac -> ../../sda8
五、修改fstab文件
文件路徑為:/mnt/etc/fstab
將含root.disk和swap.disk的兩行分別改動(dòng)如下:
/dev/sda8 / ext3 defaults 0 0
/host/swap.disk none swap loop,sw 0 0
上邊的第二行表示swap文件,我們需要將swap也搬遷過來,命令如下:
sudo mkdir /mnt/host
sudo cp -a /host/ubuntu/disks/swap.disk /mnt/host
至此,所有搬遷工作結(jié)束了,重新啟動(dòng),試試看能進(jìn)入新系統(tǒng)嗎?

