3 # Copyright (C) 2008-2011 Intel
5 # install.sh [device_name] [rootfs_name] [video_mode] [vga_mode]
8 PATH=/sbin:/bin:/usr/sbin:/usr/bin
10 # figure out how big of a boot partition we need
11 boot_size=$(du -ms /run/media/$1/ | awk '{print $1}')
12 # remove rootfs.img ($2) from the size if it exists, as its not installed to /boot
13 if [ -e /run/media/$1/$2 ]; then
14 boot_size=$(( boot_size - $( du -ms /run/media/$1/$2 | awk '{print $1}') ))
16 # remove initrd from size since its not currently installed
17 if [ -e /run/media/$1/initrd ]; then
18 boot_size=$(( boot_size - $( du -ms /run/media/$1/initrd | awk '{print $1}') ))
20 # add 10M to provide some extra space for users and account
21 # for rounding in the above subtractions
22 boot_size=$(( boot_size + 10 ))
27 # Get a list of hard drives
29 live_dev_name=`cat /proc/mounts | grep ${1%/} | awk '{print $1}'`
30 live_dev_name=${live_dev_name#\/dev/}
31 # Only strip the digit identifier if the device is not an mmc
32 case $live_dev_name in
38 live_dev_name=${live_dev_name%%[0-9]*}
42 echo "Searching for hard drives ..."
44 # Some eMMC devices have special sub devices such as mmcblk0boot0 etc
45 # we're currently only interested in the root device so pick them wisely
46 devices=`ls /sys/block/ | grep -v mmcblk` || true
47 mmc_devices=`ls /sys/block/ | grep "mmcblk[0-9]\{1,\}$"` || true
48 devices="$devices $mmc_devices"
50 for device in $devices; do
62 # skip the device LiveOS is on
63 # Add valid hard drive name to the list
66 # skip the device we are running from
69 hdnamelist="$hdnamelist $device"
77 for hdname in $hdnamelist; do
78 # Display found hard drives and their basic info
79 echo "-------------------------------"
81 if [ -r /sys/block/$hdname/device/vendor ]; then
83 cat /sys/block/$hdname/device/vendor
85 if [ -r /sys/block/$hdname/device/model ]; then
87 cat /sys/block/$hdname/device/model
89 if [ -r /sys/block/$hdname/device/uevent ]; then
91 cat /sys/block/$hdname/device/uevent
98 echo "Please select an install target or press n to exit ($hdnamelist ): "
100 if [ "$answer" = "n" ]; then
101 echo "Installation manually aborted."
104 for hdname in $hdnamelist; do
105 if [ "$answer" = "$hdname" ]; then
106 TARGET_DEVICE_NAME=$answer
110 if [ -n "$TARGET_DEVICE_NAME" ]; then
115 if [ -n "$TARGET_DEVICE_NAME" ]; then
116 echo "Installing image on /dev/$TARGET_DEVICE_NAME ..."
118 echo "No hard drive selected. Installation aborted."
122 device=/dev/$TARGET_DEVICE_NAME
125 # The udev automounter can cause pain here, kill it
127 #rm -f /etc/udev/rules.d/automount.rules
128 #rm -f /etc/udev/scripts/mount*
131 # Unmount anything the automounter had mounted
133 #umount ${device}* 2> /dev/null || /bin/true
135 for dir in `awk '/\/dev.* \/run\/media/{print $2}' /proc/mounts | grep $TARGET_DEVICE_NAME`; do
139 if [ ! -b /dev/loop0 ] ; then
140 mknod /dev/loop0 b 7 0
144 if [ ! -L /etc/mtab ] && [ -e /proc/mounts ]; then
145 ln -sf /proc/mounts /etc/mtab
148 disk_size=$(parted ${device} unit mb print | grep '^Disk .*: .*MB' | cut -d" " -f 3 | sed -e "s/MB//")
150 grub_version=$(grub-install -V|sed 's/.* \([0-9]\).*/\1/')
152 if [ $grub_version -eq 0 ] ; then
155 # For GRUB 2 we need separate parition to store stage2 grub image
156 # 2Mb value is chosen to align partition for best performance.
160 swap_size=$((disk_size*swap_ratio/100))
161 rootfs_size=$((disk_size-bios_boot_size-boot_size-swap_size))
163 boot_start=$((bios_boot_size))
164 rootfs_start=$((bios_boot_size+boot_size))
165 rootfs_end=$((rootfs_start+rootfs_size))
166 swap_start=$((rootfs_end))
168 # MMC devices are special in a couple of ways
169 # 1) they use a partition prefix character 'p'
170 # 2) they are detected asynchronously (need rootwait)
173 if [ ! "${device#/dev/mmcblk}" = "${device}" ] || \
174 [ ! "${device#/dev/nvme}" = "${device}" ]; then
179 # USB devices also require rootwait
180 if [ -n `readlink /dev/disk/by-id/usb* | grep $TARGET_DEVICE_NAME` ]; then
184 if [ $grub_version -eq 0 ] ; then
186 bootfs=${device}${part_prefix}1
187 rootfs=${device}${part_prefix}2
188 swap=${device}${part_prefix}3
190 bios_boot=${device}${part_prefix}1
191 bootfs=${device}${part_prefix}2
192 rootfs=${device}${part_prefix}3
193 swap=${device}${part_prefix}4
196 echo "*****************"
197 [ $grub_version -ne 0 ] && echo "BIOS boot partition size: $bios_boot_size MB ($bios_boot)"
198 echo "Boot partition size: $boot_size MB ($bootfs)"
199 echo "Rootfs partition size: $rootfs_size MB ($rootfs)"
200 echo "Swap partition size: $swap_size MB ($swap)"
201 echo "*****************"
202 echo "Deleting partition table on ${device} ..."
203 dd if=/dev/zero of=${device} bs=512 count=35
205 echo "Creating new partition table on ${device} ..."
206 if [ $grub_version -eq 0 ] ; then
207 parted ${device} mktable msdos
208 echo "Creating boot partition on $bootfs"
209 parted ${device} mkpart primary ext3 0% $boot_size
211 parted ${device} mktable gpt
212 echo "Creating BIOS boot partition on $bios_boot"
213 parted ${device} mkpart bios_boot 0% $bios_boot_size
214 parted ${device} set 1 bios_grub on
215 echo "Creating boot partition on $bootfs"
216 parted ${device} mkpart boot ext3 $boot_start $boot_size
219 echo "Creating rootfs partition on $rootfs"
220 [ $grub_version -eq 0 ] && pname='primary' || pname='root'
221 parted ${device} mkpart $pname ext4 $rootfs_start $rootfs_end
223 echo "Creating swap partition on $swap"
224 [ $grub_version -eq 0 ] && pname='primary' || pname='swap'
225 parted ${device} mkpart $pname linux-swap $swap_start 100%
227 parted ${device} print
229 echo "Waiting for device nodes..."
231 while [ $C -ne 3 ] && [ ! -e $bootfs -o ! -e $rootfs -o ! -e $swap ]; do
236 echo "Formatting $bootfs to ext3..."
239 echo "Formatting $rootfs to ext4..."
242 echo "Formatting swap partition...($swap)"
249 # Handling of the target root partition
250 mount $rootfs /tgt_root
252 if [ ! -f /run/media/$1/$2 ]; then
253 mkdir -p /run/media/$1
254 mount /dev/$1 /run/media/$1
256 mount -o rw,loop,noatime,nodiratime /run/media/$1/$2 /src_root
258 echo "Copying rootfs files..."
259 cp -a /src_root/* /tgt_root
260 if [ -d /tgt_root/etc/ ] ; then
261 if [ $grub_version -ne 0 ] ; then
262 boot_uuid=$(blkid -o value -s UUID ${bootfs})
263 swap_part_uuid=$(blkid -o value -s PARTUUID ${swap})
264 bootdev="UUID=$boot_uuid"
265 swapdev=/dev/disk/by-partuuid/$swap_part_uuid
270 echo "$swapdev swap swap defaults 0 0" >> /tgt_root/etc/fstab
271 echo "$bootdev /boot ext3 defaults 1 2" >> /tgt_root/etc/fstab
272 # We dont want udev to mount our root device while we're booting...
273 if [ -d /tgt_root/etc/udev/ ] ; then
274 echo "${device}" >> /tgt_root/etc/udev/mount.blacklist
280 echo "Looking for kernels to use as boot target.."
281 # Find kernel to boot to
282 # Give user options if multiple are found
283 kernels="$(find /run/media/$1/ -type f \
284 -name bzImage* -o -name zImage* \
285 -o -name vmlinux* -o -name vmlinuz* \
288 if [ -n "$(echo $kernels)" ]; then
289 # only one kernel entry if no space
290 if [ -z "$(echo $kernels | grep " ")" ]; then
292 echo "$kernel will be used as the boot target"
294 echo "Which kernel do we want to boot by default? The following kernels were found:"
300 echo "No kernels found, exiting..."
304 # Handling of the target boot partition
306 echo "Preparing boot partition..."
308 if [ -f /etc/grub.d/00_header -a $grub_version -ne 0 ] ; then
309 echo "Preparing custom grub2 menu..."
310 root_part_uuid=$(blkid -o value -s PARTUUID ${rootfs})
311 boot_uuid=$(blkid -o value -s UUID ${bootfs})
312 GRUBCFG="/boot/grub/grub.cfg"
313 mkdir -p $(dirname $GRUBCFG)
318 search --no-floppy --fs-uuid $boot_uuid --set root
319 linux /$kernel root=PARTUUID=$root_part_uuid $rootwait rw $5 $3 $4 quiet
324 grub-install ${device}
326 if [ $grub_version -eq 0 ] ; then
327 echo "(hd0) ${device}" > /boot/grub/device.map
328 echo "Preparing custom grub menu..."
329 echo "default 0" > /boot/grub/menu.lst
330 echo "timeout 30" >> /boot/grub/menu.lst
331 echo "title Live Boot/Install-Image" >> /boot/grub/menu.lst
332 echo "root (hd0,0)" >> /boot/grub/menu.lst
333 echo "kernel /$kernel root=$rootfs rw $3 $4 quiet" >> /boot/grub/menu.lst
336 # Copy kernel artifacts. To add more artifacts just add to types
337 # For now just support kernel types already being used by something in OE-core
338 for types in bzImage zImage vmlinux vmlinuz fitImage; do
339 for kernel in `find /run/media/$1/ -name $types*`; do
348 echo "Remove your installation media, and press ENTER"