-
Notifications
You must be signed in to change notification settings - Fork 1
/
mk_hdd.sh
executable file
·64 lines (52 loc) · 1.27 KB
/
mk_hdd.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
#!/bin/bash
#Start
INPUT=${1}
OUTPUT=${2}
SIZE=${3}
TMPDIR=${4}
SECTOR_SIZE=512
SECTOR_COUNT=`expr ${SIZE} / ${SECTOR_SIZE}`
SECTOR_PART_START=2048
function die()
{
echo -e "$1"
exit -1
}
#Generate image
dd if=/dev/zero of=${OUTPUT} bs=${SECTOR_SIZE} count=${SECTOR_COUNT} > /dev/null || die "Could not create image file"
#Create primary partition
fdisk ${OUTPUT} <<EOF
n
p
1
${SECTOR_PART_START}
a
w
EOF
#Create loop devices
LOOP_DEV=`sudo losetup -f ${OUTPUT} --show` || die "Could not create dev loop device"
LOOP_PART=`sudo losetup -f ${OUTPUT} --offset=$((SECTOR_PART_START * SECTOR_SIZE)) --show`
if [ $? -ne 0 ]; then
sudo losetup -d ${LOOP_DEV}
die "Could not create part loop device"
fi
#Create ext2 filesystem
sudo mkfs.ext2 -q ${LOOP_PART} -L YourOS
if [ $? -ne 0 ]; then
sudo losetup -d ${LOOP_DEV}
sudo losetup -d ${LOOP_PART}
die "Could not create filesystem"
fi
#Mount partition
MOUNT_PATH=${TMPDIR}/mnt
mkdir -p ${MOUNT_PATH}
sudo mount ${LOOP_PART} ${MOUNT_PATH}
sudo chmod 777 -R ${MOUNT_PATH}
#Copy files
cp -r ${INPUT}/* ${MOUNT_PATH}
#Install grub
sudo grub-install --root-directory=${MOUNT_PATH} --no-floppy --modules="normal part_msdos ext2 multiboot" ${LOOP_DEV}
#Cleanup
sudo umount ${MOUNT_PATH}
sudo losetup -d ${LOOP_DEV}
sudo losetup -d ${LOOP_PART}