findutils: fix compile with debugging options
[openadk.git] / scripts / create-image.sh
blob2b5b34bc9eaf9277fb88320f0d3e2b0fdc769fc9
1 #!/usr/bin/env bash
3 filesystem=ext2
5 while getopts "f:i" option
6 do
7 case $option in
8 f)
9 filesystem=$OPTARG
12 initramfs=1
15 printf "Option not recognized\n"
16 exit 1
18 esac
19 done
20 shift $(($OPTIND - 1))
22 if [ $(id -u) -ne 0 ];then
23 printf "Installation is only possible as root\n"
24 exit 1
27 printf "Checking if mkfs is installed"
28 mkfs=$(which mkfs.$filesystem)
30 if [ ! -z $mkfs -a -x $mkfs ];then
31 printf "...okay\n"
32 else
33 printf "...failed\n"
34 exit 1
37 printf "Checking if parted is installed"
38 parted=$(which parted)
40 if [ ! -z $parted -a -x $parted ];then
41 printf "...okay\n"
42 else
43 printf "...failed\n"
44 exit 1
47 printf "Checking if qemu-img is installed"
48 qimg=$(which qemu-img)
50 if [ ! -z $qimg -a -x $qimg ];then
51 printf "...okay\n"
52 else
53 printf "...failed\n"
54 exit 1
57 if [ -z $1 ];then
58 printf "Please give the name of the image file\n"
59 exit 1
60 fi
62 if [ -z $initramfs ];then
63 if [ -z $2 ];then
64 printf "Please give the name of the openadk archive file\n"
65 exit 1
66 fi
67 else
68 if [ -z $2 ];then
69 printf "Please give the full path prefix to kernel/initramfs\n"
70 exit 1
75 printf "Generate qemu image (512 MB)\n"
76 $qimg create -f raw $1 512M >/dev/null
78 printf "Creating filesystem $filesystem\n"
80 printf "Create partition and filesystem\n"
81 $parted -s $1 mklabel msdos
82 $parted -s $1 -- mkpart primary ext2 0 -0
83 $parted -s $1 set 1 boot on
85 offset=$(parted $1 unit b print | tail -2 | head -1 | cut -f 1 --delimit="B" | cut -c 9-)
87 dd if=$1 of=mbr bs=$offset count=1 2>/dev/null
88 dd if=$1 skip=$offset of=$1.new 2>/dev/null
90 if [ "$filesystem" = "ext2" -o "$filesystem" = "ext3" -o "$filesystem" = "ext4" ];then
91 mkfsopts=-F
94 mkfs.$filesystem $mkfsopts ${1}.new >/dev/null
96 if [ $? -eq 0 ];then
97 printf "Successfully created partition\n"
98 #$parted $1 print
99 else
100 printf "Partition creation failed, Exiting.\n"
101 exit 1
104 cat mbr ${1}.new > $1
105 rm ${1}.new
106 rm mbr
108 tmp=$(mktemp -d)
110 mount -o loop,offset=$offset -t $filesystem $1 $tmp
112 if [ -z $initramfs ];then
113 printf "Extracting install archive\n"
114 tar -C $tmp -xzpf $2
115 printf "Fixing permissions\n"
116 chmod 1777 $tmp/tmp
117 chmod 4755 $tmp/bin/busybox
118 else
119 printf "Copying kernel/initramfs\n"
120 mkdir $tmp/boot $tmp/dev
121 cp $2-kernel $tmp/boot/kernel
122 cp $2-initramfs $tmp/boot/initramfs
125 umount $tmp
126 printf "Successfully installed.\n"
127 printf "Be sure $1 is writable for the user which use qemu\n"
128 exit 0