]> git.dujemihanovic.xyz Git - nameless-os.git/blob - nameless2disk
kernel: Enable -O2 and LTO again
[nameless-os.git] / nameless2disk
1 #!/bin/sh
2 # Shell script to install Nameless to a disk.
3
4 BINARIES=( boot/x86/mbr boot/x86/vbr-fat32 boot/x86/stage3/LOADER.BIN kernel/kernel.elf )
5
6 check_root() {
7 local user=$(whoami)
8 if [ $user != "root" ]; then
9 echo nameless2disk needs to be run as root!
10 exit 1
11 fi
12 }
13
14 check_binaries() {
15 for i in "${BINARIES[@]}"; do
16 if ! [ -e $i ]; then
17 echo $i does not exist, have you compiled Nameless?
18 exit 1
19 fi
20 done
21 }
22
23 install_blkdev() {
24 # Ask the user are they sure they want to install.
25 local prompt
26 echo About to install Nameless to real disk $target.
27 echo This will WIPE ALL DATA on $target, type YES if you want to continue.
28 read -r prompt
29 if [ $prompt != "YES" ]; then
30 echo -e "Aborting."
31 exit 1
32 fi
33
34 echo OK, installing Nameless to $target.
35 echo Creating partition table on $target...
36
37 # Create a partition table on the disk.
38 fdisk $target <<- END > /dev/null
39 o
40 n
41 p
42 1
43
44
45 a
46 t
47 0c
48 w
49 END
50 if ! [ $? -eq 0 ]; then
51 echo An error occurred!
52 exit 1
53 fi
54 # Create a FAT32 filesystem on the disk.
55 echo Formatting $target...
56 mkfs.fat -F 32 "$target"1
57 if ! [ $? -eq 0 ]; then
58 echo An error occurred!
59 exit 1
60 fi
61 # Write MBR and VBR to the disk.
62 # TODO: write VBR to the backup BPB as well?
63 echo Writing bootsectors to $target...
64 dd 'if=boot/x86/mbr' "of=$target" 'bs=440' 'count=1'
65 if ! [ $? -eq 0 ]; then
66 echo An error occurred!
67 exit 1
68 fi
69 dd 'if=boot/x86/vbr-fat32' 'of='"$target"1 'bs=1' 'skip=90' 'seek=90'
70 if ! [ $? -eq 0 ]; then
71 echo An error occurred!
72 exit 1
73 fi
74 # Mount the partition and copy stage3 and kernel to it.
75 echo Copying files to $target...
76 local mountpoint=$(mktemp -d)
77 mount "$target"1 $mountpoint
78 if ! [ $? -eq 0 ]; then
79 echo An error occurred!
80 exit 1
81 fi
82 cp boot/x86/stage3/LOADER.BIN $mountpoint/LOADER.BIN
83 cp kernel/kernel.elf $mountpoint/KERNEL.ELF
84 if ! [ $? -eq 0 ]; then
85 echo An error occurred!
86 exit 1
87 fi
88 # Unmount the partition and flush write cache to make sure the OS
89 # binaries have actually been written to the disk.
90 echo Unmounting $target...
91 umount $mountpoint
92 rm -rf $mountpoint
93 echo Flushing cache...
94 sync
95 echo Nameless has been successfully installed to $target!
96 return
97 }
98
99 # Check if all the required tools are installed.
100 [ -z $(command -v fdisk) ] && echo fdisk not found, is util-linux installed? && exit 127
101 [ -z $(command -v mkfs.fat) ] && echo mkfs.fat not found, is dosfstools installed? && exit 127
102
103 # Check for root privileges.
104 check_root
105
106 # Make sure that Nameless has been compiled.
107 check_binaries
108
109 # cd to the directory we're in.
110 cd $(dirname $(realpath $0))
111
112 # For convenience, list all connected disks.
113 fdisk -l
114
115 # Ask the user where to install.
116 echo -n "Choose the disk you want to install to: "
117 read -r target
118
119 # If the target does not exist or is not a blkdev, exit.
120 [ -b $target ] || echo $target does not exist or is not a block device! || exit 1
121 install_blkdev
122 exit 0