原创:将通过wubi安装的ubuntu从windows分区下的虚拟磁盘搬迁到真实的分区上

系统安装、升级讨论
版面规则
我们都知道新人的确很菜,也喜欢抱怨,并且带有浓厚的Windows习惯,但既然在这里询问,我们就应该有责任帮助他们解决问题,而不是直接泼冷水、简单的否定或发表对解决问题没有任何帮助的帖子。乐于分享,以人为本,这正是Ubuntu的精神所在。
duanbp
帖子: 21
注册时间: 2008-06-05 16:24

原创:将通过wubi安装的ubuntu从windows分区下的虚拟磁盘搬迁到真实的分区上

#1

帖子 duanbp » 2008-06-17 11:51

ubuntu为了增强windows用户的体验,拉拢windows用户加入到linux阵营,做了一些重要改进:允许直接在windows下安装ubuntu,就是运行安装盘中那个叫做wubi.exe的windows执行程序;允许windows用户在没有剩余未分区磁盘空间的情况下,将ubuntu安装在windows分区的一个文件内(类似vmware的虚拟硬盘)。

我当初也是直接在windows下安装的,用了一段时间后,觉得ubuntu8.04很不错,就想将它从虚拟文件中搬移出来,直接存放到我新建的分区下以提高ubuntu的磁盘访问性能。毕竟虚拟磁盘没有真正的磁盘快。经过了一天的研究,最终搬迁成功了,拿出来和大家一起分享。写此文的目的不是鼓励大家也和我一样也进行类似的搬迁行为,只是和大家一起学习,更能认识到linux操作系统的一些特长和技术。

步骤如下:
一、创建新分区
( 因为本人安装windows时将磁盘的最后50G没有分区,留作以后它用,所以有剩余空间创建新分区)
创建分区需要在ubuntu操作系统下进行,命令为:
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:~$

说明:
进入fdisk后,出现提示符Command (m for help):要求输入命令,常用命令如下:
p 显示已有分区
n 创建新分区
m 显示帮助
w 将改动写入磁盘
二、格式化新建分区
创建分区后使用fdisk 的p命令再次显示分区情况,如下:
/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


可以看到新分区的设备号为/dev/sda8 ,格式化它!命令为 sudo mkfs -j /dev/sda8

格式化完成后重启一次。

三、拷贝ubuntu操作系统的文件到新分区.
以下操作最好在维护模式下进行,就是进入ubuntu系统时在grub下选择recovery mode。本人在一般模式下试过,拷贝某些目录时报了一些错误,但是没有验证这些错误对搬迁后的系统有没有影响,就直接进入了维护模式进行搬迁。
我们看一下都有那些文件和目录需要拷贝,执行命令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

上边列出的文件和目录中,以下几个有特殊的用途,可以无须拷贝:
host目录 host目录实际上映射的是一个windows分区 ,这个分区下存放着ubuntu的虚拟磁盘文件。
lost+found 这个和文件系统维护有关,其下存放的是系统非法关闭后文件系统丢失的数据。
media 其他windows分区和移动磁盘一般映射到这个目录
mnt 我们将把新建的分区mount到这个目录
proc 内核维护的一个虚拟文件系统

下边我们以归档的方式拷贝所有其他文件和目录. 下边命令的参数-a表示归档方式。归档方式拷贝时会保留文件和目录的所有属性。
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 ,新搬迁的系统 为 /mnt/boot/grub/menu.lst。
(如果你想彻底卸载windows下的ubuntu,就修改/mnt/boot/grub/menu.lst文件,并在卸载后在windows下安装grub for dos)

这两个文件的主要内容为:
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)新建对应新建的分区/dev/sda8; b484f1e0-dc6c-40bf-9d0c-8a7bd9e080ac表示新建分区的UUID编号.
另外可以看到删除了loop=/ubuntu/disks/root.disk,这句话指出了虚拟磁盘的文件名。搬迁后的系统不再使用虚拟文件,所以删除它。

分区的UUID编号可以通过以下方式查看: 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的两行分别改动如下:
/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


至此,所有搬迁工作结束了,重新启动,试试看能进入新系统吗?
上次由 duanbp 在 2008-06-17 12:54,总共编辑 1 次。
头像
HuntXu
帖子: 5776
注册时间: 2007-09-29 3:09

#2

帖子 HuntXu » 2008-06-17 12:01

很不错,原创的?继续加油~

一点小意见是个人觉得fstab也用uuid比较好... :lol:
HUNT Unfortunately No Talent...
duanbp
帖子: 21
注册时间: 2008-06-05 16:24

#3

帖子 duanbp » 2008-06-17 12:58

一点小意见是个人觉得fstab也用uuid比较好...
不知道UUID有什么优势?还望指教。
头像
ptptptptptpt
帖子: 3711
注册时间: 2006-09-19 18:16

#4

帖子 ptptptptptpt » 2008-06-18 8:48

好帖!

一点建议:

第三步 拷贝ubuntu操作系统的文件到新分区 ,可以在另外一个系统(比如 live cd )中操作,这样就没什么顾虑,直接全部拷贝即可

需以 loop 方式挂载磁盘映像文件,类似 mount root.disk /temp -o loop

然后 cp -a /temp/* /目标分区/

注意如果不只一个磁盘映像文件(如还有 usr.disk 等),需要分别挂载、拷贝

另外,swap 文件也可以丢弃不用,单独创建 swap 分区,这样迁移得更彻底
头像
ptptptptptpt
帖子: 3711
注册时间: 2006-09-19 18:16

#5

帖子 ptptptptptpt » 2008-06-18 8:48

duanbp 写了:
一点小意见是个人觉得fstab也用uuid比较好...
不知道UUID有什么优势?还望指教。
便于移动使用
头像
HuntXu
帖子: 5776
注册时间: 2007-09-29 3:09

#6

帖子 HuntXu » 2008-06-18 9:17

4楼的建议不错 :lol:

继续支持~
HUNT Unfortunately No Talent...
头像
20sanguo
帖子: 794
注册时间: 2008-05-10 9:53

#7

帖子 20sanguo » 2008-06-18 10:17

分区可以用gparted,直观些,不太容易出错。有图形就不用字符,对一般用户来说比较容易操作。

cp也可以写到一起如 cp -a bin usr lib ... /

请教:cp这种情况应该怎么写就是 cp 除了这个文件夹以外的到 /

可以把操作做成像wubi那样的一个脚本,执行一次就行了。把繁琐的工作交给电脑不就是咱们要得效果?

上面的操作完全可以在正常ubuntu系统中使用,不用进别的模式。

双系统我建议/boot放在外面,这样修复、备份各种操作都方便,因为不是人人都有livecd,出错的时候就会发现好处多多,把备份的复制进去就好了,不用在busybox里搞,一般用户估计都不会用,只能重装了。
头像
beitiaosi
帖子: 118
注册时间: 2008-05-23 13:21

#8

帖子 beitiaosi » 2008-06-23 11:00

试了,无论是正常模式还是recover模式,拷贝文件都有部分错误,启动时进入不了图形界面……
头像
20sanguo
帖子: 794
注册时间: 2008-05-10 9:53

#9

帖子 20sanguo » 2008-06-23 11:29

具体哪些文件错误?进不了图形提示什么?

wubi官方的脚本出了
https://wiki.ubuntu.com/WubiGuide
https://wiki.ubuntu.com/WubiGuide?actio ... -partition

As an alternative, the following script can be used with Wubi 8.04.

Download wubi-move-to-partition

Open a terminal and run:

sudo sh wubi-move-to-partition /dev/sda9 /dev/sda10

Replace /dev/sda9 with the partition where you would like to migrate the Wubi installation to, and /dev/sda10 with the appropriate swap partition (you can omit the second argument completely, in which case no swap will be setup). The two partitions must already exist and be empty (you can use any partitioning tool such as gparted to create them in advance). Note that the script will install grub as main bootloader replacing the existing bootloader, and it may not be easy to undo the changes (if things do not work as expected you will have to boot from a Live CD and replace/edit the bootloader manually). Also note that if you have multiple hard-disks, the disk order might have to be adjusted manually.

有空我在汉化下,呵呵。



看了下这个脚本,不需要汉化了,需要注意的就是:
1、先用你喜欢的工具建两个大小合适的分区。
2、mkfs.ext3 $dev 可以改成你需要的格式
3、没有用cp而用的是rsync同步命令, exclude是忽略参数
4、如果没有修改这个脚本直接运行,bootloader会被修改,有问题后只能用 Live CD,所以之前先备份下。把ubuntu那个目录备份好就行了,刻张盘费不了啥事,swap不用刻,文件还可以压缩。我还是觉得/boot留在win分区里方便些,ubuntu升级很容易搞死img文件。

搞的时候注意些,问题不大。
测试版(包括8.10这样的短支持版都归为测试版)会带来数据丢失、不显示、无法启动、网卡不能、以及无法想象的故障
不能独立解决问题,尽量不用测试版

阿里巴巴的东西我都不用。
头像
ssfjhh
帖子: 1289
注册时间: 2007-11-08 21:23
来自: 中国

#10

帖子 ssfjhh » 2008-06-24 18:02

这个帖子应该置顶呀,怎么那个WUBI的一个小BUG报告都能置顶,这个不能置顶?
这个功能如何实现?
http://yufanyufan.spaces.live.com/blog/ ... !149.entry
要实现的功能为,若当前标签页为空白页,则在当前页打开,否则新建一个标签页。
头像
chdf199736
帖子: 53
注册时间: 2008-06-21 1:55

#11

帖子 chdf199736 » 2008-06-29 1:23

我是菜鸟,在学习。。。。。。。。。。
qiye01
帖子: 29
注册时间: 2008-07-17 9:26

#12

帖子 qiye01 » 2008-07-25 11:47

回贴收藏。谢谢分享!
头像
geye
帖子: 149
注册时间: 2008-09-02 19:16
来自: 家里

#13

帖子 geye » 2008-09-05 2:54

我无法分区阿。问题好多阿。我都说不清了。。
开机..启动..进入..关机..
这是一个无比漫长的过程...
回复