-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdisk_image
More file actions
executable file
·107 lines (82 loc) · 2.7 KB
/
Copy pathdisk_image
File metadata and controls
executable file
·107 lines (82 loc) · 2.7 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/sh -e
cleanup() {
set +e
umount -f "$ROOT"
kpartx -dsv "$lodev"
losetup -d "$lodev"
exit
}
trap cleanup INT TERM EXIT
# Default vars
disk_size="${disk_size:-75M}"
mypass="${mypass:-merepass}"
ROOT=$(mktemp -d)
# Create an empty system root directory
rm -rf "$ROOT"
install -d "$ROOT"
# Create a sparse image
IMG='/tmp/meredisk.raw'
rm -f "$IMG"
dd if=/dev/zero of="$IMG" bs=1 seek="$disk_size" count=0
# If this is being run inside a container, the /dev/loopX devices may
# not be present. Create them manually if not.
for i in 0 1 2 3 4 5 6 7 ; do
[ -e "/dev/loop${i}" ] || mknod -m 660 "/dev/loop${i}" b 7 "$i"
done
# setup the image to a loopback device
losetup -f "$IMG"
lodev=$(losetup -a | grep "$IMG" | cut -d: -f1)
[ -n "$lodev" ]
# Create partitions on the new image
sgdisk -N=1 -A 1:set:2 "$lodev"
partuuid=$(sgdisk -i=1 "$lodev" 2>&1 | grep unique | awk '{print $NF}')
# Discover the newly made partition name and assert it is present
lopart=/dev/mapper/$(kpartx -asv "$lodev" 2>/dev/null | cut -d' ' -f3)
[ -n "$lopart" ]
# Create an ext4 filesystem without 64bit support since extlinux cannot
# boot from that currently.
mkfs.ext4 -O ^64bit "$lopart"
# Mount the partition and install basic packages to it
mount -o loop "$lopart" "$ROOT"
install -d "${ROOT}/var/lib/pacman" "${ROOT}/dev"
mknod -m 600 "${ROOT}/dev/console" c 5 1
mknod -m 666 "${ROOT}/dev/null" c 1 3
pacman -Sy --noconfirm -r "$ROOT" -b "${ROOT}/var/lib/pacman" \
base-layout busybox pacman
pacman -Sy --noconfirm -r "$ROOT" -b "${ROOT}/var/lib/pacman" --needed \
base dropbear
# Enable dropbear to start on boot
chroot "$ROOT" /sbin/service enable dropbear
# Set the root password
yes "$mypass" | chroot "$ROOT" /bin/passwd
# Set the hostname
echo 'mere' > "${ROOT}/etc/hostname"
# Clear the initial screen
clear >"${ROOT}/etc/issue"
# Install a basic networking configuration
cat > "${ROOT}/etc/network/interfaces" << EOF
auto lo eth0
iface lo inet loopback
iface eth0 inet dhcp
EOF
echo '127.0.0.1 localhost' >"${ROOT}/etc/hosts"
# Add an initial fstab file to describe mountable file systems
cat > "${ROOT}/etc/fstab" << EOF
devpts /dev/pts devpts defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
EOF
mkinitramfs
mv /tmp/initrd.gz "${ROOT}/boot/"
# Configure the system to boot with extlinux from the syslinux package
install -d "${ROOT}/boot/extlinux"
cat >"${ROOT}/boot/extlinux/extlinux.conf" <<EOF
SERIAL 0
DEFAULT mere
LABEL mere
LINUX /boot/vmlinux
APPEND initrd=/boot/initrd.gz root=PARTUUID=${partuuid} console=ttyS0,38400n8d rootresize
EOF
# Install extlinux and syslinux MBR for GPT
extlinux -i "${ROOT}/boot/extlinux"
cat /usr/share/syslinux/gptmbr.bin >"$lodev"
printf 'Disk image created at %s\n' "$IMG"