카테고리

asm (27) bootloader_x86_grub (1) C (92) compile (11) config (76) CPP (13) CSS (1) debugging (7) gimp (1) Go (1) html (1) Java (1) JavaScript (1) kernel (19) LibreOffice (3) Linux system progamming (21) MFC (1) opencv (4) OpenGL (1) PHP (1) Python (4) qemu (29) shell (3) socket (7) troubleshooting (2) ubuntu18.04 (2) windows (1)

2019/01/05

debootstrap_x86 qemu 자동 설치 스크립트

#!/usr/bin/env bash

set -eux

suite=xenial

debootstrap_dir=$suite
img_file=${suite}.ext4.img
kernel=/vmlinuz
initrd=/initrd.img

sudo apt install -y \
  debootstrap \
  qemu-system-x86 \
;

if [ ! -d "$debootstrap_dir" ]; then
  # Create debootstrap directory.
  sudo debootstrap \
    "$suite" \
    "$debootstrap_dir" \
    http://archive.ubuntu.com/ubuntu \
  ;

  # Set root password.
  echo 'root:root' | sudo chroot "$debootstrap_dir" chpasswd

  # Remount root filesystem as rw.
  cat << EOF | sudo tee "$debootstrap_dir/etc/fstab"
/dev/sda / ext4 errors=remount-ro,acl 0 1
EOF

  # Automaticaly start networking.
  # Otherwise network commands fail with:
  #     Temporary failure in name resolution
  # https://gist.github.com/corvax19/6230283#gistcomment-1940694
  cat << EOF | sudo tee "$debootstrap_dir/etc/systemd/system/dhclient.service"
[Unit]
Description=DHCP Client
Documentation=man:dhclient(8)
Wants=network.target
Before=network.target
[Service]
Type=forking
PIDFile=/var/run/dhclient.pid
ExecStart=/sbin/dhclient -4 -q
[Install]
WantedBy=multi-user.target
EOF

  sudo ln -sf /etc/systemd/system/dhclient.service \
    "${debootstrap_dir}/etc/systemd/system/multi-user.target.wants/dhclient.service"
fi

if [ ! -f "$img_file" ]; then
  # Create disk image
  dd if=/dev/null of="$img_file" bs=1M seek=2048
  mkfs.ext4 "$img_file"
  mnt_dir="${suite}_mnt"
  mkdir "$mnt_dir"
  sudo mount -t ext4 "$img_file" "$mnt_dir"
  sudo cp -r "$debootstrap_dir/." "$mnt_dir"
  sudo umount "$mnt_dir"
  rmdir "$mnt_dir"
fi

sudo qemu-system-x86_64 \
  -append 'console=ttyS0 root=/dev/sda' \
  -drive "file=${img_file},format=raw" \
  -enable-kvm \
  -nographic \
  -serial mon:stdio \
  -m 2G \
  -kernel "$kernel" \
  -initrd "$initrd" \
;

댓글 없음:

댓글 쓰기