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