Skip to content

Commit

Permalink
Works
Browse files Browse the repository at this point in the history
  • Loading branch information
ironyman committed Jan 14, 2020
1 parent 4dbe0a0 commit b47b0d9
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 60 deletions.
35 changes: 13 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
# Prerequisites

Requires ubuntu.
```
apt install -y e2fsprogs coreutils debootstrap qemu-system-x86
apt install -y e2fsprogs coreutils debootstrap qemu-system-x86 openssh-client sudo
```


# Create vm
# Start firefox in vm
This will setup vm if not done yet. It will request root via sudo to do things
like mount and debootstrap and chroot.
```
./firefox-vm.sh
```
sudo ./create-vm.sh

Repeated invocations of firefox-vm.sh will reuse running vm. To stop vm
```
./stop-firefox-vm.sh
```

# Run firefox
Start vm
```
qemu-system-x86_64 \
-drive file=root.img,format=raw \
-kernel /boot/vmlinuz-`uname -r` \
-initrd /boot/initrd.img-`uname -r` \
-append "root=/dev/sda rdinit=/sbin/init console=tty1,115200 console=ttyS0,115200" \
-m $MEMORY \
-smp ${CPU:-1} \
-machine ubuntu,accel=kvm \
-nographic \
-device virtio-net,netdev=vmnic -netdev user,id=vmnic,hostfwd=tcp::5555-:22
```
Run firefox
```
ssh user@localhost -p5555 -Y firefox
To remove vm
```
git clean -x -d -f
```
8 changes: 8 additions & 0 deletions config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export DISK=3G
export MEMORY=2G
export CPU=4
export SSH_PORT=5555
export PID_FILE=/tmp/firefox-vm.pid
export USER=firefox_user
export ROOT=root.img
export KEY=key
83 changes: 45 additions & 38 deletions create-vm.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,42 +1,49 @@
#!/bin/bash

DISK=3G
MEMORY=2G
CPU=4

truncate root.img --size ${DISK:-3G}
mkfs.ext4 root.img -L ROOT

export MOUNTPOINT=$(mktemp -d)
mount root.img $MOUNTPOINT
debootstrap --include=firefox,openssh-server,xauth --components=main,universe bionic $MOUNTPOINT
cat <<"EOF" | bash
chroot $MOUNTPOINT
useradd -m user
passwd -d user
echo LABEL=ROOT / ext4 rw 0 1 > /etc/fstab
echo "user ALL=(ALL:ALL) NOPASSWD: ALL" >> /etc/sudoers
cat<<"EOF2" > /etc/systemd/system/dhclient.service
[Unit]
Description=dhclient
#After=network.target
[Service]
Type=simple
ExecStart=/bin/bash -c 'modprobe virtio-net; while [[ ! $( ip l | grep -e "^2:") ]]; do sleep 1; done; /sbin/dhclient -v -w'
StandardOutput=journal
[Install]
WantedBy=multi-user.target
EOF2
systemctl enable dhclient
EOF

# apt doesn't find it for some reason
# sudo apt-get -o Dir=$MOUNTPOINT update
# sudo apt-get -o Dir=$MOUNTPOINT install linux-modules-`uname -r`
mkdir $MOUNTPOINT/lib/modules/
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

. $DIR/config.sh

if [[ $EUID -ne 0 ]]; then
exec sudo /bin/bash $0 $@ --original-user $UID
fi

options=$(getopt -o '' --long original-user: -- "$@")
eval set -- "$options"
while true; do
case "$1" in
--original-user)
shift
original_user=$1
echo Original user was $1
break
;;
--)
shift
break
;;
esac
shift
done

truncate $ROOT --size ${DISK:-3G}
chown $original_user:$original_user $ROOT
mkfs.ext4 $ROOT -L ROOT

ssh-keygen -f $KEY -q -N ''
chown $original_user:$original_user $KEY $KEY.pub

MOUNTPOINT=$(mktemp -d)

sudo mount $ROOT $MOUNTPOINT
sudo debootstrap --include=firefox,openssh-server,xauth --components=main bionic $MOUNTPOINT

cp $DIR/$KEY.pub $MOUNTPOINT/
mkdir -p $MOUNTPOINT/lib/modules/
cp /lib/modules/`uname -r` $MOUNTPOINT/lib/modules/ -r
cp $DIR/dhclient.service $MOUNTPOINT/etc/systemd/system/dhclient.service
cp $DIR/setup-guest.sh $MOUNTPOINT/

chroot $MOUNTPOINT /setup-guest.sh

umount $MOUNTPOINT
11 changes: 11 additions & 0 deletions dhclient.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Unit]
Description=dhclient
#After=network.target

[Service]
Type=simple
ExecStart=/bin/bash -c '/sbin/modprobe virtio-net; /sbin/dhclient -v -w'
StandardOutput=journal

[Install]
WantedBy=multi-user.target
39 changes: 39 additions & 0 deletions firefox-vm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
. $DIR/config.sh

if [[ ! -f $ROOT ]]; then
$DIR/create-vm.sh
fi

if [[ ! -f $PID_FILE ]]; then
sudo setsid qemu-system-x86_64 \
-drive file=${ROOT},format=raw \
-kernel /boot/vmlinuz-`uname -r` \
-initrd /boot/initrd.img-`uname -r` \
-append "root=/dev/sda rdinit=/sbin/init console=tty1,115200 console=ttyS0,115200" \
-m ${MEMORY:-2G} \
-smp ${CPU:-1} \
-machine ubuntu,accel=kvm \
-nographic \
-device virtio-net,netdev=vmnic -netdev user,id=vmnic,hostfwd=tcp::${SSH_PORT}-:22 \
2>&1 > /dev/null &
disown
echo $! > $PID_FILE
fi

ssh-keygen -R [localhost]:5555 2>&1 >/dev/null
TRIES=10

while [[ $TRIES -gt 0 ]]; do
sleep 10
ssh $USER@localhost -o StrictHostKeyChecking=no -p$SSH_PORT -Y -i$KEY -q exit
if [[ $? -eq 0 ]]; then
break
fi
TRIES=$((TRIES - 1))
echo $TRIES tries left
done

ssh $USER@localhost -o StrictHostKeyChecking=no -p$SSH_PORT -Y -i$KEY firefox "$@"
7 changes: 7 additions & 0 deletions setup-guest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
useradd -m $USER
passwd -d $USER
echo LABEL=ROOT / ext4 rw 0 1 >> /etc/fstab
echo "$USER ALL=(ALL:ALL) NOPASSWD: ALL" >> /etc/sudoers
systemctl enable dhclient
install -Dm 0600 -o $USER -g $USER /$KEY.pub /home/$USER/.ssh/authorized_keys
28 changes: 28 additions & 0 deletions stop-firefox-vm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
. $DIR/config.sh

if [[ $EUID -ne 0 ]]; then
exec sudo /bin/bash $0 $@
fi

pid=$(<$PID_FILE)

TRIES=10

while [[ $TRIES -gt 0 ]]; do
if kill $pid 2>/dev/null; then
echo It exited.
break
fi
TRIES=$((TRIES - 1))
echo kill returned $?. $TRIES tries left
sleep 10
done

if [[ $TRIES -eq 0 ]]; then
kill -9 $pid
fi

rm $PID_FILE

0 comments on commit b47b0d9

Please sign in to comment.