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