Move 90-zfs udev rule from dracut to udev/rules.d
[zfs.git] / dracut / 90zfs / mount-zfs.sh.in
1 #!/bin/sh
2
3 . /lib/dracut-lib.sh
4
5 ZPOOL_FORCE=""
6
7 if getargbool 0 zfs_force -y zfs.force -y zfsforce ; then
8         warn "ZFS: Will force-import pools if necessary."
9         ZPOOL_FORCE="-f"
10 fi
11
12 case "$root" in
13         zfs:*)
14                 # We have ZFS modules loaded, so we're able to import pools now.
15                 if [ "$root" = "zfs:AUTO" ] ; then
16                         # Need to parse bootfs attribute
17                         info "ZFS: Attempting to detect root from imported ZFS pools."
18
19                         # Might be imported by the kernel module, so try searching before
20                         # we import anything.
21                         zfsbootfs=`zpool list -H -o bootfs | sed 'q'`
22                         if [ "$?" != "0" ] || [ "$zfsbootfs" = "" ] || \
23                                 [ "$zfsbootfs" = "no pools available" ] ; then
24                                 # Not there, so we need to import everything.
25                                 info "ZFS: Attempting to import additional pools."
26                                 zpool import -N -a ${ZPOOL_FORCE}
27                                 zfsbootfs=`zpool list -H -o bootfs | sed 'q'`
28                                 if [ "$?" != "0" ] || [ "$zfsbootfs" = "" ] || \
29                                         [ "$zfsbootfs" = "no pools available" ] ; then
30                                         rootok=0
31                                         pool=""
32
33                                         warn "ZFS: No bootfs attribute found in importable pools."
34
35                                         # Re-export everything since we're not prepared to take
36                                         # responsibility for them.
37                                         zpool list -H | while read fs rest ; do
38                                                 zpool export "$fs"
39                                         done
40
41                                         return 1
42                                 fi
43                         fi
44                         info "ZFS: Using ${zfsbootfs} as root."
45                 else
46                         # Should have an explicit pool set, so just import it and we're done.
47                         zfsbootfs="${root#zfs:}"
48                         pool="${zfsbootfs%%/*}"
49                         if ! zpool list -H $pool > /dev/null ; then
50                                 # pool wasn't imported automatically by the kernel module, so
51                                 # try it manually.
52                                 info "ZFS: Importing pool ${pool}..."
53                                 if ! zpool import -N ${ZPOOL_FORCE} $pool ; then
54                                         warn "ZFS: Unable to import pool ${pool}."
55                                         rootok=0
56
57                                         return 1
58                                 fi
59                         fi
60                 fi
61
62                 # Above should have left our rpool imported and pool/dataset in $root.
63                 # We need zfsutil for non-legacy mounts and not for legacy mounts.
64                 mountpoint=`zfs get -H -o value mountpoint $zfsbootfs`
65                 if [ "$mountpoint" = "legacy" ] ; then
66                         mount -t zfs "$zfsbootfs" "$NEWROOT" && ROOTFS_MOUNTED=yes
67                 else
68                         mount -o zfsutil -t zfs "$zfsbootfs" "$NEWROOT" && ROOTFS_MOUNTED=yes
69                 fi
70                 ;;
71 esac