Moving initramfs-tools entry-point to backends.
[debian-live-boot.git] / scripts / boot / 9990-toram-todisk.sh
blob2643eb8bad0ffd30b07ae91ba5d0735ca8b609bc
1 #!/bin/sh
3 #set -e
5 copy_live_to ()
7 copyfrom="${1}"
8 copytodev="${2}"
9 copyto="${copyfrom}_swap"
11 if [ -z "${MODULETORAM}" ]
12 then
13 size=$(fs_size "" ${copyfrom}/${LIVE_MEDIA_PATH} "used")
14 else
15 MODULETORAMFILE="${copyfrom}/${LIVE_MEDIA_PATH}/${MODULETORAM}"
17 if [ -f "${MODULETORAMFILE}" ]
18 then
19 size=$( expr $(ls -la ${MODULETORAMFILE} | awk '{print $5}') / 1024 + 5000 )
20 else
21 log_warning_msg "Error: toram-module ${MODULETORAM} (${MODULETORAMFILE}) could not be read."
22 return 1
26 if [ "${copytodev}" = "ram" ]
27 then
28 # copying to ram:
29 freespace=$(awk '/^MemFree:/{f=$2} /^Cached:/{c=$2} END{print f+c}' /proc/meminfo)
30 mount_options="-o size=${size}k"
31 free_string="memory"
32 fstype="tmpfs"
33 dev="/dev/shm"
34 else
35 # it should be a writable block device
36 if [ -b "${copytodev}" ]
37 then
38 dev="${copytodev}"
39 free_string="space"
40 fstype=$(get_fstype "${dev}")
41 freespace=$(fs_size "${dev}")
42 else
43 log_warning_msg "${copytodev} is not a block device."
44 return 1
48 if [ "${freespace}" -lt "${size}" ]
49 then
50 log_warning_msg "Not enough free ${free_string} (${freespace}k free, ${size}k needed) to copy live media in ${copytodev}."
51 return 1
54 # Custom ramdisk size
55 if [ -z "${mount_options}" ] && [ -n "${ramdisk_size}" ]
56 then
57 # FIXME: should check for wrong values
58 mount_options="-o size=${ramdisk_size}"
61 # begin copying (or uncompressing)
62 mkdir "${copyto}"
63 log_begin_msg "mount -t ${fstype} ${mount_options} ${dev} ${copyto}"
64 mount -t "${fstype}" ${mount_options} "${dev}" "${copyto}"
66 if [ "${extension}" = "tgz" ]
67 then
68 cd "${copyto}"
69 tar zxf "${copyfrom}/${LIVE_MEDIA_PATH}/$(basename ${FETCH})"
70 rm -f "${copyfrom}/${LIVE_MEDIA_PATH}/$(basename ${FETCH})"
71 mount -r -o move "${copyto}" "${rootmnt}"
72 cd "${OLDPWD}"
73 else
74 if [ -n "${MODULETORAMFILE}" ]
75 then
76 if [ -x /bin/rsync ]
77 then
78 echo " * Copying $MODULETORAMFILE to RAM" 1>/dev/console
79 rsync -a --progress ${MODULETORAMFILE} ${copyto} 1>/dev/console # copy only the filesystem module
80 else
81 cp ${MODULETORAMFILE} ${copyto} # copy only the filesystem module
83 else
84 if [ -x /bin/rsync ]
85 then
86 echo " * Copying whole medium to RAM" 1>/dev/console
87 rsync -a --progress ${copyfrom}/* ${copyto} 1>/dev/console # "cp -a" from busybox also copies hidden files
88 else
89 mkdir -p ${copyto}/${LIVE_MEDIA_PATH}
90 cp -a ${copyfrom}/${LIVE_MEDIA_PATH}/* ${copyto}/${LIVE_MEDIA_PATH}
91 if [ -e ${copyfrom}/${LIVE_MEDIA_PATH}/.disk ]
92 then
93 cp -a ${copyfrom}/${LIVE_MEDIA_PATH}/.disk ${copyto}
98 umount ${copyfrom}
99 mount -r -o move ${copyto} ${copyfrom}
102 rmdir ${copyto}
103 return 0