-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzeros_build_shell_gentoo.sh
executable file
·105 lines (96 loc) · 3.01 KB
/
zeros_build_shell_gentoo.sh
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
#!/bin/bash
BINVERSION=$(equery -q l binutils | awk -e 'BEGIN{FS="-"} {printf "%s-%s", $3, $4}')
# GCCVERSION="9.2.0-r2"
TARGET="i386-elf"
REPO="cross-$TARGET"
REPODIR="/var/db/repos/$REPO"
REPOSCONF="/etc/portage/repos.conf"
PORT_LOG="/var/log/portage/$REPO-*.log"
ISO="zeros.iso"
MOUNT="/mnt"
LOOP1=""
LOOP2=""
# Detect root
if [ $(id -u) -ne 0 ]; then
echo "Need superuser privileges!"
# Restart script with higher privs
# sudoer's username is automatically saved in $SUDO_USER
sudo $0
exit
fi
# Detect $TARGET toolchain, creating if needed
if [ ! -d $REPODIR ]; then
echo "Repo $REPO doesn't exist, creating..."
mkdir -p $REPODIR/{profiles,metadata}
echo "$REPO" > $REPODIR/profiles/repo_name
echo 'masters = gentoo' > $REPODIR/metadata/layout.conf
echo "[$REPO]" > $REPOSCONF/$REPO.conf
echo "location = $REPODIR" >> $REPOSCONF/$REPO.conf
echo 'priority = 10' >> $REPOSCONF/$REPO.conf
echo 'masters = gentoo' >> $REPOSCONF/$REPO.conf
echo 'auto-sync = no' >> $REPOSCONF/$REPO.conf
chown -R portage:portage $REPODIR
# Create the cross-compiler
echo "Creating cross-compiler for $TARGET..."
echo "binutils version: $BINVERSION"
# echo "GCC version: $GCCVERSION"
# crossdev --stage1 --binutils $BINVERSION --gcc $GCCVERSION \
# --target $TARGET --portage -a --portage -v
crossdev --target $TARGET \
--stage0 --binutils $BINVERSION \
--ex-gdb \
--denv 'USE="xml"' \
--portage -a --portage -v
else
echo "Repo $REPO exists"
fi
# Create the ISO if it doesn't exist
if [ -a $ISO ]; then
echo "ISO exists"
else
echo "Creating image of size 100MiB"
dd if=/dev/zero of=$ISO bs=1M count=100
fi
# Create loopback devices if needed
if [ "$(losetup -j $ISO)" ]; then
echo "ISO already has loopback devices"
LOOP1="$(losetup -l -n -O NAME -j $ISO | sort | head -1)"
LOOP2="$(losetup -l -n -O NAME -j $ISO | sort | tail -1)"
else
echo "Creating loopback devices for ISO"
LOOP1="$(losetup -f)"
losetup $LOOP1 $ISO
LOOP2="$(losetup -f)"
losetup $LOOP2 $ISO -o 1048576
fi
# Install GRUB and create ext2 filesystem if needed
if [ "$(dd if=$LOOP1 bs=512 count=1 | xxd | grep 'GRUB')" ]; then
echo "GRUB already installed on ISO"
else
echo "Creating partition on iso"
sfdisk $ISO < zeros.sfdisk
echo "Creating ext2 filesystem on ISO"
mkfs.ext2 $LOOP2
echo "Mounting ISO at $MOUNT"
mount $LOOP2 $MOUNT
echo "Installing GRUB on ISO"
grub-install --target=i386-pc --root-directory=$MOUNT --no-floppy \
--modules="normal part_msdos ext2 multiboot biosdisk" $LOOP1
echo "Creating GRUB config"
cat > $MOUNT/boot/grub/grub.cfg << EOF
menuentry "ZerOS" {
multiboot /boot/kernel.bin
}
EOF
echo "Making ISO world rwx"
chmod 777 $ISO
chmod -R 777 $MOUNT
sync
fi
# Mount ISO if needed
if [ "$(findmnt -n -o TARGET $LOOP2)" ]; then
echo "ISO already mounted"
else
echo "Mounting ISO at $MOUNT"
mount $LOOP2 $MOUNT
fi