change "while read x" to cope with EOF without newline
[dracut.git] / modules.d / 95rootfs-block / mount-root.sh
blob7e9c5bb6f27c504da50fb1e763d47480987d8729
1 #!/bin/sh
3 type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh
4 type det_fs >/dev/null 2>&1 || . /lib/fs-lib.sh
6 mount_root() {
7 local _ret
8 local _rflags_ro
9 # sanity - determine/fix fstype
10 rootfs=$(det_fs "${root#block:}" "$fstype")
12 journaldev=$(getarg "root.journaldev=")
13 if [ -n "$journaldev" ]; then
14 case "$rootfs" in
15 xfs)
16 rflags="${rflags:+${rflags},}logdev=$journaldev"
18 reiserfs)
19 fsckoptions="-j $journaldev $fsckoptions"
20 rflags="${rflags:+${rflags},}jdev=$journaldev"
22 *);;
23 esac
26 _rflags_ro="$rflags,ro"
27 _rflags_ro="${_rflags_ro##,}"
29 while ! mount -t ${rootfs} -o "$_rflags_ro" "${root#block:}" "$NEWROOT"; do
30 warn "Failed to mount -t ${rootfs} -o $_rflags_ro ${root#block:} $NEWROOT"
31 fsck_ask_err
32 done
34 READONLY=
35 fsckoptions=
36 if [ -f "$NEWROOT"/etc/sysconfig/readonly-root ]; then
37 . "$NEWROOT"/etc/sysconfig/readonly-root
40 if getargbool 0 "readonlyroot=" -y readonlyroot; then
41 READONLY=yes
44 if getarg noreadonlyroot ; then
45 READONLY=no
48 if [ -f "$NEWROOT"/fastboot ] || getargbool 0 fastboot ; then
49 fastboot=yes
52 if ! getargbool 0 rd.skipfsck; then
53 if [ -f "$NEWROOT"/fsckoptions ]; then
54 fsckoptions=$(cat "$NEWROOT"/fsckoptions)
57 if [ -f "$NEWROOT"/forcefsck ] || getargbool 0 forcefsck ; then
58 fsckoptions="-f $fsckoptions"
59 elif [ -f "$NEWROOT"/.autofsck ]; then
60 [ -f "$NEWROOT"/etc/sysconfig/autofsck ] && \
61 . "$NEWROOT"/etc/sysconfig/autofsck
62 if [ "$AUTOFSCK_DEF_CHECK" = "yes" ]; then
63 AUTOFSCK_OPT="$AUTOFSCK_OPT -f"
65 if [ -n "$AUTOFSCK_SINGLEUSER" ]; then
66 warn "*** Warning -- the system did not shut down cleanly. "
67 warn "*** Dropping you to a shell; the system will continue"
68 warn "*** when you leave the shell."
69 action_on_fail
71 fsckoptions="$AUTOFSCK_OPT $fsckoptions"
75 rootopts=
76 if getargbool 1 rd.fstab -d -n rd_NO_FSTAB \
77 && ! getarg rootflags \
78 && [ -f "$NEWROOT/etc/fstab" ] \
79 && ! [ -L "$NEWROOT/etc/fstab" ]; then
80 # if $NEWROOT/etc/fstab contains special mount options for
81 # the root filesystem,
82 # remount it with the proper options
83 rootopts="defaults"
84 while read dev mp fs opts dump fsck || [ -n "$dev" ]; do
85 # skip comments
86 [ "${dev%%#*}" != "$dev" ] && continue
88 if [ "$mp" = "/" ]; then
89 # sanity - determine/fix fstype
90 rootfs=$(det_fs "${root#block:}" "$fs")
91 rootopts=$opts
92 rootfsck=$fsck
93 break
95 done < "$NEWROOT/etc/fstab"
98 # we want rootflags (rflags) to take precedence so prepend rootopts to
99 # them
100 rflags="${rootopts},${rflags}"
101 rflags="${rflags#,}"
102 rflags="${rflags%,}"
104 # backslashes are treated as escape character in fstab
105 # esc_root=$(echo ${root#block:} | sed 's,\\,\\\\,g')
106 # printf '%s %s %s %s 1 1 \n' "$esc_root" "$NEWROOT" "$rootfs" "$rflags" >/etc/fstab
108 ran_fsck=0
109 if fsck_able "$rootfs" && \
110 [ "$rootfsck" != "0" -a -z "$fastboot" -a "$READONLY" != "yes" ] && \
111 ! strstr "${rflags}" _netdev && \
112 ! getargbool 0 rd.skipfsck; then
113 umount "$NEWROOT"
114 fsck_single "${root#block:}" "$rootfs" "$rflags" "$fsckoptions"
115 _ret=$?
116 ran_fsck=1
119 echo "${root#block:} $NEWROOT $rootfs ${rflags:-defaults} 0 $rootfsck" >> /etc/fstab
121 if ! ismounted "$NEWROOT"; then
122 info "Mounting ${root#block:} with -o ${rflags}"
123 mount "$NEWROOT" 2>&1 | vinfo
124 elif ! are_lists_eq , "$rflags" "$_rflags_ro" defaults; then
125 info "Remounting ${root#block:} with -o ${rflags}"
126 mount -o remount "$NEWROOT" 2>&1 | vinfo
129 if ! getargbool 0 rd.skipfsck; then
130 [ -f "$NEWROOT"/forcefsck ] && rm -f -- "$NEWROOT"/forcefsck 2>/dev/null
131 [ -f "$NEWROOT"/.autofsck ] && rm -f -- "$NEWROOT"/.autofsck 2>/dev/null
135 if [ -n "$root" -a -z "${root%%block:*}" ]; then
136 mount_root