File size: 2,497 Bytes
ae8629b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
FROM ubuntu:22.04

ENV DEBIAN_FRONTEND=noninteractive
ENV HOSTNAME=Nobita

# -----------------------------
# Install Required Packages
# -----------------------------
RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates \
    curl \
    wget \
    git \
    sudo \
    qemu-system-x86 \
    cloud-image-utils \
    openssh-client \
    htop \
    neovim \
    && rm -rf /var/lib/apt/lists/*

# -----------------------------
# Install code-server
# -----------------------------
RUN curl -fsSL https://code-server.dev/install.sh | sh

# -----------------------------
# Create VM Directory
# -----------------------------
WORKDIR /workspace
RUN mkdir -p /vm/ubuntu22

# -----------------------------
# Download Ubuntu 22.04 Cloud Image (.img is actually qcow2)
# -----------------------------
RUN wget https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img \
    -O /vm/ubuntu22/ubuntu22.qcow2

# -----------------------------
# Cloud-Init Config (User: ubuntu)
# -----------------------------
RUN cat <<EOF > /vm/ubuntu22/user-data
#cloud-config
ssh_pwauth: true
disable_root: false

chpasswd:
  list: |
    root:root
  expire: false

# Ini wajib supaya SSH root tidak diblokir oleh sistem
runcmd:
  - sed -i 's/^#PermitRootLogin.*/PermitRootLogin yes/' /etc/ssh/sshd_config
  - systemctl restart ssh
EOF

RUN cat <<EOF > /vm/ubuntu22/meta-data
instance-id: ubuntu22
local-hostname: ubuntu22
EOF

RUN cloud-localds /vm/ubuntu22/seed.iso \
    /vm/ubuntu22/user-data \
    /vm/ubuntu22/meta-data

# -----------------------------
# Expose Ports
# -----------------------------
EXPOSE 7860 2222

# -----------------------------
# Startup Script
# -----------------------------
CMD bash -c "\
DISK_FREE_MB=\$(df -m /home | awk 'NR==2 {print \$4}'); \
VM_DISK=\$((DISK_FREE_MB * 80 / 100)); \
qemu-img resize /vm/ubuntu22/ubuntu22.qcow2 \${VM_DISK}M; \
TOTAL_RAM=\$(awk '/MemTotal/ {print int(\$2/1024)}' /proc/meminfo); \
VM_RAM=\$((TOTAL_RAM*70/100)); \
CPU_CORES=\$(nproc); \
echo \"Detected RAM: \$TOTAL_RAM MB\"; \
echo \"Allocating VM RAM: \$VM_RAM\"; \
echo \"Allocating VM DISK: \$VM_DISK MB\"; \
echo \"Detected CPU Cores: \$CPU_CORES\"; \
qemu-system-x86_64 \
-m \$VM_RAM \
-smp \$CPU_CORES \
-cpu EPYC \
-drive file=/vm/ubuntu22/ubuntu22.qcow2,format=qcow2,if=virtio \
-drive file=/vm/ubuntu22/seed.iso,format=raw,if=virtio \
-net nic \
-net user,hostfwd=tcp::2222-:22 \
-nographic & \
code-server --bind-addr 0.0.0.0:7860 --auth none"