您的位置:首页 > 其它

gem5创建自己的disk image

2017-03-21 21:00 381 查看
之所以跑来学习自己创建disk image,是因为现有的Linux-x86.img,额,太不好用了,问题一直无法解决,所以我就尝试自己来新建disk。

1)创建空白磁盘映像(通常是.img文件),默认是ext2格式化。

zzh@ubuntu14:~/gem5$ util/gem5img.py init ubuntu-14.04.img 4096


这里主要是用到gem5里面的gem5img.py,其中ubuntu-14.04.img是disk的名字,4096是大小,单位为MB。

如若你没有创建环回设备的权限,可能会要输入sudo密码。如出现下图的显示:





2)由于是模拟X86机器,选择下载ubuntu-core-14.04-core-amd64.tar.gz。Ubuntu核心下载链接

3)已创建空白磁盘,我们就需要填充所有的操作系统文件。首先挂载空白磁盘,并将所有文件复制到磁盘上。

zzh@ubuntu14:~/gem5$ mkdir mnt
zzh@ubuntu14:~/gem5$ sudo util/gem5img.py mount ubuntu-14.04.img mnt
%> /sbin/losetup /dev/loop0 ubuntu-14.04.img
%> /sbin/losetup -d /dev/loop0
%> /sbin/losetup -o 32256 /dev/loop0 ubuntu-14.04.img
%> /bin/mount /dev/loop0 mnt


zzh@ubuntu14:~/gem5$ sudo tar xzvf ubuntu-core-14.04-core-amd64.tar.gz -C mnt
zzh@ubuntu14:~/gem5$ sudo cp /etc/resolv.conf mnt/etc/


4)设定gem5特定的文件,创建串行终端。

zzh@ubuntu14:~/gem5$ sudo vim mnt/etc/init/tty-gem5.conf


# ttyS0 - getty
#
#This service maintains a getty on ttyS0 from the point the system is
# started until it is shut down again, unless there is a script passed to gem5.
# If there is a script, the script is executed then simulation is stopped.

start on stopped rc RUNLEVEL=[12345]
stop on runlevel [!12345]

console owner
respawn
script
# Create the serial tty if it doesn't already exist
if [ ! -c /dev/ttyS0  ]
then
mknod /dev/ttyS0 -m 660 /dev/ttyS0 c 4 64
fi

# Try to read in the script from the host system
/sbin/m5 readfile > /tmp/script
chmod 755 /tmp/script
if [ -s /tmp/script  ]
then
# If there is a script, execute the script and then exit the simulation
exec su root -c '/tmp/script' # gives script full privileges as root user in multi-user mode
/sbin/m5 exit
else
# If there is no script, login the root user and drop to a console
# Use m5term to connect to this console
exec /sbin/getty --autologin root -8 38400 ttyS0
fi

end script


默认情况下,gem5使用串行端口允许从主机系统到仿真系统的通信。 要使用它,我们需要创建一个串行tty。由于Ubuntu使用upstart来控制init进程,我们需要向/ etc / init中添加一个文件,该文件将初始化我们的终端。此外,在此文件中,我们将添加一些代码来检测是否有脚本传递到模拟系统。 如果有一个脚本,我们将执行脚本,而不是创建一个终端。

5)安装localhost

zzh@ubuntu14:~/gem5$ sudo vim mnt/etc/hosts


如果我们要使用任何使用它的应用程序,我们还需要设置localhost loopback设备。 为此,我们需要将以下内容添加到/ etc / hosts文件中。

127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts


6)更新fstab

zzh@ubuntu14:~/gem5$ sudo vim mnt/etc/fstab


我们需要在/ etc / fstab中为每个我们希望能够从模拟系统访问的分区创建一个条目。 只有一个分区是绝对必需的(/); 但是,您可能需要添加其他分区,如交换分区。

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system>    <mount point>   <type>  <options>       <dump>  <pass>
/dev/hda1            /              ext3        noatime             0 1


7)将m5二进制文件复制到磁盘

gem5附带一个额外的二进制应用程序,执行伪指令以允许模拟系统与主机系统交互。在gem5/util/m5/目录下运行make -f Makefile.,其中 是您正在模拟的IAS(例如x86),将此文件复制到新创建的磁盘上的/ sbin

zzh@ubuntu14:~/gem5/util/m5$ ls
jni           m5op_alpha.S    m5op.h        m5op_x86.S        Makefile.arm    Makefile.x86
jni_gem5Op.c  m5op_arm_A64.S  m5ops.h       Makefile.aarch64  Makefile.sparc  Makefile.x86.bak
m5.c          m5op_arm.S      m5op_sparc.S  Makefile.alpha    Makefile.thumb


zzh@ubuntu14:~/gem5/util/m5$ make -f Makefile.x86
gcc  -O2 -DM5OP_ADDR=0xFFFF0000 -o m5.o -c m5.c
m5.c: In function ‘read_file’:
m5.c:120:14: warning: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Wunused-result]
write(dest_fid, buf, len);
^
gcc -O2 -DM5OP_ADDR=0xFFFF0000 -o m5op_x86.o -c m5op_x86.S
gcc -o m5 m5.o m5op_x86.o

zzh@ubuntu14:~/gem5$ sudo cp util/m5/Makefile.x86 mnt/sbin/


除非要继续添加更多应用程序或复制其他文件,否则在使用所有gem5特定文件更新磁盘后,请卸载磁盘映像。

8)安装新应用

将新应用程序安装到磁盘上的最简单的方法是使用chroot。 这个程序在逻辑上将根目录(“/”)更改为不同的目录,在这种情况下是mnt。 在您可以更改根之前,您首先必须在新根目录中设置特殊目录。 为此,我们使用mount -o bind。

zzh@ubuntu14:~/gem5$ sudo /bin/mount -o bind /sys mnt/sys
zzh@ubuntu14:~/gem5$ sudo /bin/mount -o bind /dev mnt/dev
zzh@ubuntu14:~/gem5$ sudo /bin/mount -o bind /proc mnt/proc


在binding这些目录之后,你现在可以chroot

zzh@ubuntu14:~/gem5$ sudo /usr/sbin/chroot mnt /bin/bash


此时,您将看到一个根提示符,您将位于新磁盘的/目录中。您应该更新您的存储库信息。

root@ubuntu14:/# apt-get update


您可能想要使用以下命令将Universe存储库添加到列表中。 注意:第一个命令是14.04中必需的。

root@ubuntu14:/# apt-get install software-properties-common

root@ubuntu14:/# add-apt-repository universe
'universe' distribution component enabled for all sources.

root@ubuntu14:/# apt-get update


现在,您可以通过apt-get安装任何可以在本地Ubuntu机器上安装的应用程序。

退出后,需要卸载我们使用的所有目录。

root@ubuntu14:/# exit
exit
zzh@ubuntu14:~/gem5$ sudo /bin/umount mnt/sys
zzh@ubuntu14:~/gem5$ sudo /bin/umount mnt/proc
zzh@ubuntu14:~/gem5$ sudo /bin/umount mnt/dev
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ubuntu gem5 disk img文件 x86