**作者:张华 发表于:2016-11-04
版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明
( http://blog.csdn.net/quqi99 )**
#!/bin/bash
RELEASE='yakkety'
MOUNTPOINT="/tmp/nbd-${RANDOM}"
DISK_DEV=/dev/nbd0
BOOT_DEV=/dev/nbd0p1
ROOT_DEV=/dev/nbd0p2
PASSWORD='password'
IMG_DISK_SIZE=8G
BASE_MAC="52:54:74:b7:10:"
NETWORK="192.168.123."
qemu-img create -f qcow2 /images/guest.qcow2 ${IMG_DISK_SIZE}
modprobe nbd max_part=16
qemu-nbd -c ${DISK_DEV} /images/guest.qcow2
fdisk ${DISK_DEV} << EOF
n,p,1,\n,+256M
n,p,2,\n,\n
w
EOF
fdisk -l ${DISK_DEV} |grep dev
mkfs.ext2 -LBOOT ${BOOT_DEV}
mkfs.ext4 -LROOT ${ROOT_DEV}
mkdir -p ${MOUNTPOINT}/boot
mount ${BOOT_DEV} ${MOUNTPOINT}/boot
mount ${ROOT_DEV} ${MOUNTPOINT}
apt install -y apt-cacher-ng
echo 'Acquire::http::Proxy "http://127.0.0.1:3142";' | sudo tee /etc/apt/apt.conf.d/01acng
debootstrap --include=less,vim,sudo,openssh-server,bash-completion,wget,rsync,git,build-essential,gdb,crash,grub-pc,screen,open-iscsi $RELEASE ${MOUNTPOINT} http://127.0.0.1:3142/cn.archive.ubuntu.com/ubuntu/
tee "${MOUNTPOINT}/etc/fstab" <<EOF
LABEL=boot /boot ext2 sync 0 2
LABEL=root / ext4 errors=remount-ro 0 1
EOF
tee "${MOUNTPOINT}/etc/network/interfaces" <<EOF
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
EOF
tee "${MOUNTPOINT}/etc/apt/sources.list" <<EOF
deb http://cn.archive.ubuntu.com/ubuntu/ ${RELEASE} main restricted universe multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ ${RELEASE}-updates main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu ${RELEASE}-security main restricted universe multiverse
EOF
tee "${MOUNTPOINT}/etc/apt/apt.conf" <<EOF
#Acquire::http::Proxy "http://127.0.0.1:3142/";
APT::Install-Recommends "true";
APT::Install-Suggests "false";
# APT::Get::Assume-Yes "true";
# APT::Get::Show-Upgraded "true";
APT::Quiet "true";
DPkg::Options {"--force-confdef";"--force-confmiss";"--force-confold"};
Debug::pkgProblemResolver "true";
Acquire::Languages "none";
EOF
echo guest > "${MOUNTPOINT}/etc/hostname"
echo "root:${PASSWORD}" | chpasswd -R "${MOUNTPOINT}"
mkdir -p "${MOUNTPOINT}/root/.ssh"
cat /home/${SUDO_USER}/.ssh/{id_*.pub,authorized_keys} 2>/dev/null | sort -u > "${MOUNTPOINT}/root/.ssh/authorized_keys"
# /dev, /dev/pts, /proc, /sys. These are used by the grub-install and apt-get install.
mount -o bind /dev ${MOUNTPOINT}/dev
mount -o bind /sys ${MOUNTPOINT}/sys
mount -o bind /proc ${MOUNTPOINT}/proc
mount -o bind /dev/pts ${MOUNTPOINT}/dev/pts
# Install the kernel
LANG=C DEBIAN_FRONTEND=noninteractive chroot "${MOUNTPOINT}" bash -c 'apt-get update && apt-get dist-upgrade && apt-get -y install linux-image-generic linux-headers-generic'
LANG=C DEBIAN_FRONTEND=noninteractive chroot "${MOUNTPOINT}" bash -c 'apt-get -y install locales locales-all gdbserver'
chroot "${MOUNTPOINT}" bash -c 'export LC_ALL=en_US.UTF-8 && export LANGUAGE=en_US.UTF-8 && export LANG=en_US.UTF-8 && locale-gen en_US.UTF-8'
chroot "${MOUNTPOINT}" grub-install --modules="biosdisk part_msdos" --no-floppy ${DISK_DEV}
chroot "${MOUNTPOINT}" update-grub
sed -i -e "s|${BOOT_DEV}|/dev/vda1|g;s|${ROOT_DEV}|/dev/vda2|g;s|${DISK_DEV}|/dev/vda|g" "${MOUNTPOINT}/boot/grub/grub.cfg"
tee "${MOUNTPOINT}/etc/default/grub" <<EOF
GRUB_DEFAULT=0
#GRUB_HIDDEN_TIMEOUT=0
GRUB_HIDDEN_TIMEOUT_QUIET=true
GRUB_TIMEOUT=2
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT="root=/dev/vda1 console=tty0 console=ttyS0,38400n8 apparmor=0 crashkernel=384M-:256M"
GRUB_CMDLINE_LINUX=""
GRUB_TERMINAL=serial
GRUB_SERIAL_COMMAND="serial --speed=38400 --unit=0 --word=8 --parity=no --stop=1"
#GRUB_BADRAM="0x01234567,0xfefefefe,0x89abcdef,0xefefefef"
#GRUB_TERMINAL=console
#GRUB_GFXMODE=640x480
#GRUB_DISABLE_LINUX_UUID=true
#GRUB_DISABLE_RECOVERY="true"
#GRUB_INIT_TUNE="480 440 1"
EOF
# Add one user
chroot "${MOUNTPOINT}" useradd hua
echo "hua:${PASSWORD}" | chpasswd -R "${MOUNTPOINT}"
tee "${MOUNTPOINT}/etc/default/grub" <<EOF
echo 'hua ALL=(ALL) NOPASSWD: ALL' >> ${MOUNTPOINT}/etc/sudoers
# Exit
umount /target/dev/pts
umount /target/dev
umount /target/sys
umount /target/proc
umount /target/boot
umount /target
qemu-nbd -d ${DISK_DEV}
# Boot one VM
brctl addbr demo0
ip link set demo0 up
sleep 1
ip addr add ${NETWORK}1/24 dev demo0
sleep 1
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
apt-get install -y dnsmasq && service dnsmasq stop
echo "${BASE_MAC}fd,${NETWORK}249,guest" > /tmp/dhcphosts
echo "${BASE_MAC}fe,${NETWORK}250,guest" >> /tmp/dhcphosts
tee "/tmp/dnsmasq.conf" <<EOF
interface=demo0
except-interface=lo
bind-interfaces
dhcp-range=192.168.123.1,192.168.123.250,12h
EOF
dnsmasq -C /tmp/dnsmasq.conf --dhcp-hostsfile=/tmp/dhcphosts --pid-file=/tmp/demo0-dnsmasq.pid
tee "/tmp/qemu-ifup" <<EOF
#!/bin/sh
switch=demo0
if [ -n "\$1" ];then
sudo tunctl -u \`whoami\` -t \$1
sudo ip link set \$1 up 2>/dev/null
sleep 0.5s
sudo brctl addif \$switch \$1
exit 0
else
echo "Error: no interface specified"
exit 1
fi
EOF
tee "/tmp/qemu-ifdown" <<EOF
#!/bin/sh
switch=demo0
if [ -n "\$1" ];then
sudo ip link set \$1 down
sudo brctl delif \$switch \$1 2>/dev/null
sudo tunctl -d \$1
exit 0
else
echo "Error: no interface specified"
exit 1
fi
EOF
apt-get install -y uml-utilities
chmod 777 /tmp/qemu-ifup && chmod 777 /tmp/qemu-ifdown
qemu-system-x86_64 -enable-kvm -machine q35 -smp 8 -m 4096 \
-device virtio-net-pci,netdev=net0,mac=${BASE_MAC}fe \
-netdev tap,id=net0,script=/tmp/qemu-ifup,downscript=/tmp/qemu-ifdown \
-device virtio-net-pci,netdev=net1,mac=${BASE_MAC}fd \
-netdev tap,id=net1,script=/tmp/qemu-ifup,downscript=/tmp/qemu-ifdown \
-drive file=/images/guest.qcow2,index=0,media=disk,if=virtio \
-numa node,nodeid=0,cpus=0-3 \
-numa node,nodeid=1,cpus=4-7 \
-curses \
-name guest \
-S \
-nographic \
-chardev socket,id=monitor,path=/tmp/guest.monitor,server,nowait \
-monitor chardev:monitor \
-chardev socket,id=serial,path=/tmp/guest.serial,server,nowait \
-serial chardev:serial \
# -incoming tcp:0:4444
echo " - SSH: hua@${NETWORK}250 (password: ${PASSWORD})"
echo " OR: minicom -D unix\#/tmp/guest.monitor"