【虚拟机】VirtualBox知识和常用操作方法

勤奋不是嘴上说说而已,而是实际的行动,在勤奋的苦度中持之以恒,永不退却。业精于勤,荒于嬉;行成于思,毁于随。在人生的仕途上,我们毫不迟疑地选择勤奋,她是几乎于世界上一切成就的催产婆。只要我们拥着勤奋去思考,拥着勤奋的手去耕耘,用抱勤奋的心去对待工作,浪迹红尘而坚韧不拔,那么,我们的生命就会绽放火花,让人生的时光更加的闪亮而精彩。

导读:本篇文章讲解 【虚拟机】VirtualBox知识和常用操作方法,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

VirtualBox基础

版权声明:本文为博主原创文章,转载请注明出处,谢谢。http://blog.csdn.net/lukeunique

笔者作为数据库/IT从业者,在学习和工作的测试过程中,越来越频繁的使用虚拟机,针对Oracle虚拟机软件VirtualBox在使用过程中常常遇到的问题和操作,在此进行简单的梳理和总结。

以下为Oracle VirtualBox的官方网站:

https://www.virtualbox.org/

1.下载
可以通过以下的链接进行下载virtualbox的最新版本和历史版本。
https://www.virtualbox.org/wiki/Downloads

因为Extension Pack提供很多增强功能,所以建议一并下载和安转 Oracle VM VirtualBox Extension Pack。

2.安装
图形化界面,不做介绍

3.使用方法
如果有时间建议阅读官方操作手册。
http://www.virtualbox.org/manual/
如果没有那么大的精力,可以查看本篇以后的更新。

VirtualBox磁盘(Disk)介绍

VirtualBox的磁盘格式

VirtualBox上,创建新的磁盘时,有很多的选项,这些选项都代表什么意思?
作为使用者我们该选择哪种格式呢?

参考:
Chapter 5. Virtual storage

https://www.virtualbox.org/manual/ch05.html

创建新的磁盘时,主要有以下的选项:

  • VID (VirtualBox Disk Image) –VirtualBox磁盘映像
  • VMDK (Virtual Machine Disk) –虚拟机磁盘
  • VHD (Virtual Hard Disk) –虚拟硬盘
  • HDD (Parallels Hard Disk) –并口硬盘
  • QED (QEMU enhanced disk) –QEMU增强型磁盘
  • QCOW (QEMU Copy-On-Write) –QEMU写入时复制

这里写图片描述

VDI形式 (VirtualBox),vmdk形式 (VMware)、vhd形式 (Microsoft Virtual PC)、hdd形式 (Parallels)等 其实就是都是虚拟化镜像格式,各个不同的厂家的不同产品的不同文件格式而已。

VDI:
VirtualBox的独自格式,一般在VirtualBox中的处理速度更好。

https://www.virtualbox.org/

VMDK:
VMware虚拟机的格式,兼容性更好,颇为流行,这种文件在很多虚拟机中都可以使用。

http://www.vmware.com/cn

VHD: 微软VirtualPC虚拟机的格式

http://www.microsoft.com/windows/virtual-pc/

HDD: Mac Parallels虚拟机的格式

http://www.parallelsdesktop.cn/

QED/QCOW:
http://wiki.qemu.org/Main_Page

在VirtualBox的使用过程中,应用最广泛的形式是VDI和VMDK,因为VDI的处理速度更好,建议使用VDI。

磁盘的操作(Linux篇)

1.追加一个新的磁盘,用以扩充操作系统的磁盘容量

1.1 创建新的虚拟盘

创建新的虚拟盘

VDI

动态分配

这里写图片描述

这里写图片描述

1.2 Virtualbox的Linux挂载分区

启动系统后,进行如下操作进行挂载分区。

fdisk -l
fdisk /dev/sdb
m
n
p
1
1
w

fdisk -l
mkfs -t ext3 /dev/sdb1
df -h
mkdir /u02
mount /dev/sdb1 /u02
df -h
1.2.1 查看现在系统的磁盘空间
[root@db12102 ~]# fdisk -l
Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        3916    31350847+  8e  Linux LVM

Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdb doesn't contain a valid partition table
1.2.2 创建新分区
[root@db12102 ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.


The number of cylinders for this disk is set to 1044.
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)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

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
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1044, default 1): 1
Last cylinder or +size or +sizeM or +sizeK (1-1044, default 1044): 
Using default value 1044

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@db12102 ~]# fdisk -l

Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        3916    31350847+  8e  Linux LVM

Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1044     8385898+  83  Linux
1.2.3 格式化和挂载新磁盘
[root@db12102 ~]# mkfs -t ext3 /dev/sdb1
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
1048576 inodes, 2096474 blocks
104823 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2147483648
64 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Writing inode tables: done                            
Creating journal (32768 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@db12102 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                       26G  2.6G   22G  11% /
/dev/sda1              99M   13M   82M  14% /boot
tmpfs                1006M     0 1006M   0% /dev/shm
share                  79G   40G   39G  51% /media/sf_share
[root@db12102 ~]# mkdir /u02
[root@db12102 ~]# mount /dev/sdb1 /u02
[root@db12102 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                       26G  2.6G   22G  11% /
/dev/sda1              99M   13M   82M  14% /boot
tmpfs                1006M     0 1006M   0% /dev/shm
share                  79G   40G   39G  51% /media/sf_share
/dev/sdb1             7.9G  147M  7.4G   2% /u02
[root@db12102 ~]# 
1.2.4 修改启动加载文件,使每次开机时都能自动挂载磁盘

在/etc/fstab文件中追加以下内容

/dev/sdb1               /u02                    ext3    defaults        0 0

执行例:

[root@db12102 etc]# more /etc/fstab
/dev/VolGroup00/LogVol00 /                       ext3    defaults        1 1
LABEL=/boot             /boot                   ext3    defaults        1 2
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/VolGroup00/LogVol01 swap                    swap    defaults        0 0
[root@db12102 etc]# vi /etc/fstab
[root@db12102 etc]# more /etc/fstab                                            
/dev/VolGroup00/LogVol00 /                       ext3    defaults        1 1
LABEL=/boot             /boot                   ext3    defaults        1 2
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/VolGroup00/LogVol01 swap                    swap    defaults        0 0

/dev/sdb1               /u02                    ext3    defaults        0 0
[root@db12102 etc]# 

到此,成功把新加的磁盘挂载到/u02路径下。

1.2.5 把/u02的访问权限赋予给Oracle用户。

变更文件夹的所有者和所有组

[root@db12102 /]# chgrp -R dba /u02
[root@db12102 /]# chown oracle /u02
[root@db12102 /]# ls -larth
...
drwxr-xr-x   3 oracle dba  4.0K  4月  4 09:17 u02
...

2.追加一个共享磁盘,用以方便主机和虚拟机数据文件共享

2.1 关闭虚拟机,添加共享文件夹

这里写图片描述

2.1 启动虚拟机,设定权限

查看新追加的共享文件夹

[root@db12102 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                       26G  2.6G   22G  11% /
/dev/sda1              99M   13M   82M  14% /boot
tmpfs                1006M     0 1006M   0% /dev/shm
VBOX_share             79G   40G   39G  51% /media/sf_VBOX_share  新追加的共享文件夹
share                  79G   40G   39G  51% /media/sf_share

如何让Oracle数据库用户操作追加的共享文件夹?

[root@db12102 ~]# ls -la /media/sf_VBOX_share
total 8
drwxrwx--- 1 root vboxsf    0 Jan 15 17:27 .
drwxr-xr-x 4 root root   4096 Jan 15 17:29 ..

[root@db12102 ~]# su - oracle
[oracle@db12102 ~]$ id
uid=54321(oracle) gid=1105(oinstall) groups=1101(oper),1102(backupdba),1103(dgdba),1104(kmdba),1105(oinstall),1106(dba)
[oracle@db12102 ~]$ cd /media/sf_VBOX_share
-bash: cd: /media/sf_VBOX_share: Permission denied ---Oracle数据库用户无法访问共享文件夹

通过usermod命令把新追加的共享文件夹的组赋予Oracle数据库用户(或其他用户)

# usermod
Usage: usermod [options] LOGIN

Options:
  -a, --append                  append the user to the supplemental GROUPS
                                (use only with -G)
  -c, --comment COMMENT         new value of the GECOS field
  -d, --home HOME_DIR           new home directory for the user account
  -e, --expiredate EXPIRE_DATE  set account expiration date to EXPIRE_DATE
  -f, --inactive INACTIVE       set password inactive after expiration
                                to INACTIVE
  -g, --gid GROUP               force use GROUP as new primary group
  -G, --groups GROUPS           new list of supplementary GROUPS
  -h, --help                    display this help message and exit
  -l, --login NEW_LOGIN         new value of the login name
  -L, --lock                    lock the user account
  -m, --move-home               move contents of the home directory to the new
                                location (use only with -d)
  -o, --non-unique              allow using duplicate (non-unique) UID
  -p, --password PASSWORD       use encrypted password for the new password
  -s, --shell SHELL             new login shell for the user account
  -u, --uid UID                 new UID for the user account
  -U, --unlock                  unlock the user account
  -Z, --selinux-user    new selinux user mapping for the user account
[root@db12102 ~]# usermod -a -G vboxsf oracle
[root@db12102 ~]# id oracle
uid=54321(oracle) gid=1105(oinstall) groups=1105(oinstall),500(vboxsf),1101(oper),1102(backupdba),1103(dgdba),1104(kmdba),1106(dba)
[root@db12102 ~]# su - oracle
[oracle@db12102 ~]$ cd /media/sf_VBOX_share
[oracle@db12102 sf_VBOX_share]$ 

通过以上的设定,就可以在主机和虚拟机数据文件方便的共享文件了。

虚拟机系统快照

在做测试时,有可能是破坏性的测试,如果反复重做虚拟机会非常浪费时间,还好,
VirtualBox提供了快照备份和恢复功能,可以在测试前保存一个备份,然后每次做完测试后再恢复到最初的状态去。

生成系统快照的方法

可以通过左上角的【控制】–》【生成备份】来生成系统快照。

这里写图片描述

这里写图片描述

这里写图片描述

恢复为初始状态

当关闭虚拟机时,可以选择【强制退出】-》【恢复到当前备份】‘备份名’,来恢复到初始状态。

这里写图片描述

查看和操作备份的系统快照

可通过VM管理器右上角的
【备份[系统快照]】来查看备份过的系统快照和进行恢复,以及删除等管理操作。

这里写图片描述

参考

官方在线文档:
http://www.virtualbox.org/manual/

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/141632.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!