目录是/vm/custom
下载linux 内核版本 4.10.4
/vm/custom/linux-4.10.4$
make x86_64_defconfig
应该会打开
KERNEL CONFIG_DEVTMPFS=y
Device Drivers --->
Generic Driver Options --->
[*] Maintain a devtmpfs filesystem to mount at /dev
KERNEL CONFIG_UEVENT_HELPER=y
Device Drivers --->
Generic Driver Options --->
[*] Support for uevent helper
make -j 8
下载busybox busybox-1.26.2
/vm/custom/busybox-1.26.2$
mkdir -pv ../obj/busybox-x86
make O=../obj/busybox-x86 defconfig
make O=../obj/busybox-x86 menuconfig
type /, search for “static”. You’ll see that the option is located at:
-> Busybox Settings
-> Build Options
[ ] Build BusyBox as a static binary (no shared libs)
Go to that location, select it, save, and exit.
Now build busybox:
$ cd ../obj/busybox-x86
$ make -j2
$ make install
cd /vm/custom
mkdir -pv initramfs/x86-busybox
cd initramfs/x86-busybox
mkdir -pv {bin,sbin,etc,proc,sys,usr/{bin,sbin}}
一定要创建 /etc /proc /sys 否则 init 会mount 不上去,出现 kernel pannic
mkdir /root
创建root目录
cp -av ../../obj/busybox-x86/_install/* .
initramfs/x86-busybox目录下
init文件的任何错误都可能导致kernel panic
vim init
加入
#!/bin/sh
mount -t proc none /proc
mount -t sysfs none /sys
echo -e "\nBoot took $(cut -d' ' -f1 /proc/uptime) seconds\n"
mount -t devtmpfs none /dev
exec /bin/sh
保存后
chmod +x init
制作rootfs
find . -print0 \
| cpio --null -ov --format=newc \
| gzip -9 > ../../obj/initramfs-busybox-x86.cpio.gz
然后运行
qemu-system-x86_64 \
-kernel linux-4.10.4/arch/x86_64/boot/bzImage \
-initrd obj/initramfs-busybox-x86.cpio.gz \
-nographic -append "console=ttyS0" -enable-kvm
使用console 可以看到更多的输出信息
linux可以正常启动,第一步完成
添加硬盘
cd /vm/custom
cd obj
dd if=/dev/zero of=hda.img bs=1 count=1024M
mkfs -t ext2 hda.img
sudo mount hda.img mnt
cd /vm/custom
sudo cp -r initramfs/x86-busybox/* obj/mnt/
启动虚拟机
qemu-system-x86_64 \
-kernel linux-4.10.4/arch/x86_64/boot/bzImage \
-initrd obj/initramfs-busybox-x86.cpio.gz \
-nographic -append "console=ttyS0" -enable-kvm -hda obj/hda.img
启动后
ls /dev
查看到硬盘是 /dev/sda
修改initramfs/x86-busybox下的init文件
#!/bin/sh
mount -t proc none /proc
mount -t sysfs none /sys
mount -t devtmpfs none /dev
echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s
mount -o rw /dev/sda /root
echo -e "\nBoot took $(cut -d' ' -f1 /proc/uptime) seconds\n"
#exec /bin/sh
# Boot the real thing.
exec switch_root /root /sbin/init
mount -t proc proc /proc
mount -t devtmpfs none /dev
/bin/mount -n -t sysfs none /sys
/bin/mount -t ramfs none /dev
/sbin/mdev -s
mdev加载设备,switch_root切换到硬盘的rootfs 一定要pid为1的init做
然后运行
qemu-system-x86_64 \
-kernel linux-4.10.4/arch/x86_64/boot/bzImage \
-initrd obj/initramfs-busybox-x86.cpio.gz \
-nographic -append "console=ttyS0" -enable-kvm -hda obj/hda.img
发现rootfs切换成功,但是控制台输出
can't run '/etc/init.d/rcS': No such file or directory
can't open /dev/tty4: No such file or directory
can't open /dev/tty3: No such file or directory
can't open /dev/tty2: No such file or directory
从busybox的/vm/custom/busybox-1.26.2/examples目录下
copy bootfloppy/etc/init.d下的rcS文件到initramfs/x86-busybox/etc/init.d目录下
然后编辑rcS
#!/bin/sh
/bin/mount -a
/bin/mount -n -t sysfs none /sys
/bin/mount -t ramfs none /dev
/sbin/mdev -s
copy bootfloppy/etc目录下fstab和inittab到initramfs/x86-busybox/etc/目录下
编辑
::sysinit:/etc/init.d/rcS
::respawn:-/bin/sh
::ctrlaltdel:/bin/umount -a -r
然后运行
initramfs/x86-busybox
find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../../obj/initramfs-busybox-x86.cpio.gz
cd /vm/custom
sudo cp -r initramfs/x86-busybox/* obj/mnt/
cd obj/mnt/
/vm/custom/obj/mnt/etc/init.d$ sudo chmod +x rcS
可以执行权限
然后运行
qemu-system-x86_64 \
-kernel linux-4.10.4/arch/x86_64/boot/bzImage \
-initrd obj/initramfs-busybox-x86.cpio.gz \
-nographic -append "console=ttyS0" -enable-kvm -hda obj/hda.img
一切正常
sudo qemu-system-x86_64 \
-kernel linux-4.10.4/arch/x86_64/boot/bzImage \
-initrd obj/initramfs-busybox-x86.cpio.gz \
-enable-kvm -drive format=raw,file=obj/hda.img -net nic,model=rtl8139 -net tap -vga std -serial stdio
阅读(2825) | 评论(0) | 转发(0) |